From c54a51aa9501c437419dbcd9a738ec38487da2e6 Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Wed, 13 Jun 2018 12:01:42 -0400 Subject: [PATCH] Make the spaghetti IF conditions more manageable --- VMInstance.py | 69 +++++++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/VMInstance.py b/VMInstance.py index eb46070c..4c922cb1 100644 --- a/VMInstance.py +++ b/VMInstance.py @@ -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 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 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) + + 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" and self.hypervisor != self.thishypervisor.name: - self.terminate_vm() + # VM should be running but not on this hypervisor + elif running == libvirt.VIR_DOMAIN_RUNNING and self.state != "migrate": + 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: - 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: - 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: - # 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