Use zkhandler for everything; add delete operation

This commit is contained in:
Joshua Boniface 2018-10-27 15:27:08 -04:00
parent 29d44444e5
commit 2b115160a2
5 changed files with 84 additions and 82 deletions

View File

@ -58,7 +58,7 @@ def stopZKConnection(zk_conn):
# #
def getDomainXML(zk_conn, dom_uuid): def getDomainXML(zk_conn, dom_uuid):
try: try:
xml = zk_conn.get('/domains/%s/xml' % dom_uuid)[0].decode('ascii') xml = zkhandler.readdata(zk_conn, '/domains/{}/xml'.format(dom_uuid))
except: except:
return None return None
@ -171,7 +171,7 @@ def getDomainControllers(parsed_xml):
# #
def verifyNode(zk_conn, node): def verifyNode(zk_conn, node):
try: try:
zk_conn.get('/nodes/{}'.format(node)) zkhandler.readdata('/nodes/{}'.format(node))
return True return True
except: except:
return False return False
@ -205,16 +205,16 @@ def getPrimaryNode(zk_conn):
# #
def getNodes(zk_conn, dom_uuid): def getNodes(zk_conn, dom_uuid):
valid_node_list = [] valid_node_list = []
full_node_list = zk_conn.get_children('/nodes') full_node_list = zkhandler.list_children(zk_conn, '/nodes')
try: try:
current_node = zk_conn.get('/domains/{}/node'.format(dom_uuid))[0].decode('ascii') current_node = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(dom_uuid))
except: except:
current_node = None current_node = None
for node in full_node_list: for node in full_node_list:
daemon_state = zk_conn.get('/nodes/{}/daemonstate'.format(node))[0].decode('ascii') daemon_state = zkhandler.readdata(zk_conn, '/nodes/{}/daemonstate'.format(node))
domain_state = zk_conn.get('/nodes/{}/domainstate'.format(node))[0].decode('ascii') domain_state = zkhandler.readdata(zk_conn, '/nodes/{}/domainstate'.format(node))
if node == current_node: if node == current_node:
continue continue

View File

