From 4444bca39dcb911304ed8b730ddb198bbb1ae175 Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Sun, 17 Jun 2018 21:55:39 -0400 Subject: [PATCH] Mass rename of connection variables to sensible names --- pvc.py | 330 +++++++++++++++++++++---------------------- pvcd.py | 63 +++++---- pvcd/NodeInstance.py | 94 ++++++------ pvcd/VMInstance.py | 70 ++++----- 4 files changed, 279 insertions(+), 278 deletions(-) diff --git a/pvc.py b/pvc.py index 23d15232..f4eae7e8 100755 --- a/pvc.py +++ b/pvc.py @@ -50,13 +50,13 @@ def validateUUID(dom_uuid): # Connect and disconnect from Zookeeper # def startZKConnection(zk_host): - zk = kazoo.client.KazooClient(hosts=zk_host) - zk.start() - return zk + zk_conn = kazoo.client.KazooClient(hosts=zk_host) + zk_conn.start() + return zk_conn -def stopZKConnection(zk): - zk.stop() - zk.close() +def stopZKConnection(zk_conn): + zk_conn.stop() + zk_conn.close() return 0 @@ -149,9 +149,9 @@ def getDomainControllers(parsed_xml): return dcontrollers # Parse an XML object -def getDomainXML(zk, dom_uuid): +def getDomainXML(zk_conn, dom_uuid): 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: return None @@ -160,21 +160,21 @@ def getDomainXML(zk, dom_uuid): return parsed_xml # Root functions -def getInformationFromNode(zk, node_name, long_output): - node_daemon_state = zk.get('/nodes/{}/daemonstate'.format(node_name))[0].decode('ascii') - node_domain_state = zk.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_kernel = zk.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_arch = zk.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_free = zk.get('/nodes/{}/memfree'.format(node_name))[0].decode('ascii') +def getInformationFromNode(zk_conn, node_name, long_output): + node_daemon_state = zk_conn.get('/nodes/{}/daemonstate'.format(node_name))[0].decode('ascii') + node_domain_state = zk_conn.get('/nodes/{}/domainstate'.format(node_name))[0].decode('ascii') + node_cpu_count = zk_conn.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[0] + node_kernel = zk_conn.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[1] + node_os = zk_conn.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[2] + node_arch = zk_conn.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[3] + node_mem_used = zk_conn.get('/nodes/{}/memused'.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_domains_count = zk.get('/nodes/{}/domainscount'.format(node_name))[0].decode('ascii') - node_running_domains = zk.get('/nodes/{}/runningdomains'.format(node_name))[0].decode('ascii').split() + node_domains_count = zk_conn.get('/nodes/{}/domainscount'.format(node_name))[0].decode('ascii') + node_running_domains = zk_conn.get('/nodes/{}/runningdomains'.format(node_name))[0].decode('ascii').split() node_mem_allocated = 0 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) node_mem_allocated += int(dmemory) @@ -220,19 +220,19 @@ def getInformationFromNode(zk, node_name, long_output): return information -def getInformationFromXML(zk, uuid, long_output): +def getInformationFromXML(zk_conn, uuid, long_output): # Obtain the contents of the XML from Zookeeper try: - dstate = zk.get('/domains/{}/state'.format(uuid))[0].decode('ascii') - dhypervisor = zk.get('/domains/{}/hypervisor'.format(uuid))[0].decode('ascii') - dlasthypervisor = zk.get('/domains/{}/lasthypervisor'.format(uuid))[0].decode('ascii') + dstate = zk_conn.get('/domains/{}/state'.format(uuid))[0].decode('ascii') + dhypervisor = zk_conn.get('/domains/{}/hypervisor'.format(uuid))[0].decode('ascii') + dlasthypervisor = zk_conn.get('/domains/{}/lasthypervisor'.format(uuid))[0].decode('ascii') except: return None if dlasthypervisor == '': dlasthypervisor = 'N/A' - parsed_xml = getDomainXML(zk, uuid) + parsed_xml = getDomainXML(zk_conn, uuid) duuid, dname, dmemory, dvcpu, dvcputopo = getDomainMainDetails(parsed_xml) if long_output == True: @@ -307,19 +307,19 @@ def getInformationFromXML(zk, uuid, long_output): # # Cluster search functions # -def getClusterDomainList(zk): +def getClusterDomainList(zk_conn): # 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 = [] # 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')) + name_list.append(zk_conn.get('/domains/%s' % uuid)[0].decode('ascii')) return uuid_list, name_list -def searchClusterByUUID(zk, uuid): +def searchClusterByUUID(zk_conn, uuid): try: # 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 index = uuid_list.index(uuid) # Get the name_list element at that index @@ -330,10 +330,10 @@ def searchClusterByUUID(zk, uuid): return name -def searchClusterByName(zk, name): +def searchClusterByName(zk_conn, name): try: # 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 index = name_list.index(name) # Get the uuid_list element at that index @@ -382,11 +382,11 @@ def flush_host(node): """ # Open a Zookeeper connection - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # Verify node is valid try: - zk.get('/nodes/{}'.format(node)) + zk_conn.get('/nodes/{}'.format(node)) except: click.echo('ERROR: No node named {} is present in the cluster.'.format(node)) exit(1) @@ -394,12 +394,12 @@ def flush_host(node): click.echo('Flushing hypervisor {} of running VMs.'.format(node)) # Add the new domain to Zookeeper - transaction = zk.transaction() + transaction = zk_conn.transaction() transaction.set_data('/nodes/{}/domainstate'.format(node), 'flush'.encode('ascii')) results = transaction.commit() # Close the Zookeeper connection - stopZKConnection(zk) + stopZKConnection(zk_conn) ############################################################################### @@ -415,11 +415,11 @@ def ready_host(node): """ # Open a Zookeeper connection - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # Verify node is valid try: - zk.get('/nodes/{}'.format(node)) + zk_conn.get('/nodes/{}'.format(node)) except: click.echo('ERROR: No node named {} is present in the cluster.'.format(node)) exit(1) @@ -427,12 +427,12 @@ def ready_host(node): click.echo('Restoring hypervisor {} to active service.'.format(node)) # Add the new domain to Zookeeper - transaction = zk.transaction() + transaction = zk_conn.transaction() transaction.set_data('/nodes/{}/domainstate'.format(node), 'unflush'.encode('ascii')) results = transaction.commit() # Close the Zookeeper connection - stopZKConnection(zk) + stopZKConnection(zk_conn) ############################################################################### @@ -452,17 +452,17 @@ def node_info(node, long_output): """ # Open a Zookeeper connection - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # Verify node is valid try: - zk.get('/nodes/{}'.format(node)) + zk_conn.get('/nodes/{}'.format(node)) except: click.echo('ERROR: No node named {} is present in the cluster.'.format(node)) exit(1) # Get information about node in a pretty format - information = getInformationFromNode(zk, node, long_output) + information = getInformationFromNode(zk_conn, node, long_output) if information == None: 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) # Close the Zookeeper connection - stopZKConnection(zk) + stopZKConnection(zk_conn) ############################################################################### @@ -491,9 +491,9 @@ def node_list(): """ # 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_daemon_state = {} node_daemon_state = {} @@ -508,17 +508,17 @@ def node_list(): # Gather information for printing for node_name in node_list: - node_daemon_state[node_name] = zk.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_cpu_count[node_name] = zk.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_free[node_name] = zk.get('/nodes/{}/memfree'.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_conn.get('/nodes/{}/domainstate'.format(node_name))[0].decode('ascii') + node_cpu_count[node_name] = zk_conn.get('/nodes/{}/staticdata'.format(node_name))[0].decode('ascii').split()[0] + node_mem_used[node_name] = zk_conn.get('/nodes/{}/memused'.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_domains_count[node_name] = zk.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_domains_count[node_name] = zk_conn.get('/nodes/{}/domainscount'.format(node_name))[0].decode('ascii') + node_running_domains[node_name] = zk_conn.get('/nodes/{}/runningdomains'.format(node_name))[0].decode('ascii').split() node_mem_allocated[node_name] = 0 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) 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))) # 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)) # Open a Zookeeper connection - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # 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/{}/state'.format(dom_uuid), 'stop'.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() # Close the Zookeeper connection - stopZKConnection(zk) + stopZKConnection(zk_conn) ############################################################################### @@ -673,26 +673,26 @@ def undefine_vm(domain): return # Open a Zookeeper connection - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # Validate and obtain alternate passed value if validateUUID(domain): - dom_name = searchClusterByUUID(zk, domain) - dom_uuid = searchClusterByName(zk, dom_name) + dom_name = searchClusterByUUID(zk_conn, domain) + dom_uuid = searchClusterByName(zk_conn, dom_name) else: - dom_uuid = searchClusterByName(zk, domain) - dom_name = searchClusterByUUID(zk, dom_uuid) + dom_uuid = searchClusterByName(zk_conn, domain) + dom_name = searchClusterByUUID(zk_conn, dom_uuid) if dom_uuid == None: click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) - stopZKConnection(zk) + stopZKConnection(zk_conn) 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': click.echo('Forcibly stopping VM "{}".'.format(dom_uuid)) # 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.commit() @@ -702,11 +702,11 @@ def undefine_vm(domain): # Gracefully terminate the class instances 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) # Delete the configurations click.echo('Undefining VM "{}".'.format(dom_uuid)) - transaction = zk.transaction() + transaction = zk_conn.transaction() transaction.delete('/domains/{}/state'.format(dom_uuid)) transaction.delete('/domains/{}/hypervisor'.format(dom_uuid)) transaction.delete('/domains/{}/lasthypervisor'.format(dom_uuid)) @@ -715,7 +715,7 @@ def undefine_vm(domain): transaction.commit() # Close the Zookeeper connection - stopZKConnection(zk) + stopZKConnection(zk_conn) ############################################################################### @@ -731,27 +731,27 @@ def start_vm(domain): """ # Open a Zookeeper connection - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # Validate and obtain alternate passed value if validateUUID(domain): - dom_name = searchClusterByUUID(zk, domain) - dom_uuid = searchClusterByName(zk, dom_name) + dom_name = searchClusterByUUID(zk_conn, domain) + dom_uuid = searchClusterByName(zk_conn, dom_name) else: - dom_uuid = searchClusterByName(zk, domain) - dom_name = searchClusterByUUID(zk, dom_uuid) + dom_uuid = searchClusterByName(zk_conn, domain) + dom_name = searchClusterByUUID(zk_conn, dom_uuid) if dom_uuid == None: click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) - stopZKConnection(zk) + stopZKConnection(zk_conn) return # Set the VM to start 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 - stopZKConnection(zk) + stopZKConnection(zk_conn) ############################################################################### @@ -767,33 +767,33 @@ def restart_vm(domain): """ # Open a Zookeeper connection - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # Validate and obtain alternate passed value if validateUUID(domain): - dom_name = searchClusterByUUID(zk, domain) - dom_uuid = searchClusterByName(zk, dom_name) + dom_name = searchClusterByUUID(zk_conn, domain) + dom_uuid = searchClusterByName(zk_conn, dom_name) else: - dom_uuid = searchClusterByName(zk, domain) - dom_name = searchClusterByUUID(zk, dom_uuid) + dom_uuid = searchClusterByName(zk_conn, domain) + dom_name = searchClusterByUUID(zk_conn, dom_uuid) if dom_uuid == None: click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) - stopZKConnection(zk) + stopZKConnection(zk_conn) return # 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': click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid)) return # Set the VM to start 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 - stopZKConnection(zk) + stopZKConnection(zk_conn) ############################################################################### @@ -809,33 +809,33 @@ def shutdown_vm(domain): """ # Open a Zookeeper connection - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # Validate and obtain alternate passed value if validateUUID(domain): - dom_name = searchClusterByUUID(zk, domain) - dom_uuid = searchClusterByName(zk, dom_name) + dom_name = searchClusterByUUID(zk_conn, domain) + dom_uuid = searchClusterByName(zk_conn, dom_name) else: - dom_uuid = searchClusterByName(zk, domain) - dom_name = searchClusterByUUID(zk, dom_uuid) + dom_uuid = searchClusterByName(zk_conn, domain) + dom_name = searchClusterByUUID(zk_conn, dom_uuid) if dom_uuid == None: click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) - stopZKConnection(zk) + stopZKConnection(zk_conn) return # 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': click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid)) return # Set the VM to shutdown 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 - stopZKConnection(zk) + stopZKConnection(zk_conn) ############################################################################### @@ -851,33 +851,33 @@ def stop_vm(domain): """ # Open a Zookeeper connection - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # Validate and obtain alternate passed value if validateUUID(domain): - dom_name = searchClusterByUUID(zk, domain) - dom_uuid = searchClusterByName(zk, dom_name) + dom_name = searchClusterByUUID(zk_conn, domain) + dom_uuid = searchClusterByName(zk_conn, dom_name) else: - dom_uuid = searchClusterByName(zk, domain) - dom_name = searchClusterByUUID(zk, dom_uuid) + dom_uuid = searchClusterByName(zk_conn, domain) + dom_name = searchClusterByUUID(zk_conn, dom_uuid) if dom_uuid == None: click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) - stopZKConnection(zk) + stopZKConnection(zk_conn) return # 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': click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid)) return # Set the VM to start 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 - stopZKConnection(zk) + stopZKConnection(zk_conn) ############################################################################### @@ -897,40 +897,40 @@ def move_vm(domain, target_hypervisor): """ # Open a Zookeeper connection - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # Validate and obtain alternate passed value if validateUUID(domain): - dom_name = searchClusterByUUID(zk, domain) - dom_uuid = searchClusterByName(zk, dom_name) + dom_name = searchClusterByUUID(zk_conn, domain) + dom_uuid = searchClusterByName(zk_conn, dom_name) else: - dom_uuid = searchClusterByName(zk, domain) - dom_name = searchClusterByUUID(zk, dom_uuid) + dom_uuid = searchClusterByName(zk_conn, domain) + dom_name = searchClusterByUUID(zk_conn, dom_uuid) if dom_uuid == None: click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) - stopZKConnection(zk) + stopZKConnection(zk_conn) return # 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': click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid)) 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: # 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 for hypervisor in hypervisor_list: - daemon_state = zk.get('/nodes/{}/daemonstate'.format(hypervisor))[0].decode('ascii') - domain_state = zk.get('/nodes/{}/domainstate'.format(hypervisor))[0].decode('ascii') + daemon_state = zk_conn.get('/nodes/{}/daemonstate'.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: 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: most_memfree = memfree 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)) 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': 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/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii')) transaction.commit() else: 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/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii')) transaction.commit() # 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 - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # Validate and obtain alternate passed value if validateUUID(domain): - dom_name = searchClusterByUUID(zk, domain) - dom_uuid = searchClusterByName(zk, dom_name) + dom_name = searchClusterByUUID(zk_conn, domain) + dom_uuid = searchClusterByName(zk_conn, dom_name) else: - dom_uuid = searchClusterByName(zk, domain) - dom_name = searchClusterByUUID(zk, dom_uuid) + dom_uuid = searchClusterByName(zk_conn, domain) + dom_name = searchClusterByUUID(zk_conn, dom_uuid) if dom_uuid == None: click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) - stopZKConnection(zk) + stopZKConnection(zk_conn) return # 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': click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid)) return - current_hypervisor = zk.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii') - last_hypervisor = zk.get('/domains/{}/lasthypervisor'.format(dom_uuid))[0].decode('ascii') + current_hypervisor = zk_conn.get('/domains/{}/hypervisor'.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: 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: # 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 for hypervisor in hypervisor_list: - daemon_state = zk.get('/nodes/{}/daemonstate'.format(hypervisor))[0].decode('ascii') - domain_state = zk.get('/nodes/{}/domainstate'.format(hypervisor))[0].decode('ascii') + daemon_state = zk_conn.get('/nodes/{}/daemonstate'.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: 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: most_memfree = memfree target_hypervisor = hypervisor @@ -1030,14 +1030,14 @@ def migrate_vm(domain, target_hypervisor, force_migrate): return 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/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), current_hypervisor.encode('ascii')) transaction.commit() # Close the Zookeeper connection - stopZKConnection(zk) + stopZKConnection(zk_conn) ############################################################################### @@ -1053,42 +1053,42 @@ def unmigrate_vm(domain): """ # Open a Zookeeper connection - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # Validate and obtain alternate passed value if validateUUID(domain): - dom_name = searchClusterByUUID(zk, domain) - dom_uuid = searchClusterByName(zk, dom_name) + dom_name = searchClusterByUUID(zk_conn, domain) + dom_uuid = searchClusterByName(zk_conn, dom_name) else: - dom_uuid = searchClusterByName(zk, domain) - dom_name = searchClusterByUUID(zk, dom_uuid) + dom_uuid = searchClusterByName(zk_conn, domain) + dom_name = searchClusterByUUID(zk_conn, dom_uuid) if dom_uuid == None: click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) - stopZKConnection(zk) + stopZKConnection(zk_conn) return # 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': click.echo('ERROR: The VM "{}" is not in "start" state!'.format(dom_uuid)) 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 == '': click.echo('ERROR: The VM "{}" has not been previously migrated.'.format(dom_uuid)) return 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/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii')) transaction.commit() # Close the Zookeeper connection - stopZKConnection(zk) + stopZKConnection(zk_conn) ############################################################################### @@ -1108,27 +1108,27 @@ def vm_info(domain, long_output): """ # Open a Zookeeper connection - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # Validate and obtain alternate passed value if validateUUID(domain): - dom_name = searchClusterByUUID(zk, domain) - dom_uuid = searchClusterByName(zk, dom_name) + dom_name = searchClusterByUUID(zk_conn, domain) + dom_uuid = searchClusterByName(zk_conn, dom_name) else: - dom_uuid = searchClusterByName(zk, domain) - dom_name = searchClusterByUUID(zk, dom_uuid) + dom_uuid = searchClusterByName(zk_conn, domain) + dom_name = searchClusterByUUID(zk_conn, dom_uuid) if dom_uuid == None: click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) - stopZKConnection(zk) + stopZKConnection(zk_conn) return # 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) # Close the Zookeeper connection - stopZKConnection(zk) + stopZKConnection(zk_conn) ############################################################################### @@ -1149,9 +1149,9 @@ def get_vm_list(hypervisor): """ # 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_output = [] @@ -1166,7 +1166,7 @@ def get_vm_list(hypervisor): # If we're limited, remove other nodes' VMs for vm in vm_list_raw: # 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 vm_hypervisor[vm] == hypervisor: vm_list.append(vm) @@ -1175,14 +1175,14 @@ def get_vm_list(hypervisor): # Gather information for printing for vm in vm_list: - vm_state[vm] = zk.get('/domains/{}/state'.format(vm))[0].decode('ascii') - vm_lasthypervisor = zk.get('/domains/{}/lasthypervisor'.format(vm))[0].decode('ascii') + vm_state[vm] = zk_conn.get('/domains/{}/state'.format(vm))[0].decode('ascii') + vm_lasthypervisor = zk_conn.get('/domains/{}/lasthypervisor'.format(vm))[0].decode('ascii') if vm_lasthypervisor != '': vm_migrated[vm] = 'from {}'.format(vm_lasthypervisor) else: 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) # Determine optimal column widths @@ -1270,7 +1270,7 @@ def get_vm_list(hypervisor): click.echo('\n'.join(sorted(vm_list_output))) # 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)) # Open a Zookeeper connection - zk = startZKConnection(zk_host) + zk_conn = startZKConnection(zk_host) # Destroy the existing data try: - zk.delete('/domains', recursive=True) - zk.delete('nodes', recursive=True) + zk_conn.delete('/domains', recursive=True) + zk_conn.delete('nodes', recursive=True) except: pass # Create the root keys - transaction = zk.transaction() + transaction = zk_conn.transaction() transaction.create('/domains', ''.encode('ascii')) transaction.create('/nodes', ''.encode('ascii')) transaction.commit() # Close the Zookeeper connection - stopZKConnection(zk) + stopZKConnection(zk_conn) click.echo('Successfully initialized new cluster. Any running PVC daemons will need to be restarted.') diff --git a/pvcd.py b/pvcd.py index 6904e5cf..27de767d 100755 --- a/pvcd.py +++ b/pvcd.py @@ -97,25 +97,26 @@ config = readConfig(pvcd_config_file, myhostname) libvirt_check_name = "qemu+tcp://127.0.0.1:16509/system" try: print('Connecting to Libvirt instance at {}'.format(libvirt_check_name)) - conn = libvirt.open(libvirt_check_name) - if conn == None: + lv_conn = libvirt.open(libvirt_check_name) + if lv_conn == None: raise except: print('ERROR: Failed to open local libvirt connection via TCP; required for PVC!') exit(1) +lv_conn.close() # Connect to local zookeeper -zk = kazoo.client.KazooClient(hosts=config['zookeeper']) +zk_conn = kazoo.client.KazooClient(hosts=config['zookeeper']) try: print('Connecting to Zookeeper instance at {}'.format(config['zookeeper'])) - zk.start() + zk_conn.start() except: print('ERROR: Failed to connect to Zookeeper') exit(1) # Handle zookeeper failures def zk_listener(state): - global zk, update_timer + global zk_conn, update_timer if state == kazoo.client.KazooState.SUSPENDED: ansiiprint.echo('Connection to Zookeeper lost; retrying', '', 'e') @@ -123,10 +124,10 @@ def zk_listener(state): stopKeepaliveTimer(update_timer) while True: - _zk = kazoo.client.KazooClient(hosts=config['zookeeper']) + _zk_conn = kazoo.client.KazooClient(hosts=config['zookeeper']) try: - _zk.start() - zk = _zk + _zk_conn.start() + zk_conn = _zk_conn break except: time.sleep(1) @@ -138,15 +139,15 @@ def zk_listener(state): else: pass -zk.add_listener(zk_listener) +zk_conn.add_listener(zk_listener) # Cleanup function def cleanup(signum, frame): ansiiprint.echo('Terminating daemon', '', 'e') # 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 - zk.close() + zk_conn.close() # Stop keepalive thread stopKeepaliveTimer(update_timer) # 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])) # 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") # 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: print("Node is " + ansiiprint.red() + "absent" + ansiiprint.end() + " in Zookeeper; adding new node") 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 - zk.create('/nodes/{}/daemonstate'.format(myhostname), 'stop'.encode('ascii')) - zk.create('/nodes/{}/domainstate'.format(myhostname), 'ready'.encode('ascii')) - zk.create('/nodes/{}/staticdata'.format(myhostname), ' '.join(staticdata).encode('ascii')) - zk.create('/nodes/{}/memfree'.format(myhostname), '0'.encode('ascii')) - zk.create('/nodes/{}/memused'.format(myhostname), '0'.encode('ascii')) - zk.create('/nodes/{}/cpuload'.format(myhostname), '0.0'.encode('ascii')) - zk.create('/nodes/{}/runningdomains'.format(myhostname), ''.encode('ascii')) - zk.create('/nodes/{}/domainscount'.format(myhostname), '0'.encode('ascii')) + zk_conn.create('/nodes/{}/daemonstate'.format(myhostname), 'stop'.encode('ascii')) + zk_conn.create('/nodes/{}/domainstate'.format(myhostname), 'ready'.encode('ascii')) + zk_conn.create('/nodes/{}/staticdata'.format(myhostname), ' '.join(staticdata).encode('ascii')) + zk_conn.create('/nodes/{}/memfree'.format(myhostname), '0'.encode('ascii')) + zk_conn.create('/nodes/{}/memused'.format(myhostname), '0'.encode('ascii')) + zk_conn.create('/nodes/{}/cpuload'.format(myhostname), '0.0'.encode('ascii')) + zk_conn.create('/nodes/{}/runningdomains'.format(myhostname), ''.encode('ascii')) + zk_conn.create('/nodes/{}/domainscount'.format(myhostname), '0'.encode('ascii')) # Keepalives and fencing information - zk.create('/nodes/{}/keepalive'.format(myhostname), str(keepalive_time).encode('ascii')) - zk.create('/nodes/{}/ipmihostname'.format(myhostname), config['ipmi_hostname'].encode('ascii')) - zk.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/{}/keepalive'.format(myhostname), str(keepalive_time).encode('ascii')) + zk_conn.create('/nodes/{}/ipmihostname'.format(myhostname), config['ipmi_hostname'].encode('ascii')) + zk_conn.create('/nodes/{}/ipmiusername'.format(myhostname), config['ipmi_username'].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() s_domain = dict() node_list = [] domain_list = [] -@zk.ChildrenWatch('/nodes') +@zk_conn.ChildrenWatch('/nodes') def updatenodes(new_node_list): global node_list node_list = new_node_list @@ -214,16 +215,16 @@ def updatenodes(new_node_list): if node in t_node: t_node[node].updatenodelist(t_node) 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): global domain_list domain_list = new_domain_list print(ansiiprint.blue() + 'Domain list: ' + ansiiprint.end() + '{}'.format(' '.join(domain_list))) for domain in domain_list: 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: if node in t_node: t_node[node].updatedomainlist(s_domain) diff --git a/pvcd/NodeInstance.py b/pvcd/NodeInstance.py index 0bf1b9c6..7900f8ac 100644 --- a/pvcd/NodeInstance.py +++ b/pvcd/NodeInstance.py @@ -25,9 +25,9 @@ import pvcd.ansiiprint as ansiiprint class NodeInstance(): # 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 - self.zk = zk + self.zk_conn = zk_conn self.config = config self.this_node = this_node self.name = name @@ -46,14 +46,14 @@ class NodeInstance(): self.inflush = False # 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=""): try: self.daemon_state = data.decode('ascii') except AttributeError: 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=""): try: self.domain_state = data.decode('ascii') @@ -68,28 +68,28 @@ class NodeInstance(): self.unflush() - @zk.DataWatch('/nodes/{}/memfree'.format(self.name)) + @zk_conn.DataWatch('/nodes/{}/memfree'.format(self.name)) def watch_hypervisor_memfree(data, stat, event=""): try: self.memfree = data.decode('ascii') except AttributeError: 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=""): try: self.memused = data.decode('ascii') except AttributeError: 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=""): try: self.domain_list = data.decode('ascii').split() except AttributeError: 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=""): try: self.domains_count = data.decode('ascii') @@ -130,53 +130,53 @@ class NodeInstance(): for dom_uuid in self.domain_list: most_memfree = 0 target_hypervisor = None - hypervisor_list = self.zk.get_children('/nodes') - current_hypervisor = self.zk.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii') + hypervisor_list = self.zk_conn.get_children('/nodes') + current_hypervisor = self.zk_conn.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii') if current_hypervisor != self.this_node: continue for hypervisor in hypervisor_list: - daemon_state = self.zk.get('/nodes/{}/daemonstate'.format(hypervisor))[0].decode('ascii') - domain_state = self.zk.get('/nodes/{}/domainstate'.format(hypervisor))[0].decode('ascii') + daemon_state = self.zk_conn.get('/nodes/{}/daemonstate'.format(hypervisor))[0].decode('ascii') + domain_state = self.zk_conn.get('/nodes/{}/domainstate'.format(hypervisor))[0].decode('ascii') if hypervisor == current_hypervisor: continue if daemon_state != 'run' or domain_state != 'ready': 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: most_memfree = memfree target_hypervisor = hypervisor if target_hypervisor == None: 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.commit() else: 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/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), current_hypervisor.encode('ascii')) transaction.commit() - self.zk.set('/nodes/{}/runningdomains'.format(self.name), ''.encode('ascii')) - self.zk.set('/nodes/{}/domainstate'.format(self.name), 'flushed'.encode('ascii')) + self.zk_conn.set('/nodes/{}/runningdomains'.format(self.name), ''.encode('ascii')) + self.zk_conn.set('/nodes/{}/domainstate'.format(self.name), 'flushed'.encode('ascii')) self.inflush = False def unflush(self): self.inflush = True 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: - 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: continue 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/{}/hypervisor'.format(dom_uuid), self.name.encode('ascii')) transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii')) @@ -187,16 +187,16 @@ class NodeInstance(): def update_zookeeper(self): # Connect to libvirt libvirt_name = "qemu:///system" - conn = libvirt.open(libvirt_name) - if conn == None: + lv_conn = libvirt.open(libvirt_name) + if lv_conn == None: ansiiprint.echo('Failed to open connection to "{}"'.format(libvirt_name), '', 'e') return # 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': 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: self.daemon_state = 'run' @@ -210,17 +210,17 @@ class NodeInstance(): raise except Exception as e: # 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 - self.name = conn.getHostname() + self.name = lv_conn.getHostname() self.memused = int(psutil.virtual_memory().used / 1024 / 1024) self.memfree = int(psutil.virtual_memory().free / 1024 / 1024) self.cpuload = os.getloadavg()[0] - self.domains_count = len(conn.listDomainsID()) + self.domains_count = len(lv_conn.listDomainsID()) keepalive_time = int(time.time()) 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/{}/memfree'.format(self.name), str(self.memfree).encode('ascii')) transaction.set_data('/nodes/{}/cpuload'.format(self.name), str(self.cpuload).encode('ascii')) @@ -232,7 +232,7 @@ class NodeInstance(): return # Close the Libvirt connection - conn.close() + lv_conn.close() # Display node information to the terminal ansiiprint.echo('{}{} keepalive{}'.format(ansiiprint.purple(), self.name, ansiiprint.end()), '', 't') @@ -241,9 +241,9 @@ class NodeInstance(): # Update our local node lists for node_name in self.t_node: try: - node_daemon_state = self.zk.get('/nodes/{}/daemonstate'.format(node_name))[0].decode('ascii') - node_domain_state = self.zk.get('/nodes/{}/domainstate'.format(node_name))[0].decode('ascii') - node_keepalive = int(self.zk.get('/nodes/{}/keepalive'.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_conn.get('/nodes/{}/domainstate'.format(node_name))[0].decode('ascii') + node_keepalive = int(self.zk_conn.get('/nodes/{}/keepalive'.format(node_name))[0].decode('ascii')) except: node_daemon_state = 'unknown' node_domain_state = 'unknown' @@ -255,8 +255,8 @@ class NodeInstance(): node_deadtime = int(time.time()) - ( int(self.config['keepalive_interval']) * 6 ) if node_keepalive < node_deadtime and node_daemon_state == 'run': ansiiprint.echo('Node {} seems dead - starting monitor for fencing'.format(node_name), '', 'w') - self.zk.set('/nodes/{}/daemonstate'.format(node_name), 'dead'.encode('ascii')) - fence_thread = threading.Thread(target=fenceNode, args=(node_name, self.zk), kwargs={}) + self.zk_conn.set('/nodes/{}/daemonstate'.format(node_name), 'dead'.encode('ascii')) + fence_thread = threading.Thread(target=fenceNode, args=(node_name, self.zk_conn), kwargs={}) fence_thread.start() # Update the arrays @@ -300,13 +300,13 @@ class NodeInstance(): # # Fence thread entry function # -def fenceNode(node_name, zk): +def fenceNode(node_name, zk_conn): failcount = 0 while failcount < 3: # Wait 5 seconds time.sleep(5) # 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' if node_daemon_state == 'dead': failcount += 1 @@ -318,39 +318,39 @@ def fenceNode(node_name, zk): 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_username = zk.get('/nodes/{}/ipmiusername'.format(node_name))[0].decode('ascii') - ipmi_password = zk.get('/nodes/{}/ipmipassword'.format(node_name))[0].decode('ascii') + ipmi_hostname = zk_conn.get('/nodes/{}/ipmihostname'.format(node_name))[0].decode('ascii') + ipmi_username = zk_conn.get('/nodes/{}/ipmiusername'.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) time.sleep(5) 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: most_memfree = 0 - hypervisor_list = zk.get_children('/nodes') - current_hypervisor = zk.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii') + hypervisor_list = zk_conn.get_children('/nodes') + current_hypervisor = zk_conn.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii') for hypervisor in hypervisor_list: print(hypervisor) - daemon_state = zk.get('/nodes/{}/daemonstate'.format(hypervisor))[0].decode('ascii') - domain_state = zk.get('/nodes/{}/domainstate'.format(hypervisor))[0].decode('ascii') + daemon_state = zk_conn.get('/nodes/{}/daemonstate'.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': 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: most_memfree = memfree target_hypervisor = hypervisor 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/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii')) transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), current_hypervisor.encode('ascii')) transaction.commit() # 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 diff --git a/pvcd/VMInstance.py b/pvcd/VMInstance.py index 206c5259..63187d92 100644 --- a/pvcd/VMInstance.py +++ b/pvcd/VMInstance.py @@ -25,10 +25,10 @@ import pvcd.ansiiprint as ansiiprint class VMInstance: # Initialization function - def __init__(self, domuuid, zk, config, thishypervisor): + def __init__(self, domuuid, zk_conn, config, thishypervisor): # Passed-in variables on creation self.domuuid = domuuid - self.zk = zk + self.zk_conn = zk_conn self.config = config self.thishypervisor = thishypervisor @@ -45,7 +45,7 @@ class VMInstance: self.dom = self.lookupByUUID(self.domuuid) # 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=""): # If we get a delete state, just terminate outselves if data == None: @@ -71,16 +71,16 @@ class VMInstance: # Start up a new Libvirt connection libvirt_name = "qemu:///system" - conn = libvirt.open(libvirt_name) - if conn == None: + lv_conn = libvirt.open(libvirt_name) + if lv_conn == None: ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e') self.instart = False return try: # Grab the domain information from Zookeeper - xmlconfig = self.zk.get('/domains/{}/xml'.format(self.domuuid))[0].decode('ascii') - dom = conn.createXML(xmlconfig, 0) + xmlconfig = self.zk_conn.get('/domains/{}/xml'.format(self.domuuid))[0].decode('ascii') + dom = lv_conn.createXML(xmlconfig, 0) if not self.domuuid in self.thishypervisor.domain_list: self.thishypervisor.domain_list.append(self.domuuid) @@ -88,10 +88,10 @@ class VMInstance: self.dom = dom except libvirt.libvirtError as 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 - conn.close() + lv_conn.close() self.instart = False # Restart the VM @@ -101,8 +101,8 @@ class VMInstance: # Start up a new Libvirt connection libvirt_name = "qemu:///system" - conn = libvirt.open(libvirt_name) - if conn == None: + lv_conn = libvirt.open(libvirt_name) + if lv_conn == None: ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e') self.inrestart = False return @@ -114,8 +114,8 @@ class VMInstance: except libvirt.libvirtError as e: ansiiprint.echo('Failed to restart VM', '{}:'.format(self.domuuid), 'e') - self.zk.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii')) - conn.close() + self.zk_conn.set('/domains/{}/state'.format(self.domuuid), 'start'.encode('ascii')) + lv_conn.close() self.inrestart = False # Stop the VM forcibly without updating state @@ -150,7 +150,7 @@ class VMInstance: pass 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') self.dom = None @@ -182,7 +182,7 @@ class VMInstance: pass 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') self.dom = None @@ -190,24 +190,24 @@ class VMInstance: def live_migrate_vm(self, dest_hypervisor): try: - dest_conn = libvirt.open('qemu+tcp://{}/system'.format(self.hypervisor)) - if dest_conn == None: + dest_lv_conn = libvirt.open('qemu+tcp://{}/system'.format(self.hypervisor)) + if dest_lv_conn == None: raise except: ansiiprint.echo('Failed to open connection to qemu+tcp://{}/system; aborting migration.'.format(self.hypervisor), '{}:'.format(self.domuuid), 'e') return 1 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: raise ansiiprint.echo('Successfully migrated VM', '{}:'.format(self.domuuid), 'o') except: - dest_conn.close() + dest_lv_conn.close() return 1 - dest_conn.close() + dest_lv_conn.close() return 0 # 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') self.shutdown_vm() 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: try: self.thishypervisor.domain_list.remove(self.domuuid) except ValueError: pass 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 @@ -236,7 +236,7 @@ class VMInstance: ansiiprint.echo('Receiving migration', '{}:'.format(self.domuuid), 'i') while True: 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) if self.dom == None and self.state == 'migrate': @@ -255,10 +255,10 @@ class VMInstance: if not self.domuuid in self.thishypervisor.domain_list: self.thishypervisor.domain_list.append(self.domuuid) 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: 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 @@ -270,8 +270,8 @@ class VMInstance: time.sleep(0.2) # 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.hypervisor = self.zk.get('/domains/{}/hypervisor'.format(self.domuuid))[0].decode('ascii') + self.state = self.zk_conn.get('/domains/{}/state'.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 try: @@ -311,7 +311,7 @@ class VMInstance: self.thishypervisor.domain_list.append(self.domuuid) # VM is already running and should be but stuck in migrate state 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: self.thishypervisor.domain_list.append(self.domuuid) # VM should be restarted @@ -332,7 +332,7 @@ class VMInstance: self.receive_migrate() # VM should be restarted (i.e. started since it isn't running) 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 elif self.state == "shutdown": 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 # 2. Try's it and returns a sensible value if not def lookupByUUID(self, tuuid): - conn = None + lv_conn = None dom = None libvirt_name = "qemu:///system" @@ -367,13 +367,13 @@ class VMInstance: # Try try: # Open a libvirt connection - conn = libvirt.open(libvirt_name) - if conn == None: + lv_conn = libvirt.open(libvirt_name) + if lv_conn == None: ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e') return dom # Lookup the UUID - dom = conn.lookupByUUID(buuid) + dom = lv_conn.lookupByUUID(buuid) # Fail except: @@ -382,8 +382,8 @@ class VMInstance: # After everything finally: # Close the libvirt connection - if conn != None: - conn.close() + if lv_conn != None: + lv_conn.close() # Return the dom object (or None) return dom