Recently I got a request to provide a python program to shutdown all the vms in ovirt DC using python-sdk and also to start Vms in the DC. Below program is submitted as a quick solution.. Sharing it here hoping it will help you.
To shutdown all the VMs in an Ovirt (www.ovirt.org) Data Center, you can try the below program called ‘shutdown_all_up_vms.py’. You need to configure below parameters in the program depending on your setup :
Programs can be downloaded from here .
[NOTE] : Before we proceed, just keep in mind that,
vmObj.stopmakes a virtual machine power off, change it with vmObj.shutdown to procede with a regular/graceful shutdown
Here is the copy of the program:
#! /usr/bin/python
# Author: Humble Chirammal
# Please make sure you are changing below parameters in api :
#url =”Your ovirt engine api url” #username =”user to access the api” #password =”password of above user” #ca_file =”location of ca cert file”
from ovirtsdk.api import API from ovirtsdk.xml import params from threading import Thread import time import logging
#Configure
APIURL=”https://myrhevm.humblec.com/api” APIUSER=”admin@internal” APIPASS=”somepassword” CAFILE=”/root/ca.crt” LOGFILENAME=”/tmp/shutdown_vms_dc.log”
threads=[] failedVms=[]
logging.basicConfig(level=logging.DEBUG, format=’%(asctime)s %(levelname)s %(message)s’, filename=LOGFILENAME, filemode=’w’) def start_vms(vmObj): logging.info(‘Thread to stop %s’, vmObj.name) try: vmObj.stop() #time.sleep(5) except Exception as e: logging.debug(‘Exception caught on VM ( %s) stop:\n%s’ % (vmObj.name, str(e))) failedVms.append(vmObj.name) if __name__ == “__main__”: try: api = API(url=APIURL, username=APIUSER, password=APIPASS, ca_file=CAFILE) print ‘Connected to RHEVM API %s Successfully’ % APIURL logging.info ( ‘Successfully Connected to %s’ % APIURL) try: print ‘ \n I am logging in %s \n’ % LOGFILENAME vmsList = api.vms.list() for i in vmsList: print i.name if i.status.state != ‘down’: logging.warning(‘%s is not down, trying to stop it’ % i.name) threadMe = Thread(target=start_vms, args=[i]) threadMe.start() threads.append(threadMe) except Exception as e: logging.debug(‘Error:\n%s’ % str(e))
logging.warning (‘No of VMs to stop : %s’ % len(threads)) print ‘No of VMs to stop: %s’ % len(threads) for th in threads: logging.info (‘Waiting for %s to join’ % th) th.join (30) if not th.isAlive(): logging.info (‘Thread : %s terminated’ % (th.getName()))
else: logging.debug( ‘Thread : %s is still alive, you may check this task..’ % (th)) logging.debug (‘ Below Vms failed to stop with an exception:%s’ % (failedVms)); api.disconnect()
except Exception as ex: logging.debug(‘Unexpected error: %s’ % ex)
Another program ( almost replica) is available for ‘start’ vms (start_all_down_vms.py ) as well.. You can download it from https://github.com/humblec/python-sdk-ovirt
If you execute any of the above program, the console output may look like this:
I am logging in /tmp/start_vms_dc.log
vm1 vm2 IPA mailserver
No of VMs to start: 4
As you can see above, the program logged better inside ‘/tmp/start_vms_dc.log’
[root@ ~]# cat /tmp/start_vms_dc.log
2013-10-07 08:31:19,625 WARNING vm1 is not up, trying to start it
2013-10-07 08:31:19,626 INFO Thread to start vm1
2013-10-07 08:31:19,626 WARNING vm2 is not up, trying to start it
2013-10-07 08:31:19,628 WARNING IPA is not up, trying to start it
2013-10-07 08:31:19,627 INFO Thread to start vm2
2013-10-07 08:31:19,629 INFO Thread to start IPA
2013-10-07 08:31:19,629 WARNING mailserver is not up, trying to start it
2013-10-07 08:31:19,634 WARNING No of VMs to start : 4
2013-10-07 08:31:19,634 INFO Waiting for
status: 400 reason: Bad Request detail: Cannot run VM without at least one bootable disk. Alternatives: -Create a disk for this VM, and rerun the VM. -Change the boot sequence using the Edit VM command (Boot Option Sub-Tab). -Use the Run-Once command to select a different boot option and rerun the VM. 2013-10-07 08:31:20,249 DEBUG Exception caught on VM ( IPA) start:
status: 400
reason: Bad Request
detail: Cannot run VM. There are no available running Hosts with sufficient memory in VM’s Cluster .
2013-10-07 08:31:23,309 INFO Thread : Thread-1 terminated
2013-10-07 08:31:23,309 INFO Waiting for
Hi. I’m not getting /root/ca.crt-find the file. What can I do to find out what certain file?
Thanks in advance
Hi all,
be carefull because method vmObj.stop makes a power off, change it with vmObj.shutdown to procede with a regular shutdown.
Bye
Enrico
Yeah.. I forgot to put a disclaimer about it. Adding an update to the article as well. Thanks @Enrico.