6
from charmhelpers.core import hookenv
9
def default_execd_dir():
10
return os.path.join(os.environ['CHARM_DIR'], 'exec.d')
13
def execd_module_paths(execd_dir=None):
14
"""Generate a list of full paths to modules within execd_dir."""
16
execd_dir = default_execd_dir()
18
if not os.path.exists(execd_dir):
21
for subpath in os.listdir(execd_dir):
22
module = os.path.join(execd_dir, subpath)
23
if os.path.isdir(module):
27
def execd_submodule_paths(command, execd_dir=None):
28
"""Generate a list of full paths to the specified command within exec_dir.
30
for module_path in execd_module_paths(execd_dir):
31
path = os.path.join(module_path, command)
32
if os.access(path, os.X_OK) and os.path.isfile(path):
36
def execd_run(command, execd_dir=None, die_on_error=False, stderr=None):
37
"""Run command for each module within execd_dir which defines it."""
38
for submodule_path in execd_submodule_paths(command, execd_dir):
40
subprocess.check_call(submodule_path, shell=True, stderr=stderr)
41
except subprocess.CalledProcessError as e:
42
hookenv.log("Error ({}) running {}. Output: {}".format(
43
e.returncode, e.cmd, e.output))
45
sys.exit(e.returncode)
48
def execd_preinstall(execd_dir=None):
49
"""Run charm-pre-install for each module within execd_dir."""
50
execd_run('charm-pre-install', execd_dir=execd_dir)