~corey.bryant/charms/trusty/ceph/render

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/host.py

  • Committer: Edward Hope-Morley
  • Date: 2014-11-25 18:29:07 UTC
  • mfrom: (85.2.4 ceph)
  • Revision ID: edward.hope-morley@canonical.com-20141125182907-oyi0vts2tw6o0a5p
[niedbalski,r=hopem]

Adds sysctl config option to allow dict of
sysctl key/val pairs to be set on ceph nodes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
import subprocess
15
15
import hashlib
16
16
from contextlib import contextmanager
17
 
 
18
17
from collections import OrderedDict
19
18
 
20
 
from hookenv import log
21
 
from fstab import Fstab
 
19
import six
 
20
 
 
21
from .hookenv import log
 
22
from .fstab import Fstab
22
23
 
23
24
 
24
25
def service_start(service_name):
54
55
def service_running(service):
55
56
    """Determine whether a system service is running"""
56
57
    try:
57
 
        output = subprocess.check_output(['service', service, 'status'], stderr=subprocess.STDOUT)
 
58
        output = subprocess.check_output(
 
59
            ['service', service, 'status'],
 
60
            stderr=subprocess.STDOUT).decode('UTF-8')
58
61
    except subprocess.CalledProcessError:
59
62
        return False
60
63
    else:
67
70
def service_available(service_name):
68
71
    """Determine whether a system service is available"""
69
72
    try:
70
 
        subprocess.check_output(['service', service_name, 'status'], stderr=subprocess.STDOUT)
 
73
        subprocess.check_output(
 
74
            ['service', service_name, 'status'],
 
75
            stderr=subprocess.STDOUT).decode('UTF-8')
71
76
    except subprocess.CalledProcessError as e:
72
77
        return 'unrecognized service' not in e.output
73
78
    else:
115
120
    cmd.append(from_path)
116
121
    cmd.append(to_path)
117
122
    log(" ".join(cmd))
118
 
    return subprocess.check_output(cmd).strip()
 
123
    return subprocess.check_output(cmd).decode('UTF-8').strip()
119
124
 
120
125
 
121
126
def symlink(source, destination):
130
135
    subprocess.check_call(cmd)
131
136
 
132
137
 
133
 
def mkdir(path, owner='root', group='root', perms=0555, force=False):
 
138
def mkdir(path, owner='root', group='root', perms=0o555, force=False):
134
139
    """Create a directory"""
135
140
    log("Making dir {} {}:{} {:o}".format(path, owner, group,
136
141
                                          perms))
146
151
    os.chown(realpath, uid, gid)
147
152
 
148
153
 
149
 
def write_file(path, content, owner='root', group='root', perms=0444):
 
154
def write_file(path, content, owner='root', group='root', perms=0o444):
150
155
    """Create or overwrite a file with the contents of a string"""
151
156
    log("Writing file {} {}:{} {:o}".format(path, owner, group, perms))
152
157
    uid = pwd.getpwnam(owner).pw_uid
177
182
    cmd_args.extend([device, mountpoint])
178
183
    try:
179
184
        subprocess.check_output(cmd_args)
180
 
    except subprocess.CalledProcessError, e:
 
185
    except subprocess.CalledProcessError as e:
181
186
        log('Error mounting {} at {}\n{}'.format(device, mountpoint, e.output))
182
187
        return False
183
188
 
191
196
    cmd_args = ['umount', mountpoint]
192
197
    try:
193
198
        subprocess.check_output(cmd_args)
194
 
    except subprocess.CalledProcessError, e:
 
199
    except subprocess.CalledProcessError as e:
195
200
        log('Error unmounting {}\n{}'.format(mountpoint, e.output))
196
201
        return False
197
202
 
218
223
    """
219
224
    if os.path.exists(path):
220
225
        h = getattr(hashlib, hash_type)()
221
 
        with open(path, 'r') as source:
222
 
            h.update(source.read())  # IGNORE:E1101 - it does have update
 
226
        with open(path, 'rb') as source:
 
227
            h.update(source.read())
223
228
        return h.hexdigest()
224
229
    else:
225
230
        return None
297
302
    if length is None:
298
303
        length = random.choice(range(35, 45))
299
304
    alphanumeric_chars = [
300
 
        l for l in (string.letters + string.digits)
 
305
        l for l in (string.ascii_letters + string.digits)
301
306
        if l not in 'l0QD1vAEIOUaeiou']
302
307
    random_chars = [
303
308
        random.choice(alphanumeric_chars) for _ in range(length)]
306
311
 
307
312
def list_nics(nic_type):
308
313
    '''Return a list of nics of given type(s)'''
309
 
    if isinstance(nic_type, basestring):
 
314
    if isinstance(nic_type, six.string_types):
310
315
        int_types = [nic_type]
311
316
    else:
312
317
        int_types = nic_type
313
318
    interfaces = []
314
319
    for int_type in int_types:
315
320
        cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
316
 
        ip_output = subprocess.check_output(cmd).split('\n')
 
321
        ip_output = subprocess.check_output(cmd).decode('UTF-8').split('\n')
317
322
        ip_output = (line for line in ip_output if line)
318
323
        for line in ip_output:
319
324
            if line.split()[1].startswith(int_type):
335
340
 
336
341
def get_nic_mtu(nic):
337
342
    cmd = ['ip', 'addr', 'show', nic]
338
 
    ip_output = subprocess.check_output(cmd).split('\n')
 
343
    ip_output = subprocess.check_output(cmd).decode('UTF-8').split('\n')
339
344
    mtu = ""
340
345
    for line in ip_output:
341
346
        words = line.split()
346
351
 
347
352
def get_nic_hwaddr(nic):
348
353
    cmd = ['ip', '-o', '-0', 'addr', 'show', nic]
349
 
    ip_output = subprocess.check_output(cmd)
 
354
    ip_output = subprocess.check_output(cmd).decode('UTF-8')
350
355
    hwaddr = ""
351
356
    words = ip_output.split()
352
357
    if 'link/ether' in words: