Mass rename of connection variables to sensible names

This commit is contained in:
Joshua Boniface 2018-06-17 21:55:39 -04:00
parent da474125ba
commit 4444bca39d
4 changed files with 279 additions and 278 deletions

330
pvc.py
View File

@ -50,13 +50,13 @@ def validateUUID(dom_uuid):
# Connect and disconnect from Zookeeper # Connect and disconnect from Zookeeper
# #
def startZKConnection(zk_host): def startZKConnection(zk_host):
zk = kazoo.client.KazooClient(hosts=zk_host) zk_conn = kazoo.client.KazooClient(hosts=zk_host)
zk.start() zk_conn.start()
return zk return zk_conn
def stopZKConnection(zk): def stopZKConnection(zk_conn):
zk.stop() zk_conn.stop()
zk.close() zk_conn.close()
return 0 return 0
@ -149,9 +149,9 @@ def getDomainControllers(parsed_xml):
return dcontrollers return dcontrollers
# Parse an XML object # Parse an XML object
def getDomainXML(zk, dom_uuid): def getDomainXML(zk_conn, dom_uuid):
try: try:
xml = zk.get('/domains/%s/xml' % dom_uuid)[0].decode('ascii') xml = zk_conn.get('/domains/%s/xml' % dom_uuid)[0].decode('ascii')
except: except:
return None return None
@ -160,21 +160,21 @@ def getDomainXML(zk, dom_uuid):
return parsed_xml return parsed_xml
# Root functions # Root functions
def getInformationFromNode(zk, node_name, long_output): def getInformationFromNode(zk_conn, node_name, long_output):
node_daemon_state = zk.get('/nodes/{}/daemonstate'.format(node_name))[0].decode('ascii') node_daemon_state = zk_conn.get('/nodes/{}/daemonstate'.format(node_name))[0].decode('ascii')
node_domain_state = zk.get('/nodes/{}/domainstate'.format(node_name))[0].decode('ascii') node_domain_state = zk_conn.get('/nodes/{}/domainstate'.format(node_name))[0].decode('ascii')
node_cpu_count = zk.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[0] node_cpu_count = zk_conn.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[0]
node_kernel = zk.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[1] node_kernel = zk_conn.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[1]
node_os = zk.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[2] node_os = zk_conn.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[2]
node_arch = zk.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[3] node_arch = zk_conn.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[3]
node_mem_used = zk.get('/nodes/{}/memused'.format(node_name))[0].decode('ascii') node_mem_used = zk_conn.get('/nodes/{}/memused'.format(node_name))[0].decode('ascii')
node_mem_free = zk.get('/nodes/{}/memfree'.format(node_name))[0].decode('ascii') node_mem_free = zk_conn.get('/nodes/{}/memfree'.format(node_name))[0].decode('ascii')
node_mem_total = int(node_mem_used) + int(node_mem_free) node_mem_total = int(node_mem_used) + int(node_mem_free)
node_domains_count = zk.get('/nodes/{}/domainscount'.format(node_name))[0].decode('ascii') node_domains_count = zk_conn.get('/nodes/{}/domainscount'.format(node_name))[0].decode('ascii')
node_running_domains = zk.get('/nodes/{}/runningdomains'.format(node_name))[0].decode('ascii').split() node_running_domains = zk_conn.get('/nodes/{}/runningdomains'.format(node_name))[0].decode('ascii').split()
node_mem_allocated = 0 node_mem_allocated = 0
for domain in node_running_domains: for domain in node_running_domains:
parsed_xml = getDomainXML(zk, domain) parsed_xml = getDomainXML(zk_conn, domain)
duuid, dname, dmemory, dvcpu, dvcputopo = getDomainMainDetails(parsed_xml) duuid, dname, dmemory, dvcpu, dvcputopo = getDomainMainDetails(parsed_xml)
node_mem_allocated += int(dmemory) node_mem_allocated += int(dmemory)
@ -220,19 +220,19 @@ def getInformationFromNode(zk, node_name, long_output):
return information return information
def getInformationFromXML(zk, uuid, long_output): def getInformationFromXML(zk_conn, uuid, long_output):
# Obtain the contents of the XML from Zookeeper # Obtain the contents of the XML from Zookeeper
try: try:
dstate = zk.get('/domains/{}/state'.format(uuid))[0].decode('ascii') dstate = zk_conn.get('/domains/{}/state'.format(uuid))[0].decode('ascii')
dhypervisor = zk.get('/domains/{}/hypervisor'.format(uuid))[0].decode('ascii') dhypervisor = zk_conn.get('/domains/{}/hypervisor'.format(uuid))[0].decode('ascii')
dlasthypervisor = zk.get('/domains/{}/lasthypervisor'.format(uuid))[0].decode('ascii') dlasthypervisor = zk_conn.get('/domains/{}/lasthypervisor'.format(uuid))[0].decode('ascii')
except: except:
return None return None
if dlasthypervisor == '': if dlasthypervisor == '':
dlasthypervisor = 'N/A' dlasthypervisor = 'N/A'
parsed_xml = getDomainXML(zk, uuid) parsed_xml = getDomainXML(zk_conn, uuid)
duuid, dname, dmemory, dvcpu, dvcputopo = getDomainMainDetails(parsed_xml) duuid, dname, dmemory, dvcpu, dvcputopo = getDomainMainDetails(parsed_xml)
if long_output == True: if long_output == True:
@ -307,19 +307,19 @@ def getInformationFromXML(zk, uuid, long_output):
# #
# Cluster search functions # Cluster search functions
# #
def getClusterDomainList(zk): def getClusterDomainList(zk_conn):
# Get a list of UUIDs by listing the children of /domains # Get a list of UUIDs by listing the children of /domains
uuid_list = zk.get_children('/domains') uuid_list = zk_conn.get_children('/domains')
name_list = [] name_list = []
# For each UUID, get the corresponding name from the data # For each UUID, get the corresponding name from the data
for uuid in uuid_list: for uuid in uuid_list:
name_list.append(zk.get('/domains/%s' % uuid)[0].decode('ascii')) name_list.append(zk_conn.get('/domains/%s' % uuid)[0].decode('ascii'))
return uuid_list, name_list return uuid_list, name_list
def searchClusterByUUID(zk, uuid): def searchClusterByUUID(zk_conn, uuid):
try: try:
# Get the lists # Get the lists
uuid_list, name_list = getClusterDomainList(zk) uuid_list, name_list = getClusterDomainList(zk_conn)
# We're looking for UUID, so find that element ID # We're looking for UUID, so find that element ID
index = uuid_list.index(uuid) index = uuid_list.index(uuid)
# Get the name_list element at that index # Get the name_list element at that index
@ -330,10 +330,10 @@ def searchClusterByUUID(zk, uuid):
return name return name
def searchClusterByName(zk, name): def searchClusterByName(zk_conn, name):
try: try:
# Get the lists # Get the lists
uuid_list, name_list = getClusterDomainList(zk) uuid_list, name_list = getClusterDomainList(zk_conn)
# We're looking for name, so find that element ID # We're looking for name, so find that element ID
index = name_list.index(name) index = name_list.index(name)
# Get the uuid_list element at that index # Get the uuid_list element at that index
@ -382,11 +382,11 @@ def flush_host(node):
""" """
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Verify node is valid # Verify node is valid
try: try:
zk.get('/nodes/{}'.format(node)) zk_conn.get('/nodes/{}'.format(node))
except: except:
click.echo('ERROR: No node named {} is present in the cluster.'.format(node)) click.echo('ERROR: No node named {} is present in the cluster.'.format(node))
exit(1) exit(1)
@ -394,12 +394,12 @@ def flush_host(node):
click.echo('Flushing hypervisor {} of running VMs.'.format(node)) click.echo('Flushing hypervisor {} of running VMs.'.format(node))
# Add the new domain to Zookeeper # Add the new domain to Zookeeper
transaction = zk.transaction() transaction = zk_conn.transaction()
transaction.set_data('/nodes/{}/domainstate'.format(node), 'flush'.encode('ascii')) transaction.set_data('/nodes/{}/domainstate'.format(node), 'flush'.encode('ascii'))
results = transaction.commit() results = transaction.commit()
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -415,11 +415,11 @@ def ready_host(node):
""" """
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Verify node is valid # Verify node is valid
try: try:
zk.get('/nodes/{}'.format(node)) zk_conn.get('/nodes/{}'.format(node))
except: except:
click.echo('ERROR: No node named {} is present in the cluster.'.format(node)) click.echo('ERROR: No node named {} is present in the cluster.'.format(node))
exit(1) exit(1)
@ -427,12 +427,12 @@ def ready_host(node):
click.echo('Restoring hypervisor {} to active service.'.format(node)) click.echo('Restoring hypervisor {} to active service.'.format(node))
# Add the new domain to Zookeeper # Add the new domain to Zookeeper
transaction = zk.transaction() transaction = zk_conn.transaction()
transaction.set_data('/nodes/{}/domainstate'.format(node), 'unflush'.encode('ascii')) transaction.set_data('/nodes/{}/domainstate'.format(node), 'unflush'.encode('ascii'))
results = transaction.commit() results = transaction.commit()
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -452,17 +452,17 @@ def node_info(node, long_output):
""" """
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Verify node is valid # Verify node is valid
try: try:
zk.get('/nodes/{}'.format(node)) zk_conn.get('/nodes/{}'.format(node))
except: except:
click.echo('ERROR: No node named {} is present in the cluster.'.format(node)) click.echo('ERROR: No node named {} is present in the cluster.'.format(node))
exit(1) exit(1)
# Get information about node in a pretty format # Get information about node in a pretty format
information = getInformationFromNode(zk, node, long_output) information = getInformationFromNode(zk_conn, node, long_output)
if information == None: if information == None:
click.echo('ERROR: Could not find a node matching that name.') click.echo('ERROR: Could not find a node matching that name.')
@ -478,7 +478,7 @@ def node_info(node, long_output):
get_vm_list(node) get_vm_list(node)
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -491,9 +491,9 @@ def node_list():
""" """
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
node_list = zk.get_children('/nodes') node_list = zk_conn.get_children('/nodes')
node_list_output = [] node_list_output = []
node_daemon_state = {} node_daemon_state = {}
node_daemon_state = {} node_daemon_state = {}
@ -508,17 +508,17 @@ def node_list():
# Gather information for printing # Gather information for printing
for node_name in node_list: for node_name in node_list:
node_daemon_state[node_name] = zk.get('/nodes/{}/daemonstate'.format(node_name))[0].decode('ascii') node_daemon_state[node_name] = zk_conn.get('/nodes/{}/daemonstate'.format(node_name))[0].decode('ascii')
node_domain_state[node_name] = zk.get('/nodes/{}/domainstate'.format(node_name))[0].decode('ascii') node_domain_state[node_name] = zk_conn.get('/nodes/{}/domainstate'.format(node_name))[0].decode('ascii')
node_cpu_count[node_name] = zk.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[0] node_cpu_count[node_name] = zk_conn.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[0]
node_mem_used[node_name] = zk.get('/nodes/{}/memused'.format(node_name))[0].decode('ascii') node_mem_used[node_name] = zk_conn.get('/nodes/{}/memused'.format(node_name))[0].decode('ascii')
node_mem_free[node_name] = zk.get('/nodes/{}/memfree'.format(node_name))[0].decode('ascii') node_mem_free[node_name] = zk_conn.get('/nodes/{}/memfree'.format(node_name))[0].decode('ascii')
node_mem_total[node_name] = int(node_mem_used[node_name]) + int(node_mem_free[node_name]) node_mem_total[node_name] = int(node_mem_used[node_name]) + int(node_mem_free[node_name])
node_domains_count[node_name] = zk.get('/nodes/{}/domainscount'.format(node_name))[0].decode('ascii') node_domains_count[node_name] = zk_conn.get('/nodes/{}/domainscount'.format(node_name))[0].decode('ascii')
node_running_domains[node_name] = zk.get('/nodes/{}/runningdomains'.format(node_name))[0].decode('ascii').split() node_running_domains[node_name] = zk_conn.get('/nodes/{}/runningdomains'.format(node_name))[0].decode('ascii').split()
node_mem_allocated[node_name] = 0 node_mem_allocated[node_name] = 0
for domain in node_running_domains[node_name]: for domain in node_running_domains[node_name]:
parsed_xml = getDomainXML(zk, domain) parsed_xml = getDomainXML(zk_conn, domain)
duuid, dname, dmemory, dvcpu, dvcputopo = getDomainMainDetails(parsed_xml) duuid, dname, dmemory, dvcpu, dvcputopo = getDomainMainDetails(parsed_xml)
node_mem_allocated[node_name] += int(dmemory) node_mem_allocated[node_name] += int(dmemory)
@ -599,7 +599,7 @@ RAM (MiB): {node_mem_total: <6} {node_mem_used: <6} {node_mem_free: <6} {node_me
click.echo('\n'.join(sorted(node_list_output))) click.echo('\n'.join(sorted(node_list_output)))
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -640,10 +640,10 @@ def define_vm(config, target_hypervisor):
click.echo('Adding new VM with Name "{}" and UUID "{}" to database.'.format(dom_name, dom_uuid)) click.echo('Adding new VM with Name "{}" and UUID "{}" to database.'.format(dom_name, dom_uuid))
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Add the new domain to Zookeeper # Add the new domain to Zookeeper
transaction = zk.transaction() transaction = zk_conn.transaction()
transaction.create('/domains/{}'.format(dom_uuid), dom_name.encode('ascii')) transaction.create('/domains/{}'.format(dom_uuid), dom_name.encode('ascii'))
transaction.create('/domains/{}/state'.format(dom_uuid), 'stop'.encode('ascii')) transaction.create('/domains/{}/state'.format(dom_uuid), 'stop'.encode('ascii'))
transaction.create('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii')) transaction.create('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii'))
@ -652,7 +652,7 @@ def define_vm(config, target_hypervisor):
results = transaction.commit() results = transaction.commit()
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -673,26 +673,26 @@ def undefine_vm(domain):
return return
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Validate and obtain alternate passed value # Validate and obtain alternate passed value
if validateUUID(domain): if validateUUID(domain):
dom_name = searchClusterByUUID(zk, domain) dom_name = searchClusterByUUID(zk_conn, domain)
dom_uuid = searchClusterByName(zk, dom_name) dom_uuid = searchClusterByName(zk_conn, dom_name)
else: else:
dom_uuid = searchClusterByName(zk, domain) dom_uuid = searchClusterByName(zk_conn, domain)
dom_name = searchClusterByUUID(zk, dom_uuid) dom_name = searchClusterByUUID(zk_conn, dom_uuid)
if dom_uuid == None: if dom_uuid == None:
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain))
stopZKConnection(zk) stopZKConnection(zk_conn)
return return
current_vm_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_vm_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if current_vm_state != 'stop': if current_vm_state != 'stop':
click.echo('Forcibly stopping VM "{}".'.format(dom_uuid)) click.echo('Forcibly stopping VM "{}".'.format(dom_uuid))
# Set the domain into stop mode # Set the domain into stop mode
transaction = zk.transaction() transaction = zk_conn.transaction()
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'stop'.encode('ascii')) transaction.set_data('/domains/{}/state'.format(dom_uuid), 'stop'.encode('ascii'))
transaction.commit() transaction.commit()
@ -702,11 +702,11 @@ def undefine_vm(domain):
# Gracefully terminate the class instances # Gracefully terminate the class instances
click.echo('Deleting VM "{}" from nodes.'.format(dom_uuid)) click.echo('Deleting VM "{}" from nodes.'.format(dom_uuid))
zk.set('/domains/{}/state'.format(dom_uuid), 'delete'.encode('ascii')) zk_conn.set('/domains/{}/state'.format(dom_uuid), 'delete'.encode('ascii'))
time.sleep(5) time.sleep(5)
# Delete the configurations # Delete the configurations
click.echo('Undefining VM "{}".'.format(dom_uuid)) click.echo('Undefining VM "{}".'.format(dom_uuid))
transaction = zk.transaction() transaction = zk_conn.transaction()
transaction.delete('/domains/{}/state'.format(dom_uuid)) transaction.delete('/domains/{}/state'.format(dom_uuid))
transaction.delete('/domains/{}/hypervisor'.format(dom_uuid)) transaction.delete('/domains/{}/hypervisor'.format(dom_uuid))
transaction.delete('/domains/{}/lasthypervisor'.format(dom_uuid)) transaction.delete('/domains/{}/lasthypervisor'.format(dom_uuid))
@ -715,7 +715,7 @@ def undefine_vm(domain):
transaction.commit() transaction.commit()
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -731,27 +731,27 @@ def start_vm(domain):
""" """
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Validate and obtain alternate passed value # Validate and obtain alternate passed value
if validateUUID(domain): if validateUUID(domain):
dom_name = searchClusterByUUID(zk, domain) dom_name = searchClusterByUUID(zk_conn, domain)
dom_uuid = searchClusterByName(zk, dom_name) dom_uuid = searchClusterByName(zk_conn, dom_name)
else: else:
dom_uuid = searchClusterByName(zk, domain) dom_uuid = searchClusterByName(zk_conn, domain)
dom_name = searchClusterByUUID(zk, dom_uuid) dom_name = searchClusterByUUID(zk_conn, dom_uuid)
if dom_uuid == None: if dom_uuid == None:
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain))
stopZKConnection(zk) stopZKConnection(zk_conn)
return return
# Set the VM to start # Set the VM to start
click.echo('Starting VM "{}".'.format(dom_uuid)) click.echo('Starting VM "{}".'.format(dom_uuid))
zk.set('/domains/%s/state' % dom_uuid, 'start'.encode('ascii')) zk_conn.set('/domains/%s/state' % dom_uuid, 'start'.encode('ascii'))
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -767,33 +767,33 @@ def restart_vm(domain):
""" """
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Validate and obtain alternate passed value # Validate and obtain alternate passed value
if validateUUID(domain): if validateUUID(domain):
dom_name = searchClusterByUUID(zk, domain) dom_name = searchClusterByUUID(zk_conn, domain)
dom_uuid = searchClusterByName(zk, dom_name) dom_uuid = searchClusterByName(zk_conn, dom_name)
else: else:
dom_uuid = searchClusterByName(zk, domain) dom_uuid = searchClusterByName(zk_conn, domain)
dom_name = searchClusterByUUID(zk, dom_uuid) dom_name = searchClusterByUUID(zk_conn, dom_uuid)
if dom_uuid == None: if dom_uuid == None:
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain))
stopZKConnection(zk) stopZKConnection(zk_conn)
return return
# Get state and verify we're OK to proceed # Get state and verify we're OK to proceed
current_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if current_state != 'start': if current_state != 'start':
click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid)) click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid))
return return
# Set the VM to start # Set the VM to start
click.echo('Restarting VM "{}".'.format(dom_uuid)) click.echo('Restarting VM "{}".'.format(dom_uuid))
zk.set('/domains/%s/state' % dom_uuid, 'restart'.encode('ascii')) zk_conn.set('/domains/%s/state' % dom_uuid, 'restart'.encode('ascii'))
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -809,33 +809,33 @@ def shutdown_vm(domain):
""" """
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Validate and obtain alternate passed value # Validate and obtain alternate passed value
if validateUUID(domain): if validateUUID(domain):
dom_name = searchClusterByUUID(zk, domain) dom_name = searchClusterByUUID(zk_conn, domain)
dom_uuid = searchClusterByName(zk, dom_name) dom_uuid = searchClusterByName(zk_conn, dom_name)
else: else:
dom_uuid = searchClusterByName(zk, domain) dom_uuid = searchClusterByName(zk_conn, domain)
dom_name = searchClusterByUUID(zk, dom_uuid) dom_name = searchClusterByUUID(zk_conn, dom_uuid)
if dom_uuid == None: if dom_uuid == None:
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain))
stopZKConnection(zk) stopZKConnection(zk_conn)
return return
# Get state and verify we're OK to proceed # Get state and verify we're OK to proceed
current_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if current_state != 'start': if current_state != 'start':
click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid)) click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid))
return return
# Set the VM to shutdown # Set the VM to shutdown
click.echo('Shutting down VM "{}".'.format(dom_uuid)) click.echo('Shutting down VM "{}".'.format(dom_uuid))
zk.set('/domains/%s/state' % dom_uuid, 'shutdown'.encode('ascii')) zk_conn.set('/domains/%s/state' % dom_uuid, 'shutdown'.encode('ascii'))
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -851,33 +851,33 @@ def stop_vm(domain):
""" """
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Validate and obtain alternate passed value # Validate and obtain alternate passed value
if validateUUID(domain): if validateUUID(domain):
dom_name = searchClusterByUUID(zk, domain) dom_name = searchClusterByUUID(zk_conn, domain)
dom_uuid = searchClusterByName(zk, dom_name) dom_uuid = searchClusterByName(zk_conn, dom_name)
else: else:
dom_uuid = searchClusterByName(zk, domain) dom_uuid = searchClusterByName(zk_conn, domain)
dom_name = searchClusterByUUID(zk, dom_uuid) dom_name = searchClusterByUUID(zk_conn, dom_uuid)
if dom_uuid == None: if dom_uuid == None:
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain))
stopZKConnection(zk) stopZKConnection(zk_conn)
return return
# Get state and verify we're OK to proceed # Get state and verify we're OK to proceed
current_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if current_state != 'start': if current_state != 'start':
click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid)) click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid))
return return
# Set the VM to start # Set the VM to start
click.echo('Forcibly stopping VM "{}".'.format(dom_uuid)) click.echo('Forcibly stopping VM "{}".'.format(dom_uuid))
zk.set('/domains/%s/state' % dom_uuid, 'stop'.encode('ascii')) zk_conn.set('/domains/%s/state' % dom_uuid, 'stop'.encode('ascii'))
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -897,40 +897,40 @@ def move_vm(domain, target_hypervisor):
""" """
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Validate and obtain alternate passed value # Validate and obtain alternate passed value
if validateUUID(domain): if validateUUID(domain):
dom_name = searchClusterByUUID(zk, domain) dom_name = searchClusterByUUID(zk_conn, domain)
dom_uuid = searchClusterByName(zk, dom_name) dom_uuid = searchClusterByName(zk_conn, dom_name)
else: else:
dom_uuid = searchClusterByName(zk, domain) dom_uuid = searchClusterByName(zk_conn, domain)
dom_name = searchClusterByUUID(zk, dom_uuid) dom_name = searchClusterByUUID(zk_conn, dom_uuid)
if dom_uuid == None: if dom_uuid == None:
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain))
stopZKConnection(zk) stopZKConnection(zk_conn)
return return
# Get state and verify we're OK to proceed # Get state and verify we're OK to proceed
current_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if current_state != 'start': if current_state != 'start':
click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid)) click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid))
return return
current_hypervisor = zk.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii') current_hypervisor = zk_conn.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii')
if target_hypervisor == None: if target_hypervisor == None:
# Determine the best hypervisor to migrate the VM to based on active memory usage # Determine the best hypervisor to migrate the VM to based on active memory usage
hypervisor_list = zk.get_children('/nodes') hypervisor_list = zk_conn.get_children('/nodes')
most_memfree = 0 most_memfree = 0
for hypervisor in hypervisor_list: for hypervisor in hypervisor_list:
daemon_state = zk.get('/nodes/{}/daemonstate'.format(hypervisor))[0].decode('ascii') daemon_state = zk_conn.get('/nodes/{}/daemonstate'.format(hypervisor))[0].decode('ascii')
domain_state = zk.get('/nodes/{}/domainstate'.format(hypervisor))[0].decode('ascii') domain_state = zk_conn.get('/nodes/{}/domainstate'.format(hypervisor))[0].decode('ascii')
if daemon_state != 'run' or domain_state != 'ready' or hypervisor == current_hypervisor: if daemon_state != 'run' or domain_state != 'ready' or hypervisor == current_hypervisor:
continue continue
memfree = int(zk.get('/nodes/{}/memfree'.format(hypervisor))[0].decode('ascii')) memfree = int(zk_conn.get('/nodes/{}/memfree'.format(hypervisor))[0].decode('ascii'))
if memfree > most_memfree: if memfree > most_memfree:
most_memfree = memfree most_memfree = memfree
target_hypervisor = hypervisor target_hypervisor = hypervisor
@ -939,23 +939,23 @@ def move_vm(domain, target_hypervisor):
click.echo('ERROR: The VM "{}" is already running on hypervisor "{}".'.format(dom_uuid, current_hypervisor)) click.echo('ERROR: The VM "{}" is already running on hypervisor "{}".'.format(dom_uuid, current_hypervisor))
return return
current_vm_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_vm_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if current_vm_state == 'start': if current_vm_state == 'start':
click.echo('Permanently migrating VM "{}" to hypervisor "{}".'.format(dom_uuid, target_hypervisor)) click.echo('Permanently migrating VM "{}" to hypervisor "{}".'.format(dom_uuid, target_hypervisor))
transaction = zk.transaction() transaction = zk_conn.transaction()
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii')) transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii'))
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii'))
transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii')) transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii'))
transaction.commit() transaction.commit()
else: else:
click.echo('Permanently moving VM "{}" to hypervisor "{}".'.format(dom_uuid, target_hypervisor)) click.echo('Permanently moving VM "{}" to hypervisor "{}".'.format(dom_uuid, target_hypervisor))
transaction = zk.transaction() transaction = zk_conn.transaction()
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii'))
transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii')) transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii'))
transaction.commit() transaction.commit()
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -979,29 +979,29 @@ def migrate_vm(domain, target_hypervisor, force_migrate):
""" """
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Validate and obtain alternate passed value # Validate and obtain alternate passed value
if validateUUID(domain): if validateUUID(domain):
dom_name = searchClusterByUUID(zk, domain) dom_name = searchClusterByUUID(zk_conn, domain)
dom_uuid = searchClusterByName(zk, dom_name) dom_uuid = searchClusterByName(zk_conn, dom_name)
else: else:
dom_uuid = searchClusterByName(zk, domain) dom_uuid = searchClusterByName(zk_conn, domain)
dom_name = searchClusterByUUID(zk, dom_uuid) dom_name = searchClusterByUUID(zk_conn, dom_uuid)
if dom_uuid == None: if dom_uuid == None:
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain))
stopZKConnection(zk) stopZKConnection(zk_conn)
return return
# Get state and verify we're OK to proceed # Get state and verify we're OK to proceed
current_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if current_state != 'start': if current_state != 'start':
click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid)) click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid))
return return
current_hypervisor = zk.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii') current_hypervisor = zk_conn.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii')
last_hypervisor = zk.get('/domains/{}/lasthypervisor'.format(dom_uuid))[0].decode('ascii') last_hypervisor = zk_conn.get('/domains/{}/lasthypervisor'.format(dom_uuid))[0].decode('ascii')
if last_hypervisor != '' and force_migrate != True: if last_hypervisor != '' and force_migrate != True:
click.echo('ERROR: The VM "{}" has been previously migrated.'.format(dom_uuid)) click.echo('ERROR: The VM "{}" has been previously migrated.'.format(dom_uuid))
@ -1012,15 +1012,15 @@ def migrate_vm(domain, target_hypervisor, force_migrate):
if target_hypervisor == None: if target_hypervisor == None:
# Determine the best hypervisor to migrate the VM to based on active memory usage # Determine the best hypervisor to migrate the VM to based on active memory usage
hypervisor_list = zk.get_children('/nodes') hypervisor_list = zk_conn.get_children('/nodes')
most_memfree = 0 most_memfree = 0
for hypervisor in hypervisor_list: for hypervisor in hypervisor_list:
daemon_state = zk.get('/nodes/{}/daemonstate'.format(hypervisor))[0].decode('ascii') daemon_state = zk_conn.get('/nodes/{}/daemonstate'.format(hypervisor))[0].decode('ascii')
domain_state = zk.get('/nodes/{}/domainstate'.format(hypervisor))[0].decode('ascii') domain_state = zk_conn.get('/nodes/{}/domainstate'.format(hypervisor))[0].decode('ascii')
if daemon_state != 'run' or domain_state != 'ready' or hypervisor == current_hypervisor: if daemon_state != 'run' or domain_state != 'ready' or hypervisor == current_hypervisor:
continue continue
memfree = int(zk.get('/nodes/{}/memfree'.format(hypervisor))[0].decode('ascii')) memfree = int(zk_conn.get('/nodes/{}/memfree'.format(hypervisor))[0].decode('ascii'))
if memfree > most_memfree: if memfree > most_memfree:
most_memfree = memfree most_memfree = memfree
target_hypervisor = hypervisor target_hypervisor = hypervisor
@ -1030,14 +1030,14 @@ def migrate_vm(domain, target_hypervisor, force_migrate):
return return
click.echo('Migrating VM "{}" to hypervisor "{}".'.format(dom_uuid, target_hypervisor)) click.echo('Migrating VM "{}" to hypervisor "{}".'.format(dom_uuid, target_hypervisor))
transaction = zk.transaction() transaction = zk_conn.transaction()
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii')) transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii'))
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii'))
transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), current_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), current_hypervisor.encode('ascii'))
transaction.commit() transaction.commit()
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -1053,42 +1053,42 @@ def unmigrate_vm(domain):
""" """
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Validate and obtain alternate passed value # Validate and obtain alternate passed value
if validateUUID(domain): if validateUUID(domain):
dom_name = searchClusterByUUID(zk, domain) dom_name = searchClusterByUUID(zk_conn, domain)
dom_uuid = searchClusterByName(zk, dom_name) dom_uuid = searchClusterByName(zk_conn, dom_name)
else: else:
dom_uuid = searchClusterByName(zk, domain) dom_uuid = searchClusterByName(zk_conn, domain)
dom_name = searchClusterByUUID(zk, dom_uuid) dom_name = searchClusterByUUID(zk_conn, dom_uuid)
if dom_uuid == None: if dom_uuid == None:
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain))
stopZKConnection(zk) stopZKConnection(zk_conn)
return return
# Get state and verify we're OK to proceed # Get state and verify we're OK to proceed
current_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if current_state != 'start': if current_state != 'start':
click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid)) click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid))
return return
target_hypervisor = zk.get('/domains/{}/lasthypervisor'.format(dom_uuid))[0].decode('ascii') target_hypervisor = zk_conn.get('/domains/{}/lasthypervisor'.format(dom_uuid))[0].decode('ascii')
if target_hypervisor == '': if target_hypervisor == '':
click.echo('ERROR: The VM "{}" has not been previously migrated.'.format(dom_uuid)) click.echo('ERROR: The VM "{}" has not been previously migrated.'.format(dom_uuid))
return return
click.echo('Unmigrating VM "{}" back to hypervisor "{}".'.format(dom_uuid, target_hypervisor)) click.echo('Unmigrating VM "{}" back to hypervisor "{}".'.format(dom_uuid, target_hypervisor))
transaction = zk.transaction() transaction = zk_conn.transaction()
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii')) transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii'))
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii'))
transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii')) transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii'))
transaction.commit() transaction.commit()
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -1108,27 +1108,27 @@ def vm_info(domain, long_output):
""" """
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Validate and obtain alternate passed value # Validate and obtain alternate passed value
if validateUUID(domain): if validateUUID(domain):
dom_name = searchClusterByUUID(zk, domain) dom_name = searchClusterByUUID(zk_conn, domain)
dom_uuid = searchClusterByName(zk, dom_name) dom_uuid = searchClusterByName(zk_conn, dom_name)
else: else:
dom_uuid = searchClusterByName(zk, domain) dom_uuid = searchClusterByName(zk_conn, domain)
dom_name = searchClusterByUUID(zk, dom_uuid) dom_name = searchClusterByUUID(zk_conn, dom_uuid)
if dom_uuid == None: if dom_uuid == None:
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain))
stopZKConnection(zk) stopZKConnection(zk_conn)
return return
# Gather information from XML config and print it # Gather information from XML config and print it
information = getInformationFromXML(zk, dom_uuid, long_output) information = getInformationFromXML(zk_conn, dom_uuid, long_output)
click.echo(information) click.echo(information)
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -1149,9 +1149,9 @@ def get_vm_list(hypervisor):
""" """
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
vm_list_raw = zk.get_children('/domains') vm_list_raw = zk_conn.get_children('/domains')
vm_list = [] vm_list = []
vm_list_output = [] vm_list_output = []
@ -1166,7 +1166,7 @@ def get_vm_list(hypervisor):
# If we're limited, remove other nodes' VMs # If we're limited, remove other nodes' VMs
for vm in vm_list_raw: for vm in vm_list_raw:
# Check hypervisor to avoid unneeded ZK calls # Check hypervisor to avoid unneeded ZK calls
vm_hypervisor[vm] = zk.get('/domains/{}/hypervisor'.format(vm))[0].decode('ascii') vm_hypervisor[vm] = zk_conn.get('/domains/{}/hypervisor'.format(vm))[0].decode('ascii')
if hypervisor != None: if hypervisor != None:
if vm_hypervisor[vm] == hypervisor: if vm_hypervisor[vm] == hypervisor:
vm_list.append(vm) vm_list.append(vm)
@ -1175,14 +1175,14 @@ def get_vm_list(hypervisor):
# Gather information for printing # Gather information for printing
for vm in vm_list: for vm in vm_list:
vm_state[vm] = zk.get('/domains/{}/state'.format(vm))[0].decode('ascii') vm_state[vm] = zk_conn.get('/domains/{}/state'.format(vm))[0].decode('ascii')
vm_lasthypervisor = zk.get('/domains/{}/lasthypervisor'.format(vm))[0].decode('ascii') vm_lasthypervisor = zk_conn.get('/domains/{}/lasthypervisor'.format(vm))[0].decode('ascii')
if vm_lasthypervisor != '': if vm_lasthypervisor != '':
vm_migrated[vm] = 'from {}'.format(vm_lasthypervisor) vm_migrated[vm] = 'from {}'.format(vm_lasthypervisor)
else: else:
vm_migrated[vm] = 'no' vm_migrated[vm] = 'no'
vm_xml = getDomainXML(zk, vm) vm_xml = getDomainXML(zk_conn, vm)
vm_uuid[vm], vm_name[vm], vm_memory[vm], vm_vcpu[vm], vm_vcputopo = getDomainMainDetails(vm_xml) vm_uuid[vm], vm_name[vm], vm_memory[vm], vm_vcpu[vm], vm_vcputopo = getDomainMainDetails(vm_xml)
# Determine optimal column widths # Determine optimal column widths
@ -1270,7 +1270,7 @@ def get_vm_list(hypervisor):
click.echo('\n'.join(sorted(vm_list_output))) click.echo('\n'.join(sorted(vm_list_output)))
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
############################################################################### ###############################################################################
@ -1288,23 +1288,23 @@ def init_cluster():
click.echo('Initializing a new cluster with Zookeeper address "{}".'.format(zk_host)) click.echo('Initializing a new cluster with Zookeeper address "{}".'.format(zk_host))
# Open a Zookeeper connection # Open a Zookeeper connection
zk = startZKConnection(zk_host) zk_conn = startZKConnection(zk_host)
# Destroy the existing data # Destroy the existing data
try: try:
zk.delete('/domains', recursive=True) zk_conn.delete('/domains', recursive=True)
zk.delete('nodes', recursive=True) zk_conn.delete('nodes', recursive=True)
except: except:
pass pass
# Create the root keys # Create the root keys
transaction = zk.transaction() transaction = zk_conn.transaction()
transaction.create('/domains', ''.encode('ascii')) transaction.create('/domains', ''.encode('ascii'))
transaction.create('/nodes', ''.encode('ascii')) transaction.create('/nodes', ''.encode('ascii'))
transaction.commit() transaction.commit()
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk) stopZKConnection(zk_conn)
click.echo('Successfully initialized new cluster. Any running PVC daemons will need to be restarted.') click.echo('Successfully initialized new cluster. Any running PVC daemons will need to be restarted.')

