summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2013-03-17 14:50:47 +0000
committerGerrit Code Review <review@openstack.org>2013-03-17 14:50:47 +0000
commit87a560400439acfeb63e94e08c6848fa6ec8243f (patch)
tree1342eec0686822336917190e8dd8e0734794f741
parent9ee8c946f2cf0f541f14a25505ceaaac7c0a53cd (diff)
parentfff0f422f647b689bb9a62b1eff2203354a9f885 (diff)
Merge "Make ssh key injection work with xenapi agent"
-rw-r--r--nova/tests/test_xenapi.py52
-rw-r--r--nova/virt/xenapi/agent.py19
-rw-r--r--nova/virt/xenapi/vmops.py3
3 files changed, 63 insertions, 11 deletions
diff --git a/nova/tests/test_xenapi.py b/nova/tests/test_xenapi.py
index 6714161f1..4d02da6cd 100644
--- a/nova/tests/test_xenapi.py
+++ b/nova/tests/test_xenapi.py
@@ -622,7 +622,8 @@ class XenAPIVMTestCase(stubs.XenAPITestBase):
create_record=True, empty_dns=False,
image_meta={'id': IMAGE_VHD,
'disk_format': 'vhd'},
- block_device_info=None):
+ block_device_info=None,
+ key_data=None):
if injected_files is None:
injected_files = []
@@ -634,16 +635,17 @@ class XenAPIVMTestCase(stubs.XenAPITestBase):
if create_record:
instance_values = {'id': instance_id,
- 'project_id': self.project_id,
- 'user_id': self.user_id,
- 'image_ref': image_ref,
- 'kernel_id': kernel_id,
- 'ramdisk_id': ramdisk_id,
- 'root_gb': 20,
- 'instance_type_id': instance_type_id,
- 'os_type': os_type,
- 'hostname': hostname,
- 'architecture': architecture}
+ 'project_id': self.project_id,
+ 'user_id': self.user_id,
+ 'image_ref': image_ref,
+ 'kernel_id': kernel_id,
+ 'ramdisk_id': ramdisk_id,
+ 'root_gb': 20,
+ 'instance_type_id': instance_type_id,
+ 'os_type': os_type,
+ 'hostname': hostname,
+ 'key_data': key_data,
+ 'architecture': architecture}
instance = create_instance_with_system_metadata(self.context,
instance_values)
else:
@@ -885,6 +887,34 @@ class XenAPIVMTestCase(stubs.XenAPITestBase):
self.assertEquals(vif_rec['qos_algorithm_params']['kbps'],
str(3 * 10 * 1024))
+ def test_spawn_ssh_key_injection(self):
+ # Test spawning with key_data on an instance. Should use
+ # agent file injection.
+ actual_injected_files = []
+
+ def fake_inject_file(self, method, args):
+ path = base64.b64decode(args['b64_path'])
+ contents = base64.b64decode(args['b64_contents'])
+ actual_injected_files.append((path, contents))
+ return jsonutils.dumps({'returncode': '0', 'message': 'success'})
+
+ def noop(*args, **kwargs):
+ pass
+
+ self.stubs.Set(stubs.FakeSessionForVMTests,
+ '_plugin_agent_inject_file', fake_inject_file)
+ self.stubs.Set(agent.XenAPIBasedAgent,
+ 'set_admin_password', noop)
+
+ expected_data = ('\n# The following ssh key was injected by '
+ 'Nova\nfake_keydata\n')
+
+ injected_files = [('/root/.ssh/authorized_keys', expected_data)]
+ self._test_spawn(IMAGE_VHD, None, None,
+ os_type="linux", architecture="x86-64",
+ key_data='fake_keydata')
+ self.assertEquals(actual_injected_files, injected_files)
+
def test_spawn_injected_files(self):
# Test spawning with injected_files.
actual_injected_files = []
diff --git a/nova/virt/xenapi/agent.py b/nova/virt/xenapi/agent.py
index 1fe6dff7e..a8074e8d0 100644
--- a/nova/virt/xenapi/agent.py
+++ b/nova/virt/xenapi/agent.py
@@ -224,6 +224,25 @@ class XenAPIBasedAgent(object):
return resp['message']
+ def inject_ssh_key(self):
+ sshkey = self.instance.get('key_data')
+ if not sshkey:
+ return
+ if self.instance['os_type'] == 'windows':
+ LOG.warning(_("Skipping setting of ssh key for Windows."),
+ instance=self.instance)
+ return
+ sshkey = str(sshkey)
+ keyfile = '/root/.ssh/authorized_keys'
+ key_data = ''.join([
+ '\n',
+ '# The following ssh key was injected by Nova',
+ '\n',
+ sshkey.strip(),
+ '\n',
+ ])
+ return self.inject_file(keyfile, key_data)
+
def inject_file(self, path, contents):
LOG.debug(_('Injecting file path: %r'), path, instance=self.instance)
diff --git a/nova/virt/xenapi/vmops.py b/nova/virt/xenapi/vmops.py
index cd7311678..028b20910 100644
--- a/nova/virt/xenapi/vmops.py
+++ b/nova/virt/xenapi/vmops.py
@@ -651,6 +651,9 @@ class VMOps(object):
# instance, but skip the admin password configuration
no_agent = version is None
+ # Inject ssh key.
+ agent.inject_ssh_key()
+
# Inject files, if necessary
if injected_files:
# Inject any files, if specified