cloud-init-dev team mailing list archive
-
cloud-init-dev team
-
Mailing list archive
-
Message #00973
[Merge] lp:~harlowja/cloud-init/cloud-init-enhanced-bootcmd into lp:cloud-init
Joshua Harlow has proposed merging lp:~harlowja/cloud-init/cloud-init-enhanced-bootcmd into lp:cloud-init.
Requested reviews:
cloud init development team (cloud-init-dev)
For more details, see:
https://code.launchpad.net/~harlowja/cloud-init/cloud-init-enhanced-bootcmd/+merge/298254
Some small enhancements to bootcmd module.
--
Your team cloud init development team is requested to review the proposed merge of lp:~harlowja/cloud-init/cloud-init-enhanced-bootcmd into lp:cloud-init.
=== modified file 'cloudinit/config/cc_bootcmd.py'
--- cloudinit/config/cc_bootcmd.py 2016-05-12 17:56:26 +0000
+++ cloudinit/config/cc_bootcmd.py 2016-06-23 20:04:24 +0000
@@ -26,6 +26,25 @@
frequency = PER_ALWAYS
+def create_env(cloud):
+ env = os.environ.copy()
+ iid = cloud.get_instance_id()
+ if iid:
+ env['INSTANCE_ID'] = str(iid)
+ launch_index = cloud.launch_index()
+ if launch_index is not None:
+ env['LAUNCH_INDEX'] = str(launch_index)
+ hostname = cloud.get_hostname()
+ if hostname:
+ env['HOSTNAME'] = str(hostname)
+ for k, v in [('AVAILABILITY_ZONE', cloud.datasource.availability_zone),
+ ('REGION', cloud.datasource.region),
+ ('LOCALE', cloud.get_locale())]:
+ if v:
+ env[k] = str(v)
+ return env
+
+
def handle(name, cfg, cloud, log, _args):
if "bootcmd" not in cfg:
@@ -43,12 +62,8 @@
raise
try:
- env = os.environ.copy()
- iid = cloud.get_instance_id()
- if iid:
- env['INSTANCE_ID'] = str(iid)
cmd = ['/bin/sh', tmpf.name]
- util.subp(cmd, env=env, capture=False)
+ util.subp(cmd, env=create_env(cloud), capture=False)
except Exception:
util.logexc(log, "Failed to run bootcmd module %s", name)
raise
=== modified file 'cloudinit/util.py'
--- cloudinit/util.py 2016-06-16 03:32:22 +0000
+++ cloudinit/util.py 2016-06-23 20:04:24 +0000
@@ -1746,16 +1746,14 @@
# for each entry in the list
# if it is an array, shell protect it (with single ticks)
# if it is a string, do nothing
-def shellify(cmdlist, add_header=True):
+def shellify(cmdlist, add_header=True, header="#!/bin/sh\n"):
content = ''
- if add_header:
- content += "#!/bin/sh\n"
escaped = "%s%s%s%s" % ("'", '\\', "'", "'")
cmds_made = 0
for args in cmdlist:
# If the item is a list, wrap all items in single tick.
# If its not, then just write it directly.
- if isinstance(args, list):
+ if isinstance(args, (list, tuple)):
fixed = []
for f in args:
fixed.append("'%s'" % (six.text_type(f).replace("'", escaped)))
@@ -1769,6 +1767,8 @@
" which is not a list or string")
% (type_utils.obj_name(args)))
LOG.debug("Shellified %s commands.", cmds_made)
+ if add_header and header and not content.startswith(header):
+ content = header + content
return content
Follow ups