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

16
pvc.py
View File

@ -962,11 +962,8 @@ 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)
return
current_vm_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii') current_vm_state = zk_conn.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if current_vm_state != 'stop': if current_vm_state != 'stop':
click.echo('Forcibly stopping VM "{}".'.format(dom_uuid)) click.echo('Forcibly stopping VM "{}".'.format(dom_uuid))
@ -978,12 +975,19 @@ def undefine_vm(domain):
# Wait for 3 seconds to allow state to flow to all hypervisors # Wait for 3 seconds to allow state to flow to all hypervisors
click.echo('Waiting for cluster to update.') click.echo('Waiting for cluster to update.')
time.sleep(1) time.sleep(1)
except:
pass
# Gracefully terminate the class instances # Gracefully terminate the class instances
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')) zk_conn.set('/domains/{}/state'.format(dom_uuid), 'delete'.encode('ascii'))
time.sleep(5) time.sleep(5)
except:
pass
# Delete the configurations # Delete the configurations
try:
click.echo('Undefining VM "{}".'.format(dom_uuid)) click.echo('Undefining VM "{}".'.format(dom_uuid))
transaction = zk_conn.transaction() transaction = zk_conn.transaction()
transaction.delete('/domains/{}/state'.format(dom_uuid)) transaction.delete('/domains/{}/state'.format(dom_uuid))
@ -993,6 +997,8 @@ def undefine_vm(domain):
transaction.delete('/domains/{}/xml'.format(dom_uuid)) transaction.delete('/domains/{}/xml'.format(dom_uuid))
transaction.delete('/domains/{}'.format(dom_uuid)) transaction.delete('/domains/{}'.format(dom_uuid))
transaction.commit() transaction.commit()
except:
pass
# Close the Zookeeper connection # Close the Zookeeper connection
stopZKConnection(zk_conn) stopZKConnection(zk_conn)