← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~powersj/cloud-init:cii-move-genkeys into cloud-init:master

 

Joshua Powers has proposed merging ~powersj/cloud-init:cii-move-genkeys into cloud-init:master.

Commit message:
tests: move SSH key gen to platform superclass

Allow each platform to determine if and when they need to generate SSH
keys. For example, this prevents LXD from unnecessarily creating SSH keys
and allows other platforms to do things, like upload them to clouds, to the
generated keys.

Drive by:
* nocloud-kvm: call super before initializing other variables
* Remove unnecessary touch of image to print version of cloud-init


Requested reviews:
  cloud-init commiters (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~powersj/cloud-init/+git/cloud-init/+merge/335051
-- 
Your team cloud-init commiters is requested to review the proposed merge of ~powersj/cloud-init:cii-move-genkeys into cloud-init:master.
diff --git a/tests/cloud_tests/platforms/nocloudkvm/image.py b/tests/cloud_tests/platforms/nocloudkvm/image.py
index 09ff2a3..8fff422 100644
--- a/tests/cloud_tests/platforms/nocloudkvm/image.py
+++ b/tests/cloud_tests/platforms/nocloudkvm/image.py
@@ -24,6 +24,8 @@ class NoCloudKVMImage(Image):
         @param config: image configuration
         @param img_path: path to the image
         """
+        super(NoCloudKVMImage, self).__init__(platform, config)
+
         self.modified = False
         self._workd = tempfile.mkdtemp(prefix='NoCloudKVMImage')
         self._orig_img_path = orig_img_path
@@ -33,8 +35,6 @@ class NoCloudKVMImage(Image):
         c_util.subp(['qemu-img', 'create', '-f', 'qcow2',
                     '-b', orig_img_path, self._img_path])
 
-        super(NoCloudKVMImage, self).__init__(platform, config)
-
     @property
     def properties(self):
         """Dictionary containing: 'arch', 'os', 'version', 'release'."""
diff --git a/tests/cloud_tests/platforms/nocloudkvm/platform.py b/tests/cloud_tests/platforms/nocloudkvm/platform.py
index 8593346..f7c915f 100644
--- a/tests/cloud_tests/platforms/nocloudkvm/platform.py
+++ b/tests/cloud_tests/platforms/nocloudkvm/platform.py
@@ -21,6 +21,11 @@ class NoCloudKVMPlatform(Platform):
 
     platform_name = 'nocloud-kvm'
 
+    def __init__(self, config):
+        """Set up platform."""
+        super(NoCloudKVMPlatform, self).__init__(config)
+        self._generate_ssh_keys(config['data_dir'])
+
     def get_image(self, img_conf):
         """Get image using specified image configuration.
 
diff --git a/tests/cloud_tests/platforms/nocloudkvm/snapshot.py b/tests/cloud_tests/platforms/nocloudkvm/snapshot.py
index 2dae359..1070dff 100644
--- a/tests/cloud_tests/platforms/nocloudkvm/snapshot.py
+++ b/tests/cloud_tests/platforms/nocloudkvm/snapshot.py
@@ -22,14 +22,15 @@ class NoCloudKVMSnapshot(Snapshot):
         @param features: supported feature flags
         @param image_path: image file to snapshot.
         """
+        super(NoCloudKVMSnapshot, self).__init__(
+            platform, properties, config, features
+        )
+
         self._workd = tempfile.mkdtemp(prefix='NoCloudKVMSnapshot')
         snapshot = os.path.join(self._workd, 'snapshot')
         shutil.copyfile(image_path, snapshot)
         self._image_path = snapshot
 
-        super(NoCloudKVMSnapshot, self).__init__(
-            platform, properties, config, features)
-
     def launch(self, user_data, meta_data=None, block=True, start=True,
                use_desc=None):
         """Launch instance.
diff --git a/tests/cloud_tests/platforms/platforms.py b/tests/cloud_tests/platforms/platforms.py
index 2897536..2791287 100644
--- a/tests/cloud_tests/platforms/platforms.py
+++ b/tests/cloud_tests/platforms/platforms.py
@@ -1,6 +1,9 @@
 # This file is part of cloud-init. See LICENSE file for license information.
 
 """Base platform class."""
+import os
+
+from cloudinit import util as c_util
 
 
 class Platform(object):
@@ -24,4 +27,16 @@ class Platform(object):
         """Clean up platform data."""
         pass
 
+    def _generate_ssh_keys(self, data_dir):
+        """Generate SSH keys to be used with image."""
+        filename = os.path.join(data_dir, 'id_rsa')
+
+        if os.path.exists(filename):
+            c_util.del_file(filename)
+
+        c_util.subp(['ssh-keygen', '-t', 'rsa', '-b', '4096',
+                     '-f', filename, '-P', '',
+                     '-C', 'ubuntu@cloud_test'],
+                    capture=True)
+
 # vi: ts=4 expandtab
diff --git a/tests/cloud_tests/setup_image.py b/tests/cloud_tests/setup_image.py
index 179f40d..6d24211 100644
--- a/tests/cloud_tests/setup_image.py
+++ b/tests/cloud_tests/setup_image.py
@@ -5,7 +5,6 @@
 from functools import partial
 import os
 
-from cloudinit import util as c_util
 from tests.cloud_tests import LOG
 from tests.cloud_tests import stage, util
 
@@ -192,20 +191,6 @@ def enable_repo(args, image):
     image.execute(cmd, description=msg)
 
 
-def generate_ssh_keys(data_dir):
-    """Generate SSH keys to be used with image."""
-    LOG.info('generating SSH keys')
-    filename = os.path.join(data_dir, 'id_rsa')
-
-    if os.path.exists(filename):
-        c_util.del_file(filename)
-
-    c_util.subp(['ssh-keygen', '-t', 'rsa', '-b', '4096',
-                 '-f', filename, '-P', '',
-                 '-C', 'ubuntu@cloud_test'],
-                capture=True)
-
-
 def setup_image(args, image):
     """Set up image as specified in args.
 
@@ -239,9 +224,6 @@ def setup_image(args, image):
     LOG.info('setting up %s', image)
     res = stage.run_stage(
         'set up for {}'.format(image), calls, continue_after_error=False)
-    LOG.debug('after setup complete, installed cloud-init version is: %s',
-              installed_package_version(image, 'cloud-init'))
-    generate_ssh_keys(args.data_dir)
     return res
 
 # vi: ts=4 expandtab

Follow ups