@ -43,11 +43,11 @@ import client_lib.common as common
# #
def getClusterNetworkList(zk_conn): def getClusterNetworkList(zk_conn):
# Get a list of VNIs by listing the children of /networks # Get a list of VNIs by listing the children of /networks
vni_list = zk_conn.get_children('/networks') vni_list = zkhandler.readdata(zk_conn, '/networks')
description_list = [] description_list = []
# For each VNI, get the corresponding description from the data # For each VNI, get the corresponding description from the data
for vni in vni_list: for vni in vni_list:
description_list.append(zk_conn.get('/networks/{}'.format(vni))[0].decode('ascii')) description_list.append(zkhandler.readdata(zk_conn, '/networks/{}'))
return vni_list, description_list return vni_list, description_list
def searchClusterByVNI(zk_conn, vni): def searchClusterByVNI(zk_conn, vni):
@ -194,7 +194,7 @@ def formatNetworkInformation(zk_conn, vni, long_output):
for line in dhcp_reservations_string.split('\n'): for line in dhcp_reservations_string.split('\n'):
ainformation.append(line) ainformation.append(line)
firewall_rules = zk_conn.get_children('/networks/{}/firewall_rules'.format(vni)) firewall_rules = zkhandler.list_children(zk_conn, '/networks/{}/firewall_rules'.format(vni))
if firewall_rules: if firewall_rules:
ainformation.append('') ainformation.append('')
ainformation.append('{}Network firewall rules:{}'.format(ansiprint.bold(), ansiprint.end())) ainformation.append('{}Network firewall rules:{}'.format(ansiprint.bold(), ansiprint.end()))
@ -511,7 +511,7 @@ def add_network(zk_conn, vni, description, domain, ip_network, ip_gateway, dhcp_
return False, 'ERROR: DHCP start and end addresses are required for a DHCP-enabled network.' return False, 'ERROR: DHCP start and end addresses are required for a DHCP-enabled network.'
# Check if a network with this VNI or description already exists # Check if a network with this VNI or description already exists
if zk_conn.exists('/networks/{}'.format(vni)): if zkhandler.exists(zk_conn, '/networks/{}'.format(vni)):
return False, 'ERROR: A network with VNI {} already exists!'.format(vni) return False, 'ERROR: A network with VNI {} already exists!'.format(vni)
for network in zkhandler.listchildren(zk_conn, '/networks'): for network in zkhandler.listchildren(zk_conn, '/networks'):
network_description = zkhandler.readdata(zk_conn, '/networks/{}'.format(network)) network_description = zkhandler.readdata(zk_conn, '/networks/{}'.format(network))
@ -538,7 +538,6 @@ def add_network(zk_conn, vni, description, domain, ip_network, ip_gateway, dhcp_
def modify_network(zk_conn, vni, **parameters): def modify_network(zk_conn, vni, **parameters):
# Add the new network to Zookeeper # Add the new network to Zookeeper
transaction = zk_conn.transaction()
zk_data = {} zk_data = {}
if parameters['description'] != None: if parameters['description'] != None:
zk_data.update({'/networks/{}'.format(vni): parameters['description']}) zk_data.update({'/networks/{}'.format(vni): parameters['description']})
@ -567,10 +566,7 @@ def remove_network(zk_conn, network):
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network) return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
# Delete the configuration # Delete the configuration
try: zkhandler.deletekey(zk_conn, '/networks/{}'.format(vni))
zk_conn.delete('/networks/{}'.format(vni), recursive=True)
except:
pass
return True, 'Network "{}" removed successfully!'.format(description) return True, 'Network "{}" removed successfully!'.format(description)
@ -590,7 +586,7 @@ def add_dhcp_reservation(zk_conn, network, ipaddress, macaddress, hostname):
if not isValidIP(ipaddress): if not isValidIP(ipaddress):
return False, 'ERROR: IP address "{}" is not valid!'.format(macaddress) return False, 'ERROR: IP address "{}" is not valid!'.format(macaddress)
if zk_conn.exists('/networks/{}/dhcp_reservations/{}'.format(net_vni, macaddress)): if zkhandler.exists(zk_conn, '/networks/{}/dhcp_reservations/{}'.format(net_vni, macaddress)):
return False, 'ERROR: A reservation with MAC "{}" already exists!'.format(macaddress) return False, 'ERROR: A reservation with MAC "{}" already exists!'.format(macaddress)
# Add the new static lease to ZK # Add the new static lease to ZK
@ -626,7 +622,7 @@ def remove_dhcp_reservation(zk_conn, network, reservation):
# Remove the entry from zookeeper # Remove the entry from zookeeper
try: try:
zk_conn.delete('/networks/{}/dhcp_reservations/{}'.format(net_vni, match_description), recursive=True) zkhandler.deletekey(zk_conn, '/networks/{}/dhcp_reservations/{}'.format(net_vni, match_description))
except: except:
return False, 'ERROR: Failed to write to Zookeeper!' return False, 'ERROR: Failed to write to Zookeeper!'
@ -644,7 +640,7 @@ def add_acl(zk_conn, network, direction, description, rule, order):
else: else:
direction = "out" direction = "out"
if zk_conn.exists('/networks/{}/firewall_rules/{}/{}'.format(net_vni, direction, description)): if zkhandler.exists(zk_conn, '/networks/{}/firewall_rules/{}/{}'.format(net_vni, direction, description)):
return False, 'ERROR: A rule with description "{}" already exists!'.format(description) return False, 'ERROR: A rule with description "{}" already exists!'.format(description)
# Handle reordering # Handle reordering
@ -713,7 +709,7 @@ def remove_acl(zk_conn, network, rule, direction):
# Remove the entry from zookeeper # Remove the entry from zookeeper
try: try:
zk_conn.delete('/networks/{}/firewall_rules/{}/{}'.format(net_vni, direction, match_description), recursive=True) zkhandler.deletekey(zk_conn, '/networks/{}/firewall_rules/{}/{}'.format(net_vni, direction, match_description))
except Exception as e: except Exception as e:
return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e) return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e)
@ -747,7 +743,7 @@ def get_info(zk_conn, network, long_output):
def get_list(zk_conn, limit): def get_list(zk_conn, limit):
net_list = [] net_list = []
full_net_list = zk_conn.get_children('/networks') full_net_list = zkhandler.list_children(zk_conn, '/networks')
for net in full_net_list: for net in full_net_list:
description = zkhandler.readdata(zk_conn, '/networks/{}'.format(net)) description = zkhandler.readdata(zk_conn, '/networks/{}'.format(net))

