~verterok/charms/trusty/mongodb/fix-data_directory-migration

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/payload/execd.py

  • Committer: Matt Bruzek
  • Date: 2015-01-13 17:51:52 UTC
  • mfrom: (53.6.1 merge-precise-patches)
  • Revision ID: matthew.bruzek@canonical.com-20150113175152-9czpslqsolbf9i9l
[wes] Merge exec.d and volume relation_id patches from precise charm
[mbruzek] Fixed lint errors in hooks.py file so tests would pass.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)