~moon127/charms/trusty/ceilometer/add-execd-preinstall

50 by Alexander List
[alexlist] add missing execd payload
1
#!/usr/bin/env python
2
3
import os
4
import sys
5
import subprocess
6
from charmhelpers.core import hookenv
7
8
9
def default_execd_dir():
10
    return os.path.join(os.environ['CHARM_DIR'], 'exec.d')
11
12
13
def execd_module_paths(execd_dir=None):
14
    """Generate a list of full paths to modules within execd_dir."""
15
    if not execd_dir:
16
        execd_dir = default_execd_dir()
17
18
    if not os.path.exists(execd_dir):
19
        return
20
21
    for subpath in os.listdir(execd_dir):
22
        module = os.path.join(execd_dir, subpath)
23
        if os.path.isdir(module):
24
            yield module
25
26
27
def execd_submodule_paths(command, execd_dir=None):
28
    """Generate a list of full paths to the specified command within exec_dir.
29
    """
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):
33
            yield path
34
35
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):
39
        try:
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))
44
            if die_on_error:
45
                sys.exit(e.returncode)
46
47
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)