Write and use wrapper function for lookupByUUID

This commit is contained in:
Joshua Boniface 2018-06-04 01:09:42 -04:00
parent 89e7a2b5b3
commit 8904e25beb
3 changed files with 42 additions and 6 deletions

View File

@ -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)

View File

@ -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

37
pvcdomf.py Normal file
View File

@ -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