'Initialize this instance during service init.'

def _init_instance(self, context, instance):

    if (instance.host != self.host):
        LOG.warning('Instance    %(uuid)s    appears    to    not    be    owned    by    this    host,    but    by    %(host)s.    Startup    processing    is    being    skipped.', {'uuid': instance.uuid, 'host': instance.host})
        return
    if ((instance.vm_state == vm_states.SOFT_DELETED) or ((instance.vm_state == vm_states.ERROR) and (instance.task_state not in (task_states.RESIZE_MIGRATING, task_states.DELETING)))):
        LOG.debug('Instance    is    in    %s    state.', instance.vm_state, instance=instance)
        return
    if (instance.vm_state == vm_states.DELETED):
        try:
            self._complete_partial_deletion(context, instance)
        except Exception:
            LOG.exception('Failed    to    complete    a    deletion', instance=instance)
        return
    if ((instance.vm_state == vm_states.BUILDING) or (instance.task_state in [task_states.SCHEDULING, task_states.BLOCK_DEVICE_MAPPING, task_states.NETWORKING, task_states.SPAWNING])):
        LOG.debug('Instance    failed    to    spawn    correctly,    setting    to    ERROR    state', instance=instance)
        instance.task_state = None
        instance.vm_state = vm_states.ERROR
        instance.save()
        return
    if ((instance.vm_state in [vm_states.ACTIVE, vm_states.STOPPED]) and (instance.task_state in [task_states.REBUILDING, task_states.REBUILD_BLOCK_DEVICE_MAPPING, task_states.REBUILD_SPAWNING])):
        LOG.debug('Instance    failed    to    rebuild    correctly,    setting    to    ERROR    state', instance=instance)
        instance.task_state = None
        instance.vm_state = vm_states.ERROR
        instance.save()
        return
    if ((instance.vm_state != vm_states.ERROR) and (instance.task_state in [task_states.IMAGE_SNAPSHOT_PENDING, task_states.IMAGE_PENDING_UPLOAD, task_states.IMAGE_UPLOADING, task_states.IMAGE_SNAPSHOT])):
        LOG.debug('Instance    in    transitional    state    %s    at    start-up    clearing    task    state', instance.task_state, instance=instance)
        try:
            self._post_interrupted_snapshot_cleanup(context, instance)
        except Exception:
            LOG.exception('Failed    to    cleanup    snapshot.', instance=instance)
        instance.task_state = None
        instance.save()
    if ((instance.vm_state != vm_states.ERROR) and (instance.task_state in [task_states.RESIZE_PREP])):
        LOG.debug('Instance    in    transitional    state    %s    at    start-up    clearing    task    state', instance['task_state'], instance=instance)
        instance.task_state = None
        instance.save()
    if (instance.task_state == task_states.DELETING):
        try:
            LOG.info('Service    started    deleting    the    instance    during    the    previous    run,    but    did    not    finish.    Restarting    the    deletion    now.', instance=instance)
            instance.obj_load_attr('metadata')
            instance.obj_load_attr('system_metadata')
            bdms = objects.BlockDeviceMappingList.get_by_instance_uuid(context, instance.uuid)
            self._delete_instance(context, instance, bdms)
        except Exception:
            LOG.exception('Failed    to    complete    a    deletion', instance=instance)
            self._set_instance_obj_error_state(context, instance)
        return
    current_power_state = self._get_power_state(context, instance)
    (try_reboot, reboot_type) = self._retry_reboot(context, instance, current_power_state)
    if try_reboot:
        LOG.debug('Instance    in    transitional    state    (%(task_state)s)    at    start-up    and    power    state    is    (%(power_state)s),    triggering    reboot', {'task_state': instance.task_state, 'power_state': current_power_state}, instance=instance)
        soft_types = [task_states.REBOOT_STARTED, task_states.REBOOT_PENDING, task_states.REBOOTING]
        if ((instance.task_state in soft_types) and (reboot_type == 'HARD')):
            instance.task_state = task_states.REBOOT_PENDING_HARD
            instance.save()
        self.reboot_instance(context, instance, block_device_info=None, reboot_type=reboot_type)
        return
    elif ((current_power_state == power_state.RUNNING) and (instance.task_state in [task_states.REBOOT_STARTED, task_states.REBOOT_STARTED_HARD, task_states.PAUSING, task_states.UNPAUSING])):
        LOG.warning('Instance    in    transitional    state    (%(task_state)s)    at    start-up    and    power    state    is    (%(power_state)s),    clearing    task    state', {'task_state': instance.task_state, 'power_state': current_power_state}, instance=instance)
        instance.task_state = None
        instance.vm_state = vm_states.ACTIVE
        instance.save()
    elif ((current_power_state == power_state.PAUSED) and (instance.task_state == task_states.UNPAUSING)):
        LOG.warning('Instance    in    transitional    state    (%(task_state)s)    at    start-up    and    power    state    is    (%(power_state)s),    clearing    task    state    and    unpausing    the    instance', {'task_state': instance.task_state, 'power_state': current_power_state}, instance=instance)
        try:
            self.unpause_instance(context, instance)
        except NotImplementedError:
            pass
        except Exception:
            LOG.exception('Failed    to    unpause    instance', instance=instance)
        return
    if (instance.task_state == task_states.POWERING_OFF):
        try:
            LOG.debug('Instance    in    transitional    state    %s    at    start-up    retrying    stop    request', instance.task_state, instance=instance)
            self.stop_instance(context, instance, True)
        except Exception:
            LOG.exception('Failed    to    stop    instance', instance=instance)
        return
    if (instance.task_state == task_states.POWERING_ON):
        try:
            LOG.debug('Instance    in    transitional    state    %s    at    start-up    retrying    start    request', instance.task_state, instance=instance)
            self.start_instance(context, instance)
        except Exception:
            LOG.exception('Failed    to    start    instance', instance=instance)
        return
    net_info = instance.get_network_info()
    try:
        self.driver.plug_vifs(instance, net_info)
    except NotImplementedError as e:
        LOG.debug(e, instance=instance)
    except exception.VirtualInterfacePlugException:
        LOG.exception('Vifs    plug    failed', instance=instance)
        self._set_instance_obj_error_state(context, instance)
        return
    if (instance.task_state == task_states.RESIZE_MIGRATING):
        try:
            power_on = (instance.system_metadata.get('old_vm_state') != vm_states.STOPPED)
            block_dev_info = self._get_instance_block_device_info(context, instance)
            self.driver.finish_revert_migration(context, instance, net_info, block_dev_info, power_on)
        except Exception:
            LOG.exception('Failed    to    revert    crashed    migration', instance=instance)
        finally:
            LOG.info('Instance    found    in    migrating    state    during    startup.    Resetting    task_state', instance=instance)
            instance.task_state = None
            instance.save()
    if (instance.task_state == task_states.MIGRATING):
        instance.task_state = None
        instance.save(expected_task_state=[task_states.MIGRATING])
    db_state = instance.power_state
    drv_state = self._get_power_state(context, instance)
    expect_running = ((db_state == power_state.RUNNING) and (drv_state != db_state))
    LOG.debug('Current    state    is    %(drv_state)s,    state    in    DB    is    %(db_state)s.', {'drv_state': drv_state, 'db_state': db_state}, instance=instance)
    if (expect_running and CONF.resume_guests_state_on_host_boot):
        LOG.info('Rebooting    instance    after    nova-compute    restart.', instance=instance)
        block_device_info = self._get_instance_block_device_info(context, instance)
        try:
            self.driver.resume_state_on_host_boot(context, instance, net_info, block_device_info)
        except NotImplementedError:
            LOG.warning('Hypervisor    driver    does    not    support    resume    guests', instance=instance)
        except Exception:
            LOG.warning('Failed    to    resume    instance', instance=instance)
            self._set_instance_obj_error_state(context, instance)
    elif (drv_state == power_state.RUNNING):
        try:
            self.driver.ensure_filtering_rules_for_instance(instance, net_info)
        except NotImplementedError:
            LOG.debug('Hypervisor    driver    does    not    support    firewall    rules', instance=instance)
