summaryrefslogtreecommitdiff
path: root/cloudinit/subp.py
diff options
context:
space:
mode:
authorScott Moser <smoser@brickies.net>2020-06-08 14:58:55 -0400
committerGitHub <noreply@github.com>2020-06-08 12:58:55 -0600
commitb36b61debe1556f6dfd2d7ff61fa7bd6b3335fa0 (patch)
tree98f7f27730593895bf8fcba5e859fa2845d79c51 /cloudinit/subp.py
parent3c551f6ebc12f7729a2755c89b19b9000e27cc88 (diff)
downloadcloud-init-git-b36b61debe1556f6dfd2d7ff61fa7bd6b3335fa0.tar.gz
Move runparts to subp. (#420)
runparts (run a directory of scripts) seems to fit well in subp module. The request to move it there was raised in #416. Replace use of logexc with LOG.debug as logexc comes from util.
Diffstat (limited to 'cloudinit/subp.py')
-rw-r--r--cloudinit/subp.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/cloudinit/subp.py b/cloudinit/subp.py
index f8400b1f..804ef3ca 100644
--- a/cloudinit/subp.py
+++ b/cloudinit/subp.py
@@ -351,4 +351,36 @@ def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
+def runparts(dirp, skip_no_exist=True, exe_prefix=None):
+ if skip_no_exist and not os.path.isdir(dirp):
+ return
+
+ failed = []
+ attempted = []
+
+ if exe_prefix is None:
+ prefix = []
+ elif isinstance(exe_prefix, str):
+ prefix = [str(exe_prefix)]
+ elif isinstance(exe_prefix, list):
+ prefix = exe_prefix
+ else:
+ raise TypeError("exe_prefix must be None, str, or list")
+
+ for exe_name in sorted(os.listdir(dirp)):
+ exe_path = os.path.join(dirp, exe_name)
+ if is_exe(exe_path):
+ attempted.append(exe_path)
+ try:
+ subp(prefix + [exe_path], capture=False)
+ except ProcessExecutionError as e:
+ LOG.debug(e)
+ failed.append(exe_name)
+
+ if failed and attempted:
+ raise RuntimeError(
+ 'Runparts: %s failures (%s) in %s attempted commands' %
+ (len(failed), ",".join(failed), len(attempted)))
+
+
# vi: ts=4 expandtab