#!/usr/bin/python # -*- coding: utf-8 -*- # # This is a replacement program for start_vms.sh, a shell wrapper around # ovirt-shell that performs the following functions: # 1) Wait for ovirt engine to respond # 2) Wait for system storage domains to come online # 3) Start the list of VMs, in order, with either a wait-for-up and/or # delay between. # # Written by: Derek Atkins # import logging import ovirtsdk4 as sdk import ovirtsdk4.types as types from datetime import datetime import time import imp # Load in VM List. Note this is a PY2 and will need to migrate to PY3 data_file = "/etc/sysconfig/vm_list.py" with open(data_file, 'U') as f: vm_list = imp.load_module('vm_data', f, data_file, ('.py', 'U', imp.PY_SOURCE)) # Set up logging to target logging.basicConfig(level=logging.INFO, filename='/var/log/start_vms_info') # Create the connection to the server: connection = sdk.Connection( url='https://ovirt/ovirt-engine/api', username='', password='', ca_file='/root/ca.pem', debug=True, log=logging.getLogger(), ) print("Starting at " + unicode(datetime.now())) # wait for the engine to respond while not connection.test(raise_exception=False): print("Not ready... Sleeping...") time.sleep(60) # Now wait for the storage domains to appear active print("Engine up at {}. Waiting 60s for resources to reset.".format(datetime.now())) # The 4.3.x engine keeps stale data, so let's wait for it to update # to the correct state before we start looking for storage domains time.sleep(60) print("Searching for disks at " + unicode(datetime.now())) api = connection.system_service().get() total_disks = api.summary.storage_domains.total - 1 active_disks = api.summary.storage_domains.active while active_disks < total_disks: print("Storage Domains not active yet. Only found {}/{}. Waiting...".format(active_disks, total_disks)) time.sleep(60) api = connection.system_service().get() active_disks = api.summary.storage_domains.active # Now start all the VMs in the requested order. print("All storage mounted. Starting VMs at " + unicode(datetime.now())) # First, get the reference to the "vms" service vms_service = connection.system_service().vms_service() for vm_obj in vm_list.vm_list: vm_name = vm_obj['name'] print("Starting " + vm_name + " at " + unicode(datetime.now())) # Lookup the VM vm = vms_service.list(search='name=' + vm_name)[0] # Then looking the vm service vm_service = vms_service.vm_service(vm.id) # Finally, start the vm vm_service.start() # Wait till the virtual machine is up if specified: if ("wait" in vm_obj and vm_obj["wait"]): print("Waiting for machine to come up") while True: time.sleep(5) vm = vm_service.get() if vm.status == types.VmStatus.UP: break print("Up at " + unicode(datetime.now())) # Delay until the next VM if ("delay" in vm_obj): time.sleep(vm_obj["delay"]) else: time.sleep(vm_list.default_delay) # Done. Close the connection to the server connection.close()