summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRussell Bryant <rbryant@redhat.com>2012-07-30 15:39:18 -0400
committerRussell Bryant <rbryant@redhat.com>2012-07-30 20:18:39 -0400
commit99f6c32bf8a5204da0e07137486c61cdba5318a3 (patch)
treeddb820f6a89ea1ae85a6b40b511c78f8ec1110d4
parent36dc58791482d44d63d63e9780451f9499619f05 (diff)
Send a full instance in set_admin_password.
Change the set_admin_password method of the compute rpc API to take a full instance over rpc instead of just the instance UUID. This cuts down on database access needed by nova-compute. Part of blueprint no-db-messaging. Change-Id: Iddcc7cb068090faa98f0bb87307e09d5b0ebf0c2
-rw-r--r--nova/compute/manager.py30
-rw-r--r--nova/compute/rpcapi.py8
-rw-r--r--nova/tests/compute/test_compute.py12
-rw-r--r--nova/tests/compute/test_rpcapi.py4
4 files changed, 31 insertions, 23 deletions
diff --git a/nova/compute/manager.py b/nova/compute/manager.py
index 426577d5c..6d8a52d16 100644
--- a/nova/compute/manager.py
+++ b/nova/compute/manager.py
@@ -272,7 +272,7 @@ def _get_image_meta(context, image_ref):
class ComputeManager(manager.SchedulerDependentManager):
"""Manages the running instances from creation to destruction."""
- RPC_API_VERSION = '1.32'
+ RPC_API_VERSION = '1.33'
def __init__(self, compute_driver=None, *args, **kwargs):
"""Load configuration options and connect to the hypervisor."""
@@ -1221,7 +1221,8 @@ class ComputeManager(manager.SchedulerDependentManager):
@exception.wrap_exception(notifier=notifier, publisher_id=publisher_id())
@checks_instance_lock
@wrap_instance_fault
- def set_admin_password(self, context, instance_uuid, new_pass=None):
+ def set_admin_password(self, context, instance=None, instance_uuid=None,
+ new_pass=None):
"""Set the root/admin password for an instance on this host.
This is generally only called by API password resets after an
@@ -1234,43 +1235,44 @@ class ComputeManager(manager.SchedulerDependentManager):
# Generate a random password
new_pass = utils.generate_password(FLAGS.password_length)
+ if not instance:
+ instance = self.db.instance_get_by_uuid(context, instance_uuid)
+
max_tries = 10
for i in xrange(max_tries):
- instance_ref = self.db.instance_get_by_uuid(context, instance_uuid)
-
- current_power_state = self._get_power_state(context, instance_ref)
+ current_power_state = self._get_power_state(context, instance)
expected_state = power_state.RUNNING
if current_power_state != expected_state:
- self._instance_update(context, instance_ref['uuid'],
+ self._instance_update(context, instance['uuid'],
task_state=None)
_msg = _('Failed to set admin password. Instance %s is not'
- ' running') % instance_ref["uuid"]
+ ' running') % instance["uuid"]
raise exception.Invalid(_msg)
else:
try:
- self.driver.set_admin_password(instance_ref, new_pass)
- LOG.audit(_("Root password set"), instance=instance_ref)
+ self.driver.set_admin_password(instance, new_pass)
+ LOG.audit(_("Root password set"), instance=instance)
self._instance_update(context,
- instance_ref['uuid'],
+ instance['uuid'],
task_state=None)
break
except NotImplementedError:
# NOTE(dprince): if the driver doesn't implement
# set_admin_password we break to avoid a loop
LOG.warn(_('set_admin_password is not implemented '
- 'by this driver.'), instance=instance_ref)
+ 'by this driver.'), instance=instance)
self._instance_update(context,
- instance_ref['uuid'],
+ instance['uuid'],
task_state=None)
break
except Exception, e:
# Catch all here because this could be anything.
- LOG.exception(e, instance=instance_ref)
+ LOG.exception(e, instance=instance)
if i == max_tries - 1:
self._set_instance_error_state(context,
- instance_ref['uuid'])
+ instance['uuid'])
# We create a new exception here so that we won't
# potentially reveal password information to the
# API caller. The real exception is logged above
diff --git a/nova/compute/rpcapi.py b/nova/compute/rpcapi.py
index c3b7f068d..15d1db81a 100644
--- a/nova/compute/rpcapi.py
+++ b/nova/compute/rpcapi.py
@@ -104,6 +104,8 @@ class ComputeAPI(nova.openstack.common.rpc.proxy.RpcProxy):
1.31 - Remove instance_uuid, add instance argument to revert_resize()
1.32 - Remove instance_id, add instance argument to
rollback_live_migration_at_destination()
+ 1.33 - Remove instance_uuid, add instance argument to
+ set_admin_password()
'''
BASE_RPC_API_VERSION = '1.0'
@@ -395,9 +397,11 @@ class ComputeAPI(nova.openstack.common.rpc.proxy.RpcProxy):
version='1.32')
def set_admin_password(self, ctxt, instance, new_pass):
+ instance_p = jsonutils.to_primitive(instance)
self.cast(ctxt, self.make_msg('set_admin_password',
- instance_uuid=instance['uuid'], new_pass=new_pass),
- topic=_compute_topic(self.topic, ctxt, None, instance))
+ instance=instance_p, new_pass=new_pass),
+ topic=_compute_topic(self.topic, ctxt, None, instance),
+ version='1.33')
def set_host_enabled(self, ctxt, enabled, host):
topic = _compute_topic(self.topic, ctxt, host, None)
diff --git a/nova/tests/compute/test_compute.py b/nova/tests/compute/test_compute.py
index 030314dc5..4dbcd3522 100644
--- a/nova/tests/compute/test_compute.py
+++ b/nova/tests/compute/test_compute.py
@@ -589,7 +589,7 @@ class ComputeTestCase(BaseTestCase):
def test_set_admin_password(self):
"""Ensure instance can have its admin password set"""
- instance = self._create_fake_instance()
+ instance = jsonutils.to_primitive(self._create_fake_instance())
self.compute.run_instance(self.context, instance['uuid'])
db.instance_update(self.context, instance['uuid'],
{'task_state': task_states.UPDATING_PASSWORD})
@@ -598,7 +598,7 @@ class ComputeTestCase(BaseTestCase):
self.assertEqual(inst_ref['vm_state'], vm_states.ACTIVE)
self.assertEqual(inst_ref['task_state'], task_states.UPDATING_PASSWORD)
- self.compute.set_admin_password(self.context, instance['uuid'])
+ self.compute.set_admin_password(self.context, instance=instance)
inst_ref = db.instance_get_by_uuid(self.context, instance['uuid'])
self.assertEqual(inst_ref['vm_state'], vm_states.ACTIVE)
@@ -613,7 +613,8 @@ class ComputeTestCase(BaseTestCase):
db.instance_update(self.context, instance['uuid'], {
"power_state": power_state.NOSTATE,
})
- instance = db.instance_get_by_uuid(self.context, instance['uuid'])
+ instance = jsonutils.to_primitive(db.instance_get_by_uuid(
+ self.context, instance['uuid']))
self.assertEqual(instance['power_state'], power_state.NOSTATE)
@@ -630,7 +631,7 @@ class ComputeTestCase(BaseTestCase):
self.assertRaises(exception.Invalid,
self.compute.set_admin_password,
self.context,
- instance['uuid'])
+ instance=instance)
self.compute.terminate_instance(self.context, instance['uuid'])
def test_set_admin_password_driver_error(self):
@@ -660,7 +661,8 @@ class ComputeTestCase(BaseTestCase):
#so a new error is raised
self.assertRaises(exception.NovaException,
self.compute.set_admin_password,
- self.context, instance['uuid'])
+ self.context,
+ instance=jsonutils.to_primitive(inst_ref))
inst_ref = db.instance_get_by_uuid(self.context, instance['uuid'])
self.assertEqual(inst_ref['vm_state'], vm_states.ERROR)
diff --git a/nova/tests/compute/test_rpcapi.py b/nova/tests/compute/test_rpcapi.py
index 9b55bbb97..e726606d4 100644
--- a/nova/tests/compute/test_rpcapi.py
+++ b/nova/tests/compute/test_rpcapi.py
@@ -60,7 +60,7 @@ class ComputeRpcAPITestCase(test.TestCase):
'rebuild_instance', 'remove_fixed_ip_from_instance',
'remove_volume_connection', 'rescue_instance', 'reset_network',
'resize_instance', 'resume_instance', 'revert_resize',
- 'rollback_live_migration_at_destination',
+ 'rollback_live_migration_at_destination', 'set_admin_password',
'start_instance', 'stop_instance', 'suspend_instance',
'unpause_instance'
]
@@ -294,7 +294,7 @@ class ComputeRpcAPITestCase(test.TestCase):
def test_set_admin_password(self):
self._test_compute_api('set_admin_password', 'cast',
- instance=self.fake_instance, new_pass='pw')
+ instance=self.fake_instance, new_pass='pw', version='1.33')
def test_set_host_enabled(self):
self._test_compute_api('set_host_enabled', 'call',