Add some validations that a VM is in start state before doing actions

This commit is contained in:
Joshua Boniface 2018-06-14 22:45:34 -04:00
parent 5a0599d2bc
commit 8b294d4bdb
1 changed files with 36 additions and 0 deletions

36
pvc.py
View File

@ -873,6 +873,12 @@ def restart_vm(dom_name, dom_uuid):
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(message_name))
return
# Get state and verify we're OK to proceed
current_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if curent_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'))
@ -923,6 +929,12 @@ def shutdown_vm(dom_name, dom_uuid):
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(message_name))
return
# Get state and verify we're OK to proceed
current_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if curent_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'))
@ -973,6 +985,12 @@ def stop_vm(dom_name, dom_uuid):
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(message_name))
return
# Get state and verify we're OK to proceed
current_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if curent_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'))
@ -1027,6 +1045,12 @@ def move_vm(dom_name, dom_uuid, target_hypervisor):
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(message_name))
return
# Get state and verify we're OK to proceed
current_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if curent_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')
if target_hypervisor == None:
@ -1116,6 +1140,12 @@ def migrate_vm(dom_name, dom_uuid, target_hypervisor, force_migrate):
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(message_name))
return
# Get state and verify we're OK to proceed
current_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if curent_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')
@ -1198,6 +1228,12 @@ def unmigrate_vm(dom_name, dom_uuid):
click.echo('ERROR: Could not find VM "{}" in the cluster!'.format(message_name))
return
# Get state and verify we're OK to proceed
current_state = zk.get('/domains/{}/state'.format(dom_uuid))[0].decode('ascii')
if curent_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')
if target_hypervisor == '':