pvc/pvcf.py

58 lines
1.3 KiB
Python
Raw Normal View History

#!/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(tuuid):
2018-06-04 01:35:23 -04:00
conn = None
dom = None
2018-06-04 01:35:23 -04:00
libvirt_name = "qemu:///system"
# Convert the text UUID to bytes
buuid = uuid.UUID(tuuid).bytes
2018-06-04 01:35:23 -04:00
# Flush and disable stdout and stderr
sys.stdout.flush()
sys.stderr.flush()
sys.stdout = open(os.devnull, 'w')
2018-06-04 01:35:23 -04:00
sys.stderr = open(os.devnull, 'w')
# Try
try:
# Open a libvirt connection
conn = libvirt.open(libvirt_name)
if conn == None:
print('>>> %s - Failed to open local libvirt connection.' % self.domuuid)
return dom
# Lookup the UUID
dom = conn.lookupByUUID(buuid)
# Fail
except:
2018-06-04 01:35:23 -04:00
pass
# After everything
finally:
# Close the libvirt connection
if conn != None:
conn.close()
2018-06-04 01:35:23 -04:00
# Flush and enable stdout and stderr
sys.stdout.flush()
sys.stderr.flush()
sys.stdout = sys.__stdout__
2018-06-04 01:35:23 -04:00
sys.stderr = sys.__stderr__
# Return the dom object (or None)
return dom