'Destroy and re-make this instance.
A \'rebuild\' effectively purges all existing data from the system and
remakes the VM with given \'metadata\' and \'personalities\'.
:param context: `nova.RequestContext` object
:param instance: Instance object
:param orig_image_ref: Original image_ref before rebuild
:param image_ref: New image_ref for rebuild
:param injected_files: Files to inject
:param new_pass: password to set on rebuilt instance
:param orig_sys_metadata: instance system metadata from pre-rebuild
:param bdms: block-device-mappings to use for rebuild
:param recreate: True if the instance is being recreated (e.g. the
hypervisor it was on failed) - cleanup of old state will be
skipped.
:param on_shared_storage: True if instance files on shared storage.
If not provided then information from the
driver will be used to decide if the instance
files are available or not on the target host
:param preserve_ephemeral: True if the default ephemeral storage
partition must be preserved on rebuild
:param migration: a Migration object if one was created for this
rebuild operation (if it\'s a part of evacuate)
:param scheduled_node: A node of the host chosen by the scheduler. If a
host was specified by the user, this will be
None
:param limits: Overcommit limits set by the scheduler. If a host was
specified by the user, this will be None'

@messaging.expected_exceptions(exception.PreserveEphemeralNotSupported)
@wrap_exception()
@reverts_task_state
@wrap_instance_event(prefix='compute')
@wrap_instance_fault
def rebuild_instance(self, context, instance, orig_image_ref, image_ref, injected_files, new_pass, orig_sys_metadata, bdms, recreate, on_shared_storage=None, preserve_ephemeral=False, migration=None, scheduled_node=None, limits=None):

    context = context.elevated()
    LOG.info('Rebuilding    instance', instance=instance)
    if (recreate or (scheduled_node is not None)):
        rt = self._get_resource_tracker()
        rebuild_claim = rt.rebuild_claim
    else:
        rebuild_claim = claims.NopClaim
    image_meta = {}
    if image_ref:
        image_meta = self.image_api.get(context, image_ref)
    if (not scheduled_node):
        if recreate:
            try:
                compute_node = self._get_compute_info(context, self.host)
                scheduled_node = compute_node.hypervisor_hostname
            except exception.ComputeHostNotFound:
                LOG.exception('Failed    to    get    compute_info    for    %s', self.host)
        else:
            scheduled_node = instance.node
    with self._error_out_instance_on_exception(context, instance):
        try:
            claim_ctxt = rebuild_claim(context, instance, scheduled_node, limits=limits, image_meta=image_meta, migration=migration)
            self._do_rebuild_instance_with_claim(claim_ctxt, context, instance, orig_image_ref, image_ref, injected_files, new_pass, orig_sys_metadata, bdms, recreate, on_shared_storage, preserve_ephemeral)
        except exception.ComputeResourcesUnavailable as e:
            LOG.debug('Could    not    rebuild    instance    on    this    host,    not    enough    resources    available.', instance=instance)
            self._set_migration_status(migration, 'failed')
            self._notify_instance_rebuild_error(context, instance, e)
            raise exception.BuildAbortException(instance_uuid=instance.uuid, reason=e.format_message())
        except (exception.InstanceNotFound, exception.UnexpectedDeletingTaskStateError) as e:
            LOG.debug('Instance    was    deleted    while    rebuilding', instance=instance)
            self._set_migration_status(migration, 'failed')
            self._notify_instance_rebuild_error(context, instance, e)
        except Exception as e:
            self._set_migration_status(migration, 'failed')
            self._notify_instance_rebuild_error(context, instance, e)
            raise
        else:
            instance.apply_migration_context()
            instance.host = self.host
            instance.node = scheduled_node
            instance.save()
            instance.drop_migration_context()
            self._set_migration_status(migration, 'done')
