'Set the root/admin password for an instance on this host.
This is generally only called by API password resets after an
image has been built.
@param context: Nova auth context.
@param instance: Nova instance object.
@param new_pass: The admin password for the instance.'

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

    context = context.elevated()
    if (new_pass is None):
        new_pass = utils.generate_password()
    current_power_state = self._get_power_state(context, instance)
    expected_state = power_state.RUNNING
    if (current_power_state != expected_state):
        instance.task_state = None
        instance.save(expected_task_state=task_states.UPDATING_PASSWORD)
        _msg = (_('instance    %s    is    not    running') % instance.uuid)
        raise exception.InstancePasswordSetFailed(instance=instance.uuid, reason=_msg)
    try:
        self.driver.set_admin_password(instance, new_pass)
        LOG.info('Root    password    set', instance=instance)
        instance.task_state = None
        instance.save(expected_task_state=task_states.UPDATING_PASSWORD)
    except exception.InstanceAgentNotEnabled:
        with excutils.save_and_reraise_exception():
            LOG.debug('Guest    agent    is    not    enabled    for    the    instance.', instance=instance)
            instance.task_state = None
            instance.save(expected_task_state=task_states.UPDATING_PASSWORD)
    except exception.SetAdminPasswdNotSupported:
        with excutils.save_and_reraise_exception():
            LOG.info('set_admin_password    is    not    supported    by    this    driver    or    guest    instance.', instance=instance)
            instance.task_state = None
            instance.save(expected_task_state=task_states.UPDATING_PASSWORD)
    except NotImplementedError:
        LOG.warning('set_admin_password    is    not    implemented    by    this    driver    or    guest    instance.', instance=instance)
        instance.task_state = None
        instance.save(expected_task_state=task_states.UPDATING_PASSWORD)
        raise NotImplementedError(_('set_admin_password    is    not    implemented    by    this    driver    or    guest    instance.'))
    except exception.UnexpectedTaskStateError:
        raise
    except Exception:
        LOG.exception('set_admin_password    failed', instance=instance)
        self._set_instance_obj_error_state(context, instance)
        _msg = _('error    setting    admin    password')
        raise exception.InstancePasswordSetFailed(instance=instance.uuid, reason=_msg)
