summaryrefslogtreecommitdiff
path: root/cloudinit/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit/util.py')
-rw-r--r--cloudinit/util.py258
1 files changed, 234 insertions, 24 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py
index e6489648..9133426c 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -32,6 +32,7 @@ import re
import socket
import sys
import time
+import tempfile
import traceback
import urlparse
@@ -208,16 +209,18 @@ def runparts(dirp, skip_no_exist=True):
if skip_no_exist and not os.path.isdir(dirp):
return
- # per bug 857926, Fedora's run-parts will exit failure on empty dir
- if os.path.isdir(dirp) and os.listdir(dirp) == []:
- return
-
- cmd = ['run-parts', '--regex', '.*', dirp]
- sp = subprocess.Popen(cmd)
- sp.communicate()
- if sp.returncode is not 0:
- raise subprocess.CalledProcessError(sp.returncode, cmd)
- return
+ failed = 0
+ for exe_name in sorted(os.listdir(dirp)):
+ exe_path = os.path.join(dirp, exe_name)
+ if os.path.isfile(exe_path) and os.access(exe_path, os.X_OK):
+ popen = subprocess.Popen([exe_path])
+ popen.communicate()
+ if popen.returncode is not 0:
+ failed += 1
+ sys.stderr.write("failed: %s [%i]\n" %
+ (exe_path, popen.returncode))
+ if failed:
+ raise RuntimeError('runparts: %i failures' % failed)
def subp(args, input_=None):
@@ -515,30 +518,70 @@ def dos2unix(string):
return(string.replace('\r\n', '\n'))
-def islxc():
- # is this host running lxc?
+def is_container():
+ # is this code running in a container of some sort
+
+ for helper in ('running-in-container', 'lxc-is-container'):
+ try:
+ # try to run a helper program. if it returns true
+ # then we're inside a container. otherwise, no
+ sp = subprocess.Popen(helper, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ sp.communicate(None)
+ return(sp.returncode == 0)
+ except OSError as e:
+ if e.errno != errno.ENOENT:
+ raise
+
+ # this code is largely from the logic in
+ # ubuntu's /etc/init/container-detect.conf
try:
- with open("/proc/1/cgroup") as f:
- if f.read() == "/":
- return True
+ # Detect old-style libvirt
+ # Detect OpenVZ containers
+ pid1env = get_proc_env(1)
+ if "container" in pid1env:
+ return True
+
+ if "LIBVIRT_LXC_UUID" in pid1env:
+ return True
+
except IOError as e:
if e.errno != errno.ENOENT:
- raise
+ pass
+
+ # Detect OpenVZ containers
+ if os.path.isdir("/proc/vz") and not os.path.isdir("/proc/bc"):
+ return True
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)
- sp.communicate(None)
- return(sp.returncode == 0)
- except OSError as e:
+ # Detect Vserver containers
+ with open("/proc/self/status") as fp:
+ lines = fp.read().splitlines()
+ for line in lines:
+ if line.startswith("VxID:"):
+ (_key, val) = line.strip().split(":", 1)
+ if val != "0":
+ return True
+ except IOError as e:
if e.errno != errno.ENOENT:
- raise
+ pass
return False
+def get_proc_env(pid):
+ # return the environment in a dict that a given process id was started with
+ env = {}
+ with open("/proc/%s/environ" % pid) as fp:
+ toks = fp.read().split("\0")
+ for tok in toks:
+ if tok == "":
+ continue
+ (name, val) = tok.split("=", 1)
+ env[name] = val
+ return env
+
+
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
@@ -630,3 +673,170 @@ def close_stdin():
return
with open(os.devnull) as fp:
os.dup2(fp.fileno(), sys.stdin.fileno())
+
+
+def find_devs_with(criteria):
+ """
+ find devices matching given criteria (via blkid)
+ criteria can be *one* of:
+ TYPE=<filesystem>
+ LABEL=<label>
+ UUID=<uuid>
+ """
+ try:
+ (out, _err) = subp(['blkid', '-t%s' % criteria, '-odevice'])
+ except subprocess.CalledProcessError:
+ return([])
+ return(str(out).splitlines())
+
+
+class mountFailedError(Exception):
+ pass
+
+
+def mount_callback_umount(device, callback, data=None):
+ """
+ mount the device, call method 'callback' passing the directory
+ in which it was mounted, then unmount. Return whatever 'callback'
+ returned. If data != None, also pass data to callback.
+ """
+
+ def _cleanup(umount, tmpd):
+ if umount:
+ try:
+ subp(["umount", '-l', umount])
+ except subprocess.CalledProcessError:
+ raise
+ if tmpd:
+ os.rmdir(tmpd)
+
+ # go through mounts to see if it was already mounted
+ fp = open("/proc/mounts")
+ mounts = fp.readlines()
+ fp.close()
+
+ tmpd = None
+
+ mounted = {}
+ for mpline in mounts:
+ (dev, mp, fstype, _opts, _freq, _passno) = mpline.split()
+ mp = mp.replace("\\040", " ")
+ mounted[dev] = (dev, fstype, mp, False)
+
+ umount = False
+ if device in mounted:
+ mountpoint = "%s/" % mounted[device][2]
+ else:
+ tmpd = tempfile.mkdtemp()
+
+ mountcmd = ["mount", "-o", "ro", device, tmpd]
+
+ try:
+ (_out, _err) = subp(mountcmd)
+ umount = tmpd
+ except subprocess.CalledProcessError as exc:
+ _cleanup(umount, tmpd)
+ raise mountFailedError(exc.output[1])
+
+ mountpoint = "%s/" % tmpd
+
+ try:
+ if data == None:
+ ret = callback(mountpoint)
+ else:
+ ret = callback(mountpoint, data)
+
+ except Exception as exc:
+ _cleanup(umount, tmpd)
+ raise exc
+
+ _cleanup(umount, tmpd)
+
+ return(ret)
+
+
+def wait_for_url(urls, max_wait=None, timeout=None,
+ status_cb=None, headers_cb=None):
+ """
+ urls: a list of urls to try
+ max_wait: roughly the maximum time to wait before giving up
+ The max time is *actually* len(urls)*timeout as each url will
+ be tried once and given the timeout provided.
+ timeout: the timeout provided to urllib2.urlopen
+ status_cb: call method with string message when a url is not available
+ headers_cb: call method with single argument of url to get headers
+ for request.
+
+ the idea of this routine is to wait for the EC2 metdata service to
+ come up. On both Eucalyptus and EC2 we have seen the case where
+ the instance hit the MD before the MD service was up. EC2 seems
+ to have permenantely fixed this, though.
+
+ In openstack, the metadata service might be painfully slow, and
+ unable to avoid hitting a timeout of even up to 10 seconds or more
+ (LP: #894279) for a simple GET.
+
+ Offset those needs with the need to not hang forever (and block boot)
+ on a system where cloud-init is configured to look for EC2 Metadata
+ service but is not going to find one. It is possible that the instance
+ data host (169.254.169.254) may be firewalled off Entirely for a sytem,
+ meaning that the connection will block forever unless a timeout is set.
+ """
+ starttime = time.time()
+
+ sleeptime = 1
+
+ def nullstatus_cb(msg):
+ return
+
+ if status_cb == None:
+ status_cb = nullstatus_cb
+
+ def timeup(max_wait, starttime):
+ return((max_wait <= 0 or max_wait == None) or
+ (time.time() - starttime > max_wait))
+
+ loop_n = 0
+ while True:
+ sleeptime = int(loop_n / 5) + 1
+ for url in urls:
+ now = time.time()
+ if loop_n != 0:
+ if timeup(max_wait, starttime):
+ break
+ if timeout and (now + timeout > (starttime + max_wait)):
+ # shorten timeout to not run way over max_time
+ timeout = int((starttime + max_wait) - now)
+
+ reason = ""
+ try:
+ if headers_cb != None:
+ headers = headers_cb(url)
+ else:
+ headers = {}
+
+ req = urllib2.Request(url, data=None, headers=headers)
+ resp = urllib2.urlopen(req, timeout=timeout)
+ if resp.read() != "":
+ return url
+ reason = "empty data [%s]" % resp.getcode()
+ except urllib2.HTTPError as e:
+ reason = "http error [%s]" % e.code
+ except urllib2.URLError as e:
+ reason = "url error [%s]" % e.reason
+ except socket.timeout as e:
+ reason = "socket timeout [%s]" % e
+ except Exception as e:
+ reason = "unexpected error [%s]" % e
+
+ status_cb("'%s' failed [%s/%ss]: %s" %
+ (url, int(time.time() - starttime), max_wait,
+ reason))
+
+ if timeup(max_wait, starttime):
+ break
+
+ loop_n = loop_n + 1
+ time.sleep(sleeptime)
+
+ return False