def reboot(adapter, instance, hard): """Reboots a VM. :param adapter: A pypowervm.adapter.Adapter. :param instance: The nova instance to reboot. :param hard: Boolean True if hard reboot, False otherwise. :raises: InstanceRebootFailure """ # Synchronize power-on and power-off ops on a given instance with lockutils.lock('power_%s' % instance.uuid): try: entry = get_instance_wrapper(adapter, instance) if entry.state != pvm_bp.LPARState.NOT_ACTIVATED: power.power_off(entry, None, force_immediate=hard, restart=True) else: # pypowervm does NOT throw an exception if "already down". # Any other exception from pypowervm is a legitimate failure; # let it raise up. # If we get here, pypowervm thinks the instance is down. power.power_on(entry, None) except pvm_exc.Error as e: LOG.exception("PowerVM error during reboot.", instance=instance) raise exc.InstanceRebootFailure(reason=six.text_type(e))
def test_pwroff_soft_standard_fail(self): """Soft PowerOff flow, non-IBMi, with Job failure.""" # When OS shutdown fails (non-timeout), we try VSP normal first. part = self.mock_partition() self.run_job.side_effect = ( # OS immediate (non-IBMi always adds immediate). # Make sure restart percolates through, bogus params ignored. self.validate_run( part, ex_parms={ 'operation=osshutdown', 'immediate=true', 'restart=true' }, ex_timeout=300, result=self.efail(), # VSP normal, timeout reset to default nxt=self.validate_run( part, ex_parms={'operation=shutdown', 'restart=true'}, result=self.efail(), # VSP hard nxt=self.validate_run(part, ex_parms={ 'operation=shutdown', 'immediate=true', 'restart=true' })))) power.power_off(part, None, timeout=300, restart=True, add_parms={'foo': 'bar'}) self.assertEqual(3, self.run_job.call_count)
def power_off(adapter, instance, host_uuid, entry=None, add_parms=None, force_immediate=False): if entry is None: entry = get_instance_wrapper(adapter, instance) # Get the current state and see if we can stop the VM LOG.debug("Powering off request for instance %(inst)s which is in " "state %(state)s. Force Immediate Flag: %(force)s.", {'inst': instance.name, 'state': entry.state, 'force': force_immediate}) if entry.state in POWERVM_STOPABLE_STATE: # Now stop the lpar try: LOG.debug("Power off executing for instance %(inst)s.", {'inst': instance.name}) power.power_off(entry, host_uuid, force_immediate=force_immediate, add_parms=add_parms) except Exception as e: LOG.exception(e) raise exception.InstancePowerOffFailure(reason=six.text_type(e)) return True else: LOG.debug("Power off not required for instance %(inst)s.", {'inst': instance.name}) return False
def test_pwroff_already_off(self): """PowerOff when the system is already powered off.""" part = self.mock_partition() for prefix in power._ALREADY_POWERED_OFF_ERRS: self.run_job.side_effect = self.validate_run( part, ex_parms={'operation=osshutdown', 'immediate=true'}, result=self.efail(error="Foo %s bar" % prefix)) power.power_off(part, None) self.assertEqual(1, self.run_job.call_count) self.run_job.reset_mock() # If restart was specified, this is a failure. (Force, to KISS) self.run_job.side_effect = self.validate_run( part, ex_parms={ 'operation=shutdown', 'immediate=true', 'restart=true' }, result=self.efail(error="Foo %s bar" % prefix)) self.assertRaises(pexc.VMPowerOffFailure, power.power_off, part, None, restart=True, force_immediate=power.Force.TRUE) self.assertEqual(1, self.run_job.call_count) self.run_job.reset_mock()
def revert(self, lpar_wrap, result, flow_failures): LOG.info(_LI('Powering off instance: %s'), self.instance.name) if isinstance(result, task_fail.Failure): # The power on itself failed...can't power off. LOG.debug('Power on failed. Not performing power off.') return power.power_off(lpar_wrap, self.host_uuid, force_immediate=True)
def power_off(adapter, instance, opts=None, force_immediate=False, timeout=None): """Powers off a VM. :param adapter: A pypowervm.adapter.Adapter. :param instance: The nova instance to power off. :param opts: (Optional) Additional parameters to the pypowervm power_off method. See that method's docstring for details. :param force_immediate: (Optional, Default False) Should it be immediately shut down. :param timeout: (Optional, Default None) How long to wait for the job to complete. By default, is None which indicates it should use the default from pypowervm's power off method. :return: True if the instance was stopped. False if it was not in a stoppable state. :raises: InstancePowerOffFailure """ # Synchronize power-on and power-off ops on a given instance with lockutils.lock('power_%s' % instance.uuid): entry = get_instance_wrapper(adapter, instance) # Get the current state and see if we can stop the VM LOG.debug( "Powering off request for instance %(inst)s which is in " "state %(state)s. Force Immediate Flag: %(force)s.", { 'inst': instance.name, 'state': entry.state, 'force': force_immediate }) if entry.state in POWERVM_STOPABLE_STATE: # Now stop the lpar try: LOG.debug("Power off executing for instance %(inst)s.", {'inst': instance.name}) kwargs = {'timeout': timeout} if timeout else {} force_flag = (power.Force.TRUE if force_immediate else power.Force.ON_FAILURE) power.power_off(entry, None, force_immediate=force_flag, add_parms=opts, **kwargs) except Exception as e: LOG.exception(e) raise exception.InstancePowerOffFailure( reason=six.text_type(e)) return True else: LOG.debug("Power off not required for instance %(inst)s.", {'inst': instance.name}) return False
def test_pwroff_soft_ibmi_norm(self): """Soft PowerOff flow, IBMi, normal (no immediate).""" part = self.mock_partition(env=pvm_bp.LPARType.OS400) # This works the same whether intervening Job exceptions are Timeout or # Failure. for exc in (self.etimeout(), self.efail()): self.run_job.side_effect = ( # OS normal self.validate_run( part, ex_parms={'operation=osshutdown'}, ex_timeout=100, result=exc, # OS immediate (timeout is defaulted from this point) nxt=self.validate_run( part, ex_parms={'operation=osshutdown', 'immediate=true'}, result=exc, # VSP normal nxt=self.validate_run( part, ex_parms={'operation=shutdown'}, result=exc, # VSP hard (default timeout) nxt=self.validate_run(part, ex_parms={ 'operation=shutdown', 'immediate=true' }))))) # Run it power.power_off(part, None, timeout=100) self.assertEqual(4, self.run_job.call_count) self.run_job.reset_mock() # If one of the interim calls succeeds, the operation succeeds. self.run_job.side_effect = ( # OS normal self.validate_run( part, ex_parms={'operation=osshutdown'}, result=self.efail(), # OS immediate (timeout is defaulted from this point) nxt=self.validate_run( part, ex_parms={'operation=osshutdown', 'immediate=true'}, result=self.etimeout(), # VSP normal - succeeds nxt=self.validate_run( part, ex_parms={'operation=shutdown'}, # Not reached nxt=self.fail)))) power.power_off(part, None) self.assertEqual(3, self.run_job.call_count)
def power_off(adapter, instance, host_uuid, entry=None, add_parms=None): if entry is None: entry = get_instance_wrapper(adapter, instance, host_uuid) # Get the current state and see if we can stop the VM if entry.state in POWERVM_STOPABLE_STATE: # Now stop the lpar power.power_off(entry, host_uuid, add_parms=add_parms) return True return False
def test_pwroff_force_immed(self): """Test power_off with force_immediate=Force.TRUE.""" # PowerOff with force-immediate works the same regardless of partition # type, RMC state, or management partition status. for env in (pvm_bp.LPARType.OS400, pvm_bp.LPARType.AIXLINUX, pvm_bp.LPARType.VIOS): for rmc in (pvm_bp.RMCState.ACTIVE, pvm_bp.RMCState.BUSY, pvm_bp.RMCState.INACTIVE): for mgmt in (True, False): part = self.mock_partition(env=env, rmc_state=rmc, mgmt=mgmt) self.run_job.side_effect = self.validate_run( part, ex_parms={'operation=shutdown', 'immediate=true'}) power.power_off(part, None, force_immediate=power.Force.TRUE) self.assertEqual(1, self.run_job.call_count) self.run_job.reset_mock() # Restart, timeout, additional params ignored part = self.mock_partition() self.run_job.side_effect = self.validate_run( part, ex_parms={'operation=shutdown', 'immediate=true', 'restart=true'}, ex_timeout=10, result=self.etimeout()) self.assertRaises(pexc.VMPowerOffTimeout, power.power_off, part, None, force_immediate=power.Force.TRUE, restart=True, timeout=10, add_parms=dict(one=1)) self.assertEqual(1, self.run_job.call_count) self.run_job.reset_mock() # Failure self.run_job.side_effect = self.validate_run( part, ex_parms={'operation=shutdown', 'immediate=true'}, result=self.efail()) self.assertRaises(pexc.VMPowerOffFailure, power.power_off, part, None, force_immediate=power.Force.TRUE) self.assertEqual(1, self.run_job.call_count)
def test_pwroff_new_opts(self): """Test power_off where add_parms is PowerOffOpts (not legacy).""" part = self.mock_partition() # VSP hard self.run_job.side_effect = self.validate_run( part, ex_parms={'operation=shutdown', 'immediate=true'}) power.power_off(part, None, add_parms=popts.PowerOffOpts().vsp_hard()) self.assertEqual(1, self.run_job.call_count) self.run_job.reset_mock() # VSP normal self.run_job.side_effect = self.validate_run( part, ex_parms={'operation=shutdown'}) power.power_off(part, None, add_parms=popts.PowerOffOpts().vsp_normal()) self.run_job.reset_mock() # OS immediate self.run_job.side_effect = self.validate_run( part, ex_parms={'operation=osshutdown', 'immediate=true'}) power.power_off(part, None, add_parms=popts.PowerOffOpts().os_immediate()) self.run_job.reset_mock() # OS normal self.run_job.side_effect = self.validate_run( part, ex_parms={'operation=osshutdown'}) power.power_off(part, None, add_parms=popts.PowerOffOpts().os_normal())
def power_off(adapter, instance, host_uuid, entry=None, add_parms=None): if entry is None: entry = get_instance_wrapper(adapter, instance, host_uuid) # Get the current state and see if we can stop the VM if entry.state in POWERVM_STOPABLE_STATE: # Now stop the lpar try: power.power_off(entry, host_uuid, add_parms=add_parms) except Exception as e: LOG.exception(e) raise exception.InstancePowerOffFailure(reason=six.text_type(e)) return True return False
def power_off(adapter, instance, host_uuid, entry=None, add_parms=None, force_immediate=False): if entry is None: entry = get_instance_wrapper(adapter, instance, host_uuid) # Get the current state and see if we can stop the VM if entry.state in POWERVM_STOPABLE_STATE: # Now stop the lpar try: power.power_off(entry, host_uuid, force_immediate=force_immediate, add_parms=add_parms) except Exception as e: LOG.exception(e) raise exception.InstancePowerOffFailure(reason=six.text_type(e)) return True return False
def test_pwroff_soft_standard_timeout(self): """Soft PowerOff flow, non-IBMi, with timeout.""" # When OS shutdown times out, go straight to VSP hard. part = self.mock_partition() self.run_job.side_effect = ( # OS normal. Non-IBMi always adds immediate. self.validate_run( part, ex_parms={'operation=osshutdown', 'immediate=true'}, ex_timeout=100, result=self.etimeout(), # VSP hard nxt=self.validate_run( part, ex_parms={'operation=shutdown', 'immediate=true'}))) # Run it power.power_off(part, None, timeout=100) self.assertEqual(2, self.run_job.call_count) self.run_job.reset_mock() # Same if invoked with immediate. But since we're running again, add # restart and another param; make sure restart comes through but the # bogus one is ignored. self.run_job.side_effect = ( # OS immediate (non-IBMi always adds immediate). self.validate_run( part, ex_parms={ 'operation=osshutdown', 'immediate=true', 'restart=true' }, ex_timeout=200, result=self.etimeout(), # VSP hard nxt=self.validate_run(part, ex_parms={ 'operation=shutdown', 'immediate=true', 'restart=true' }))) # Run it power.power_off(part, None, timeout=200, restart=True, add_parms={'foo': 'bar'}) self.assertEqual(2, self.run_job.call_count)
def power_off(adapter, instance, force_immediate=False, timeout=None): """Powers off a VM. :param adapter: A pypowervm.adapter.Adapter. :param instance: The nova instance to power off. :param timeout: (Optional, Default None) How long to wait for the job to complete. By default, is None which indicates it should use the default from pypowervm's power off method. :param force_immediate: (Optional, Default False) Should it be immediately shut down. :raises: InstancePowerOffFailure """ # Synchronize power-on and power-off ops on a given instance with lockutils.lock('power_%s' % instance.uuid): entry = get_instance_wrapper(adapter, instance) # Get the current state and see if we can stop the VM LOG.debug("Powering off request for instance %(inst)s which is in " "state %(state)s. Force Immediate Flag: %(force)s.", {'inst': instance.name, 'state': entry.state, 'force': force_immediate}) if entry.state in _POWERVM_STOPPABLE_STATE: # Now stop the lpar try: LOG.debug("Power off executing for instance %(inst)s.", {'inst': instance.name}) kwargs = {'timeout': timeout} if timeout else {} force = (power.Force.TRUE if force_immediate else power.Force.ON_FAILURE) power.power_off(entry, None, force_immediate=force, **kwargs) except pvm_exc.Error as e: LOG.exception("PowerVM error during power_off.", instance=instance) raise exc.InstancePowerOffFailure(reason=six.text_type(e)) else: LOG.debug("Power off not required for instance %(inst)s.", {'inst': instance.name})