2018-06-04 01:09:42 -04:00
|
|
|
#!/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
|
2018-06-04 01:39:39 -04:00
|
|
|
# 2. Try's it and returns a sensible value if not
|
2018-06-04 01:22:18 -04:00
|
|
|
def lookupByUUID(tuuid):
|
2018-06-04 01:35:23 -04:00
|
|
|
conn = None
|
2018-06-04 01:09:42 -04:00
|
|
|
dom = None
|
2018-06-04 01:35:23 -04:00
|
|
|
libvirt_name = "qemu:///system"
|
2018-06-04 01:09:42 -04:00
|
|
|
|
|
|
|
# Convert the text UUID to bytes
|
|
|
|
buuid = uuid.UUID(tuuid).bytes
|
|
|
|
|
|
|
|
# Try
|
|
|
|
try:
|
2018-06-04 01:22:18 -04:00
|
|
|
# Open a libvirt connection
|
|
|
|
conn = libvirt.open(libvirt_name)
|
|
|
|
if conn == None:
|
|
|
|
print('>>> %s - Failed to open local libvirt connection.' % self.domuuid)
|
|
|
|
return dom
|
|
|
|
|
2018-06-04 01:09:42 -04:00
|
|
|
# Lookup the UUID
|
|
|
|
dom = conn.lookupByUUID(buuid)
|
2018-06-04 01:22:18 -04:00
|
|
|
|
2018-06-04 01:09:42 -04:00
|
|
|
# Fail
|
|
|
|
except:
|
2018-06-04 01:35:23 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
# After everything
|
|
|
|
finally:
|
2018-06-04 01:22:18 -04:00
|
|
|
# Close the libvirt connection
|
|
|
|
if conn != None:
|
|
|
|
conn.close()
|
2018-06-04 01:09:42 -04:00
|
|
|
|
|
|
|
# Return the dom object (or None)
|
|
|
|
return dom
|
|
|
|
|