~junaidali/charms/trusty/plumgrid-director/pg-restart

« back to all changes in this revision

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

  • Committer: bbaqar at plumgrid
  • Date: 2015-07-29 18:07:31 UTC
  • Revision ID: bbaqar@plumgrid.com-20150729180731-ioynar8x3u5pxytc
Addressed reviews by Charmers

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
# Copyright 2014-2015 Canonical Limited.
4
 
#
5
 
# This file is part of charm-helpers.
6
 
#
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.
10
 
#
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.
15
 
#
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/>.
18
 
 
19
 
import os
20
 
import sys
21
 
import subprocess
22
 
from charmhelpers.core import hookenv
23
 
 
24
 
 
25
 
def default_execd_dir():
26
 
    return os.path.join(os.environ['CHARM_DIR'], 'exec.d')
27
 
 
28
 
 
29
 
def execd_module_paths(execd_dir=None):
30
 
    """Generate a list of full paths to modules within execd_dir."""
31
 
    if not execd_dir:
32
 
        execd_dir = default_execd_dir()
33
 
 
34
 
    if not os.path.exists(execd_dir):
35
 
        return
36
 
 
37
 
    for subpath in os.listdir(execd_dir):
38
 
        module = os.path.join(execd_dir, subpath)
39
 
        if os.path.isdir(module):
40
 
            yield module
41
 
 
42
 
 
43
 
def execd_submodule_paths(command, execd_dir=None):
44
 
    """Generate a list of full paths to the specified command within exec_dir.
45
 
    """
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):
49
 
            yield path
50
 
 
51
 
 
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):
55
 
        try:
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))
60
 
            if die_on_error:
61
 
                sys.exit(e.returncode)
62
 
 
63
 
 
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)