From 8b294d4bdb45ed015048a271afdadc83955c3e31 Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Thu, 14 Jun 2018 22:45:34 -0400 Subject: [PATCH] Add some validations that a VM is in start state before doing actions --- pvc.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pvc.py b/pvc.py index 3ec9c926..6decb542 100755 --- a/pvc.py +++ b/pvc.py @@ -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 == '':