From 87954559a1caa02a5cfb227b3c63ba8ce1799f51 Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Sat, 2 Jun 2018 15:26:37 -0400 Subject: [PATCH] Make this work better and stop bad VMs --- NodeInstance.py | 11 +++++------ VMInstance.py | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/NodeInstance.py b/NodeInstance.py index 9c373689..33f6975d 100644 --- a/NodeInstance.py +++ b/NodeInstance.py @@ -95,7 +95,11 @@ class NodeInstance(threading.Thread): print("CPUs: %s" % self.cpucount) while True: - # Make sure that the VMs we think we're running actually are + # Toggle state management of all VMs + for domain, instance in self.s_domain.items(): + instance.manage_vm_state() + + # Remove non-running VMs from our list for domain in self.domain_list: try: buuid = uuid.UUID(domain).bytes @@ -106,11 +110,6 @@ class NodeInstance(threading.Thread): except: self.domain_list.remove(domain) - # Toggle state management of all VMs to start any that are failed - for domain, instance in self.s_domain.items(): - if instance.gethypervisor() == self.name and ( instance.getstate() == 'start' or instance.getstate() == 'migrate' ): - instance.manage_vm_state() - # Set our information in zookeeper self.memfree = conn.getFreeMemory() self.cpuload = os.getloadavg()[0] diff --git a/VMInstance.py b/VMInstance.py index 7c97017a..a2c7ac8b 100644 --- a/VMInstance.py +++ b/VMInstance.py @@ -114,24 +114,34 @@ class VMInstance: except: running = libvirt.VIR_DOMAIN_NOSTATE + # VM should be stopped if running == libvirt.VIR_DOMAIN_RUNNING and self.state == "stop" and self.hypervisor == self.thishypervisor.name: self.stop_vm() - if running == libvirt.VIR_DOMAIN_RUNNING and self.state == "shutdown" and self.hypervisor == self.thishypervisor.name: + # VM should not be running on this hypervisor + elif running == libvirt.VIR_DOMAIN_RUNNING and self.state == "start" and self.hypervisor != self.thishypervisor.name: + 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 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 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 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(self.zkey + '/xml') domxml = str(domxml.decode('ascii')) self.dom = self.start_vm(conn, domxml) + # 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)