diff --git a/pvcd/VMInstance.py b/pvcd/VMInstance.py index f0d0f17c..2d3c4d1f 100644 --- a/pvcd/VMInstance.py +++ b/pvcd/VMInstance.py @@ -64,6 +64,21 @@ class VMInstance: def getdom(self): return self.dom + # Manage local node domain_list + def addDomainToList(self): + if not self.domuuid in self.thishypervisor.domain_list: + try: + self.thishypervisor.domain_list.append(self.domuuid) + except Exception as e: + ansiiprint.echo('ERROR: {}'.format(e), '', 'c') + + def removeDomainFromList(self): + if self.domuuid in self.thishypervisor.domain_list: + try: + self.thishypervisor.domain_list.remove(self.domuuid) + except Exception as e: + ansiiprint.echo('ERROR: {}'.format(e), '', 'c') + # Start up the VM def start_vm(self): ansiiprint.echo('Starting VM', '{}:'.format(self.domuuid), 'i') @@ -81,9 +96,7 @@ class VMInstance: # Grab the domain information from Zookeeper xmlconfig = self.zk_conn.get('/domains/{}/xml'.format(self.domuuid))[0].decode('ascii') dom = lv_conn.createXML(xmlconfig, 0) - if not self.domuuid in self.thishypervisor.domain_list: - self.thishypervisor.domain_list.append(self.domuuid) - + self.addDomainToList() ansiiprint.echo('Successfully started VM', '{}:'.format(self.domuuid), 'o') self.dom = dom except libvirt.libvirtError as e: @@ -110,6 +123,7 @@ class VMInstance: try: self.shutdown_vm() self.start_vm() + self.addDomainToList() ansiiprint.echo('Successfully restarted VM', '{}:'.format(self.domuuid), 'o') except libvirt.libvirtError as e: ansiiprint.echo('Failed to restart VM', '{}:'.format(self.domuuid), 'e') @@ -126,11 +140,7 @@ class VMInstance: self.dom.destroy() except AttributeError: ansiiprint.echo('Failed to terminate VM', '{}:'.format(self.domuuid), 'e') - if self.domuuid in self.thishypervisor.domain_list: - try: - self.thishypervisor.domain_list.remove(self.domuuid) - except ValueError: - pass + self.removeDomainFromList() ansiiprint.echo('Successfully terminated VM', '{}:'.format(self.domuuid), 'o') self.dom = None self.instop = False @@ -143,11 +153,7 @@ class VMInstance: self.dom.destroy() except AttributeError: ansiiprint.echo('Failed to stop VM', '{}:'.format(self.domuuid), 'e') - if self.domuuid in self.thishypervisor.domain_list: - try: - self.thishypervisor.domain_list.remove(self.domuuid) - except ValueError: - pass + self.removeDomainFromList() if self.inrestart == False: self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'stop'.encode('ascii')) @@ -175,11 +181,7 @@ class VMInstance: except: pass - if self.domuuid in self.thishypervisor.domain_list: - try: - self.thishypervisor.domain_list.remove(self.domuuid) - except ValueError: - pass + self.removeDomainFromList() if self.inrestart == False: self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'stop'.encode('ascii')) @@ -220,10 +222,7 @@ class VMInstance: self.shutdown_vm() time.sleep(1) else: - try: - self.thishypervisor.domain_list.remove(self.domuuid) - except ValueError: - pass + self.removeDomainFromList() time.sleep(1) self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii')) @@ -256,8 +255,7 @@ class VMInstance: dom_state = None if dom_state == libvirt.VIR_DOMAIN_RUNNING: - if not self.domuuid in self.thishypervisor.domain_list: - self.thishypervisor.domain_list.append(self.domuuid) + self.addDomainToList() ansiiprint.echo('Successfully received migrated VM', '{}:'.format(self.domuuid), 'o') else: ansiiprint.echo('Failed to receive migrated VM', '{}:'.format(self.domuuid), 'e') @@ -309,13 +307,11 @@ class VMInstance: if running == libvirt.VIR_DOMAIN_RUNNING: # VM is already running and should be if self.state == "start": - if not self.domuuid in self.thishypervisor.domain_list: - self.thishypervisor.domain_list.append(self.domuuid) + self.addDomainToList() # VM is already running and should be but stuck in migrate state elif self.state == "migrate": self.zk_conn.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) + self.addDomainToList() # VM should be restarted elif self.state == "restart": self.restart_vm() @@ -337,12 +333,10 @@ class VMInstance: self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii')) # VM should be shut down; ensure it's gone from this node's domain_list elif self.state == "shutdown": - if self.domuuid in self.thishypervisor.domain_list: - self.thishypervisor.domain_list.remove(self.domuuid) + self.removeDomainFromList() # VM should be stoped; ensure it's gone from this node's domain_list elif self.state == "stop": - if self.domuuid in self.thishypervisor.domain_list: - self.thishypervisor.domain_list.remove(self.domuuid) + self.removeDomainFromList() else: # Conditional pass three - Is this VM currently running on this hypervisor