~pmcgarry/ceph/ceph

« back to all changes in this revision

Viewing changes to hooks/utils.py

  • Committer: James Page
  • Date: 2013-02-08 11:09:00 UTC
  • mfrom: (52.2.8 ceph)
  • Revision ID: james.page@canonical.com-20130208110900-h06io58wdv3n2996
Add support for Ceph Bobtail LTS.

- XFS and BTRFS disk formats for OSD's.
- Separate journal devices.
- Package provided upstart configurations

Fixes:

- Improve hostname -> ip address resolution in MAAS managed environments.
- Fix disk zapping for devices which have been OSD's before.
- Resync utils.py, cepy.py across ceph charms.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
import subprocess
12
12
import socket
13
13
import sys
 
14
import re
14
15
 
15
16
 
16
17
def do_hooks(hooks):
17
18
    hook = os.path.basename(sys.argv[0])
18
19
 
19
20
    try:
20
 
        hooks[hook]()
 
21
        hook_func = hooks[hook]
21
22
    except KeyError:
22
23
        juju_log('INFO',
23
24
                 "This charm doesn't know how to handle '{}'.".format(hook))
 
25
    else:
 
26
        hook_func()
24
27
 
25
28
 
26
29
def install(*pkgs):
41
44
    install('python-jinja2')
42
45
    import jinja2
43
46
 
 
47
try:
 
48
    import dns.resolver
 
49
except ImportError:
 
50
    install('python-dnspython')
 
51
    import dns.resolver
 
52
 
44
53
 
45
54
def render_template(template_name, context, template_dir=TEMPLATES_DIR):
46
55
    templates = jinja2.Environment(
88
97
        ]
89
98
    subprocess.check_call(cmd)
90
99
 
 
100
 
 
101
def enable_pocket(pocket):
 
102
    apt_sources = "/etc/apt/sources.list"
 
103
    with open(apt_sources, "r") as sources:
 
104
        lines = sources.readlines()
 
105
    with open(apt_sources, "w") as sources:
 
106
        for line in lines:
 
107
            if pocket in line:
 
108
                sources.write(re.sub('^# deb', 'deb', line))
 
109
            else:
 
110
                sources.write(line)
 
111
 
91
112
# Protocols
92
113
TCP = 'TCP'
93
114
UDP = 'UDP'
136
157
    cmd.append(attribute)
137
158
    if unit:
138
159
        cmd.append(unit)
139
 
    return subprocess.check_output(cmd).strip()  # IGNORE:E1103
 
160
    value = str(subprocess.check_output(cmd)).strip()
 
161
    if value == "":
 
162
        return None
 
163
    else:
 
164
        return value
140
165
 
141
166
 
142
167
def relation_set(**kwargs):
159
184
        'unit-get',
160
185
        attribute
161
186
        ]
162
 
    return subprocess.check_output(cmd).strip()  # IGNORE:E1103
 
187
    value = str(subprocess.check_output(cmd)).strip()
 
188
    if value == "":
 
189
        return None
 
190
    else:
 
191
        return value
163
192
 
164
193
 
165
194
def config_get(attribute):
167
196
        'config-get',
168
197
        attribute
169
198
        ]
170
 
    return subprocess.check_output(cmd).strip()  # IGNORE:E1103
 
199
    value = str(subprocess.check_output(cmd)).strip()
 
200
    if value == "":
 
201
        return None
 
202
    else:
 
203
        return value
171
204
 
172
205
 
173
206
def get_unit_hostname():
175
208
 
176
209
 
177
210
def get_host_ip(hostname=unit_get('private-address')):
178
 
    cmd = [
179
 
        'dig',
180
 
        '+short',
181
 
        hostname
182
 
        ]
183
 
    return subprocess.check_output(cmd).strip()  # IGNORE:E1103
 
211
    try:
 
212
        # Test to see if already an IPv4 address
 
213
        socket.inet_aton(hostname)
 
214
        return hostname
 
215
    except socket.error:
 
216
        # This may throw an NXDOMAIN exception; in which case
 
217
        # things are badly broken so just let it kill the hook
 
218
        answers = dns.resolver.query(hostname, 'A')
 
219
        if answers:
 
220
            return answers[0].address