def set_hostname(hostname, log): util.subp(['hostname', hostname]) platform = util.determine_platform() log.info("Setting hostname on platform: %s", platform) old_name = None if platform == util.RH_PLATFORM: old_name = set_hostname_rh(hostname, log) else: old_name = set_hostname_deb(hostname, log) return old_name
def handle(_name, cfg, cloud, log, _args): if "bootcmd" not in cfg: return try: content = util.shellify(cfg["bootcmd"]) except: log.warn("Failed to shellify bootcmd") raise try: env = os.environ.copy() env["INSTANCE_ID"] = cloud.get_instance_id() util.subp(["/bin/sh"], content, env=env) except: log.warn("Failed to run commands from bootcmd") raise
def route_info(): (route_out, err) = util.subp(["route", "-n"]) routes = [] for line in route_out.splitlines()[1:]: if not line: continue toks = line.split() if toks[0] == "Kernel" or toks[0] == "Destination": continue routes.append(toks) return routes
def netdev_info(empty=""): fields = ("hwaddr", "addr", "bcast", "mask") (ifcfg_out, err) = util.subp(["ifconfig", "-a"]) devs = {} for line in ifcfg_out.splitlines(): if len(line) == 0: continue if line[0] not in ("\t", " "): curdev = line.split()[0] devs[curdev] = {"up": False} for field in fields: devs[curdev][field] = "" toks = line.lower().strip().split() if toks[0] == "up": devs[curdev]['up'] = True fieldpost = "" if toks[0] == "inet6": fieldpost = "6" for i in range(len(toks)): if toks[i] == "hwaddr": try: devs[curdev]["hwaddr"] = toks[i + 1] except IndexError: pass for field in ("addr", "bcast", "mask"): target = "%s%s" % (field, fieldpost) if devs[curdev].get(target, ""): continue if toks[i] == "%s:" % field: try: devs[curdev][target] = toks[i + 1] except IndexError: pass elif toks[i].startswith("%s:" % field): devs[curdev][target] = toks[i][len(field) + 1:] if empty != "": for (_devname, dev) in devs.iteritems(): for field in dev: if dev[field] == "": dev[field] = empty return devs
def handle(_name, cfg, _cloud, _log, _args): if util.get_cfg_option_bool(cfg, "disable_ec2_metadata", False): util.subp(["route", "add", "-host", "169.254.169.254", "reject"])
def handle(_name, cfg, cloud, log, _args): # fs_spec, fs_file, fs_vfstype, fs_mntops, fs-freq, fs_passno defvals = [None, None, "auto", "defaults,nobootwait", "0", "2"] defvals = cfg.get("mount_default_fields", defvals) # these are our default set of mounts defmnts = [["ephemeral0", "/mnt", "auto", defvals[3], "0", "2"], ["swap", "none", "swap", "sw", "0", "0"]] cfgmnt = [] if "mounts" in cfg: cfgmnt = cfg["mounts"] # shortname matches 'sda', 'sda1', 'xvda', 'hda', 'sdb', xvdb, vda, vdd1 shortname_filter = r"^[x]{0,1}[shv]d[a-z][0-9]*$" shortname = re.compile(shortname_filter) for i in range(len(cfgmnt)): # skip something that wasn't a list if not isinstance(cfgmnt[i], list): continue # workaround, allow user to specify 'ephemeral' # rather than more ec2 correct 'ephemeral0' if cfgmnt[i][0] == "ephemeral": cfgmnt[i][0] = "ephemeral0" if is_mdname(cfgmnt[i][0]): newname = cloud.device_name_to_device(cfgmnt[i][0]) if not newname: log.debug("Iignoring nonexistant named mount %s", cfgmnt[i][0]) cfgmnt[i][1] = None else: if newname.startswith("/"): cfgmnt[i][0] = newname else: cfgmnt[i][0] = "/dev/%s" % newname else: if shortname.match(cfgmnt[i][0]): cfgmnt[i][0] = "/dev/%s" % cfgmnt[i][0] # in case the user did not quote a field (likely fs-freq, fs_passno) # but do not convert None to 'None' (LP: #898365) for j in range(len(cfgmnt[i])): if isinstance(cfgmnt[i][j], int): cfgmnt[i][j] = str(cfgmnt[i][j]) for i in range(len(cfgmnt)): # fill in values with defaults from defvals above for j in range(len(defvals)): if len(cfgmnt[i]) <= j: cfgmnt[i].append(defvals[j]) elif cfgmnt[i][j] is None: cfgmnt[i][j] = defvals[j] # if the second entry in the list is 'None' this # clears all previous entries of that same 'fs_spec' # (fs_spec is the first field in /etc/fstab, ie, that device) if cfgmnt[i][1] is None: for j in range(i): if cfgmnt[j][0] == cfgmnt[i][0]: cfgmnt[j][1] = None # for each of the "default" mounts, add them only if no other # entry has the same device name for defmnt in defmnts: devname = cloud.device_name_to_device(defmnt[0]) if devname is None: continue if devname.startswith("/"): defmnt[0] = devname else: defmnt[0] = "/dev/%s" % devname cfgmnt_has = False for cfgm in cfgmnt: if cfgm[0] == defmnt[0]: cfgmnt_has = True break if cfgmnt_has: continue cfgmnt.append(defmnt) # now, each entry in the cfgmnt list has all fstab values # if the second field is None (not the string, the value) we skip it actlist = [x for x in cfgmnt if x[1] is not None] if len(actlist) == 0: return comment = "comment=cloudconfig" cc_lines = [] needswap = False dirs = [] for line in actlist: # write 'comment' in the fs_mntops, entry, claiming this line[3] = "%s,comment=cloudconfig" % line[3] if line[2] == "swap": needswap = True if line[1].startswith("/"): dirs.append(line[1]) cc_lines.append('\t'.join(line)) fstab_lines = [] fstab = open("/etc/fstab", "r+") ws = re.compile("[%s]+" % whitespace) for line in fstab.read().splitlines(): try: toks = ws.split(line) if toks[3].find(comment) != -1: continue except: pass fstab_lines.append(line) fstab_lines.extend(cc_lines) fstab.seek(0) fstab.write("%s\n" % '\n'.join(fstab_lines)) fstab.truncate() fstab.close() if needswap: try: util.subp(["swapon", "-a"]) except: log.warn("Failed to enable swap") for d in dirs: if os.path.exists(d): continue try: os.makedirs(d) except: log.warn("Failed to make '%s' config-mount", d) try: util.subp(["mount", "-a"]) except: log.warn("'mount -a' failed")