@exception.wrap_exception(notifier=notifier, publisher_id=publisher_id())
    @checks_instance_lock
    def rebuild_instance(self, context, instance_id, **kwargs):
        """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_id: Instance identifier (integer)
        :param injected_files: Files to inject
        :param new_pass: password to set on rebuilt instance
        """
        context = context.elevated()

        instance_ref = self.db.instance_get(context, instance_id)
        LOG.audit(_("Rebuilding instance %s"), instance_id, context=context)

        current_power_state = self._get_power_state(context, instance_ref)
        self._instance_update(context,
                              instance_id,
                              power_state=current_power_state,
                              vm_state=vm_states.REBUILDING,
                              task_state=None)

        network_info = self._get_instance_nw_info(context, instance_ref)
        self.driver.destroy(instance_ref, network_info)

        self._instance_update(context,
                              instance_id,
                              vm_state=vm_states.REBUILDING,
                              task_state=task_states.BLOCK_DEVICE_MAPPING)

        instance_ref.injected_files = kwargs.get('injected_files', [])
        network_info = self.network_api.get_instance_nw_info(context,
                                                              instance_ref)
        bd_mapping = self._setup_block_device_mapping(context, instance_id)

        self._instance_update(context,
                              instance_id,
                              vm_state=vm_states.REBUILDING,
                              task_state=task_states.SPAWNING)

        # pull in new password here since the original password isn't in the db
        instance_ref.admin_pass = kwargs.get('new_pass',
                utils.generate_password(FLAGS.password_length))

        self.driver.spawn(context, instance_ref, network_info, bd_mapping)

        current_power_state = self._get_power_state(context, instance_ref)
        self._instance_update(context,
                              instance_id,
                              power_state=current_power_state,
                              vm_state=vm_states.ACTIVE,
                              task_state=None,
                              launched_at=utils.utcnow())

        usage_info = utils.usage_from_instance(instance_ref)
        notifier.notify('compute.%s' % self.host,
                            'compute.instance.rebuild',
                            notifier.INFO,
                            usage_info)
dataset/ETHPy150Open nii-cloud/dodai-compute/nova/compute/manager.py/ComputeManager.rebuild_instance/original