'Stopping an instance on this host.'

@wrap_exception()
@reverts_task_state
@wrap_instance_event(prefix='compute')
@wrap_instance_fault
def stop_instance(self, context, instance, clean_shutdown):

    @utils.synchronized(instance.uuid)
    def do_stop_instance():
        current_power_state = self._get_power_state(context, instance)
        LOG.debug('Stopping    instance;    current    vm_state:    %(vm_state)s,    current    task_state:    %(task_state)s,    current    DB    power_state:    %(db_power_state)s,    current    VM    power_state:    %(current_power_state)s', {'vm_state': instance.vm_state, 'task_state': instance.task_state, 'db_power_state': instance.power_state, 'current_power_state': current_power_state}, instance_uuid=instance.uuid)
        expected_task_state = [task_states.POWERING_OFF]
        if (current_power_state in (power_state.NOSTATE, power_state.SHUTDOWN, power_state.CRASHED)):
            LOG.info('Instance    is    already    powered    off    in    the    hypervisor    when    stop    is    called.', instance=instance)
            expected_task_state.append(None)
        self._notify_about_instance_usage(context, instance, 'power_off.start')
        compute_utils.notify_about_instance_action(context, instance, self.host, action=fields.NotificationAction.POWER_OFF, phase=fields.NotificationPhase.START)
        self._power_off_instance(context, instance, clean_shutdown)
        instance.power_state = self._get_power_state(context, instance)
        instance.vm_state = vm_states.STOPPED
        instance.task_state = None
        instance.save(expected_task_state=expected_task_state)
        self._notify_about_instance_usage(context, instance, 'power_off.end')
        compute_utils.notify_about_instance_action(context, instance, self.host, action=fields.NotificationAction.POWER_OFF, phase=fields.NotificationPhase.END)
    do_stop_instance()
