Merge lp:~harlowja/cloud-init/cloud-init-enhanced-bootcmd into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Joshua Harlow
Status: Rejected
Rejected by: Scott Moser
Proposed branch: lp:~harlowja/cloud-init/cloud-init-enhanced-bootcmd
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 73 lines (+24/-9)
2 files modified
cloudinit/config/cc_bootcmd.py (+20/-5)
cloudinit/util.py (+4/-4)
To merge this branch: bzr merge lp:~harlowja/cloud-init/cloud-init-enhanced-bootcmd
Reviewer Review Type Date Requested Status
Server Team CI bot continuous-integration Approve
cloud-init Commiters Pending
Review via email: mp+298254@code.launchpad.net

Description of the change

Some small enhancements to bootcmd module.

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Scott Moser (smoser) wrote :

Hello,
Thank you for taking the time to contribute to cloud-init. Cloud-init has moved its revision control system to git. As a result, we are marking all bzr merge proposals as 'rejected'. If you would like to re-submit this proposal for review, please do so by following the current HACKING documentation at http://cloudinit.readthedocs.io/en/latest/topics/hacking.html .

I do really like this change, please do resubmit.

Unmerged revisions

1248. By Joshua Harlow

Also handle tuples correctly.

1247. By Joshua Harlow

Only add the shellify header if its really needed.

1246. By Joshua Harlow

Allow more env values for bootcmd module

Instead of just the basic iid it can be quite
useful to pass more information into the shell
command that is eventually ran (then the script
that is ran doesn't have to gather this same
info itself).

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cloudinit/config/cc_bootcmd.py'
2--- cloudinit/config/cc_bootcmd.py 2016-05-12 17:56:26 +0000
3+++ cloudinit/config/cc_bootcmd.py 2016-06-23 20:04:24 +0000
4@@ -26,6 +26,25 @@
5 frequency = PER_ALWAYS
6
7
8+def create_env(cloud):
9+ env = os.environ.copy()
10+ iid = cloud.get_instance_id()
11+ if iid:
12+ env['INSTANCE_ID'] = str(iid)
13+ launch_index = cloud.launch_index()
14+ if launch_index is not None:
15+ env['LAUNCH_INDEX'] = str(launch_index)
16+ hostname = cloud.get_hostname()
17+ if hostname:
18+ env['HOSTNAME'] = str(hostname)
19+ for k, v in [('AVAILABILITY_ZONE', cloud.datasource.availability_zone),
20+ ('REGION', cloud.datasource.region),
21+ ('LOCALE', cloud.get_locale())]:
22+ if v:
23+ env[k] = str(v)
24+ return env
25+
26+
27 def handle(name, cfg, cloud, log, _args):
28
29 if "bootcmd" not in cfg:
30@@ -43,12 +62,8 @@
31 raise
32
33 try:
34- env = os.environ.copy()
35- iid = cloud.get_instance_id()
36- if iid:
37- env['INSTANCE_ID'] = str(iid)
38 cmd = ['/bin/sh', tmpf.name]
39- util.subp(cmd, env=env, capture=False)
40+ util.subp(cmd, env=create_env(cloud), capture=False)
41 except Exception:
42 util.logexc(log, "Failed to run bootcmd module %s", name)
43 raise
44
45=== modified file 'cloudinit/util.py'
46--- cloudinit/util.py 2016-06-16 03:32:22 +0000
47+++ cloudinit/util.py 2016-06-23 20:04:24 +0000
48@@ -1746,16 +1746,14 @@
49 # for each entry in the list
50 # if it is an array, shell protect it (with single ticks)
51 # if it is a string, do nothing
52-def shellify(cmdlist, add_header=True):
53+def shellify(cmdlist, add_header=True, header="#!/bin/sh\n"):
54 content = ''
55- if add_header:
56- content += "#!/bin/sh\n"
57 escaped = "%s%s%s%s" % ("'", '\\', "'", "'")
58 cmds_made = 0
59 for args in cmdlist:
60 # If the item is a list, wrap all items in single tick.
61 # If its not, then just write it directly.
62- if isinstance(args, list):
63+ if isinstance(args, (list, tuple)):
64 fixed = []
65 for f in args:
66 fixed.append("'%s'" % (six.text_type(f).replace("'", escaped)))
67@@ -1769,6 +1767,8 @@
68 " which is not a list or string")
69 % (type_utils.obj_name(args)))
70 LOG.debug("Shellified %s commands.", cmds_made)
71+ if add_header and header and not content.startswith(header):
72+ content = header + content
73 return content
74
75