Make the spaghetti IF conditions more manageable

This commit is contained in:
Joshua Boniface 2018-06-13 12:01:42 -04:00
parent 24678c12ca
commit c54a51aa95
1 changed files with 37 additions and 32 deletions

View File

@ -251,44 +251,49 @@ class VMInstance:
if self.instart == True or self.instop == True or self.inshutdown == True or self.inmigrate == True or self.inreceive == True:
return 0
# 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" and self.hypervisor == self.thishypervisor.name:
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:
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:
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 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
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "start" and self.hypervisor == self.thishypervisor.name:
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 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:
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 should be started
elif running != libvirt.VIR_DOMAIN_RUNNING and self.state == "start" and self.hypervisor == self.thishypervisor.name:
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)
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()
# This function is a wrapper for libvirt.lookupByUUID which fixes some problems
# 1. Takes a text UUID and handles converting it to bytes
# 2. Try's it and returns a sensible value if not