← Back to team overview

cloud-init-dev team mailing list archive

[Merge] lp:~sergiusens/cloud-init/snappy into lp:cloud-init

 

Sergio Schvezov has proposed merging lp:~sergiusens/cloud-init/snappy into lp:cloud-init.

Requested reviews:
  cloud init development team (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~sergiusens/cloud-init/snappy/+merge/243419


-- 
Your team cloud init development team is requested to review the proposed merge of lp:~sergiusens/cloud-init/snappy into lp:cloud-init.
=== added file 'cloudinit/config/cc_snappy.py'
--- cloudinit/config/cc_snappy.py	1970-01-01 00:00:00 +0000
+++ cloudinit/config/cc_snappy.py	2014-12-02 16:27:03 +0000
@@ -0,0 +1,119 @@
+# vi: ts=4 expandtab
+#
+
+from cloudinit import log as logging
+from cloudinit import templater
+from cloudinit import util
+from cloudinit.settings import PER_INSTANCE
+
+import glob
+import os
+
+LOG = logging.getLogger(__name__)
+
+frequency = PER_INSTANCE
+SNAPPY_ENV_PATH = "/opt/click.ubuntu.com/snappy.env"
+
+
+CI_SNAPPY_CFG = {
+    'env_file_path': SNAPPY_ENV_PATH,
+    'packages': [],
+    'packages_dir': '/userdata/cloud-init/click_packages',
+}
+
+"""
+snappy:
+  ssh_enabled: True
+  packages:
+    - etcd
+    - {'name': 'pkg1', 'config': "wark"}
+"""
+
+def flatten(data, fill=None, tok="_", prefix='', recurse=True):
+    if fill is None:
+        fill = {}
+    for key, val in data.items():
+        key = key.replace("-", "_")
+        if isinstance(val, dict) and recurse:
+            flatten(val, fill, tok=tok, prefix=prefix + key + tok,
+                    recurse=recurse)
+        elif isinstance(key, str):
+            fill[prefix + key] = val
+    return fill
+
+
+def render2env(data, tok="_", prefix=''):
+    flat = flatten(data, tok=tok, prefix=prefix)
+    ret = ["%s='%s'" % (key, val) for key, val in flat.items()]
+    return '\n'.join(ret) + '\n'
+
+
+def install_package(pkg_name, config=None):
+    cmd = ["snappy", "install"]
+    if config:
+        if os.path.isfile(config):
+            cmd.append("--config-file=" + config)
+        else:
+            cmd.append("--config=" + config)
+    cmd.append(pkg_name)
+    util.subp(cmd)
+
+
+def install_packages(package_dir, packages):
+    local_pkgs = glob.glob(os.path.sep.join([package_dir, '*.click']))
+    LOG.debug("installing local packages %s" % local_pkgs)
+    if local_pkgs:
+        for pkg in local_pkgs:
+            cfg = pkg.replace(".click", ".config")
+            if not os.path.isfile(cfg):
+                cfg = None
+            install_package(pkg, config=cfg)
+
+    LOG.debug("installing click packages")
+    if packages:
+        for pkg in packages:
+            if not pkg:
+                continue
+            if isinstance(pkg, str):
+                name = pkg
+                config = None
+            elif pkg:
+                name = pkg.get('name', pkg)
+                config = pkg.get('config')
+            install_package(pkg_name=name, config=config)
+
+
+def disable_enable_ssh(enabled):
+    LOG.debug("setting enablement of ssh to: %s", enabled)
+    # do something here that would enable or disable
+    not_to_be_run = "/etc/ssh/sshd_not_to_be_run"
+    if enabled:
+        util.write_file(not_to_be_run, "cloud-init\n")
+        util.subp(["systemctl", "start", "ssh"])
+        util.subp(["systemctl", "status", "ssh"])
+    else:
+        util.subp(["systemctl", "stop", "ssh"])
+        util.del_file(not_to_be_run)
+
+
+def handle(name, cfg, cloud, log, args):
+    mycfg = cfg.get('snappy', {'ssh_enabled': False})
+
+    if not mycfg:
+        LOG.debug("%s: no top level found", name)
+        return
+
+    # take out of the cfg my specific names
+    # and render the flattened environment variable style file to a path
+    # this was useful for systemd config environment files.
+    ci_cfg = CI_SNAPPY_CFG.copy()
+    for i in ci_cfg:
+        if i in mycfg:
+            ci_cfg[i] = mycfg[i]
+            del mycfg[i]
+    util.write_file(ci_cfg['env_file_path'], render2env(mycfg))
+
+    install_packages(ci_cfg['packages_dir'],
+                     ci_cfg['packages'])
+
+    disable_enable_ssh(mycfg.get('ssh_enabled', False))

=== modified file 'cloudinit/config/cc_ssh.py'
--- cloudinit/config/cc_ssh.py	2014-08-26 18:50:11 +0000
+++ cloudinit/config/cc_ssh.py	2014-12-02 16:27:03 +0000
@@ -135,4 +135,4 @@
     else:
         key_prefix = ''
 
-    ssh_util.setup_user_keys(keys, 'root', options=key_prefix)
+    #ssh_util.setup_user_keys(keys, 'root', options=key_prefix)

=== modified file 'config/cloud.cfg'
--- config/cloud.cfg	2014-07-31 17:41:22 +0000
+++ config/cloud.cfg	2014-12-02 16:27:03 +0000
@@ -29,7 +29,6 @@
  - bootcmd
  - write-files
  - growpart
- - resizefs
  - set_hostname
  - update_hostname
  - update_etc_hosts
@@ -51,6 +50,7 @@
  - grub-dpkg
  - apt-pipelining
  - apt-configure
+ - snappy
  - package-update-upgrade-install
  - landscape
  - timezone
@@ -84,7 +84,7 @@
    # Default user name + that default users groups (if added/used)
    default_user:
      name: ubuntu
-     lock_passwd: True
+     lock_passwd: False
      gecos: Ubuntu
      groups: [adm, audio, cdrom, dialout, dip, floppy, netdev, plugdev, sudo, video]
      sudo: ["ALL=(ALL) NOPASSWD:ALL"]