63
pvcd.py
View File

@ -97,25 +97,26 @@ config = readConfig(pvcd_config_file, myhostname)
libvirt_check_name = "qemu+tcp://127.0.0.1:16509/system" libvirt_check_name = "qemu+tcp://127.0.0.1:16509/system"
try: try:
print('Connecting to Libvirt instance at {}'.format(libvirt_check_name)) print('Connecting to Libvirt instance at {}'.format(libvirt_check_name))
conn = libvirt.open(libvirt_check_name) lv_conn = libvirt.open(libvirt_check_name)
if conn == None: if lv_conn == None:
raise raise
except: except:
print('ERROR: Failed to open local libvirt connection via TCP; required for PVC!') print('ERROR: Failed to open local libvirt connection via TCP; required for PVC!')
exit(1) exit(1)
lv_conn.close()
# Connect to local zookeeper # Connect to local zookeeper
zk = kazoo.client.KazooClient(hosts=config['zookeeper']) zk_conn = kazoo.client.KazooClient(hosts=config['zookeeper'])
try: try:
print('Connecting to Zookeeper instance at {}'.format(config['zookeeper'])) print('Connecting to Zookeeper instance at {}'.format(config['zookeeper']))
zk.start() zk_conn.start()
except: except:
print('ERROR: Failed to connect to Zookeeper') print('ERROR: Failed to connect to Zookeeper')
exit(1) exit(1)
# Handle zookeeper failures # Handle zookeeper failures
def zk_listener(state): def zk_listener(state):
global zk, update_timer global zk_conn, update_timer
if state == kazoo.client.KazooState.SUSPENDED: if state == kazoo.client.KazooState.SUSPENDED:
ansiiprint.echo('Connection to Zookeeper lost; retrying', '', 'e') ansiiprint.echo('Connection to Zookeeper lost; retrying', '', 'e')
@ -123,10 +124,10 @@ def zk_listener(state):
stopKeepaliveTimer(update_timer) stopKeepaliveTimer(update_timer)
while True: while True:
_zk = kazoo.client.KazooClient(hosts=config['zookeeper']) _zk_conn = kazoo.client.KazooClient(hosts=config['zookeeper'])
try: try:
_zk.start() _zk_conn.start()
zk = _zk zk_conn = _zk_conn
break break
except: except:
time.sleep(1) time.sleep(1)
@ -138,15 +139,15 @@ def zk_listener(state):
else: else:
pass pass
zk.add_listener(zk_listener) zk_conn.add_listener(zk_listener)
# Cleanup function # Cleanup function
def cleanup(signum, frame): def cleanup(signum, frame):
ansiiprint.echo('Terminating daemon', '', 'e') ansiiprint.echo('Terminating daemon', '', 'e')
# Set stop state in Zookeeper # Set stop state in Zookeeper
zk.set('/nodes/{}/daemonstate'.format(myhostname), 'stop'.encode('ascii')) zk_conn.set('/nodes/{}/daemonstate'.format(myhostname), 'stop'.encode('ascii'))
# Close the Zookeeper connection # Close the Zookeeper connection
zk.close() zk_conn.close()
# Stop keepalive thread # Stop keepalive thread
stopKeepaliveTimer(update_timer) stopKeepaliveTimer(update_timer)
# Exit # Exit
@ -175,37 +176,37 @@ print(' {0}OS:{1} {2}'.format(ansiiprint.bold(), ansiiprint.end(), staticdata[2
print(' {0}Kernel:{1} {2}'.format(ansiiprint.bold(), ansiiprint.end(), staticdata[3])) print(' {0}Kernel:{1} {2}'.format(ansiiprint.bold(), ansiiprint.end(), staticdata[3]))
# Check if our node exists in Zookeeper, and create it if not # Check if our node exists in Zookeeper, and create it if not
if zk.exists('/nodes/{}'.format(myhostname)): if zk_conn.exists('/nodes/{}'.format(myhostname)):
print("Node is " + ansiiprint.green() + "present" + ansiiprint.end() + " in Zookeeper") print("Node is " + ansiiprint.green() + "present" + ansiiprint.end() + " in Zookeeper")
# Update static data just in case it's changed # Update static data just in case it's changed
zk.set('/nodes/{}/staticdata'.format(myhostname), ' '.join(staticdata).encode('ascii')) zk_conn.set('/nodes/{}/staticdata'.format(myhostname), ' '.join(staticdata).encode('ascii'))
else: else:
print("Node is " + ansiiprint.red() + "absent" + ansiiprint.end() + " in Zookeeper; adding new node") print("Node is " + ansiiprint.red() + "absent" + ansiiprint.end() + " in Zookeeper; adding new node")
keepalive_time = int(time.time()) keepalive_time = int(time.time())
zk.create('/nodes/{}'.format(myhostname), 'hypervisor'.encode('ascii')) zk_conn.create('/nodes/{}'.format(myhostname), 'hypervisor'.encode('ascii'))
# Basic state information # Basic state information
zk.create('/nodes/{}/daemonstate'.format(myhostname), 'stop'.encode('ascii')) zk_conn.create('/nodes/{}/daemonstate'.format(myhostname), 'stop'.encode('ascii'))
zk.create('/nodes/{}/domainstate'.format(myhostname), 'ready'.encode('ascii')) zk_conn.create('/nodes/{}/domainstate'.format(myhostname), 'ready'.encode('ascii'))
zk.create('/nodes/{}/staticdata'.format(myhostname), ' '.join(staticdata).encode('ascii')) zk_conn.create('/nodes/{}/staticdata'.format(myhostname), ' '.join(staticdata).encode('ascii'))
zk.create('/nodes/{}/memfree'.format(myhostname), '0'.encode('ascii')) zk_conn.create('/nodes/{}/memfree'.format(myhostname), '0'.encode('ascii'))
zk.create('/nodes/{}/memused'.format(myhostname), '0'.encode('ascii')) zk_conn.create('/nodes/{}/memused'.format(myhostname), '0'.encode('ascii'))
zk.create('/nodes/{}/cpuload'.format(myhostname), '0.0'.encode('ascii')) zk_conn.create('/nodes/{}/cpuload'.format(myhostname), '0.0'.encode('ascii'))
zk.create('/nodes/{}/runningdomains'.format(myhostname), ''.encode('ascii')) zk_conn.create('/nodes/{}/runningdomains'.format(myhostname), ''.encode('ascii'))
zk.create('/nodes/{}/domainscount'.format(myhostname), '0'.encode('ascii')) zk_conn.create('/nodes/{}/domainscount'.format(myhostname), '0'.encode('ascii'))
# Keepalives and fencing information # Keepalives and fencing information
zk.create('/nodes/{}/keepalive'.format(myhostname), str(keepalive_time).encode('ascii')) zk_conn.create('/nodes/{}/keepalive'.format(myhostname), str(keepalive_time).encode('ascii'))
zk.create('/nodes/{}/ipmihostname'.format(myhostname), config['ipmi_hostname'].encode('ascii')) zk_conn.create('/nodes/{}/ipmihostname'.format(myhostname), config['ipmi_hostname'].encode('ascii'))
zk.create('/nodes/{}/ipmiusername'.format(myhostname), config['ipmi_username'].encode('ascii')) zk_conn.create('/nodes/{}/ipmiusername'.format(myhostname), config['ipmi_username'].encode('ascii'))
zk.create('/nodes/{}/ipmipassword'.format(myhostname), config['ipmi_password'].encode('ascii')) zk_conn.create('/nodes/{}/ipmipassword'.format(myhostname), config['ipmi_password'].encode('ascii'))
zk.set('/nodes/{}/daemonstate'.format(myhostname), 'init'.encode('ascii')) zk_conn.set('/nodes/{}/daemonstate'.format(myhostname), 'init'.encode('ascii'))
t_node = dict() t_node = dict()
s_domain = dict() s_domain = dict()
node_list = [] node_list = []
domain_list = [] domain_list = []
@zk.ChildrenWatch('/nodes') @zk_conn.ChildrenWatch('/nodes')
def updatenodes(new_node_list): def updatenodes(new_node_list):
global node_list global node_list
node_list = new_node_list node_list = new_node_list
@ -214,16 +215,16 @@ def updatenodes(new_node_list):
if node in t_node: if node in t_node:
t_node[node].updatenodelist(t_node) t_node[node].updatenodelist(t_node)
else: else:
t_node[node] = NodeInstance.NodeInstance(myhostname, node, t_node, s_domain, zk, config) t_node[node] = NodeInstance.NodeInstance(myhostname, node, t_node, s_domain, zk_conn, config)
@zk.ChildrenWatch('/domains') @zk_conn.ChildrenWatch('/domains')
def updatedomains(new_domain_list): def updatedomains(new_domain_list):
global domain_list global domain_list
domain_list = new_domain_list domain_list = new_domain_list
print(ansiiprint.blue() + 'Domain list: ' + ansiiprint.end() + '{}'.format(' '.join(domain_list))) print(ansiiprint.blue() + 'Domain list: ' + ansiiprint.end() + '{}'.format(' '.join(domain_list)))
for domain in domain_list: for domain in domain_list:
if not domain in s_domain: if not domain in s_domain:
s_domain[domain] = VMInstance.VMInstance(domain, zk, config, t_node[myhostname]); s_domain[domain] = VMInstance.VMInstance(domain, zk_conn, config, t_node[myhostname]);
for node in node_list: for node in node_list:
if node in t_node: if node in t_node:
t_node[node].updatedomainlist(s_domain) t_node[node].updatedomainlist(s_domain)

View File

@ -25,9 +25,9 @@ import pvcd.ansiiprint as ansiiprint
class NodeInstance(): class NodeInstance():
# Initialization function # Initialization function
def __init__(self, this_node, name, t_node, s_domain, zk, config): def __init__(self, this_node, name, t_node, s_domain, zk_conn, config):
# Passed-in variables on creation # Passed-in variables on creation
self.zk = zk self.zk_conn = zk_conn
self.config = config self.config = config
self.this_node = this_node self.this_node = this_node
self.name = name self.name = name
@ -46,14 +46,14 @@ class NodeInstance():
self.inflush = False self.inflush = False
# Zookeeper handlers for changed states # Zookeeper handlers for changed states
@zk.DataWatch('/nodes/{}/daemonstate'.format(self.name)) @zk_conn.DataWatch('/nodes/{}/daemonstate'.format(self.name))
def watch_hypervisor_daemonstate(data, stat, event=""): def watch_hypervisor_daemonstate(data, stat, event=""):
try: try:
self.daemon_state = data.decode('ascii') self.daemon_state = data.decode('ascii')
except AttributeError: except AttributeError:
self.daemon_state = 'stop' self.daemon_state = 'stop'
@zk.DataWatch('/nodes/{}/domainstate'.format(self.name)) @zk_conn.DataWatch('/nodes/{}/domainstate'.format(self.name))
def watch_hypervisor_domainstate(data, stat, event=""): def watch_hypervisor_domainstate(data, stat, event=""):
try: try:
self.domain_state = data.decode('ascii') self.domain_state = data.decode('ascii')
@ -68,28 +68,28 @@ class NodeInstance():
self.unflush() self.unflush()
@zk.DataWatch('/nodes/{}/memfree'.format(self.name)) @zk_conn.DataWatch('/nodes/{}/memfree'.format(self.name))
def watch_hypervisor_memfree(data, stat, event=""): def watch_hypervisor_memfree(data, stat, event=""):
try: try:
self.memfree = data.decode('ascii') self.memfree = data.decode('ascii')
except AttributeError: except AttributeError:
self.memfree = 0 self.memfree = 0
@zk.DataWatch('/nodes/{}/memused'.format(self.name)) @zk_conn.DataWatch('/nodes/{}/memused'.format(self.name))
def watch_hypervisor_memused(data, stat, event=""): def watch_hypervisor_memused(data, stat, event=""):
try: try:
self.memused = data.decode('ascii') self.memused = data.decode('ascii')
except AttributeError: except AttributeError:
self.memused = 0 self.memused = 0
@zk.DataWatch('/nodes/{}/runningdomains'.format(self.name)) @zk_conn.DataWatch('/nodes/{}/runningdomains'.format(self.name))
def watch_hypervisor_runningdomains(data, stat, event=""): def watch_hypervisor_runningdomains(data, stat, event=""):
try: try:
self.domain_list = data.decode('ascii').split() self.domain_list = data.decode('ascii').split()
except AttributeError: except AttributeError:
self.domain_list = [] self.domain_list = []
@zk.DataWatch('/nodes/{}/domainscount'.format(self.name)) @zk_conn.DataWatch('/nodes/{}/domainscount'.format(self.name))
def watch_hypervisor_domainscount(data, stat, event=""): def watch_hypervisor_domainscount(data, stat, event=""):
try: try:
self.domains_count = data.decode('ascii') self.domains_count = data.decode('ascii')
@ -130,53 +130,53 @@ class NodeInstance():
for dom_uuid in self.domain_list: for dom_uuid in self.domain_list:
most_memfree = 0 most_memfree = 0
target_hypervisor = None target_hypervisor = None
hypervisor_list = self.zk.get_children('/nodes') hypervisor_list = self.zk_conn.get_children('/nodes')
current_hypervisor = self.zk.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii') current_hypervisor = self.zk_conn.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii')
if current_hypervisor != self.this_node: if current_hypervisor != self.this_node:
continue continue
for hypervisor in hypervisor_list: for hypervisor in hypervisor_list:
daemon_state = self.zk.get('/nodes/{}/daemonstate'.format(hypervisor))[0].decode('ascii') daemon_state = self.zk_conn.get('/nodes/{}/daemonstate'.format(hypervisor))[0].decode('ascii')
domain_state = self.zk.get('/nodes/{}/domainstate'.format(hypervisor))[0].decode('ascii') domain_state = self.zk_conn.get('/nodes/{}/domainstate'.format(hypervisor))[0].decode('ascii')
if hypervisor == current_hypervisor: if hypervisor == current_hypervisor:
continue continue
if daemon_state != 'run' or domain_state != 'ready': if daemon_state != 'run' or domain_state != 'ready':
continue continue
memfree = int(self.zk.get('/nodes/{}/memfree'.format(hypervisor))[0].decode('ascii')) memfree = int(self.zk_conn.get('/nodes/{}/memfree'.format(hypervisor))[0].decode('ascii'))
if memfree > most_memfree: if memfree > most_memfree:
most_memfree = memfree most_memfree = memfree
target_hypervisor = hypervisor target_hypervisor = hypervisor
if target_hypervisor == None: if target_hypervisor == None:
ansiiprint.echo('Failed to find migration target for VM "{}"; shutting down'.format(dom_uuid), '', 'e') ansiiprint.echo('Failed to find migration target for VM "{}"; shutting down'.format(dom_uuid), '', 'e')
transaction = self.zk.transaction() transaction = self.zk_conn.transaction()
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'shutdown'.encode('ascii')) transaction.set_data('/domains/{}/state'.format(dom_uuid), 'shutdown'.encode('ascii'))
transaction.commit() transaction.commit()
else: else:
ansiiprint.echo('Migrating VM "{}" to hypervisor "{}"'.format(dom_uuid, target_hypervisor), '', 'i') ansiiprint.echo('Migrating VM "{}" to hypervisor "{}"'.format(dom_uuid, target_hypervisor), '', 'i')
transaction = self.zk.transaction() transaction = self.zk_conn.transaction()
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii')) transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii'))
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii'))
transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), current_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), current_hypervisor.encode('ascii'))
transaction.commit() transaction.commit()
self.zk.set('/nodes/{}/runningdomains'.format(self.name), ''.encode('ascii')) self.zk_conn.set('/nodes/{}/runningdomains'.format(self.name), ''.encode('ascii'))
self.zk.set('/nodes/{}/domainstate'.format(self.name), 'flushed'.encode('ascii')) self.zk_conn.set('/nodes/{}/domainstate'.format(self.name), 'flushed'.encode('ascii'))
self.inflush = False self.inflush = False
def unflush(self): def unflush(self):
self.inflush = True self.inflush = True
ansiiprint.echo('Restoring node {} to active service.'.format(self.name), '', 'i') ansiiprint.echo('Restoring node {} to active service.'.format(self.name), '', 'i')
self.zk.set('/nodes/{}/domainstate'.format(self.name), 'ready'.encode('ascii')) self.zk_conn.set('/nodes/{}/domainstate'.format(self.name), 'ready'.encode('ascii'))
for dom_uuid in self.s_domain: for dom_uuid in self.s_domain:
last_hypervisor = self.zk.get('/domains/{}/lasthypervisor'.format(dom_uuid))[0].decode('ascii') last_hypervisor = self.zk_conn.get('/domains/{}/lasthypervisor'.format(dom_uuid))[0].decode('ascii')
if last_hypervisor != self.name: if last_hypervisor != self.name:
continue continue
ansiiprint.echo('Setting unmigration for VM "{}"'.format(dom_uuid), '', 'i') ansiiprint.echo('Setting unmigration for VM "{}"'.format(dom_uuid), '', 'i')
transaction = self.zk.transaction() transaction = self.zk_conn.transaction()
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii')) transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii'))
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), self.name.encode('ascii')) transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), self.name.encode('ascii'))
transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii')) transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii'))
@ -187,16 +187,16 @@ class NodeInstance():
def update_zookeeper(self): def update_zookeeper(self):
# Connect to libvirt # Connect to libvirt
libvirt_name = "qemu:///system" libvirt_name = "qemu:///system"
conn = libvirt.open(libvirt_name) lv_conn = libvirt.open(libvirt_name)
if conn == None: if lv_conn == None:
ansiiprint.echo('Failed to open connection to "{}"'.format(libvirt_name), '', 'e') ansiiprint.echo('Failed to open connection to "{}"'.format(libvirt_name), '', 'e')
return return
# Get past state and update if needed # Get past state and update if needed
past_state = self.zk.get('/nodes/{}/daemonstate'.format(self.name))[0].decode('ascii') past_state = self.zk_conn.get('/nodes/{}/daemonstate'.format(self.name))[0].decode('ascii')
if past_state != 'run': if past_state != 'run':
self.daemon_state = 'run' self.daemon_state = 'run'
self.zk.set('/nodes/{}/daemonstate'.format(self.name), 'run'.encode('ascii')) self.zk_conn.set('/nodes/{}/daemonstate'.format(self.name), 'run'.encode('ascii'))
else: else:
self.daemon_state = 'run' self.daemon_state = 'run'
@ -210,17 +210,17 @@ class NodeInstance():
raise raise
except Exception as e: except Exception as e:
# Toggle a state "change" # Toggle a state "change"
self.zk.set('/domains/{}/state'.format(domain), instance.getstate().encode('ascii')) self.zk_conn.set('/domains/{}/state'.format(domain), instance.getstate().encode('ascii'))
# Set our information in zookeeper # Set our information in zookeeper
self.name = conn.getHostname() self.name = lv_conn.getHostname()
self.memused = int(psutil.virtual_memory().used / 1024 / 1024) self.memused = int(psutil.virtual_memory().used / 1024 / 1024)
self.memfree = int(psutil.virtual_memory().free / 1024 / 1024) self.memfree = int(psutil.virtual_memory().free / 1024 / 1024)
self.cpuload = os.getloadavg()[0] self.cpuload = os.getloadavg()[0]
self.domains_count = len(conn.listDomainsID()) self.domains_count = len(lv_conn.listDomainsID())
keepalive_time = int(time.time()) keepalive_time = int(time.time())
try: try:
transaction = self.zk.transaction() transaction = self.zk_conn.transaction()
transaction.set_data('/nodes/{}/memused'.format(self.name), str(self.memused).encode('ascii')) transaction.set_data('/nodes/{}/memused'.format(self.name), str(self.memused).encode('ascii'))
transaction.set_data('/nodes/{}/memfree'.format(self.name), str(self.memfree).encode('ascii')) transaction.set_data('/nodes/{}/memfree'.format(self.name), str(self.memfree).encode('ascii'))
transaction.set_data('/nodes/{}/cpuload'.format(self.name), str(self.cpuload).encode('ascii')) transaction.set_data('/nodes/{}/cpuload'.format(self.name), str(self.cpuload).encode('ascii'))
@ -232,7 +232,7 @@ class NodeInstance():
return return
# Close the Libvirt connection # Close the Libvirt connection
conn.close() lv_conn.close()
# Display node information to the terminal # Display node information to the terminal
ansiiprint.echo('{}{} keepalive{}'.format(ansiiprint.purple(), self.name, ansiiprint.end()), '', 't') ansiiprint.echo('{}{} keepalive{}'.format(ansiiprint.purple(), self.name, ansiiprint.end()), '', 't')
@ -241,9 +241,9 @@ class NodeInstance():
# Update our local node lists # Update our local node lists
for node_name in self.t_node: for node_name in self.t_node:
try: try:
node_daemon_state = self.zk.get('/nodes/{}/daemonstate'.format(node_name))[0].decode('ascii') node_daemon_state = self.zk_conn.get('/nodes/{}/daemonstate'.format(node_name))[0].decode('ascii')
node_domain_state = self.zk.get('/nodes/{}/domainstate'.format(node_name))[0].decode('ascii') node_domain_state = self.zk_conn.get('/nodes/{}/domainstate'.format(node_name))[0].decode('ascii')
node_keepalive = int(self.zk.get('/nodes/{}/keepalive'.format(node_name))[0].decode('ascii')) node_keepalive = int(self.zk_conn.get('/nodes/{}/keepalive'.format(node_name))[0].decode('ascii'))
except: except:
node_daemon_state = 'unknown' node_daemon_state = 'unknown'
node_domain_state = 'unknown' node_domain_state = 'unknown'
@ -255,8 +255,8 @@ class NodeInstance():
node_deadtime = int(time.time()) - ( int(self.config['keepalive_interval']) * 6 ) node_deadtime = int(time.time()) - ( int(self.config['keepalive_interval']) * 6 )
if node_keepalive < node_deadtime and node_daemon_state == 'run': if node_keepalive < node_deadtime and node_daemon_state == 'run':
ansiiprint.echo('Node {} seems dead - starting monitor for fencing'.format(node_name), '', 'w') ansiiprint.echo('Node {} seems dead - starting monitor for fencing'.format(node_name), '', 'w')
self.zk.set('/nodes/{}/daemonstate'.format(node_name), 'dead'.encode('ascii')) self.zk_conn.set('/nodes/{}/daemonstate'.format(node_name), 'dead'.encode('ascii'))
fence_thread = threading.Thread(target=fenceNode, args=(node_name, self.zk), kwargs={}) fence_thread = threading.Thread(target=fenceNode, args=(node_name, self.zk_conn), kwargs={})
fence_thread.start() fence_thread.start()
# Update the arrays # Update the arrays
@ -300,13 +300,13 @@ class NodeInstance():
# #
# Fence thread entry function # Fence thread entry function
# #
def fenceNode(node_name, zk): def fenceNode(node_name, zk_conn):
failcount = 0 failcount = 0
while failcount < 3: while failcount < 3:
# Wait 5 seconds # Wait 5 seconds
time.sleep(5) time.sleep(5)
# Get the state # Get the state
node_daemon_state = zk.get('/nodes/{}/daemonstate'.format(node_name))[0].decode('ascii') node_daemon_state = zk_conn.get('/nodes/{}/daemonstate'.format(node_name))[0].decode('ascii')
# Is it still 'dead' # Is it still 'dead'
if node_daemon_state == 'dead': if node_daemon_state == 'dead':
failcount += 1 failcount += 1
@ -318,39 +318,39 @@ def fenceNode(node_name, zk):
ansiiprint.echo('Fencing node "{}" via IPMI reboot signal'.format(node_name), '', 'e') ansiiprint.echo('Fencing node "{}" via IPMI reboot signal'.format(node_name), '', 'e')
ipmi_hostname = zk.get('/nodes/{}/ipmihostname'.format(node_name))[0].decode('ascii') ipmi_hostname = zk_conn.get('/nodes/{}/ipmihostname'.format(node_name))[0].decode('ascii')
ipmi_username = zk.get('/nodes/{}/ipmiusername'.format(node_name))[0].decode('ascii') ipmi_username = zk_conn.get('/nodes/{}/ipmiusername'.format(node_name))[0].decode('ascii')
ipmi_password = zk.get('/nodes/{}/ipmipassword'.format(node_name))[0].decode('ascii') ipmi_password = zk_conn.get('/nodes/{}/ipmipassword'.format(node_name))[0].decode('ascii')
rebootViaIPMI(ipmi_hostname, ipmi_username, ipmi_password) rebootViaIPMI(ipmi_hostname, ipmi_username, ipmi_password)
time.sleep(5) time.sleep(5)
ansiiprint.echo('Moving VMs from dead hypervisor "{}" to new hosts'.format(node_name), '', 'i') ansiiprint.echo('Moving VMs from dead hypervisor "{}" to new hosts'.format(node_name), '', 'i')
dead_node_running_domains = zk.get('/nodes/{}/runningdomains'.format(node_name))[0].decode('ascii').split() dead_node_running_domains = zk_conn.get('/nodes/{}/runningdomains'.format(node_name))[0].decode('ascii').split()
for dom_uuid in dead_node_running_domains: for dom_uuid in dead_node_running_domains:
most_memfree = 0 most_memfree = 0
hypervisor_list = zk.get_children('/nodes') hypervisor_list = zk_conn.get_children('/nodes')
current_hypervisor = zk.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii') current_hypervisor = zk_conn.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii')
for hypervisor in hypervisor_list: for hypervisor in hypervisor_list:
print(hypervisor) print(hypervisor)
daemon_state = zk.get('/nodes/{}/daemonstate'.format(hypervisor))[0].decode('ascii') daemon_state = zk_conn.get('/nodes/{}/daemonstate'.format(hypervisor))[0].decode('ascii')
domain_state = zk.get('/nodes/{}/domainstate'.format(hypervisor))[0].decode('ascii') domain_state = zk_conn.get('/nodes/{}/domainstate'.format(hypervisor))[0].decode('ascii')
if daemon_state != 'run' or domain_state != 'ready': if daemon_state != 'run' or domain_state != 'ready':
continue continue
memfree = int(zk.get('/nodes/{}/memfree'.format(hypervisor))[0].decode('ascii')) memfree = int(zk_conn.get('/nodes/{}/memfree'.format(hypervisor))[0].decode('ascii'))
if memfree > most_memfree: if memfree > most_memfree:
most_memfree = memfree most_memfree = memfree
target_hypervisor = hypervisor target_hypervisor = hypervisor
ansiiprint.echo('Moving VM "{}" to hypervisor "{}"'.format(dom_uuid, target_hypervisor), '', 'i') ansiiprint.echo('Moving VM "{}" to hypervisor "{}"'.format(dom_uuid, target_hypervisor), '', 'i')
transaction = zk.transaction() transaction = zk_conn.transaction()
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'start'.encode('ascii')) transaction.set_data('/domains/{}/state'.format(dom_uuid), 'start'.encode('ascii'))
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii'))
transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), current_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), current_hypervisor.encode('ascii'))
transaction.commit() transaction.commit()
# Set node in flushed state for easy remigrating when it comes back # Set node in flushed state for easy remigrating when it comes back
zk.set('/nodes/{}/domainstate'.format(node_name), 'flushed'.encode('ascii')) zk_conn.set('/nodes/{}/domainstate'.format(node_name), 'flushed'.encode('ascii'))
# #
# Perform an IPMI fence # Perform an IPMI fence

