Rework node library for new zkhandler

This commit is contained in:
Joshua Boniface 2021-05-29 20:56:21 -04:00
parent 62cb72b62f
commit 3603b782c0
1 changed files with 49 additions and 50 deletions

View File

@ -22,31 +22,30 @@
import time
import re
import daemon_lib.zkhandler as zkhandler
import daemon_lib.common as common
def getNodeInformation(zk_conn, node_name):
def getNodeInformation(zkhandler, node_name):
"""
Gather information about a node from the Zookeeper database and return a dict() containing it.
"""
node_daemon_state = zkhandler.readdata(zk_conn, '/nodes/{}/daemonstate'.format(node_name))
node_coordinator_state = zkhandler.readdata(zk_conn, '/nodes/{}/routerstate'.format(node_name))
node_domain_state = zkhandler.readdata(zk_conn, '/nodes/{}/domainstate'.format(node_name))
node_static_data = zkhandler.readdata(zk_conn, '/nodes/{}/staticdata'.format(node_name)).split()
node_daemon_state = zkhandler.read('/nodes/{}/daemonstate'.format(node_name))
node_coordinator_state = zkhandler.read('/nodes/{}/routerstate'.format(node_name))
node_domain_state = zkhandler.read('/nodes/{}/domainstate'.format(node_name))
node_static_data = zkhandler.read('/nodes/{}/staticdata'.format(node_name)).split()
node_cpu_count = int(node_static_data[0])
node_kernel = node_static_data[1]
node_os = node_static_data[2]
node_arch = node_static_data[3]
node_vcpu_allocated = int(zkhandler.readdata(zk_conn, 'nodes/{}/vcpualloc'.format(node_name)))
node_mem_total = int(zkhandler.readdata(zk_conn, '/nodes/{}/memtotal'.format(node_name)))
node_mem_allocated = int(zkhandler.readdata(zk_conn, '/nodes/{}/memalloc'.format(node_name)))
node_mem_provisioned = int(zkhandler.readdata(zk_conn, '/nodes/{}/memprov'.format(node_name)))
node_mem_used = int(zkhandler.readdata(zk_conn, '/nodes/{}/memused'.format(node_name)))
node_mem_free = int(zkhandler.readdata(zk_conn, '/nodes/{}/memfree'.format(node_name)))
node_load = float(zkhandler.readdata(zk_conn, '/nodes/{}/cpuload'.format(node_name)))
node_domains_count = int(zkhandler.readdata(zk_conn, '/nodes/{}/domainscount'.format(node_name)))
node_running_domains = zkhandler.readdata(zk_conn, '/nodes/{}/runningdomains'.format(node_name)).split()
node_vcpu_allocated = int(zkhandler.read('nodes/{}/vcpualloc'.format(node_name)))
node_mem_total = int(zkhandler.read('/nodes/{}/memtotal'.format(node_name)))
node_mem_allocated = int(zkhandler.read('/nodes/{}/memalloc'.format(node_name)))
node_mem_provisioned = int(zkhandler.read('/nodes/{}/memprov'.format(node_name)))
node_mem_used = int(zkhandler.read('/nodes/{}/memused'.format(node_name)))
node_mem_free = int(zkhandler.read('/nodes/{}/memfree'.format(node_name)))
node_load = float(zkhandler.read('/nodes/{}/cpuload'.format(node_name)))
node_domains_count = int(zkhandler.read('/nodes/{}/domainscount'.format(node_name)))
node_running_domains = zkhandler.read('/nodes/{}/runningdomains'.format(node_name)).split()
# Construct a data structure to represent the data
node_information = {
@ -79,118 +78,118 @@ def getNodeInformation(zk_conn, node_name):
#
# Direct Functions
#
def secondary_node(zk_conn, node):
def secondary_node(zkhandler, node):
# Verify node is valid
if not common.verifyNode(zk_conn, node):
if not common.verifyNode(zkhandler, node):
return False, 'ERROR: No node named "{}" is present in the cluster.'.format(node)
# Ensure node is a coordinator
daemon_mode = zkhandler.readdata(zk_conn, '/nodes/{}/daemonmode'.format(node))
daemon_mode = zkhandler.read('/nodes/{}/daemonmode'.format(node))
if daemon_mode == 'hypervisor':
return False, 'ERROR: Cannot change router mode on non-coordinator node "{}"'.format(node)
# Ensure node is in run daemonstate
daemon_state = zkhandler.readdata(zk_conn, '/nodes/{}/daemonstate'.format(node))
daemon_state = zkhandler.read('/nodes/{}/daemonstate'.format(node))
if daemon_state != 'run':
return False, 'ERROR: Node "{}" is not active'.format(node)
# Get current state
current_state = zkhandler.readdata(zk_conn, '/nodes/{}/routerstate'.format(node))
current_state = zkhandler.read('/nodes/{}/routerstate'.format(node))
if current_state == 'primary':
retmsg = 'Setting node {} in secondary router mode.'.format(node)
zkhandler.writedata(zk_conn, {
'/primary_node': 'none'
})
zkhandler.write([
('/primary_node', 'none')
])
else:
return False, 'Node "{}" is already in secondary router mode.'.format(node)
return True, retmsg
def primary_node(zk_conn, node):
def primary_node(zkhandler, node):
# Verify node is valid
if not common.verifyNode(zk_conn, node):
if not common.verifyNode(zkhandler, node):
return False, 'ERROR: No node named "{}" is present in the cluster.'.format(node)
# Ensure node is a coordinator
daemon_mode = zkhandler.readdata(zk_conn, '/nodes/{}/daemonmode'.format(node))
daemon_mode = zkhandler.read('/nodes/{}/daemonmode'.format(node))
if daemon_mode == 'hypervisor':
return False, 'ERROR: Cannot change router mode on non-coordinator node "{}"'.format(node)
# Ensure node is in run daemonstate
daemon_state = zkhandler.readdata(zk_conn, '/nodes/{}/daemonstate'.format(node))
daemon_state = zkhandler.read('/nodes/{}/daemonstate'.format(node))
if daemon_state != 'run':
return False, 'ERROR: Node "{}" is not active'.format(node)
# Get current state
current_state = zkhandler.readdata(zk_conn, '/nodes/{}/routerstate'.format(node))
current_state = zkhandler.read('/nodes/{}/routerstate'.format(node))
if current_state == 'secondary':
retmsg = 'Setting node {} in primary router mode.'.format(node)
zkhandler.writedata(zk_conn, {
'/primary_node': node
})
zkhandler.write([
('/primary_node', node)
])
else:
return False, 'Node "{}" is already in primary router mode.'.format(node)
return True, retmsg
def flush_node(zk_conn, node, wait=False):
def flush_node(zkhandler, node, wait=False):
# Verify node is valid
if not common.verifyNode(zk_conn, node):
if not common.verifyNode(zkhandler, node):
return False, 'ERROR: No node named "{}" is present in the cluster.'.format(node)
retmsg = 'Flushing hypervisor {} of running VMs.'.format(node)
# Add the new domain to Zookeeper
zkhandler.writedata(zk_conn, {
'/nodes/{}/domainstate'.format(node): 'flush'
})
zkhandler.write([
('/nodes/{}/domainstate'.format(node), 'flush')
])
if wait:
while zkhandler.readdata(zk_conn, '/nodes/{}/domainstate'.format(node)) == 'flush':
while zkhandler.read('/nodes/{}/domainstate'.format(node)) == 'flush':
time.sleep(1)
retmsg = 'Flushed hypervisor {} of running VMs.'.format(node)
return True, retmsg
def ready_node(zk_conn, node, wait=False):
def ready_node(zkhandler, node, wait=False):
# Verify node is valid
if not common.verifyNode(zk_conn, node):
if not common.verifyNode(zkhandler, node):
return False, 'ERROR: No node named "{}" is present in the cluster.'.format(node)
retmsg = 'Restoring hypervisor {} to active service.'.format(node)
# Add the new domain to Zookeeper
zkhandler.writedata(zk_conn, {
'/nodes/{}/domainstate'.format(node): 'unflush'
})
zkhandler.write([
('/nodes/{}/domainstate'.format(node), 'unflush')
])
if wait:
while zkhandler.readdata(zk_conn, '/nodes/{}/domainstate'.format(node)) == 'unflush':
while zkhandler.read('/nodes/{}/domainstate'.format(node)) == 'unflush':
time.sleep(1)
retmsg = 'Restored hypervisor {} to active service.'.format(node)
return True, retmsg
def get_info(zk_conn, node):
def get_info(zkhandler, node):
# Verify node is valid
if not common.verifyNode(zk_conn, node):
if not common.verifyNode(zkhandler, node):
return False, 'ERROR: No node named "{}" is present in the cluster.'.format(node)
# Get information about node in a pretty format
node_information = getNodeInformation(zk_conn, node)
node_information = getNodeInformation(zkhandler, node)
if not node_information:
return False, 'ERROR: Could not get information about node "{}".'.format(node)
return True, node_information
def get_list(zk_conn, limit, daemon_state=None, coordinator_state=None, domain_state=None, is_fuzzy=True):
def get_list(zkhandler, limit, daemon_state=None, coordinator_state=None, domain_state=None, is_fuzzy=True):
node_list = []
full_node_list = zkhandler.listchildren(zk_conn, '/nodes')
full_node_list = zkhandler.children('/nodes')
for node in full_node_list:
if limit:
@ -199,11 +198,11 @@ def get_list(zk_conn, limit, daemon_state=None, coordinator_state=None, domain_s
limit = '^' + limit + '$'
if re.match(limit, node):
node_list.append(getNodeInformation(zk_conn, node))
node_list.append(getNodeInformation(zkhandler, node))
except Exception as e:
return False, 'Regex Error: {}'.format(e)
else:
node_list.append(getNodeInformation(zk_conn, node))
node_list.append(getNodeInformation(zkhandler, node))
if daemon_state or coordinator_state or domain_state:
limited_node_list = []