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,50 +249,60 @@ 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:
# Conditional pass two - Is this VM configured to run on this hypervisor
if self.hypervisor == self.thishypervisor.name: if self.hypervisor == self.thishypervisor.name:
# VM should be stopped # Conditional pass three - Is this VM currently running on this hypervisor
if running == libvirt.VIR_DOMAIN_RUNNING and self.state == "stop": if running == libvirt.VIR_DOMAIN_RUNNING:
self.stop_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":
self.receive_migrate()
# VM is already running and should be # VM is already running and should be
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "start": if self.state == "start":
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)
# VM is already running and should be but stuck in migrate state # VM is already running and should be but stuck in migrate state
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate": elif self.state == "migrate":
self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii')) self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii'))
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)
# VM should be shut down
elif self.state == "shutdown":
self.shutdown_vm()
# VM should be stopped
elif self.state == "stop":
self.stop_vm()
else:
# VM should be started # VM should be started
elif running != libvirt.VIR_DOMAIN_RUNNING and self.state == "start": if self.state == "start":
# Grab the domain information from Zookeeper self.start_vm()
domxml, domxmlstat = self.zk.get('/domains/{}/xml'.format(self.domuuid)) # VM should be migrated to this hypervisor
domxml = str(domxml.decode('ascii')) elif self.state == "migrate":
self.start_vm(domxml) self.receive_migrate()
# 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)
# 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)
else: else:
# Conditional pass three - Is this VM currently running on this hypervisor
if running == libvirt.VIR_DOMAIN_RUNNING:
# VM should be migrated away from this hypervisor # VM should be migrated away from this hypervisor
if running == libvirt.VIR_DOMAIN_RUNNING and self.state == "migrate": if self.state == "migrate":
self.migrate_vm() self.migrate_vm()
# VM should be terminated
# VM should be running but not on this hypervisor else:
elif running == libvirt.VIR_DOMAIN_RUNNING and self.state != "migrate":
self.terminate_vm() self.terminate_vm()