summaryrefslogtreecommitdiff
path: root/cloudinit/util.py
diff options
context:
space:
mode:
authorScott Moser <smoser@brickies.net>2016-08-23 16:48:42 -0400
committerScott Moser <smoser@brickies.net>2016-08-23 16:48:42 -0400
commit86e2614b6c3db342aa5a6590e91b9e459bbcb484 (patch)
tree6996805b91a0c1c31f3afea3a689348bf760de63 /cloudinit/util.py
parentc937c66dd0d1ad7b73dcc2efb5eb4c16b05f4479 (diff)
parent9f7ce5f090689b664ffce7e0b4ac78bfeafd1a79 (diff)
downloadvyos-cloud-init-86e2614b6c3db342aa5a6590e91b9e459bbcb484.tar.gz
vyos-cloud-init-86e2614b6c3db342aa5a6590e91b9e459bbcb484.zip
merge trunk at 0.7.7~bzr1245
Diffstat (limited to 'cloudinit/util.py')
-rw-r--r--cloudinit/util.py46
1 files changed, 33 insertions, 13 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py
index 0d21e11b..e5dd61a0 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -171,7 +171,8 @@ class ProcessExecutionError(IOError):
def __init__(self, stdout=None, stderr=None,
exit_code=None, cmd=None,
- description=None, reason=None):
+ description=None, reason=None,
+ errno=None):
if not cmd:
self.cmd = '-'
else:
@@ -202,6 +203,7 @@ class ProcessExecutionError(IOError):
else:
self.reason = '-'
+ self.errno = errno
message = self.MESSAGE_TMPL % {
'description': self.description,
'cmd': self.cmd,
@@ -288,7 +290,7 @@ def fork_cb(child_cb, *args, **kwargs):
try:
child_cb(*args, **kwargs)
os._exit(0)
- except:
+ except Exception:
logexc(LOG, "Failed forking and calling callback %s",
type_utils.obj_name(child_cb))
os._exit(1)
@@ -336,6 +338,16 @@ def rand_str(strlen=32, select_from=None):
return "".join([random.choice(select_from) for _x in range(0, strlen)])
+def rand_dict_key(dictionary, postfix=None):
+ if not postfix:
+ postfix = ""
+ while True:
+ newkey = rand_str(strlen=8) + "_" + postfix
+ if newkey not in dictionary:
+ break
+ return newkey
+
+
def read_conf(fname):
try:
return load_yaml(load_file(fname), default={})
@@ -472,7 +484,7 @@ def is_ipv4(instr):
try:
toks = [x for x in toks if int(x) < 256 and int(x) >= 0]
- except:
+ except Exception:
return False
return len(toks) == 4
@@ -1147,7 +1159,14 @@ def find_devs_with(criteria=None, oformat='device',
options.append(path)
cmd = blk_id_cmd + options
# See man blkid for why 2 is added
- (out, _err) = subp(cmd, rcs=[0, 2])
+ try:
+ (out, _err) = subp(cmd, rcs=[0, 2])
+ except ProcessExecutionError as e:
+ if e.errno == errno.ENOENT:
+ # blkid not found...
+ out = ""
+ else:
+ raise
entries = []
for line in out.splitlines():
line = line.strip()
@@ -1210,7 +1229,7 @@ def get_cmdline():
else:
try:
cmdline = load_file("/proc/cmdline").strip()
- except:
+ except Exception:
cmdline = ""
PROC_CMDLINE = cmdline
@@ -1380,7 +1399,7 @@ def read_write_cmdline_url(target_fn):
if not os.path.exists(target_fn):
try:
(key, url, content) = get_cmdline_url()
- except:
+ except Exception:
logexc(LOG, "Failed fetching command line url")
return
try:
@@ -1391,7 +1410,7 @@ def read_write_cmdline_url(target_fn):
elif key and not content:
LOG.debug(("Command line key %s with url"
" %s had no contents"), key, url)
- except:
+ except Exception:
logexc(LOG, "Failed writing url content to %s", target_fn)
@@ -1449,7 +1468,7 @@ def mounts():
mp = m.group(2)
fstype = m.group(3)
opts = m.group(4)
- except:
+ except Exception:
continue
# If the name of the mount point contains spaces these
# can be escaped as '\040', so undo that..
@@ -1575,7 +1594,7 @@ def copy(src, dest):
def time_rfc2822():
try:
ts = time.strftime("%a, %d %b %Y %H:%M:%S %z", time.gmtime())
- except:
+ except Exception:
ts = "??"
return ts
@@ -1601,7 +1620,7 @@ def uptime():
bootup = buf.value
uptime_str = now - bootup
- except:
+ except Exception:
logexc(LOG, "Unable to read uptime using method: %s" % method)
return uptime_str
@@ -1696,7 +1715,8 @@ def subp(args, data=None, rcs=None, env=None, capture=True, shell=False,
sp = subprocess.Popen(args, **kws)
(out, err) = sp.communicate(data)
except OSError as e:
- raise ProcessExecutionError(cmd=args, reason=e)
+ raise ProcessExecutionError(cmd=args, reason=e,
+ errno=e.errno)
rc = sp.returncode
if rc not in rcs:
raise ProcessExecutionError(stdout=out, stderr=err,
@@ -2055,7 +2075,7 @@ def log_time(logfunc, msg, func, args=None, kwargs=None, get_uptime=False):
tmsg += " (N/A)"
try:
logfunc(msg + tmsg)
- except:
+ except Exception:
pass
return ret
@@ -2190,7 +2210,7 @@ def _call_dmidecode(key, dmidecode_path):
return ""
return result
except (IOError, OSError) as _err:
- LOG.debug('failed dmidecode cmd: %s\n%s', cmd, _err.message)
+ LOG.debug('failed dmidecode cmd: %s\n%s', cmd, _err)
return None