diff options
Diffstat (limited to 'cloudinit/util.py')
-rw-r--r-- | cloudinit/util.py | 66 |
1 files changed, 60 insertions, 6 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py index fc4233de..bdc1fce2 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -141,10 +141,8 @@ def runparts(dirp, skip_no_exist=True): return def subp(args, input=None): - s_in = None - if input is not None: - s_in = subprocess.PIPE - sp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=s_in) + sp = subprocess.Popen(args, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, stdin=subprocess.PIPE) out,err = sp.communicate(input) if sp.returncode is not 0: raise subprocess.CalledProcessError(sp.returncode,args, (out,err)) @@ -189,8 +187,12 @@ def read_seeded(base="", ext="", timeout=2): md_url = "%s%s%s" % (base, "meta-data", ext) try: - md_resp = urllib2.urlopen(urllib2.Request(md_url), timeout=timeout) - ud_resp = urllib2.urlopen(urllib2.Request(ud_url), timeout=timeout) + if timeout == None: + md_resp = urllib2.urlopen(urllib2.Request(md_url)) + ud_resp = urllib2.urlopen(urllib2.Request(ud_url)) + else: + md_resp = urllib2.urlopen(urllib2.Request(md_url), timeout=timeout) + ud_resp = urllib2.urlopen(urllib2.Request(ud_url), timeout=timeout) md_str = md_resp.read() ud = ud_resp.read() @@ -389,3 +391,55 @@ def shellify(cmdlist): else: content="%s%s\n" % ( content, str(args) ) return content + +def dos2unix(input): + # find first end of line + pos = input.find('\n') + if pos <= 0 or input[pos-1] != '\r': return(input) + return(input.replace('\r\n','\n')) + +def islxc(): + # is this host running lxc? + try: + with open("/proc/1/cgroup") as f: + if f.read() == "/": + return True + except IOError as e: + if e.errno != errno.ENOENT: + raise + + try: + # try to run a program named 'lxc-is-container'. if it returns true, then + # we're inside a container. otherwise, no + sp = subprocess.Popen(['lxc-is-container'], stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out,err = sp.communicate(None) + return(sp.returncode == 0) + except OSError as e: + if e.errno != errno.ENOENT: + raise + + return False + +def get_hostname_fqdn(cfg, cloud): + # return the hostname and fqdn from 'cfg'. If not found in cfg, + # then fall back to data from cloud + if "fqdn" in cfg: + # user specified a fqdn. Default hostname then is based off that + fqdn = cfg['fqdn'] + hostname = get_cfg_option_str(cfg,"hostname",fqdn.split('.')[0]) + else: + if "hostname" in cfg and cfg['hostname'].find('.') > 0: + # user specified hostname, and it had '.' in it + # be nice to them. set fqdn and hostname from that + fqdn = cfg['hostname'] + hostname = cfg['hostname'][:fqdn.find('.')] + else: + # no fqdn set, get fqdn from cloud. + # get hostname from cfg if available otherwise cloud + fqdn = cloud.get_hostname(fqdn=True) + if "hostname" in cfg: + hostname = cfg['hostname'] + else: + hostname = cloud.get_hostname() + return(hostname, fqdn) |