2018-06-04 01:09:42 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os, sys, libvirt, uuid
|
2018-06-05 01:39:59 -04:00
|
|
|
import kazoo.client
|
|
|
|
import lxml
|
|
|
|
import click
|
2018-06-05 12:27:41 -04:00
|
|
|
import operator
|
2018-06-05 01:39:59 -04:00
|
|
|
#from click import command, option, Option, UsageError
|
2018-06-04 01:09:42 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2018-06-05 01:39:59 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Connect and disconnect from Zookeeper
|
|
|
|
#
|
|
|
|
def startZKConnection(zk_host):
|
|
|
|
zk = kazoo.client.KazooClient(hosts=zk_host)
|
|
|
|
zk.start()
|
|
|
|
return zk
|
|
|
|
|
|
|
|
def stopZKConnection(zk):
|
|
|
|
zk.stop()
|
|
|
|
zk.close()
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# XML information parsing functions
|
|
|
|
#
|
|
|
|
def getInformationFromXML(zk, uuid):
|
|
|
|
# Obtain the contents of the XML from Zookeeper
|
|
|
|
xml = zk.get('/domains/%s/xml' % uuid)[0].decode('ascii')
|
|
|
|
# Parse XML using lxml.objectify
|
|
|
|
parsed_xml = lxml.objectify.fromstring(xml)
|
|
|
|
# Now get the information we want from it
|
2018-06-05 12:27:41 -04:00
|
|
|
print(lxml.objectify.dump(parsed_xml))
|
2018-06-05 01:39:59 -04:00
|
|
|
dmemory = parsed_xml.memory
|
|
|
|
dmemory_unit = parsed_xml.memory.attrib['unit']
|
|
|
|
dvcpu = parsed_xml.vcpu
|
|
|
|
dtype = parsed_xml.os.type
|
|
|
|
darch = parsed_xml.os.type.attrib['arch']
|
|
|
|
dmachine = parsed_xml.os.type.attrib['machine']
|
|
|
|
dfeatures = []
|
|
|
|
for feature in parsed_xml.features.getchildren():
|
|
|
|
dfeatures.append(feature.tag)
|
2018-06-05 12:27:41 -04:00
|
|
|
dconsole = parsed_xml.devices.console.attrib['type']
|
2018-06-05 01:39:59 -04:00
|
|
|
ddisks = []
|
2018-06-05 12:27:41 -04:00
|
|
|
dnets = []
|
|
|
|
dcontrollers = []
|
|
|
|
for device in parsed_xml.devices.getchildren():
|
|
|
|
if device.tag == 'disk':
|
|
|
|
disk_attrib = device.source.attrib
|
|
|
|
disk_type = device.attrib['type']
|
|
|
|
if disk_type == 'network':
|
|
|
|
disk_obj = { 'type': disk_attrib.get('protocol'), 'name': disk_attrib.get('name') }
|
|
|
|
elif disk_type == 'file':
|
|
|
|
disk_obj = { 'type': 'file', 'name': disk_attrib.get('file') }
|
|
|
|
else:
|
|
|
|
disk_obj = {}
|
|
|
|
ddisks.append(disk_obj)
|
|
|
|
if device.tag == 'interface':
|
|
|
|
net_type = device.attrib['type']
|
|
|
|
net_mac = device.mac.attrib['address']
|
|
|
|
net_bridge = device.source.attrib[net_type]
|
|
|
|
net_model = device.model.attrib['type']
|
|
|
|
net_obj = { 'type': net_type, 'mac': net_mac, 'source': net_bridge, 'model': net_model }
|
|
|
|
dnets.append(net_obj)
|
|
|
|
if device.tag == 'controller':
|
|
|
|
controller_type = device.attrib['type']
|
|
|
|
try:
|
|
|
|
controller_model = device.attrib['model']
|
|
|
|
except KeyError:
|
|
|
|
controller_model = 'none'
|
|
|
|
controller_obj = { 'type': controller_type, 'model': controller_model }
|
|
|
|
dcontrollers.append(controller_obj)
|
2018-06-05 01:39:59 -04:00
|
|
|
|
|
|
|
print(ddisks)
|
2018-06-05 12:27:41 -04:00
|
|
|
print(dnets)
|
|
|
|
print(dcontrollers)
|
2018-06-05 01:39:59 -04:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Cluster search functions
|
|
|
|
#
|
|
|
|
def getClusterDomainList(zk):
|
|
|
|
# Get a list of UUIDs by listing the children of /domains
|
|
|
|
uuid_list = zk.get_children('/domains')
|
|
|
|
name_list = []
|
|
|
|
# For each UUID, get the corresponding name from the data
|
|
|
|
for uuid in uuid_list:
|
|
|
|
name_list.append(zk.get('/domains/%s' % uuid)[0].decode('ascii'))
|
|
|
|
return uuid_list, name_list
|
|
|
|
|
|
|
|
def searchClusterByUUID(zk, uuid):
|
|
|
|
try:
|
|
|
|
# Get the lists
|
|
|
|
uuid_list, name_list = getClusterDomainList(zk)
|
|
|
|
# We're looking for UUID, so find that element ID
|
|
|
|
index = uuid_list.index(uuid)
|
|
|
|
# Get the name_list element at that index
|
|
|
|
name = name_list[index]
|
|
|
|
except ValueError:
|
|
|
|
# We didn't find anything
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
def searchClusterByName(zk, name):
|
|
|
|
try:
|
|
|
|
# Get the lists
|
|
|
|
uuid_list, name_list = getClusterDomainList(zk)
|
|
|
|
# We're looking for name, so find that element ID
|
|
|
|
index = name_list.index(name)
|
|
|
|
# Get the uuid_list element at that index
|
|
|
|
uuid = uuid_list[index]
|
|
|
|
except ValueError:
|
|
|
|
# We didn't find anything
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
return uuid
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Allow mutually exclusive options in Click
|
|
|
|
#
|
|
|
|
class MutuallyExclusiveOption(click.Option):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.mutually_exclusive = set(kwargs.pop('mutually_exclusive', []))
|
|
|
|
help = kwargs.get('help', '')
|
|
|
|
if self.mutually_exclusive:
|
|
|
|
ex_str = ', '.join(self.mutually_exclusive)
|
|
|
|
kwargs['help'] = help + (
|
|
|
|
' NOTE: This argument is mutually exclusive with '
|
|
|
|
'arguments: [' + ex_str + '].'
|
|
|
|
)
|
|
|
|
super(MutuallyExclusiveOption, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def handle_parse_result(self, ctx, opts, args):
|
|
|
|
if self.mutually_exclusive.intersection(opts) and self.name in opts:
|
|
|
|
raise click.UsageError(
|
|
|
|
"Illegal usage: `{}` is mutually exclusive with "
|
|
|
|
"arguments `{}`.".format(
|
|
|
|
self.name,
|
|
|
|
', '.join(self.mutually_exclusive)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return super(MutuallyExclusiveOption, self).handle_parse_result(
|
|
|
|
ctx,
|
|
|
|
opts,
|
|
|
|
args
|
|
|
|
)
|