Merge lp:~sergiusens/cloud-init/snappy_ssh into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Sergio Schvezov
Status: Superseded
Proposed branch: lp:~sergiusens/cloud-init/snappy_ssh
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 163 lines (+123/-3)
3 files modified
cloudinit/config/cc_snappy.py (+120/-0)
cloudinit/config/cc_ssh.py (+1/-1)
config/cloud.cfg (+2/-2)
To merge this branch: bzr merge lp:~sergiusens/cloud-init/snappy_ssh
Reviewer Review Type Date Requested Status
cloud-init Commiters Pending
Review via email: mp+243438@code.launchpad.net

This proposal has been superseded by a proposal from 2014-12-02.

To post a comment you must log in.

Unmerged revisions

1041. By Sergio Schvezov

Change to add ssh inhibition file if disabled and remove if enabled

1040. By Scott Moser

start or stop ssh based on enable_ssh

1039. By Scott Moser

update config, use name in output of log message

1038. By Scott Moser

rename snappy-helper to snappy

1037. By Scott Moser

give default stuff so enable/disable gets run

1036. By Scott Moser

some snappy hacks

1035. By Scott Moser

change package dir, support disable_enable

1034. By Scott Moser

add initial snappy helper

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'cloudinit/config/cc_snappy.py'
2--- cloudinit/config/cc_snappy.py 1970-01-01 00:00:00 +0000
3+++ cloudinit/config/cc_snappy.py 2014-12-02 18:16:51 +0000
4@@ -0,0 +1,120 @@
5+# vi: ts=4 expandtab
6+#
7+
8+from cloudinit import log as logging
9+from cloudinit import templater
10+from cloudinit import util
11+from cloudinit.settings import PER_INSTANCE
12+
13+import glob
14+import os
15+
16+LOG = logging.getLogger(__name__)
17+
18+frequency = PER_INSTANCE
19+SNAPPY_ENV_PATH = "/opt/click.ubuntu.com/snappy.env"
20+
21+
22+CI_SNAPPY_CFG = {
23+ 'env_file_path': SNAPPY_ENV_PATH,
24+ 'packages': [],
25+ 'packages_dir': '/userdata/cloud-init/click_packages',
26+}
27+
28+"""
29+snappy:
30+ ssh_enabled: True
31+ packages:
32+ - etcd
33+ - {'name': 'pkg1', 'config': "wark"}
34+"""
35+
36+def flatten(data, fill=None, tok="_", prefix='', recurse=True):
37+ if fill is None:
38+ fill = {}
39+ for key, val in data.items():
40+ key = key.replace("-", "_")
41+ if isinstance(val, dict) and recurse:
42+ flatten(val, fill, tok=tok, prefix=prefix + key + tok,
43+ recurse=recurse)
44+ elif isinstance(key, str):
45+ fill[prefix + key] = val
46+ return fill
47+
48+
49+def render2env(data, tok="_", prefix=''):
50+ flat = flatten(data, tok=tok, prefix=prefix)
51+ ret = ["%s='%s'" % (key, val) for key, val in flat.items()]
52+ return '\n'.join(ret) + '\n'
53+
54+
55+def install_package(pkg_name, config=None):
56+ cmd = ["snappy", "install"]
57+ if config:
58+ if os.path.isfile(config):
59+ cmd.append("--config-file=" + config)
60+ else:
61+ cmd.append("--config=" + config)
62+ cmd.append(pkg_name)
63+ util.subp(cmd)
64+
65+
66+def install_packages(package_dir, packages):
67+ local_pkgs = glob.glob(os.path.sep.join([package_dir, '*.click']))
68+ LOG.debug("installing local packages %s" % local_pkgs)
69+ if local_pkgs:
70+ for pkg in local_pkgs:
71+ cfg = pkg.replace(".click", ".config")
72+ if not os.path.isfile(cfg):
73+ cfg = None
74+ install_package(pkg, config=cfg)
75+
76+ LOG.debug("installing click packages")
77+ if packages:
78+ for pkg in packages:
79+ if not pkg:
80+ continue
81+ if isinstance(pkg, str):
82+ name = pkg
83+ config = None
84+ elif pkg:
85+ name = pkg.get('name', pkg)
86+ config = pkg.get('config')
87+ install_package(pkg_name=name, config=config)
88+
89+
90+def disable_enable_ssh(enabled):
91+ LOG.debug("setting enablement of ssh to: %s", enabled)
92+ # do something here that would enable or disable
93+ not_to_be_run = "/etc/ssh/sshd_not_to_be_run"
94+ if enabled:
95+ util.del_file(not_to_be_run)
96+ # this is an indempotent operation
97+ util.subp(["systemctl", "start", "ssh"])
98+ else:
99+ # this is an indempotent operation
100+ util.subp(["systemctl", "stop", "ssh"])
101+ util.write_file(not_to_be_run, "cloud-init\n")
102+
103+
104+def handle(name, cfg, cloud, log, args):
105+ mycfg = cfg.get('snappy', {'ssh_enabled': False})
106+
107+ if not mycfg:
108+ LOG.debug("%s: no top level found", name)
109+ return
110+
111+ # take out of the cfg my specific names
112+ # and render the flattened environment variable style file to a path
113+ # this was useful for systemd config environment files.
114+ ci_cfg = CI_SNAPPY_CFG.copy()
115+ for i in ci_cfg:
116+ if i in mycfg:
117+ ci_cfg[i] = mycfg[i]
118+ del mycfg[i]
119+ util.write_file(ci_cfg['env_file_path'], render2env(mycfg))
120+
121+ install_packages(ci_cfg['packages_dir'],
122+ ci_cfg['packages'])
123+
124+ disable_enable_ssh(mycfg.get('ssh_enabled', False))
125
126=== modified file 'cloudinit/config/cc_ssh.py'
127--- cloudinit/config/cc_ssh.py 2014-08-26 18:50:11 +0000
128+++ cloudinit/config/cc_ssh.py 2014-12-02 18:16:51 +0000
129@@ -135,4 +135,4 @@
130 else:
131 key_prefix = ''
132
133- ssh_util.setup_user_keys(keys, 'root', options=key_prefix)
134+ #ssh_util.setup_user_keys(keys, 'root', options=key_prefix)
135
136=== modified file 'config/cloud.cfg'
137--- config/cloud.cfg 2014-07-31 17:41:22 +0000
138+++ config/cloud.cfg 2014-12-02 18:16:51 +0000
139@@ -29,7 +29,6 @@
140 - bootcmd
141 - write-files
142 - growpart
143- - resizefs
144 - set_hostname
145 - update_hostname
146 - update_etc_hosts
147@@ -51,6 +50,7 @@
148 - grub-dpkg
149 - apt-pipelining
150 - apt-configure
151+ - snappy
152 - package-update-upgrade-install
153 - landscape
154 - timezone
155@@ -84,7 +84,7 @@
156 # Default user name + that default users groups (if added/used)
157 default_user:
158 name: ubuntu
159- lock_passwd: True
160+ lock_passwd: False
161 gecos: Ubuntu
162 groups: [adm, audio, cdrom, dialout, dip, floppy, netdev, plugdev, sudo, video]
163 sudo: ["ALL=(ALL) NOPASSWD:ALL"]