Further reworking to make list consistent

This commit is contained in:
Joshua Boniface 2018-06-13 12:31:27 -04:00
parent 441ac73e14
commit f8e1039a07
1 changed files with 57 additions and 45 deletions

View File

@ -58,7 +58,7 @@ class VMInstance:
return self.dom return self.dom
# Start up the VM # Start up the VM
def start_vm(self, xmlconfig): def start_vm(self):
ansiiprint.echo('Starting VM', '{}:'.format(self.domuuid), 'i') ansiiprint.echo('Starting VM', '{}:'.format(self.domuuid), 'i')
self.instart = True self.instart = True
@ -71,6 +71,8 @@ class VMInstance:
return return
try: try:
# Grab the domain information from Zookeeper
xmlconfig = self.zk.get('/domains/{}/xml'.format(self.domuuid))[0].decode('ascii')
dom = conn.createXML(xmlconfig, 0) dom = conn.createXML(xmlconfig, 0)
if not self.domuuid in self.thishypervisor.domain_list: if not self.domuuid in self.thishypervisor.domain_list:
self.thishypervisor.domain_list.append(self.domuuid) self.thishypervisor.domain_list.append(self.domuuid)
@ -247,51 +249,61 @@ class VMInstance:
ansiiprint.echo('VM state change for "{}": {} {}'.format(self.domuuid, self.state, self.hypervisor), '', 'i') ansiiprint.echo('VM state change for "{}": {} {}'.format(self.domuuid, self.state, self.hypervisor), '', 'i')
# Conditional pass one - If we're already doing something, bail out #######################
if self.instart == True or self.instop == True or self.inshutdown == True or self.inmigrate == True or self.inreceive == True: # Handle state changes
return 0 #######################
# Valid states are:
# start
# migrate
# shutdown
# stop
# Conditional pass two - Is this VM running on this hypervisor # Conditional pass one - Are we already performing an action
print('{} {} {}'.format(self.domuuid, self.thishypervisor.name, running)) if self.instart == False and self.instop == False and self.inshutdown == False and self.inmigrate == False and self.inreceive == False:
if self.hypervisor == self.thishypervisor.name: # Conditional pass two - Is this VM configured to run on this hypervisor
# VM should be stopped if self.hypervisor == self.thishypervisor.name:
if running == libvirt.VIR_DOMAIN_RUNNING and self.state == "stop": # Conditional pass three - Is this VM currently running on this hypervisor
self.stop_vm() if running == libvirt.VIR_DOMAIN_RUNNING:
# VM is already running and should be
# VM should be shut down if self.state == "start":
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "shutdown": if not self.domuuid in self.thishypervisor.domain_list:
self.shutdown_vm() self.thishypervisor.domain_list.append(self.domuuid)
# VM is already running and should be but stuck in migrate state
# VM should be migrated to this hypervisor elif self.state == "migrate":
elif running != libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate": self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii'))
self.receive_migrate() if not self.domuuid in self.thishypervisor.domain_list:
self.thishypervisor.domain_list.append(self.domuuid)
# VM is already running and should be # VM should be shut down
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "start": elif self.state == "shutdown":
if not self.domuuid in self.thishypervisor.domain_list: self.shutdown_vm()
self.thishypervisor.domain_list.append(self.domuuid) # VM should be stopped
elif self.state == "stop":
# VM is already running and should be but stuck in migrate state self.stop_vm()
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate": else:
self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii')) # VM should be started
if not self.domuuid in self.thishypervisor.domain_list: if self.state == "start":
self.thishypervisor.domain_list.append(self.domuuid) self.start_vm()
# VM should be migrated to this hypervisor
# VM should be started elif self.state == "migrate":
elif running != libvirt.VIR_DOMAIN_RUNNING and self.state == "start": self.receive_migrate()
# Grab the domain information from Zookeeper # VM should be shut down; ensure it's gone from this node's domain_list
domxml, domxmlstat = self.zk.get('/domains/{}/xml'.format(self.domuuid)) elif self.state == "shutdown":
domxml = str(domxml.decode('ascii')) if self.domuuid in self.thishypervisor.domain_list:
self.start_vm(domxml) self.thishypervisor.domain_list.remove(self.domuuid)
# VM should be stoped; ensure it's gone from this node's domain_list
else: elif self.state == "stop":
# VM should be migrated away from this hypervisor if self.domuuid in self.thishypervisor.domain_list:
if running == libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate": self.thishypervisor.domain_list.remove(self.domuuid)
self.migrate_vm()
else:
# VM should be running but not on this hypervisor # Conditional pass three - Is this VM currently running on this hypervisor
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state != "migrate": if running == libvirt.VIR_DOMAIN_RUNNING:
self.terminate_vm() # VM should be migrated away from this hypervisor
if self.state == "migrate":
self.migrate_vm()
# VM should be terminated
else:
self.terminate_vm()
# This function is a wrapper for libvirt.lookupByUUID which fixes some problems # This function is a wrapper for libvirt.lookupByUUID which fixes some problems