I have created a python script using the oVirt SDK but I can't find an API to shutdown the hosted engine and hosts. I am guessing this isn't possible using the API as if you shutdown the hosted engine the API will stop working.
I guess the only way to stop the hosted engine and shutdown hosts is via SSH or some kind of agent?
#!/usr/bin/env python
import ovirtsdk4 as sdk
from ovirtsdk4 import types
import time
MINUTES_TO_WAIT=5
# must be run from infrastructure vlan
ovaddress = "removed"
username="removed"
password="removed"
connection = sdk.Connection(
url=ovaddress,
username=username,
password=password,
insecure=True
)
system_service = connection.system_service()
vms_service = system_service.vms_service()
# Shutdown VMs and put into global maintenance
vms = vms_service.list(search='status=up')
for vm in vms:
vm_service = vms_service.vm_service(vm.id)
if "HostedEngine" in vm.name:
vm_service.maintenance(maintenance_enabled=True)
print "Global maintenance enabled"
else:
vm_service.shutdown()
print ("%s shutdown" % (vm.name))
# Wait for VMs to shutdown
for x in range(0, MINUTES_TO_WAIT):
time.sleep(60)
vms = vms_service.list(search='status=up')
if len(vms) == 1:
# Just the Hosted Engine left
break
for vm in vms:
vm_service = vms_service.vm_service(vm.id)
print ("waiting for %s to shutdown" % (vm.name))
# Put hosts in local maintenance
hosts_service = connection.system_service().hosts_service()
hosts = hosts_service.list(search='status=up')
for host in hosts:
host_service = hosts_service.host_service(host.id)
if host.status != types.HostStatus.MAINTENANCE:
host_service.deactivate()
print ("%s put into maintenance mode" %(host.name))
# Shutdown Hosted Engine?
# Shutdown hosts?
connection.close()