3
# Copyright 2014-2015 Canonical Limited.
5
# This file is part of charm-helpers.
7
# charm-helpers is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License version 3 as
9
# published by the Free Software Foundation.
11
# charm-helpers is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Lesser General Public License for more details.
16
# You should have received a copy of the GNU Lesser General Public License
17
# along with charm-helpers. If not, see <http://www.gnu.org/licenses/>.
22
from charmhelpers.core import hookenv
25
def default_execd_dir():
26
return os.path.join(os.environ['CHARM_DIR'], 'exec.d')
29
def execd_module_paths(execd_dir=None):
30
"""Generate a list of full paths to modules within execd_dir."""
32
execd_dir = default_execd_dir()
34
if not os.path.exists(execd_dir):
37
for subpath in os.listdir(execd_dir):
38
module = os.path.join(execd_dir, subpath)
39
if os.path.isdir(module):
43
def execd_submodule_paths(command, execd_dir=None):
44
"""Generate a list of full paths to the specified command within exec_dir.
46
for module_path in execd_module_paths(execd_dir):
47
path = os.path.join(module_path, command)
48
if os.access(path, os.X_OK) and os.path.isfile(path):
52
def execd_run(command, execd_dir=None, die_on_error=False, stderr=None):
53
"""Run command for each module within execd_dir which defines it."""
54
for submodule_path in execd_submodule_paths(command, execd_dir):
56
subprocess.check_call(submodule_path, shell=True, stderr=stderr)
57
except subprocess.CalledProcessError as e:
58
hookenv.log("Error ({}) running {}. Output: {}".format(
59
e.returncode, e.cmd, e.output))
61
sys.exit(e.returncode)
64
def execd_preinstall(execd_dir=None):
65
"""Run charm-pre-install for each module within execd_dir."""
66
execd_run('charm-pre-install', execd_dir=execd_dir)