Make the spaghetti IF conditions more manageable
This commit is contained in:
parent
24678c12ca
commit
c54a51aa95
|
@ -251,43 +251,48 @@ class VMInstance:
|
|||
if self.instart == True or self.instop == True or self.inshutdown == True or self.inmigrate == True or self.inreceive == True:
|
||||
return 0
|
||||
|
||||
# VM should be stopped
|
||||
if running == libvirt.VIR_DOMAIN_RUNNING and self.state == "stop" and self.hypervisor == self.thishypervisor.name:
|
||||
self.stop_vm()
|
||||
# Conditional pass two - Is this VM running on this hypervisor
|
||||
print('{} {} {}'.format(self.domuuid, self.thishypervisor.name, running))
|
||||
if self.hypervisor == self.thishypervisor.name:
|
||||
# VM should be stopped
|
||||
if running == libvirt.VIR_DOMAIN_RUNNING and self.state == "stop":
|
||||
self.stop_vm()
|
||||
|
||||
# VM should be shut down
|
||||
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "shutdown" and self.hypervisor == self.thishypervisor.name:
|
||||
self.shutdown_vm()
|
||||
# VM should be shut down
|
||||
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "shutdown":
|
||||
self.shutdown_vm()
|
||||
|
||||
# VM should be migrated to this hypervisor
|
||||
elif running != libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate" and self.hypervisor == self.thishypervisor.name:
|
||||
self.receive_migrate()
|
||||
# VM should be migrated to this hypervisor
|
||||
elif running != libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate":
|
||||
self.receive_migrate()
|
||||
|
||||
# VM should be migrated away from this hypervisor
|
||||
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate" and self.hypervisor != self.thishypervisor.name:
|
||||
self.migrate_vm()
|
||||
# VM is already running and should be
|
||||
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "start":
|
||||
if not self.domuuid in self.thishypervisor.domain_list:
|
||||
self.thishypervisor.domain_list.append(self.domuuid)
|
||||
|
||||
# VM should be running but not on this hypervisor
|
||||
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state != "migrate" and self.hypervisor != self.thishypervisor.name:
|
||||
self.terminate_vm()
|
||||
# VM is already running and should be but stuck in migrate state
|
||||
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate":
|
||||
self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii'))
|
||||
if not self.domuuid in self.thishypervisor.domain_list:
|
||||
self.thishypervisor.domain_list.append(self.domuuid)
|
||||
|
||||
# VM is already running and should be
|
||||
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "start" and self.hypervisor == self.thishypervisor.name:
|
||||
if not self.domuuid in self.thishypervisor.domain_list:
|
||||
self.thishypervisor.domain_list.append(self.domuuid)
|
||||
# VM should be started
|
||||
elif running != libvirt.VIR_DOMAIN_RUNNING and self.state == "start":
|
||||
# Grab the domain information from Zookeeper
|
||||
domxml, domxmlstat = self.zk.get('/domains/{}/xml'.format(self.domuuid))
|
||||
domxml = str(domxml.decode('ascii'))
|
||||
self.start_vm(domxml)
|
||||
|
||||
# VM is already running and should be but stuck in migrate state
|
||||
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate" and self.hypervisor == self.thishypervisor.name:
|
||||
self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii'))
|
||||
if not self.domuuid in self.thishypervisor.domain_list:
|
||||
self.thishypervisor.domain_list.append(self.domuuid)
|
||||
else:
|
||||
# VM should be migrated away from this hypervisor
|
||||
if running == libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate":
|
||||
self.migrate_vm()
|
||||
|
||||
# VM should be running but not on this hypervisor
|
||||
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state != "migrate":
|
||||
self.terminate_vm()
|
||||
|
||||
# VM should be started
|
||||
elif running != libvirt.VIR_DOMAIN_RUNNING and self.state == "start" and self.hypervisor == self.thishypervisor.name:
|
||||
# Grab the domain information from Zookeeper
|
||||
domxml, domxmlstat = self.zk.get('/domains/{}/xml'.format(self.domuuid))
|
||||
domxml = str(domxml.decode('ascii'))
|
||||
self.start_vm(domxml)
|
||||
|
||||
# This function is a wrapper for libvirt.lookupByUUID which fixes some problems
|
||||
# 1. Takes a text UUID and handles converting it to bytes
|
||||
|
|
Loading…
Reference in New Issue