~openstack-charmers-archive/charms/trusty/ceph/next

« back to all changes in this revision

Viewing changes to hooks/utils.py

  • Committer: Paul Collins
  • Date: 2012-10-02 10:52:44 UTC
  • Revision ID: paul.collins@canonical.com-20121002105244-9mj3jk1m601wro5p
hackety hack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#
 
3
# Copyright 2012 Canonical Ltd.
 
4
#
 
5
# Authors:
 
6
#  James Page <james.page@canonical.com>
 
7
#
 
8
# Taken from lp:~james-page/charms/precise/ganglia/python-refactor
 
9
#
 
10
 
 
11
import subprocess
 
12
import os
 
13
import sys
 
14
 
 
15
def install (*pkgs):
 
16
    cmd = [
 
17
        "apt-get",
 
18
        "-y",
 
19
        "install"
 
20
          ]
 
21
    for pkg in pkgs:
 
22
        cmd.append(pkg)
 
23
    subprocess.check_call(cmd)
 
24
 
 
25
TEMPLATES_DIR="templates"
 
26
 
 
27
try:
 
28
    import jinja2
 
29
except ImportError:
 
30
    install('python-jinja2')
 
31
    import jinja2
 
32
 
 
33
def render_template (template_name, context, template_dir=TEMPLATES_DIR):
 
34
    templates = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
 
35
    template = templates.get_template(template_name)
 
36
    return template.render(context)
 
37
 
 
38
def configure_source():
 
39
    source = config_get("source")
 
40
    if (source.startswith("ppa:") or
 
41
        source.startswith("cloud:") or
 
42
        source.startswith("http:")):
 
43
        cmd = [
 
44
            "add-apt-repository",
 
45
            source
 
46
            ]
 
47
        subprocess.check_call(cmd)
 
48
    if source.startswith("http:"):
 
49
        key = config_get("key")
 
50
        cmd = [
 
51
            "apt-key",
 
52
            "import",
 
53
            key
 
54
            ]
 
55
        subprocess.check_call(cmd)
 
56
    cmd = [
 
57
        "apt-get",
 
58
        "update"
 
59
        ]
 
60
    subprocess.check_call(cmd)
 
61
 
 
62
# Protocols
 
63
TCP="TCP"
 
64
UDP="UDP"
 
65
 
 
66
def expose(port, protocol="TCP"):
 
67
    cmd = [
 
68
        "open-port",
 
69
        "%d/%s" % (port,protocol)
 
70
        ]
 
71
    subprocess.check_call(cmd)
 
72
 
 
73
def juju_log(message,severity="INFO"):
 
74
    cmd = [
 
75
        "juju-log",
 
76
        "--log-level", severity,
 
77
        message
 
78
        ]
 
79
    subprocess.check_call(cmd)
 
80
 
 
81
def relation_ids(relation):
 
82
    cmd = [
 
83
        "relation-ids",
 
84
        relation
 
85
        ]
 
86
    return subprocess.check_output(cmd).split()
 
87
 
 
88
def relation_list(rid):
 
89
    cmd = [
 
90
        "relation-list",
 
91
        "-r", rid,
 
92
        ]
 
93
    return subprocess.check_output(cmd).split()
 
94
 
 
95
def relation_get(attribute,unit=None,rid=None):
 
96
    cmd = [
 
97
        "relation-get",
 
98
        ]
 
99
    if rid:
 
100
        cmd.append("-r")
 
101
        cmd.append(rid)
 
102
    cmd.append(attribute)
 
103
    if unit:
 
104
        cmd.append(unit)
 
105
    return subprocess.check_output(cmd).strip()
 
106
 
 
107
def relation_set(*kwargs):
 
108
    cmd = [
 
109
        "relation-set"
 
110
        ]
 
111
    for k, v in kwargs.items():
 
112
        cmd.append("%s=%s" % (k,v))
 
113
    subprocess.check_call(cmd)
 
114
 
 
115
def unit_get(attribute):
 
116
    cmd = [
 
117
        "unit-get",
 
118
        attribute
 
119
        ]
 
120
    return subprocess.check_output(cmd).strip()
 
121
 
 
122
def config_get(attribute):
 
123
    cmd = [
 
124
        "config-get",
 
125
        attribute
 
126
        ]
 
127
    return subprocess.check_output(cmd).strip()
 
128
 
 
129
def juju_log(level, message):
 
130
    subprocess.call(['juju-log', '-l', level, message])