def handle(_name, _cfg, cloud, log, _args): try: ud = cloud.get_userdata_raw() except: log.warn("failed to get raw userdata in %s" % my_name) return try: mdict = parse_qs(ud) if not my_hookname in mdict: return except: log.warn("failed to urlparse.parse_qa(userdata_raw())") raise scripts_d = get_ipath_cur('scripts') i = 0 first_e = None for url in mdict[my_hookname]: fname = "%s/rightscale-%02i" % (scripts_d, i) i = i + 1 try: content = util.readurl(url) util.write_file(fname, content, mode=0700) except Exception as e: if not first_e: first_e = None log.warn("%s failed to read %s: %s" % (my_name, url, e)) if first_e: raise (e)
def handle(_name, _cfg, cloud, log, _args): try: ud = cloud.get_userdata_raw() except: log.warn("failed to get raw userdata in %s" % my_name) return try: mdict = parse_qs(ud) if not my_hookname in mdict: return except: log.warn("failed to urlparse.parse_qa(userdata_raw())") raise scripts_d = get_ipath_cur('scripts') i = 0 first_e = None for url in mdict[my_hookname]: fname = "%s/rightscale-%02i" % (scripts_d, i) i = i + 1 try: content = util.readurl(url) util.write_file(fname, content, mode=0700) except Exception as e: if not first_e: first_e = None log.warn("%s failed to read %s: %s" % (my_name, url, e)) if first_e: raise(e)
def test_shellscript(self): """Raw text starting #!/bin/sh is treated as script""" script = "#!/bin/sh\necho hello\n" outpath = cloudinit.get_ipath_cur("scripts") + "/part-001" self.mock_write(outpath, script, 0700) self.mocker.replay() ci = cloudinit.CloudInit() ci.datasource = FakeDataSource(script) ci.consume_userdata() self.assertEqual("", self.log_file.getvalue())
def run_per_instance(name, func, args, clear_on_fail=False): semfile = "%s/%s" % (cloudinit.get_ipath_cur("data"), name) if os.path.exists(semfile): return util.write_file(semfile, str(time.time())) try: func(*args) except: if clear_on_fail: os.unlink(semfile) raise
def test_mime_text_plain_shell(self): """Mime type text/plain starting #!/bin/sh is treated as script""" script = "#!/bin/sh\necho hello\n" outpath = cloudinit.get_ipath_cur("scripts") + "/part-001" self.mock_write(outpath, script, 0700) self.mocker.replay() ci = cloudinit.CloudInit() message = MIMEBase("text", "plain") message.set_payload(script) ci.datasource = FakeDataSource(message.as_string()) ci.consume_userdata() self.assertEqual("", self.log_file.getvalue())
def do_include(content, appendmsg): import os # is just a list of urls, one per line # also support '#include <url here>' includeonce = False for line in content.splitlines(): if line == "#include": continue if line == "#include-once": includeonce = True continue if line.startswith("#include-once"): line = line[len("#include-once"):].lstrip() includeonce = True elif line.startswith("#include"): line = line[len("#include"):].lstrip() if line.startswith("#"): continue if line.strip() == "": continue # urls cannot not have leading or trailing white space msum = hashlib.md5() # pylint: disable=E1101 msum.update(line.strip()) includeonce_filename = "%s/urlcache/%s" % ( cloudinit.get_ipath_cur("data"), msum.hexdigest()) try: if includeonce and os.path.isfile(includeonce_filename): with open(includeonce_filename, "r") as fp: content = fp.read() else: content = urllib.urlopen(line).read() if includeonce: util.write_file(includeonce_filename, content, mode=0600) except Exception: raise process_includes(message_from_string(decomp_str(content)), appendmsg)
def main(): # expect to be called with name of item to fetch if len(sys.argv) != 2: Usage(sys.stderr) sys.exit(1) cfg_path = cloudinit.get_ipath_cur("cloud_config") cc = cloudinit.CloudConfig.CloudConfig(cfg_path) data = { 'user_data': cc.cloud.get_userdata(), 'user_data_raw': cc.cloud.get_userdata_raw(), 'instance_id': cc.cloud.get_instance_id(), } name = sys.argv[1].replace('-', '_') if name not in data: sys.stderr.write("unknown name '%s'. Known values are:\n %s\n" % (sys.argv[1], ' '.join(data.keys()))) sys.exit(1) print data[name] sys.exit(0)
def main(): # expect to be called with # name [ freq [ args ] # run the cloud-config job 'name' at with given args # or # read cloud config jobs from config (builtin -> system) # and run all in order util.close_stdin() modename = "config" if len(sys.argv) < 2: Usage(sys.stderr) sys.exit(1) if sys.argv[1] == "all": name = "all" if len(sys.argv) > 2: modename = sys.argv[2] else: freq = None run_args = [] name = sys.argv[1] if len(sys.argv) > 2: freq = sys.argv[2] if freq == "None": freq = None if len(sys.argv) > 3: run_args = sys.argv[3:] cfg_path = cloudinit.get_ipath_cur("cloud_config") cfg_env_name = cloudinit.cfg_env_name if cfg_env_name in os.environ: cfg_path = os.environ[cfg_env_name] cloud = cloudinit.CloudInit(ds_deps=[]) # ds_deps=[], get only cached try: cloud.get_data_source() except cloudinit.DataSourceNotFoundException as e: # there was no datasource found, theres nothing to do sys.exit(0) cc = CC.CloudConfig(cfg_path, cloud) try: (outfmt, errfmt) = CC.get_output_cfg(cc.cfg, modename) CC.redirect_output(outfmt, errfmt) except Exception as e: err("Failed to get and set output config: %s\n" % e) cloudinit.logging_set_from_cfg(cc.cfg) log = logging.getLogger() log.info("cloud-init-cfg %s" % sys.argv[1:]) module_list = [] if name == "all": modlist_cfg_name = "cloud_%s_modules" % modename module_list = CC.read_cc_modules(cc.cfg, modlist_cfg_name) if not len(module_list): err("no modules to run in cloud_config [%s]" % modename, log) sys.exit(0) else: module_list.append([name, freq] + run_args) failures = CC.run_cc_modules(cc, module_list, log) if len(failures): err("errors running cloud_config [%s]: %s" % (modename, failures), log) sys.exit(len(failures))
# # Author: Scott Moser <*****@*****.**> # Author: Juerg Haefliger <*****@*****.**> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3, as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import cloudinit.util as util from cloudinit.CloudConfig import per_instance from cloudinit import get_ipath_cur frequency = per_instance runparts_path = "%s/%s" % (get_ipath_cur(), "scripts") def handle(_name, _cfg, _cloud, log, _args): try: util.runparts(runparts_path) except: log.warn("failed to run-parts in %s" % runparts_path) raise
def main(): util.close_stdin() cmds = ("start", "start-local") deps = {"start": (ds.DEP_FILESYSTEM, ds.DEP_NETWORK), "start-local": (ds.DEP_FILESYSTEM, )} cmd = "" if len(sys.argv) > 1: cmd = sys.argv[1] cfg_path = None if len(sys.argv) > 2: # this is really for debugging only # but you can invoke on development system with ./config/cloud.cfg cfg_path = sys.argv[2] if not cmd in cmds: sys.stderr.write("bad command %s. use one of %s\n" % (cmd, cmds)) sys.exit(1) now = time.strftime("%a, %d %b %Y %H:%M:%S %z", time.gmtime()) try: uptimef = open("/proc/uptime") uptime = uptimef.read().split(" ")[0] uptimef.close() except IOError as e: warn("unable to open /proc/uptime\n") uptime = "na" cmdline_msg = None cmdline_exc = None if cmd == "start": target = "%s.d/%s" % (cloudinit.system_config, "91_kernel_cmdline_url.cfg") if os.path.exists(target): cmdline_msg = "cmdline: %s existed" % target else: cmdline = util.get_cmdline() try: (key, url, content) = cloudinit.get_cmdline_url( cmdline=cmdline) if key and content: util.write_file(target, content, mode=0600) cmdline_msg = ("cmdline: wrote %s from %s, %s" % (target, key, url)) elif key: cmdline_msg = ("cmdline: %s, %s had no cloud-config" % (key, url)) except Exception: cmdline_exc = ("cmdline: '%s' raised exception\n%s" % (cmdline, traceback.format_exc())) warn(cmdline_exc) try: cfg = cloudinit.get_base_cfg(cfg_path) except Exception as e: warn("Failed to get base config. falling back to builtin: %s\n" % e) try: cfg = cloudinit.get_builtin_cfg() except Exception as e: warn("Unable to load builtin config\n") raise try: (outfmt, errfmt) = CC.get_output_cfg(cfg, "init") CC.redirect_output(outfmt, errfmt) except Exception as e: warn("Failed to get and set output config: %s\n" % e) cloudinit.logging_set_from_cfg(cfg) log = logging.getLogger() if cmdline_exc: log.debug(cmdline_exc) elif cmdline_msg: log.debug(cmdline_msg) try: cloudinit.initfs() except Exception as e: warn("failed to initfs, likely bad things to come: %s\n" % str(e)) nonet_path = "%s/%s" % (cloudinit.get_cpath("data"), "no-net") if cmd == "start": print netinfo.debug_info() stop_files = (cloudinit.get_ipath_cur("obj_pkl"), nonet_path) # if starting as the network start, there are cases # where everything is already done for us, and it makes # most sense to exit early and silently for f in stop_files: try: fp = open(f, "r") fp.close() except: continue log.debug("no need for cloud-init start to run (%s)\n", f) sys.exit(0) elif cmd == "start-local": # cache is not instance specific, so it has to be purged # but we want 'start' to benefit from a cache if # a previous start-local populated one manclean = util.get_cfg_option_bool(cfg, 'manual_cache_clean', False) if manclean: log.debug("not purging cache, manual_cache_clean = True") cloudinit.purge_cache(not manclean) try: os.unlink(nonet_path) except OSError as e: if e.errno != errno.ENOENT: raise msg = "cloud-init %s running: %s. up %s seconds" % (cmd, now, uptime) sys.stderr.write(msg + "\n") sys.stderr.flush() log.info(msg) cloud = cloudinit.CloudInit(ds_deps=deps[cmd]) try: cloud.get_data_source() except cloudinit.DataSourceNotFoundException as e: sys.stderr.write("no instance data found in %s\n" % cmd) sys.exit(0) # set this as the current instance cloud.set_cur_instance() # store the metadata cloud.update_cache() msg = "found data source: %s" % cloud.datasource sys.stderr.write(msg + "\n") log.debug(msg) # parse the user data (ec2-run-userdata.py) try: ran = cloud.sem_and_run("consume_userdata", cloudinit.per_instance, cloud.consume_userdata, [cloudinit.per_instance], False) if not ran: cloud.consume_userdata(cloudinit.per_always) except: warn("consuming user data failed!\n") raise cfg_path = cloudinit.get_ipath_cur("cloud_config") cc = CC.CloudConfig(cfg_path, cloud) # if the output config changed, update output and err try: outfmt_orig = outfmt errfmt_orig = errfmt (outfmt, errfmt) = CC.get_output_cfg(cc.cfg, "init") if outfmt_orig != outfmt or errfmt_orig != errfmt: warn("stdout, stderr changing to (%s,%s)" % (outfmt, errfmt)) CC.redirect_output(outfmt, errfmt) except Exception as e: warn("Failed to get and set output config: %s\n" % e) # send the cloud-config ready event cc_path = cloudinit.get_ipath_cur('cloud_config') cc_ready = cc.cfg.get("cc_ready_cmd", ['initctl', 'emit', 'cloud-config', '%s=%s' % (cloudinit.cfg_env_name, cc_path)]) if cc_ready: if isinstance(cc_ready, str): cc_ready = ['sh', '-c', cc_ready] subprocess.Popen(cc_ready).communicate() module_list = CC.read_cc_modules(cc.cfg, "cloud_init_modules") failures = [] if len(module_list): failures = CC.run_cc_modules(cc, module_list, log) else: msg = "no cloud_init_modules to run" sys.stderr.write(msg + "\n") log.debug(msg) sys.exit(0) sys.exit(len(failures))
import cloudinit import sys import os import stat import subprocess import datetime path = None try: path = cloudinit.get_cpath("data") except AttributeError: # pre 0.6.0 - user data executed via cloudinit, not this helper with open("/var/log/heat-provision.log", "w") as log: log.write("Unable to log provisioning, need a newer version of" " cloud-init\n") sys.exit(0) os.chmod(path + "/cfn-userdata", stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) with open("/var/log/heat-provision.log", "w") as log: log.write("Provision began: %s\n" % datetime.datetime.now()) log.flush() p = subprocess.Popen(path + "/cfn-userdata", stdout=log, stderr=log) p.wait() log.write("Provision done: %s\n" % datetime.datetime.now()) if p.returncode: sys.exit(p.returncode) with open(cloudinit.get_ipath_cur() + "/provision-finished", "w") as log: log.write("%s\n" % datetime.datetime.now())
def main(): util.close_stdin() cmds = ("start", "start-local") deps = { "start": (ds.DEP_FILESYSTEM, ds.DEP_NETWORK), "start-local": (ds.DEP_FILESYSTEM, ) } cmd = "" if len(sys.argv) > 1: cmd = sys.argv[1] cfg_path = None if len(sys.argv) > 2: # this is really for debugging only # but you can invoke on development system with ./config/cloud.cfg cfg_path = sys.argv[2] if not cmd in cmds: sys.stderr.write("bad command %s. use one of %s\n" % (cmd, cmds)) sys.exit(1) now = time.strftime("%a, %d %b %Y %H:%M:%S %z", time.gmtime()) try: uptimef = open("/proc/uptime") uptime = uptimef.read().split(" ")[0] uptimef.close() except IOError as e: warn("unable to open /proc/uptime\n") uptime = "na" cmdline_msg = None cmdline_exc = None if cmd == "start": target = "%s.d/%s" % (cloudinit.system_config, "91_kernel_cmdline_url.cfg") if os.path.exists(target): cmdline_msg = "cmdline: %s existed" % target else: cmdline = util.get_cmdline() try: (key, url, content) = cloudinit.get_cmdline_url(cmdline=cmdline) if key and content: util.write_file(target, content, mode=0600) cmdline_msg = ("cmdline: wrote %s from %s, %s" % (target, key, url)) elif key: cmdline_msg = ("cmdline: %s, %s had no cloud-config" % (key, url)) except Exception: cmdline_exc = ("cmdline: '%s' raised exception\n%s" % (cmdline, traceback.format_exc())) warn(cmdline_exc) try: cfg = cloudinit.get_base_cfg(cfg_path) except Exception as e: warn("Failed to get base config. falling back to builtin: %s\n" % e) try: cfg = cloudinit.get_builtin_cfg() except Exception as e: warn("Unable to load builtin config\n") raise try: (outfmt, errfmt) = CC.get_output_cfg(cfg, "init") CC.redirect_output(outfmt, errfmt) except Exception as e: warn("Failed to get and set output config: %s\n" % e) cloudinit.logging_set_from_cfg(cfg) log = logging.getLogger() if cmdline_exc: log.debug(cmdline_exc) elif cmdline_msg: log.debug(cmdline_msg) try: cloudinit.initfs() except Exception as e: warn("failed to initfs, likely bad things to come: %s\n" % str(e)) nonet_path = "%s/%s" % (cloudinit.get_cpath("data"), "no-net") if cmd == "start": print netinfo.debug_info() stop_files = (cloudinit.get_ipath_cur("obj_pkl"), nonet_path) # if starting as the network start, there are cases # where everything is already done for us, and it makes # most sense to exit early and silently for f in stop_files: try: fp = open(f, "r") fp.close() except: continue log.debug("no need for cloud-init start to run (%s)\n", f) sys.exit(0) elif cmd == "start-local": # cache is not instance specific, so it has to be purged # but we want 'start' to benefit from a cache if # a previous start-local populated one manclean = util.get_cfg_option_bool(cfg, 'manual_cache_clean', False) if manclean: log.debug("not purging cache, manual_cache_clean = True") cloudinit.purge_cache(not manclean) try: os.unlink(nonet_path) except OSError as e: if e.errno != errno.ENOENT: raise msg = "cloud-init %s running: %s. up %s seconds" % (cmd, now, uptime) sys.stderr.write(msg + "\n") sys.stderr.flush() log.info(msg) cloud = cloudinit.CloudInit(ds_deps=deps[cmd]) try: cloud.get_data_source() except cloudinit.DataSourceNotFoundException as e: sys.stderr.write("no instance data found in %s\n" % cmd) sys.exit(0) # set this as the current instance cloud.set_cur_instance() # store the metadata cloud.update_cache() msg = "found data source: %s" % cloud.datasource sys.stderr.write(msg + "\n") log.debug(msg) # parse the user data (ec2-run-userdata.py) try: ran = cloud.sem_and_run("consume_userdata", cloudinit.per_instance, cloud.consume_userdata, [cloudinit.per_instance], False) if not ran: cloud.consume_userdata(cloudinit.per_always) except: warn("consuming user data failed!\n") raise cfg_path = cloudinit.get_ipath_cur("cloud_config") cc = CC.CloudConfig(cfg_path, cloud) # if the output config changed, update output and err try: outfmt_orig = outfmt errfmt_orig = errfmt (outfmt, errfmt) = CC.get_output_cfg(cc.cfg, "init") if outfmt_orig != outfmt or errfmt_orig != errfmt: warn("stdout, stderr changing to (%s,%s)" % (outfmt, errfmt)) CC.redirect_output(outfmt, errfmt) except Exception as e: warn("Failed to get and set output config: %s\n" % e) # send the cloud-config ready event cc_path = cloudinit.get_ipath_cur('cloud_config') cc_ready = cc.cfg.get("cc_ready_cmd", [ 'initctl', 'emit', 'cloud-config', '%s=%s' % (cloudinit.cfg_env_name, cc_path) ]) if cc_ready: if isinstance(cc_ready, str): cc_ready = ['sh', '-c', cc_ready] subprocess.Popen(cc_ready).communicate() module_list = CC.read_cc_modules(cc.cfg, "cloud_init_modules") failures = [] if len(module_list): failures = CC.run_cc_modules(cc, module_list, log) else: msg = "no cloud_init_modules to run" sys.stderr.write(msg + "\n") log.debug(msg) sys.exit(0) sys.exit(len(failures))