'Align instance power state between the database and hypervisor.
If the instance is not found on the hypervisor, but is in the database,
then a stop() API will be called on the instance.'

def _sync_instance_power_state(self, context, db_instance, vm_power_state, use_slave=False):

    db_instance.refresh(use_slave=use_slave)
    db_power_state = db_instance.power_state
    vm_state = db_instance.vm_state
    if (self.host != db_instance.host):
        LOG.info('During    the    sync_power    process    the    instance    has    moved    from    host    %(src)s    to    host    %(dst)s', {'src': db_instance.host, 'dst': self.host}, instance=db_instance)
        return
    elif (db_instance.task_state is not None):
        LOG.info('During    sync_power_state    the    instance    has    a    pending    task    (%(task)s).    Skip.', {'task': db_instance.task_state}, instance=db_instance)
        return
    orig_db_power_state = db_power_state
    if (vm_power_state != db_power_state):
        LOG.info('During    _sync_instance_power_state    the    DB    power_state    (%(db_power_state)s)    does    not    match    the    vm_power_state    from    the    hypervisor    (%(vm_power_state)s).    Updating    power_state    in    the    DB    to    match    the    hypervisor.', {'db_power_state': db_power_state, 'vm_power_state': vm_power_state}, instance=db_instance)
        db_instance.power_state = vm_power_state
        db_instance.save()
        db_power_state = vm_power_state
    if (vm_state in (vm_states.BUILDING, vm_states.RESCUED, vm_states.RESIZED, vm_states.SUSPENDED, vm_states.ERROR)):
        pass
    elif (vm_state == vm_states.ACTIVE):
        if (vm_power_state in (power_state.SHUTDOWN, power_state.CRASHED)):
            LOG.warning('Instance    shutdown    by    itself.    Calling    the    stop    API.    Current    vm_state:    %(vm_state)s,    current    task_state:    %(task_state)s,    original    DB    power_state:    %(db_power_state)s,    current    VM    power_state:    %(vm_power_state)s', {'vm_state': vm_state, 'task_state': db_instance.task_state, 'db_power_state': orig_db_power_state, 'vm_power_state': vm_power_state}, instance=db_instance)
            try:
                if db_instance.shutdown_terminate:
                    self.compute_api.delete(context, db_instance)
                else:
                    self.compute_api.stop(context, db_instance)
            except Exception:
                LOG.exception('error    during    stop()    in    sync_power_state.', instance=db_instance)
        elif (vm_power_state == power_state.SUSPENDED):
            LOG.warning('Instance    is    suspended    unexpectedly.    Calling    the    stop    API.', instance=db_instance)
            try:
                self.compute_api.stop(context, db_instance)
            except Exception:
                LOG.exception('error    during    stop()    in    sync_power_state.', instance=db_instance)
        elif (vm_power_state == power_state.PAUSED):
            LOG.warning('Instance    is    paused    unexpectedly.    Ignore.', instance=db_instance)
        elif (vm_power_state == power_state.NOSTATE):
            LOG.warning('Instance    is    unexpectedly    not    found.    Ignore.', instance=db_instance)
    elif (vm_state == vm_states.STOPPED):
        if (vm_power_state not in (power_state.NOSTATE, power_state.SHUTDOWN, power_state.CRASHED)):
            LOG.warning('Instance    is    not    stopped.    Calling    the    stop    API.    Current    vm_state:    %(vm_state)s,    current    task_state:    %(task_state)s,    original    DB    power_state:    %(db_power_state)s,    current    VM    power_state:    %(vm_power_state)s', {'vm_state': vm_state, 'task_state': db_instance.task_state, 'db_power_state': orig_db_power_state, 'vm_power_state': vm_power_state}, instance=db_instance)
            try:
                self.compute_api.force_stop(context, db_instance)
            except Exception:
                LOG.exception('error    during    stop()    in    sync_power_state.', instance=db_instance)
    elif (vm_state == vm_states.PAUSED):
        if (vm_power_state in (power_state.SHUTDOWN, power_state.CRASHED)):
            LOG.warning('Paused    instance    shutdown    by    itself.    Calling    the    stop    API.', instance=db_instance)
            try:
                self.compute_api.force_stop(context, db_instance)
            except Exception:
                LOG.exception('error    during    stop()    in    sync_power_state.', instance=db_instance)
    elif (vm_state in (vm_states.SOFT_DELETED, vm_states.DELETED)):
        if (vm_power_state not in (power_state.NOSTATE, power_state.SHUTDOWN)):
            LOG.warning('Instance    is    not    (soft-)deleted.', instance=db_instance)