View File

@ -215,7 +215,7 @@ def get_info(zk_conn, node, long_output):
def get_list(zk_conn, limit): def get_list(zk_conn, limit):
# Match our limit # Match our limit
node_list = [] node_list = []
full_node_list = zk_conn.get_children('/nodes') full_node_list = zkhandler.list_children(zk_conn, '/nodes')
for node in full_node_list: for node in full_node_list:
if limit != None: if limit != None:
try: try:

View File

@ -43,9 +43,9 @@ import client_lib.common as common
def getInformationFromXML(zk_conn, 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_conn.get('/domains/{}/state'.format(uuid))[0].decode('ascii') dstate = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(uuid))
dnode = zk_conn.get('/domains/{}/node'.format(uuid))[0].decode('ascii') dnode = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(uuid))
dlastnode = zk_conn.get('/domains/{}/lastnode'.format(uuid))[0].decode('ascii') dlastnode = zkhandler.readdata(zk_conn, '/domains/{}/lastnode'.format(uuid))
except: except:
return None return None
@ -142,11 +142,11 @@ def getInformationFromXML(zk_conn, uuid, long_output):
# #
def getClusterDomainList(zk_conn): 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_conn.get_children('/domains') uuid_list = zkhandler.listchildren(zk_conn, '/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_conn.get('/domains/%s' % uuid)[0].decode('ascii')) name_list.append(zkhandler.readdata(zk_conn, '/domains/%s' % uuid))
return uuid_list, name_list return uuid_list, name_list
def searchClusterByUUID(zk_conn, uuid): def searchClusterByUUID(zk_conn, uuid):
@ -216,14 +216,14 @@ def define_vm(zk_conn, config_data, target_node, selector):
common.verifyNode(zk_conn, target_node) common.verifyNode(zk_conn, target_node)
# Add the new domain to Zookeeper # Add the new domain to Zookeeper
transaction = zk_conn.transaction() zkhandler.writedata(zk_conn, {
transaction.create('/domains/{}'.format(dom_uuid), dom_name.encode('ascii')) '/domains/{}'.format(dom_uuid): dom_name,
transaction.create('/domains/{}/state'.format(dom_uuid), 'stop'.encode('ascii')) '/domains/{}/state'.format(dom_uuid): 'stop',
transaction.create('/domains/{}/node'.format(dom_uuid), target_node.encode('ascii')) '/domains/{}/node'.format(dom_uuid): target_node,
transaction.create('/domains/{}/lastnode'.format(dom_uuid), ''.encode('ascii')) '/domains/{}/lastnode'.format(dom_uuid): '',
transaction.create('/domains/{}/failedreason'.format(dom_uuid), ''.encode('ascii')) '/domains/{}/failedreason'.format(dom_uuid): '',
transaction.create('/domains/{}/xml'.format(dom_uuid), config_data.encode('ascii')) '/domains/{}/xml'.format(dom_uuid): config_data
results = transaction.commit() })
return True, '' return True, ''
@ -234,12 +234,13 @@ def modify_vm(zk_conn, domain, restart, new_vm_config):
dom_name = getDomainName(zk_conn, domain) dom_name = getDomainName(zk_conn, domain)
# Add the modified config to Zookeeper # Add the modified config to Zookeeper
transaction = zk_conn.transaction() zk_data = {
transaction.set_data('/domains/{}'.format(dom_uuid), dom_name.encode('ascii')) '/domains/{}'.format(dom_uuid): dom_name,
transaction.set_data('/domains/{}/xml'.format(dom_uuid), new_vm_config.encode('ascii')) '/domains/{}/xml'.format(dom_uuid): new_vm_config
}
if restart == True: if restart == True:
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'restart'.encode('ascii')) zk_data.update({'/domains/{}/state'.format(dom_uuid): 'restart'})
results = transaction.commit() zkhandler.writedata(zk_conn, zk_data)
return True, '' return True, ''
@ -252,13 +253,11 @@ def undefine_vm(zk_conn, domain):
# Shut down the VM # Shut down the VM
try: try:
current_vm_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_vm_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
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_conn.transaction() zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'stop'})
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'stop'.encode('ascii'))
transaction.commit()
# Wait for 3 seconds to allow state to flow to all nodes # Wait for 3 seconds to allow state to flow to all nodes
click.echo('Waiting for cluster to update.') click.echo('Waiting for cluster to update.')
@ -269,7 +268,7 @@ def undefine_vm(zk_conn, domain):
# Gracefully terminate the class instances # Gracefully terminate the class instances
try: try:
click.echo('Deleting VM "{}" from nodes.'.format(dom_uuid)) click.echo('Deleting VM "{}" from nodes.'.format(dom_uuid))
zk_conn.set('/domains/{}/state'.format(dom_uuid), 'delete'.encode('ascii')) zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'delete'})
time.sleep(5) time.sleep(5)
except: except:
pass pass
@ -277,7 +276,7 @@ def undefine_vm(zk_conn, domain):
# Delete the configurations # Delete the configurations
try: try:
click.echo('Undefining VM "{}".'.format(dom_uuid)) click.echo('Undefining VM "{}".'.format(dom_uuid))
zk_conn.delete('/domains/{}'.format(dom_uuid), recursive=True) zkhandler.deletekey(zk_conn, '/domains/{}')
except: except:
pass pass
@ -292,7 +291,7 @@ def start_vm(zk_conn, domain):
# 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_conn.set('/domains/%s/state' % dom_uuid, 'start'.encode('ascii')) zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'start'})
return True, '' return True, ''
@ -304,14 +303,14 @@ def restart_vm(zk_conn, domain):
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain) return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
# Get state and verify we're OK to proceed # Get state and verify we're OK to proceed
current_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
if current_state != 'start': if current_state != 'start':
common.stopZKConnection(zk_conn) common.stopZKConnection(zk_conn)
return False, 'ERROR: VM "{}" is not in "start" state!'.format(dom_uuid) return False, 'ERROR: VM "{}" is not in "start" state!'.format(dom_uuid)
# 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_conn.set('/domains/%s/state' % dom_uuid, 'restart'.encode('ascii')) zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'restart'})
return True, '' return True, ''
@ -322,14 +321,14 @@ def shutdown_vm(zk_conn, domain):
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain) return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
# Get state and verify we're OK to proceed # Get state and verify we're OK to proceed
current_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
if current_state != 'start': if current_state != 'start':
common.stopZKConnection(zk_conn) common.stopZKConnection(zk_conn)
return False, 'ERROR: VM "{}" is not in "start" state!'.format(dom_uuid) return False, 'ERROR: VM "{}" is not in "start" state!'.format(dom_uuid)
# 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_conn.set('/domains/%s/state' % dom_uuid, 'shutdown'.encode('ascii')) zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'shutdown'})
return True, '' return True, ''
@ -341,14 +340,14 @@ def stop_vm(zk_conn, domain):
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain) return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
# Get state and verify we're OK to proceed # Get state and verify we're OK to proceed
current_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
if current_state != 'start': if current_state != 'start':
common.stopZKConnection(zk_conn) common.stopZKConnection(zk_conn)
return False, 'ERROR: VM "{}" is not in "start" state!'.format(dom_uuid) return False, 'ERROR: VM "{}" is not in "start" state!'.format(dom_uuid)
# 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_conn.set('/domains/%s/state' % dom_uuid, 'stop'.encode('ascii')) zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'stop'})
return True, '' return True, ''
@ -359,7 +358,7 @@ def move_vm(zk_conn, domain, target_node, selector):
common.stopZKConnection(zk_conn) common.stopZKConnection(zk_conn)
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain) return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
current_node = zk_conn.get('/domains/{}/node'.format(dom_uuid))[0].decode('ascii') current_node = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(dom_uuid))
if target_node == None: if target_node == None:
target_node = common.findTargetNode(zk_conn, selector, dom_uuid) target_node = common.findTargetNode(zk_conn, selector, dom_uuid)
@ -371,20 +370,20 @@ def move_vm(zk_conn, domain, target_node, selector):
# Verify node is valid # Verify node is valid
common.verifyNode(zk_conn, target_node) common.verifyNode(zk_conn, target_node)
current_vm_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_vm_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
if current_vm_state == 'start': if current_vm_state == 'start':
click.echo('Permanently migrating VM "{}" to node "{}".'.format(dom_uuid, target_node)) click.echo('Permanently migrating VM "{}" to node "{}".'.format(dom_uuid, target_node))
transaction = zk_conn.transaction() zkhandler.writedata(zk_conn, {
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii')) '/domains/{}/state'.format(dom_uuid): 'migrate',
transaction.set_data('/domains/{}/node'.format(dom_uuid), target_node.encode('ascii')) '/domains/{}/node'.format(dom_uuid): target_node,
transaction.set_data('/domains/{}/lastnode'.format(dom_uuid), ''.encode('ascii')) '/domains/{}/lastnode'.format(dom_uuid): ''
transaction.commit() })
else: else:
click.echo('Permanently moving VM "{}" to node "{}".'.format(dom_uuid, target_node)) click.echo('Permanently moving VM "{}" to node "{}".'.format(dom_uuid, target_node))
transaction = zk_conn.transaction() zkhandler.writedata(zk_conn, {
transaction.set_data('/domains/{}/node'.format(dom_uuid), target_node.encode('ascii')) '/domains/{}/node'.format(dom_uuid): target_node,
transaction.set_data('/domains/{}/lastnode'.format(dom_uuid), ''.encode('ascii')) '/domains/{}/lastnode'.format(dom_uuid): ''
transaction.commit() })
return True, '' return True, ''
@ -396,14 +395,14 @@ def migrate_vm(zk_conn, domain, target_node, selector, force_migrate):
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain) return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
# Get state and verify we're OK to proceed # Get state and verify we're OK to proceed
current_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
if current_state != 'start': if current_state != 'start':
target_state = 'start' target_state = 'start'
else: else:
target_state = 'migrate' target_state = 'migrate'
current_node = zk_conn.get('/domains/{}/node'.format(dom_uuid))[0].decode('ascii') current_node = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(dom_uuid))
last_node = zk_conn.get('/domains/{}/lastnode'.format(dom_uuid))[0].decode('ascii') last_node = zkhandler.readdata(zk_conn, '/domains/{}/lastnode'.format(dom_uuid))
if last_node != '' and force_migrate != True: if last_node != '' and force_migrate != True:
click.echo('ERROR: VM "{}" has been previously migrated.'.format(dom_uuid)) click.echo('ERROR: VM "{}" has been previously migrated.'.format(dom_uuid))
@ -424,11 +423,11 @@ def migrate_vm(zk_conn, domain, target_node, selector, force_migrate):
common.verifyNode(zk_conn, target_node) common.verifyNode(zk_conn, target_node)
click.echo('Migrating VM "{}" to node "{}".'.format(dom_uuid, target_node)) click.echo('Migrating VM "{}" to node "{}".'.format(dom_uuid, target_node))
transaction = zk_conn.transaction() zkhandler.writedata(zk_conn, {
transaction.set_data('/domains/{}/state'.format(dom_uuid), target_state.encode('ascii')) '/domains/{}/state'.format(dom_uuid): 'migrate',
transaction.set_data('/domains/{}/node'.format(dom_uuid), target_node.encode('ascii')) '/domains/{}/node'.format(dom_uuid): target_node,
transaction.set_data('/domains/{}/lastnode'.format(dom_uuid), current_node.encode('ascii')) '/domains/{}/lastnode'.format(dom_uuid): current_node
transaction.commit() })
return True, '' return True, ''
@ -440,24 +439,24 @@ def unmigrate_vm(zk_conn, domain):
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain) return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
# Get state and verify we're OK to proceed # Get state and verify we're OK to proceed
current_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
if current_state != 'start': if current_state != 'start':
target_state = 'start' target_state = 'start'
else: else:
target_state = 'migrate' target_state = 'migrate'
target_node = zk_conn.get('/domains/{}/lastnode'.format(dom_uuid))[0].decode('ascii') target_node = zkhandler.readdata(zk_conn, '/domains/{}/lastnode'.format(dom_uuid))
if target_node == '': if target_node == '':
common.stopZKConnection(zk_conn) common.stopZKConnection(zk_conn)
return False, 'ERROR: VM "{}" has not been previously migrated.'.format(dom_uuid) return False, 'ERROR: VM "{}" has not been previously migrated.'.format(dom_uuid)
click.echo('Unmigrating VM "{}" back to node "{}".'.format(dom_uuid, target_node)) click.echo('Unmigrating VM "{}" back to node "{}".'.format(dom_uuid, target_node))
transaction = zk_conn.transaction() zkhandler.writedata(zk_conn, {
transaction.set_data('/domains/{}/state'.format(dom_uuid), target_state.encode('ascii')) '/domains/{}/state'.format(dom_uuid): target_state,
transaction.set_data('/domains/{}/node'.format(dom_uuid), target_node.encode('ascii')) '/domains/{}/node'.format(dom_uuid): target_node,
transaction.set_data('/domains/{}/lastnode'.format(dom_uuid), ''.encode('ascii')) '/domains/{}/lastnode'.format(dom_uuid): ''
transaction.commit() })
return True, '' return True, ''
@ -473,7 +472,7 @@ def get_info(zk_conn, domain, long_output):
click.echo(information) click.echo(information)
# Get a failure reason if applicable # Get a failure reason if applicable
failedreason = zk_conn.get('/domains/{}/failedreason'.format(dom_uuid))[0].decode('ascii') failedreason = zkhandler.readdata(zk_conn, '/domains/{}/failedreason'.format(dom_uuid))
if failedreason != '': if failedreason != '':
click.echo('') click.echo('')
click.echo('{}Failure reason:{} {}'.format(ansiprint.purple(), ansiprint.end(), failedreason)) click.echo('{}Failure reason:{} {}'.format(ansiprint.purple(), ansiprint.end(), failedreason))
@ -487,7 +486,7 @@ def get_list(zk_conn, node, limit):
# Verify node is valid # Verify node is valid
common.verifyNode(zk_conn, node) common.verifyNode(zk_conn, node)
full_vm_list = zk_conn.get_children('/domains') full_vm_list = zkhandler.listchildren(zk_conn, '/domains')
vm_list = [] vm_list = []
vm_list_output = [] vm_list_output = []
@ -540,8 +539,8 @@ def get_list(zk_conn, node, limit):
# Gather information for printing # Gather information for printing
for vm in vm_list: for vm in vm_list:
vm_state[vm] = zk_conn.get('/domains/{}/state'.format(vm))[0].decode('ascii') vm_state[vm] = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(vm))
vm_lastnode = zk_conn.get('/domains/{}/lastnode'.format(vm))[0].decode('ascii') vm_lastnode = zkhandler.readdata(zk_conn, '/domains/{}/lastnode'.format(vm))
if vm_lastnode != '': if vm_lastnode != '':
vm_migrated[vm] = 'from {}'.format(vm_lastnode) vm_migrated[vm] = 'from {}'.format(vm_lastnode)
else: else:

View File

@ -36,6 +36,13 @@ def listchildren(zk_conn, key):
children = zk_conn.get_children(key) children = zk_conn.get_children(key)
return children return children
# Delete key function
def deletekey(zk_conn, key, recursive=True):
try:
zk_conn.delete(key, recursive=recursive)
except:
pass
# Data read function # Data read function
def readdata(zk_conn, key): def readdata(zk_conn, key):
data_raw = zk_conn.get(key) data_raw = zk_conn.get(key)