Correct failure modes when undefining VM

Undefining a VM would sometimes leave a remnant UUID in the
runningdomains list on the target hypervisor which persisted even
after restarting. This helps prevent that happening by splitting
the various undefine steps into separate try blocks, just in case
one fails.

Fixes #15
This commit is contained in:
Joshua Boniface 2018-08-09 00:29:27 -04:00
parent 2163143951
commit d205ef2072
1 changed files with 33 additions and 27 deletions

60
pvc.py
View File

@ -962,37 +962,43 @@ def undefine_vm(domain):
dom_uuid = searchClusterByName(zk_conn, domain) dom_uuid = searchClusterByName(zk_conn, domain)
dom_name = searchClusterByUUID(zk_conn, dom_uuid) dom_name = searchClusterByUUID(zk_conn, dom_uuid)
if dom_uuid == None: # Shut down the VM
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(domain)) try:
stopZKConnection(zk_conn) current_vm_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
return if current_vm_state != 'stop':
click.echo('Forcibly stopping VM "{}".'.format(dom_uuid))
# Set the domain into stop mode
transaction = zk_conn.transaction()
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'stop'.encode('ascii'))
transaction.commit()
current_vm_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') # Wait for 3 seconds to allow state to flow to all hypervisors
if current_vm_state != 'stop': click.echo('Waiting for cluster to update.')
click.echo('Forcibly stopping VM "{}".'.format(dom_uuid)) time.sleep(1)
# Set the domain into stop mode except:
transaction = zk_conn.transaction() pass
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'stop'.encode('ascii'))
transaction.commit()
# Wait for 3 seconds to allow state to flow to all hypervisors
click.echo('Waiting for cluster to update.')
time.sleep(1)
# Gracefully terminate the class instances # Gracefully terminate the class instances
click.echo('Deleting VM "{}" from nodes.'.format(dom_uuid)) try:
zk_conn.set('/domains/{}/state'.format(dom_uuid), 'delete'.encode('ascii')) click.echo('Deleting VM "{}" from nodes.'.format(dom_uuid))
time.sleep(5) zk_conn.set('/domains/{}/state'.format(dom_uuid), 'delete'.encode('ascii'))
time.sleep(5)
except:
pass
# Delete the configurations # Delete the configurations
click.echo('Undefining VM "{}".'.format(dom_uuid)) try:
transaction = zk_conn.transaction() click.echo('Undefining VM "{}".'.format(dom_uuid))
transaction.delete('/domains/{}/state'.format(dom_uuid)) transaction = zk_conn.transaction()
transaction.delete('/domains/{}/hypervisor'.format(dom_uuid)) transaction.delete('/domains/{}/state'.format(dom_uuid))
transaction.delete('/domains/{}/lasthypervisor'.format(dom_uuid)) transaction.delete('/domains/{}/hypervisor'.format(dom_uuid))
transaction.delete('/domains/{}/failedreason'.format(dom_uuid)) transaction.delete('/domains/{}/lasthypervisor'.format(dom_uuid))
transaction.delete('/domains/{}/xml'.format(dom_uuid)) transaction.delete('/domains/{}/failedreason'.format(dom_uuid))
transaction.delete('/domains/{}'.format(dom_uuid)) transaction.delete('/domains/{}/xml'.format(dom_uuid))
transaction.commit() transaction.delete('/domains/{}'.format(dom_uuid))
transaction.commit()
except:
pass
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk_conn) stopZKConnection(zk_conn)