From 09b419142714bfaee5078e099f578e3af6ab7428 Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Thu, 23 Aug 2018 19:01:57 -0400 Subject: [PATCH] Correct bug where starting a running instance triggered failed state --- pvcd/VMInstance.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/pvcd/VMInstance.py b/pvcd/VMInstance.py index 9b478f83..394d9ffd 100644 --- a/pvcd/VMInstance.py +++ b/pvcd/VMInstance.py @@ -114,20 +114,33 @@ class VMInstance: ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e') self.instart = False return - + + # Try to get the current state in case it's already running try: - # Grab the domain information from Zookeeper - xmlconfig = zkhandler.readdata(self.zk_conn, '/domains/{}/xml'.format(self.domuuid)) - dom = lv_conn.createXML(xmlconfig, 0) + self.dom = self.lookupByUUID(self.domuuid) + curstate = self.dom.state()[0] + except: + curstate = 'notstart' + + if curstate == libvirt.VIR_DOMAIN_RUNNING: + # If it is running just update the model self.addDomainToList() - ansiiprint.echo('Successfully started VM', '{}:'.format(self.domuuid), 'o') - self.dom = dom zkhandler.writedata(self.zk_conn, { '/domains/{}/failedreason'.format(self.domuuid): '' }) - except libvirt.libvirtError as e: - ansiiprint.echo('Failed to create VM', '{}:'.format(self.domuuid), 'e') - zkhandler.writedata(self.zk_conn, { '/domains/{}/state'.format(self.domuuid): 'failed' }) - zkhandler.writedata(self.zk_conn, { '/domains/{}/failedreason'.format(self.domuuid): str(e) }) - self.dom = None + else: + # Or try to create it + try: + # Grab the domain information from Zookeeper + xmlconfig = zkhandler.readdata(self.zk_conn, '/domains/{}/xml'.format(self.domuuid)) + dom = lv_conn.createXML(xmlconfig, 0) + self.addDomainToList() + ansiiprint.echo('Successfully started VM', '{}:'.format(self.domuuid), 'o') + self.dom = dom + zkhandler.writedata(self.zk_conn, { '/domains/{}/failedreason'.format(self.domuuid): '' }) + except libvirt.libvirtError as e: + ansiiprint.echo('Failed to create VM', '{}:'.format(self.domuuid), 'e') + zkhandler.writedata(self.zk_conn, { '/domains/{}/state'.format(self.domuuid): 'failed' }) + zkhandler.writedata(self.zk_conn, { '/domains/{}/failedreason'.format(self.domuuid): str(e) }) + self.dom = None lv_conn.close() self.instart = False