From fe12b264fdcb1084934b3ec69c7e3fa78522356f Mon Sep 17 00:00:00 2001 From: Andrew Laski Date: Mon, 12 Nov 2012 16:55:57 -0500 Subject: Make xenapi shutdown mode explicit Split shutdown_vm in the xenapi utilities into two methods with clear names defining their behavior rather than relying on a default argument being passed in. This makes something like the following more clear than it would be currently: if not clean_shutdown_vm(): hard_shutdown_vm() Change-Id: Ia7079197c8b8726c7e2ddc508b301162a02baa1b --- nova/virt/xenapi/vm_utils.py | 40 ++++++++++++++++++++++++++++++---------- nova/virt/xenapi/vmops.py | 14 ++++++-------- 2 files changed, 36 insertions(+), 18 deletions(-) (limited to 'nova') diff --git a/nova/virt/xenapi/vm_utils.py b/nova/virt/xenapi/vm_utils.py index f6d28ca54..61d59d83b 100644 --- a/nova/virt/xenapi/vm_utils.py +++ b/nova/virt/xenapi/vm_utils.py @@ -276,22 +276,42 @@ def destroy_vm(session, instance, vm_ref): LOG.debug(_("VM destroyed"), instance=instance) -def shutdown_vm(session, instance, vm_ref, hard=True): - vm_rec = session.call_xenapi("VM.get_record", vm_ref) - state = compile_info(vm_rec)['state'] - if state == power_state.SHUTDOWN: +def clean_shutdown_vm(session, instance, vm_ref): + if _is_vm_shutdown(session, vm_ref): LOG.warn(_("VM already halted, skipping shutdown..."), instance=instance) - return + return False - LOG.debug(_("Shutting down VM"), instance=instance) + LOG.debug(_("Shutting down VM (cleanly)"), instance=instance) try: - if hard: - session.call_xenapi('VM.hard_shutdown', vm_ref) - else: - session.call_xenapi('VM.clean_shutdown', vm_ref) + session.call_xenapi('VM.clean_shutdown', vm_ref) except session.XenAPI.Failure, exc: LOG.exception(exc) + return False + return True + + +def hard_shutdown_vm(session, instance, vm_ref): + if _is_vm_shutdown(session, vm_ref): + LOG.warn(_("VM already halted, skipping shutdown..."), + instance=instance) + return False + + LOG.debug(_("Shutting down VM (hard)"), instance=instance) + try: + session.call_xenapi('VM.hard_shutdown', vm_ref) + except session.XenAPI.Failure, exc: + LOG.exception(exc) + return False + return True + + +def _is_vm_shutdown(session, vm_ref): + vm_rec = session.call_xenapi("VM.get_record", vm_ref) + state = compile_info(vm_rec)['state'] + if state == power_state.SHUTDOWN: + return True + return False def ensure_free_mem(session, instance): diff --git a/nova/virt/xenapi/vmops.py b/nova/virt/xenapi/vmops.py index cc0d48d22..19077c7df 100644 --- a/nova/virt/xenapi/vmops.py +++ b/nova/virt/xenapi/vmops.py @@ -692,8 +692,7 @@ class VMOps(object): instance=instance) # 2. Power down the instance before resizing - vm_utils.shutdown_vm( - self._session, instance, vm_ref, hard=False) + vm_utils.clean_shutdown_vm(self._session, instance, vm_ref) self._update_instance_progress(context, instance, step=2, total_steps=RESIZE_TOTAL_STEPS) @@ -740,8 +739,7 @@ class VMOps(object): total_steps=RESIZE_TOTAL_STEPS) # 3. Now power down the instance - vm_utils.shutdown_vm( - self._session, instance, vm_ref, hard=False) + vm_utils.clean_shutdown_vm(self._session, instance, vm_ref) self._update_instance_progress(context, instance, step=3, total_steps=RESIZE_TOTAL_STEPS) @@ -1074,7 +1072,7 @@ class VMOps(object): instance=instance) return - vm_utils.shutdown_vm(self._session, instance, vm_ref) + vm_utils.hard_shutdown_vm(self._session, instance, vm_ref) # Destroy VDIs self._detach_vm_vols(instance, vm_ref, block_device_info) @@ -1125,7 +1123,7 @@ class VMOps(object): % instance['name']) vm_ref = self._get_vm_opaque_ref(instance) - vm_utils.shutdown_vm(self._session, instance, vm_ref) + vm_utils.hard_shutdown_vm(self._session, instance, vm_ref) self._acquire_bootlock(vm_ref) self.spawn(context, instance, image_meta, [], rescue_password, network_info, name_label=rescue_name_label, rescue=True) @@ -1158,7 +1156,7 @@ class VMOps(object): LOG.warning(_("VM is not present, skipping soft delete..."), instance=instance) else: - vm_utils.shutdown_vm(self._session, instance, vm_ref, hard=True) + vm_utils.hard_shutdown_vm(self._session, instance, vm_ref) self._acquire_bootlock(vm_ref) def restore(self, instance): @@ -1170,7 +1168,7 @@ class VMOps(object): def power_off(self, instance): """Power off the specified instance.""" vm_ref = self._get_vm_opaque_ref(instance) - vm_utils.shutdown_vm(self._session, instance, vm_ref, hard=True) + vm_utils.hard_shutdown_vm(self._session, instance, vm_ref) def power_on(self, instance): """Power on the specified instance.""" -- cgit