Ovirt : How to shutdown/stop or start virtual machines (VMs) in ovirt DC automatically using python/ovirt- sdk ? [Part 1]

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.stop` makes a virtual machine power off, change it with vmObj.shutdown to procede with a regular/graceful shutdown

[terminal]
APIURL=”https://myrhevm.humblec.com/api”
APIUSER=”admin@internal”
APIPASS=”somepassword”
CAFILE=”/root/ca.crt”
LOGFILENAME=”/tmp/shutdown_vms_dc.log”

Here is the copy of the program:

#! /usr/bin/python

# Author: Humble Chirammal
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# 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)

[/terminal]

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:

[terminal]
Ex: [root@ ~]# python start_all_down_vms.py

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 to join
2013-10-07 08:31:19,634 INFO Thread to start mailserver
2013-10-07 08:31:19,954 DEBUG Exception caught on VM ( vm2) start:

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 to join
2013-10-07 08:31:23,309 INFO Thread : Thread-2 terminated
2013-10-07 08:31:23,309 INFO Waiting for to join
2013-10-07 08:31:23,309 INFO Thread : Thread-3 terminated
2013-10-07 08:31:23,309 INFO Waiting for to join
2013-10-07 08:31:23,309 INFO Thread : Thread-4 terminated
2013-10-07 08:31:23,309 DEBUG Below Vms failed to start with an exception:[‘vm2’, ‘IPA’]
[/terminal]

Digiprove sealCopyright secured by Digiprove © 2020 Humble Chirammal

3 thoughts on “Ovirt : How to shutdown/stop or start virtual machines (VMs) in ovirt DC automatically using python/ovirt- sdk ? [Part 1]”

  1. Hi. I’m not getting /root/ca.crt-find the file. What can I do to find out what certain file?
    Thanks in advance

  2. 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

Comments are closed.