Merge ~smoser/cloud-init:cleanup/ci-nocloud-pubkeys-in-metadata into cloud-init:master

Proposed by Scott Moser
Status: Merged
Approved by: Scott Moser
Approved revision: 612f9896330ed9874e341934f8da18c3992633c0
Merged at revision: 05b2308aa7e30337c2a455b5d2c67871b233e25c
Proposed branch: ~smoser/cloud-init:cleanup/ci-nocloud-pubkeys-in-metadata
Merge into: cloud-init:master
Diff against target: 118 lines (+35/-24)
2 files modified
tests/cloud_tests/platforms/nocloudkvm/instance.py (+35/-4)
tests/cloud_tests/platforms/nocloudkvm/snapshot.py (+0/-20)
Reviewer Review Type Date Requested Status
Joshua Powers (community) Approve
Server Team CI bot continuous-integration Approve
cloud-init Commiters Pending
Review via email: mp+334780@code.launchpad.net

Commit message

citest: In NoCloudKVM provide keys via metadata not userdata.

The NoCloudKVM platform was inserting ssh keys via user-data
rather than through meta-data like it is done on other platforms.
This way we are not forced to change the user-data provided.

Also, provide meta-data including a uuid as the instance-id.

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:73a8a3a50efcc918afebf2999df0dcbdfc750288
https://jenkins.ubuntu.com/server/job/cloud-init-ci/584/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/584/rebuild

review: Approve (continuous-integration)
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:612f9896330ed9874e341934f8da18c3992633c0
https://jenkins.ubuntu.com/server/job/cloud-init-ci/586/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/586/rebuild

review: Approve (continuous-integration)
Revision history for this message
Joshua Powers (powersj) wrote :

LGTM thanks for adding meta data

Note: there will be a new line at the end of the public key like:

= ubuntu@cloud_test\n"

review: Approve
Revision history for this message
Scott Moser (smoser) wrote :

Thanks. We should strip that off.
I'll fix in the morning.

On December 5, 2017 6:10:12 PM EST, Joshua Powers <email address hidden> wrote:
>Review: Approve
>
>LGTM thanks for adding meta data
>
>Note: there will be a new line at the end of the public key like:
>
>= ubuntu@cloud_test\n"
>
>
>
>
>--
>https://code.launchpad.net/~smoser/cloud-init/+git/cloud-init/+merge/334780
>You are the owner of
>~smoser/cloud-init:cleanup/ci-nocloud-pubkeys-in-metadata.

There was an error fetching revisions from git servers. Please try again in a few minutes. If the problem persists, contact Launchpad support.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/tests/cloud_tests/platforms/nocloudkvm/instance.py b/tests/cloud_tests/platforms/nocloudkvm/instance.py
2index a87d76a..62298a2 100644
3--- a/tests/cloud_tests/platforms/nocloudkvm/instance.py
4+++ b/tests/cloud_tests/platforms/nocloudkvm/instance.py
5@@ -2,13 +2,16 @@
6
7 """Base NoCloud KVM instance."""
8
9+import copy
10 import os
11 import paramiko
12 import socket
13 import subprocess
14 import time
15+import uuid
16
17 from ..instances import Instance
18+from cloudinit.atomic_helper import write_json
19 from cloudinit import util as c_util
20 from tests.cloud_tests import util
21
22@@ -37,14 +40,38 @@ class NoCloudKVMInstance(Instance):
23 @param features: dictionary of supported feature flags
24 """
25 self.user_data = user_data
26- self.meta_data = meta_data
27- self.ssh_key_file = os.path.join(platform.config['data_dir'],
28- platform.config['private_key'])
29+ if meta_data:
30+ meta_data = copy.deepcopy(meta_data)
31+ else:
32+ meta_data = {}
33+
34+ if 'instance-id' in meta_data:
35+ iid = meta_data['instance-id']
36+ else:
37+ iid = str(uuid.uuid1())
38+ meta_data['instance-id'] = iid
39+
40+ self.instance_id = iid
41+ self.ssh_key_file = os.path.join(
42+ platform.config['data_dir'], platform.config['private_key'])
43+ self.ssh_pubkey_file = os.path.join(
44+ platform.config['data_dir'], platform.config['public_key'])
45+
46+ self.ssh_pubkey = None
47+ if self.ssh_pubkey_file:
48+ with open(self.ssh_pubkey_file, "r") as fp:
49+ self.ssh_pubkey = fp.read()
50+
51+ if not meta_data.get('public-keys'):
52+ meta_data['public-keys'] = []
53+ meta_data['public-keys'].append(self.ssh_pubkey)
54+
55 self.ssh_port = None
56 self.pid = None
57 self.pid_file = None
58 self.console_file = None
59 self.disk = image_path
60+ self.meta_data = meta_data
61
62 super(NoCloudKVMInstance, self).__init__(
63 platform, name, properties, config, features)
64@@ -78,11 +105,15 @@ class NoCloudKVMInstance(Instance):
65 """Generate nocloud seed from user-data"""
66 seed_file = os.path.join(tmpdir, '%s_seed.img' % self.name)
67 user_data_file = os.path.join(tmpdir, '%s_user_data' % self.name)
68+ meta_data_file = os.path.join(tmpdir, '%s_meta_data' % self.name)
69
70 with open(user_data_file, "w") as ud_file:
71 ud_file.write(self.user_data)
72
73- c_util.subp(['cloud-localds', seed_file, user_data_file])
74+ # meta-data can be yaml, but more easily pretty printed with json
75+ write_json(meta_data_file, self.meta_data)
76+ c_util.subp(['cloud-localds', seed_file, user_data_file,
77+ meta_data_file])
78
79 return seed_file
80
81diff --git a/tests/cloud_tests/platforms/nocloudkvm/snapshot.py b/tests/cloud_tests/platforms/nocloudkvm/snapshot.py
82index 0005e1f..2dae359 100644
83--- a/tests/cloud_tests/platforms/nocloudkvm/snapshot.py
84+++ b/tests/cloud_tests/platforms/nocloudkvm/snapshot.py
85@@ -41,10 +41,6 @@ class NoCloudKVMSnapshot(Snapshot):
86 @param use_desc: description of snapshot instance use
87 @return_value: an Instance
88 """
89- key_file = os.path.join(self.platform.config['data_dir'],
90- self.platform.config['public_key'])
91- user_data = self.inject_ssh_key(user_data, key_file)
92-
93 instance = self.platform.create_instance(
94 self.properties, self.config, self.features,
95 self._image_path, image_desc=str(self), use_desc=use_desc,
96@@ -55,22 +51,6 @@ class NoCloudKVMSnapshot(Snapshot):
97
98 return instance
99
100- def inject_ssh_key(self, user_data, key_file):
101- """Inject the authorized key into the user_data."""
102- with open(key_file) as f:
103- value = f.read()
104-
105- key = 'ssh_authorized_keys:'
106- value = ' - %s' % value.strip()
107- user_data = user_data.split('\n')
108- if key in user_data:
109- user_data.insert(user_data.index(key) + 1, '%s' % value)
110- else:
111- user_data.insert(-1, '%s' % key)
112- user_data.insert(-1, '%s' % value)
113-
114- return '\n'.join(user_data)
115-
116 def destroy(self):
117 """Clean up snapshot data."""
118 shutil.rmtree(self._workd)

Subscribers

People subscribed via source and target branches