View File

@ -25,10 +25,10 @@ import pvcd.ansiiprint as ansiiprint
class VMInstance: class VMInstance:
# Initialization function # Initialization function
def __init__(self, domuuid, zk, config, thishypervisor): def __init__(self, domuuid, zk_conn, config, thishypervisor):
# Passed-in variables on creation # Passed-in variables on creation
self.domuuid = domuuid self.domuuid = domuuid
self.zk = zk self.zk_conn = zk_conn
self.config = config self.config = config
self.thishypervisor = thishypervisor self.thishypervisor = thishypervisor
@ -45,7 +45,7 @@ class VMInstance:
self.dom = self.lookupByUUID(self.domuuid) self.dom = self.lookupByUUID(self.domuuid)
# Watch for changes to the state field in Zookeeper # Watch for changes to the state field in Zookeeper
@zk.DataWatch('/domains/{}/state'.format(self.domuuid)) @zk_conn.DataWatch('/domains/{}/state'.format(self.domuuid))
def watch_state(data, stat, event=""): def watch_state(data, stat, event=""):
# If we get a delete state, just terminate outselves # If we get a delete state, just terminate outselves
if data == None: if data == None:
@ -71,16 +71,16 @@ class VMInstance:
# Start up a new Libvirt connection # Start up a new Libvirt connection
libvirt_name = "qemu:///system" libvirt_name = "qemu:///system"
conn = libvirt.open(libvirt_name) lv_conn = libvirt.open(libvirt_name)
if conn == None: if lv_conn == None:
ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e') ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e')
self.instart = False self.instart = False
return return
try: try:
# Grab the domain information from Zookeeper # Grab the domain information from Zookeeper
xmlconfig = self.zk.get('/domains/{}/xml'.format(self.domuuid))[0].decode('ascii') xmlconfig = self.zk_conn.get('/domains/{}/xml'.format(self.domuuid))[0].decode('ascii')
dom = conn.createXML(xmlconfig, 0) dom = lv_conn.createXML(xmlconfig, 0)
if not self.domuuid in self.thishypervisor.domain_list: if not self.domuuid in self.thishypervisor.domain_list:
self.thishypervisor.domain_list.append(self.domuuid) self.thishypervisor.domain_list.append(self.domuuid)
@ -88,10 +88,10 @@ class VMInstance:
self.dom = dom self.dom = dom
except libvirt.libvirtError as e: except libvirt.libvirtError as e:
ansiiprint.echo('Failed to create VM', '{}:'.format(self.domuuid), 'e') ansiiprint.echo('Failed to create VM', '{}:'.format(self.domuuid), 'e')
self.zk.set('/domains/{}/state'.format(self.domuuid), 'failed'.encode('ascii')) self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'failed'.encode('ascii'))
self.dom = None self.dom = None
conn.close() lv_conn.close()
self.instart = False self.instart = False
# Restart the VM # Restart the VM
@ -101,8 +101,8 @@ class VMInstance:
# Start up a new Libvirt connection # Start up a new Libvirt connection
libvirt_name = "qemu:///system" libvirt_name = "qemu:///system"
conn = libvirt.open(libvirt_name) lv_conn = libvirt.open(libvirt_name)
if conn == None: if lv_conn == None:
ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e') ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e')
self.inrestart = False self.inrestart = False
return return
@ -114,8 +114,8 @@ class VMInstance:
except libvirt.libvirtError as e: except libvirt.libvirtError as e:
ansiiprint.echo('Failed to restart VM', '{}:'.format(self.domuuid), 'e') ansiiprint.echo('Failed to restart VM', '{}:'.format(self.domuuid), 'e')
self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii')) self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii'))
conn.close() lv_conn.close()
self.inrestart = False self.inrestart = False
# Stop the VM forcibly without updating state # Stop the VM forcibly without updating state
@ -150,7 +150,7 @@ class VMInstance:
pass pass
if self.inrestart == False: if self.inrestart == False:
self.zk.set('/domains/{}/state'.format(self.domuuid), 'stop'.encode('ascii')) self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'stop'.encode('ascii'))
ansiiprint.echo('Successfully stopped VM', '{}:'.format(self.domuuid), 'o') ansiiprint.echo('Successfully stopped VM', '{}:'.format(self.domuuid), 'o')
self.dom = None self.dom = None
@ -182,7 +182,7 @@ class VMInstance:
pass pass
if self.inrestart == False: if self.inrestart == False:
self.zk.set('/domains/{}/state'.format(self.domuuid), 'stop'.encode('ascii')) self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'stop'.encode('ascii'))
ansiiprint.echo('Successfully shutdown VM', '{}:'.format(self.domuuid), 'o') ansiiprint.echo('Successfully shutdown VM', '{}:'.format(self.domuuid), 'o')
self.dom = None self.dom = None
@ -190,24 +190,24 @@ class VMInstance:
def live_migrate_vm(self, dest_hypervisor): def live_migrate_vm(self, dest_hypervisor):
try: try:
dest_conn = libvirt.open('qemu+tcp://{}/system'.format(self.hypervisor)) dest_lv_conn = libvirt.open('qemu+tcp://{}/system'.format(self.hypervisor))
if dest_conn == None: if dest_lv_conn == None:
raise raise
except: except:
ansiiprint.echo('Failed to open connection to qemu+tcp://{}/system; aborting migration.'.format(self.hypervisor), '{}:'.format(self.domuuid), 'e') ansiiprint.echo('Failed to open connection to qemu+tcp://{}/system; aborting migration.'.format(self.hypervisor), '{}:'.format(self.domuuid), 'e')
return 1 return 1
try: try:
target_dom = self.dom.migrate(dest_conn, libvirt.VIR_MIGRATE_LIVE, None, None, 0) target_dom = self.dom.migrate(dest_lv_conn, libvirt.VIR_MIGRATE_LIVE, None, None, 0)
if target_dom == None: if target_dom == None:
raise raise
ansiiprint.echo('Successfully migrated VM', '{}:'.format(self.domuuid), 'o') ansiiprint.echo('Successfully migrated VM', '{}:'.format(self.domuuid), 'o')
except: except:
dest_conn.close() dest_lv_conn.close()
return 1 return 1
dest_conn.close() dest_lv_conn.close()
return 0 return 0
# Migrate the VM to a target host # Migrate the VM to a target host
@ -219,14 +219,14 @@ class VMInstance:
ansiiprint.echo('Could not live migrate VM; shutting down to migrate instead', '{}:'.format(self.domuuid), 'e') ansiiprint.echo('Could not live migrate VM; shutting down to migrate instead', '{}:'.format(self.domuuid), 'e')
self.shutdown_vm() self.shutdown_vm()
time.sleep(1) time.sleep(1)
self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii')) self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii'))
else: else:
try: try:
self.thishypervisor.domain_list.remove(self.domuuid) self.thishypervisor.domain_list.remove(self.domuuid)
except ValueError: except ValueError:
pass pass
time.sleep(1) time.sleep(1)
self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii')) self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii'))
self.inmigrate = False self.inmigrate = False
@ -236,7 +236,7 @@ class VMInstance:
ansiiprint.echo('Receiving migration', '{}:'.format(self.domuuid), 'i') ansiiprint.echo('Receiving migration', '{}:'.format(self.domuuid), 'i')
while True: while True:
time.sleep(0.5) time.sleep(0.5)
self.state = self.zk.get('/domains/{}/state'.format(self.domuuid))[0].decode('ascii') self.state = self.zk_conn.get('/domains/{}/state'.format(self.domuuid))[0].decode('ascii')
self.dom = self.lookupByUUID(self.domuuid) self.dom = self.lookupByUUID(self.domuuid)
if self.dom == None and self.state == 'migrate': if self.dom == None and self.state == 'migrate':
@ -255,10 +255,10 @@ class VMInstance:
if not self.domuuid in self.thishypervisor.domain_list: if not self.domuuid in self.thishypervisor.domain_list:
self.thishypervisor.domain_list.append(self.domuuid) self.thishypervisor.domain_list.append(self.domuuid)
ansiiprint.echo('Successfully received migrated VM', '{}:'.format(self.domuuid), 'o') ansiiprint.echo('Successfully received migrated VM', '{}:'.format(self.domuuid), 'o')
self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii')) self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii'))
else: else:
ansiiprint.echo('Failed to receive migrated VM', '{}:'.format(self.domuuid), 'e') ansiiprint.echo('Failed to receive migrated VM', '{}:'.format(self.domuuid), 'e')
self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii')) self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii'))
self.inreceive = False self.inreceive = False
@ -270,8 +270,8 @@ class VMInstance:
time.sleep(0.2) time.sleep(0.2)
# Get the current values from zookeeper (don't rely on the watch) # Get the current values from zookeeper (don't rely on the watch)
self.state = self.zk.get('/domains/{}/state'.format(self.domuuid))[0].decode('ascii') self.state = self.zk_conn.get('/domains/{}/state'.format(self.domuuid))[0].decode('ascii')
self.hypervisor = self.zk.get('/domains/{}/hypervisor'.format(self.domuuid))[0].decode('ascii') self.hypervisor = self.zk_conn.get('/domains/{}/hypervisor'.format(self.domuuid))[0].decode('ascii')
# Check the current state of the VM # Check the current state of the VM
try: try:
@ -311,7 +311,7 @@ class VMInstance:
self.thishypervisor.domain_list.append(self.domuuid) self.thishypervisor.domain_list.append(self.domuuid)
# VM is already running and should be but stuck in migrate state # VM is already running and should be but stuck in migrate state
elif self.state == "migrate": elif self.state == "migrate":
self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii')) self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii'))
if not self.domuuid in self.thishypervisor.domain_list: if not self.domuuid in self.thishypervisor.domain_list:
self.thishypervisor.domain_list.append(self.domuuid) self.thishypervisor.domain_list.append(self.domuuid)
# VM should be restarted # VM should be restarted
@ -332,7 +332,7 @@ class VMInstance:
self.receive_migrate() self.receive_migrate()
# VM should be restarted (i.e. started since it isn't running) # VM should be restarted (i.e. started since it isn't running)
if self.state == "restart": if self.state == "restart":
self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii')) self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii'))
# VM should be shut down; ensure it's gone from this node's domain_list # VM should be shut down; ensure it's gone from this node's domain_list
elif self.state == "shutdown": elif self.state == "shutdown":
if self.domuuid in self.thishypervisor.domain_list: if self.domuuid in self.thishypervisor.domain_list:
@ -357,7 +357,7 @@ class VMInstance:
# 1. Takes a text UUID and handles converting it to bytes # 1. Takes a text UUID and handles converting it to bytes
# 2. Try's it and returns a sensible value if not # 2. Try's it and returns a sensible value if not
def lookupByUUID(self, tuuid): def lookupByUUID(self, tuuid):
conn = None lv_conn = None
dom = None dom = None
libvirt_name = "qemu:///system" libvirt_name = "qemu:///system"
@ -367,13 +367,13 @@ class VMInstance:
# Try # Try
try: try:
# Open a libvirt connection # Open a libvirt connection
conn = libvirt.open(libvirt_name) lv_conn = libvirt.open(libvirt_name)
if conn == None: if lv_conn == None:
ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e') ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e')
return dom return dom
# Lookup the UUID # Lookup the UUID
dom = conn.lookupByUUID(buuid) dom = lv_conn.lookupByUUID(buuid)
# Fail # Fail
except: except:
@ -382,8 +382,8 @@ class VMInstance:
# After everything # After everything
finally: finally:
# Close the libvirt connection # Close the libvirt connection
if conn != None: if lv_conn != None:
conn.close() lv_conn.close()
# Return the dom object (or None) # Return the dom object (or None)
return dom return dom