From 8904e25bebc92c3440756ea1c9a0961316a780af Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Mon, 4 Jun 2018 01:09:42 -0400 Subject: [PATCH] Write and use wrapper function for lookupByUUID --- NodeInstance.py | 5 ++--- VMInstance.py | 6 +++--- pvcdomf.py | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 pvcdomf.py diff --git a/NodeInstance.py b/NodeInstance.py index 2bf3021f..c6797685 100644 --- a/NodeInstance.py +++ b/NodeInstance.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import os, socket, time, uuid, threading, libvirt, kazoo.client +import os, sys, socket, time, threading, libvirt, kazoo.client, pvcdomf class NodeInstance(threading.Thread): def __init__(self, name, node_list, s_domain, zk): @@ -105,8 +105,7 @@ class NodeInstance(threading.Thread): # Remove any non-running VMs from our list for domain in self.domain_list: try: - buuid = uuid.UUID(domain).bytes - dom = conn.lookupByUUID(buuid) + dom = pvcdomf.lookupByUUID(conn, domain) state = dom.state()[0] if state != libvirt.VIR_DOMAIN_RUNNING: self.domain_list.remove(domain) diff --git a/VMInstance.py b/VMInstance.py index 314fb1d5..6d920c0b 100644 --- a/VMInstance.py +++ b/VMInstance.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import os, time, uuid, threading, libvirt, kazoo.client +import os, sys, socket, time, threading, libvirt, kazoo.client, pvcdomf class VMInstance: def __init__(self, domuuid, zk, thishypervisor): @@ -27,7 +27,7 @@ class VMInstance: exit(1) try: - self.dom = conn.lookupByUUID(uuid.UUID(self.domuuid).bytes) + self.dom = pvcdomf.lookupByUUID(conn, self.domuuid) conn.close() except libvirt.libvirtError: self.dom = None @@ -152,7 +152,7 @@ class VMInstance: self.inreceive = True while True: try: - self.dom = conn.lookupByUUID(uuid.UUID(self.domuuid).bytes) + self.dom = pvcdomf.lookupByUUID(conn, self.domuuid) except: time.sleep(0.2) continue diff --git a/pvcdomf.py b/pvcdomf.py new file mode 100644 index 00000000..4dd79b53 --- /dev/null +++ b/pvcdomf.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +import os, sys, libvirt, uuid + +# +# Generic function helpers for PVC +# + +# > lookupByUUID +# This function is a wrapper for libvirt.lookupByUUID which fixes some problems +# 1. Takes a text UUID and handles converting it to bytes +# 2. Disables stdout to avoid stupid printouts +# 3. Try's it and returns a sensible value if not +def lookupByUUID(conn, tuuid): + dom = None + + # Convert the text UUID to bytes + buuid = uuid.UUID(tuuid).bytes + + # Disable stdout + sys.stdout = open(os.devnull, 'w') + + # Try + try: + # Lookup the UUID + dom = conn.lookupByUUID(buuid) + # Fail + except: + # Just pass + pass + + # Enable stdout + sys.stdout = sys.__stdout__ + + # Return the dom object (or None) + return dom +