Skip to content

Commit b36b61d

Browse files
authored
Move runparts to subp. (canonical#420)
runparts (run a directory of scripts) seems to fit well in subp module. The request to move it there was raised in canonical#416. Replace use of logexc with LOG.debug as logexc comes from util.
1 parent 3c551f6 commit b36b61d

File tree

7 files changed

+42
-40
lines changed

7 files changed

+42
-40
lines changed

cloudinit/config/cc_scripts_per_boot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import os
2626

27-
from cloudinit import util
27+
from cloudinit import subp
2828

2929
from cloudinit.settings import PER_ALWAYS
3030

@@ -38,7 +38,7 @@ def handle(name, _cfg, cloud, log, _args):
3838
# https://forums.aws.amazon.com/thread.jspa?threadID=96918
3939
runparts_path = os.path.join(cloud.get_cpath(), 'scripts', SCRIPT_SUBDIR)
4040
try:
41-
util.runparts(runparts_path)
41+
subp.runparts(runparts_path)
4242
except Exception:
4343
log.warning("Failed to run module %s (%s in %s)",
4444
name, SCRIPT_SUBDIR, runparts_path)

cloudinit/config/cc_scripts_per_instance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import os
2929

30-
from cloudinit import util
30+
from cloudinit import subp
3131

3232
from cloudinit.settings import PER_INSTANCE
3333

@@ -41,7 +41,7 @@ def handle(name, _cfg, cloud, log, _args):
4141
# https://forums.aws.amazon.com/thread.jspa?threadID=96918
4242
runparts_path = os.path.join(cloud.get_cpath(), 'scripts', SCRIPT_SUBDIR)
4343
try:
44-
util.runparts(runparts_path)
44+
subp.runparts(runparts_path)
4545
except Exception:
4646
log.warning("Failed to run module %s (%s in %s)",
4747
name, SCRIPT_SUBDIR, runparts_path)

cloudinit/config/cc_scripts_per_once.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import os
2727

28-
from cloudinit import util
28+
from cloudinit import subp
2929

3030
from cloudinit.settings import PER_ONCE
3131

@@ -39,7 +39,7 @@ def handle(name, _cfg, cloud, log, _args):
3939
# https://forums.aws.amazon.com/thread.jspa?threadID=96918
4040
runparts_path = os.path.join(cloud.get_cpath(), 'scripts', SCRIPT_SUBDIR)
4141
try:
42-
util.runparts(runparts_path)
42+
subp.runparts(runparts_path)
4343
except Exception:
4444
log.warning("Failed to run module %s (%s in %s)",
4545
name, SCRIPT_SUBDIR, runparts_path)

cloudinit/config/cc_scripts_user.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import os
2929

30-
from cloudinit import util
30+
from cloudinit import subp
3131

3232
from cloudinit.settings import PER_INSTANCE
3333

@@ -42,7 +42,7 @@ def handle(name, _cfg, cloud, log, _args):
4242
# go here...
4343
runparts_path = os.path.join(cloud.get_ipath_cur(), SCRIPT_SUBDIR)
4444
try:
45-
util.runparts(runparts_path)
45+
subp.runparts(runparts_path)
4646
except Exception:
4747
log.warning("Failed to run module %s (%s in %s)",
4848
name, SCRIPT_SUBDIR, runparts_path)

cloudinit/config/cc_scripts_vendor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import os
3030

31+
from cloudinit import subp
3132
from cloudinit import util
3233

3334
from cloudinit.settings import PER_INSTANCE
@@ -46,7 +47,7 @@ def handle(name, cfg, cloud, log, _args):
4647
prefix = util.get_cfg_by_path(cfg, ('vendor_data', 'prefix'), [])
4748

4849
try:
49-
util.runparts(runparts_path, exe_prefix=prefix)
50+
subp.runparts(runparts_path, exe_prefix=prefix)
5051
except Exception:
5152
log.warning("Failed to run module %s (%s in %s)",
5253
name, SCRIPT_SUBDIR, runparts_path)

cloudinit/subp.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,36 @@ def is_exe(fpath):
351351
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
352352

353353

354+
def runparts(dirp, skip_no_exist=True, exe_prefix=None):
355+
if skip_no_exist and not os.path.isdir(dirp):
356+
return
357+
358+
failed = []
359+
attempted = []
360+
361+
if exe_prefix is None:
362+
prefix = []
363+
elif isinstance(exe_prefix, str):
364+
prefix = [str(exe_prefix)]
365+
elif isinstance(exe_prefix, list):
366+
prefix = exe_prefix
367+
else:
368+
raise TypeError("exe_prefix must be None, str, or list")
369+
370+
for exe_name in sorted(os.listdir(dirp)):
371+
exe_path = os.path.join(dirp, exe_name)
372+
if is_exe(exe_path):
373+
attempted.append(exe_path)
374+
try:
375+
subp(prefix + [exe_path], capture=False)
376+
except ProcessExecutionError as e:
377+
LOG.debug(e)
378+
failed.append(exe_name)
379+
380+
if failed and attempted:
381+
raise RuntimeError(
382+
'Runparts: %s failures (%s) in %s attempted commands' %
383+
(len(failed), ",".join(failed), len(attempted)))
384+
385+
354386
# vi: ts=4 expandtab

cloudinit/util.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -749,37 +749,6 @@ def del_dir(path):
749749
shutil.rmtree(path)
750750

751751

752-
def runparts(dirp, skip_no_exist=True, exe_prefix=None):
753-
if skip_no_exist and not os.path.isdir(dirp):
754-
return
755-
756-
failed = []
757-
attempted = []
758-
759-
if exe_prefix is None:
760-
prefix = []
761-
elif isinstance(exe_prefix, str):
762-
prefix = [str(exe_prefix)]
763-
elif isinstance(exe_prefix, list):
764-
prefix = exe_prefix
765-
else:
766-
raise TypeError("exe_prefix must be None, str, or list")
767-
768-
for exe_name in sorted(os.listdir(dirp)):
769-
exe_path = os.path.join(dirp, exe_name)
770-
if os.path.isfile(exe_path) and os.access(exe_path, os.X_OK):
771-
attempted.append(exe_path)
772-
try:
773-
subp.subp(prefix + [exe_path], capture=False)
774-
except subp.ProcessExecutionError as e:
775-
logexc(LOG, "Failed running %s [%s]", exe_path, e.exit_code)
776-
failed.append(e)
777-
778-
if failed and attempted:
779-
raise RuntimeError('Runparts: %s failures in %s attempted commands'
780-
% (len(failed), len(attempted)))
781-
782-
783752
# read_optional_seed
784753
# returns boolean indicating success or failure (presense of files)
785754
# if files are present, populates 'fill' dictionary with 'user-data' and

0 commit comments

Comments
 (0)