~hopem/charms/trusty/ceph-mon/lp1523871

« back to all changes in this revision

Viewing changes to hooks/ceph.py

  • Committer: Corey Bryant
  • Date: 2016-01-14 14:46:26 UTC
  • mfrom: (125.2.6 ceph)
  • Revision ID: corey.bryant@canonical.com-20160114144626-bgo0ptiwgts4uqjc
[chris.macnaughton, r=cholcombe] Add support for Ceph Infernalis release

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
import subprocess
12
12
import time
13
13
import os
 
14
import re
 
15
import sys
14
16
from charmhelpers.core.host import (
15
17
    mkdir,
 
18
    chownr,
16
19
    service_restart,
17
20
    cmp_pkgrevno,
18
21
    lsb_release
24
27
    cached,
25
28
    status_set,
26
29
)
 
30
from charmhelpers.fetch import (
 
31
    apt_cache
 
32
)
27
33
from charmhelpers.contrib.storage.linux.utils import (
28
34
    zap_disk,
29
35
    is_block_device,
40
46
PACKAGES = ['ceph', 'gdisk', 'ntp', 'btrfs-tools', 'python-ceph', 'xfsprogs']
41
47
 
42
48
 
 
49
def ceph_user():
 
50
    if get_version() > 1:
 
51
        return 'ceph'
 
52
    else:
 
53
        return "root"
 
54
 
 
55
 
 
56
def get_version():
 
57
    '''Derive Ceph release from an installed package.'''
 
58
    import apt_pkg as apt
 
59
 
 
60
    cache = apt_cache()
 
61
    package = "ceph"
 
62
    try:
 
63
        pkg = cache[package]
 
64
    except:
 
65
        # the package is unknown to the current apt cache.
 
66
        e = 'Could not determine version of package with no installation '\
 
67
            'candidate: %s' % package
 
68
        error_out(e)
 
69
 
 
70
    if not pkg.current_ver:
 
71
        # package is known, but no version is currently installed.
 
72
        e = 'Could not determine version of uninstalled package: %s' % package
 
73
        error_out(e)
 
74
 
 
75
    vers = apt.upstream_version(pkg.current_ver.ver_str)
 
76
 
 
77
    # x.y match only for 20XX.X
 
78
    # and ignore patch level for other packages
 
79
    match = re.match('^(\d+)\.(\d+)', vers)
 
80
 
 
81
    if match:
 
82
        vers = match.group(0)
 
83
    return float(vers)
 
84
 
 
85
 
 
86
def error_out(msg):
 
87
    log("FATAL ERROR: %s" % msg,
 
88
        level=ERROR)
 
89
    sys.exit(1)
 
90
 
 
91
 
43
92
def is_quorum():
44
93
    asok = "/var/run/ceph/ceph-mon.{}.asok".format(get_unit_hostname())
45
94
    cmd = [
 
95
        "sudo",
 
96
        "-u",
 
97
        ceph_user(),
46
98
        "ceph",
47
99
        "--admin-daemon",
48
100
        asok,
67
119
def is_leader():
68
120
    asok = "/var/run/ceph/ceph-mon.{}.asok".format(get_unit_hostname())
69
121
    cmd = [
 
122
        "sudo",
 
123
        "-u",
 
124
        ceph_user(),
70
125
        "ceph",
71
126
        "--admin-daemon",
72
127
        asok,
96
151
def add_bootstrap_hint(peer):
97
152
    asok = "/var/run/ceph/ceph-mon.{}.asok".format(get_unit_hostname())
98
153
    cmd = [
 
154
        "sudo",
 
155
        "-u",
 
156
        ceph_user(),
99
157
        "ceph",
100
158
        "--admin-daemon",
101
159
        asok,
131
189
    # Scan for ceph block devices
132
190
    rescan_osd_devices()
133
191
    if cmp_pkgrevno('ceph', "0.56.6") >= 0:
134
 
        # Use ceph-disk-activate for directory based OSD's
 
192
        # Use ceph-disk activate for directory based OSD's
135
193
        for dev_or_path in devices:
136
194
            if os.path.exists(dev_or_path) and os.path.isdir(dev_or_path):
137
 
                subprocess.check_call(['ceph-disk-activate', dev_or_path])
 
195
                subprocess.check_call(['ceph-disk', 'activate', dev_or_path])
138
196
 
139
197
 
140
198
def rescan_osd_devices():
161
219
def import_osd_bootstrap_key(key):
162
220
    if not os.path.exists(_bootstrap_keyring):
163
221
        cmd = [
 
222
            "sudo",
 
223
            "-u",
 
224
            ceph_user(),
164
225
            'ceph-authtool',
165
226
            _bootstrap_keyring,
166
227
            '--create-keyring',
219
280
def import_radosgw_key(key):
220
281
    if not os.path.exists(_radosgw_keyring):
221
282
        cmd = [
 
283
            "sudo",
 
284
            "-u",
 
285
            ceph_user(),
222
286
            'ceph-authtool',
223
287
            _radosgw_keyring,
224
288
            '--create-keyring',
247
311
def get_named_key(name, caps=None):
248
312
    caps = caps or _default_caps
249
313
    cmd = [
 
314
        "sudo",
 
315
        "-u",
 
316
        ceph_user(),
250
317
        'ceph',
251
318
        '--name', 'mon.',
252
319
        '--keyring',
270
337
        # Not the MON leader OR not clustered
271
338
        return
272
339
    cmd = [
273
 
        'ceph', 'auth', 'caps', key
 
340
        "sudo", "-u", ceph_user(), 'ceph', 'auth', 'caps', key
274
341
    ]
275
342
    for subsystem, subcaps in caps.iteritems():
276
343
        cmd.extend([subsystem, '; '.join(subcaps)])
297
364
        log('bootstrap_monitor_cluster: mon already initialized.')
298
365
    else:
299
366
        # Ceph >= 0.61.3 needs this for ceph-mon fs creation
300
 
        mkdir('/var/run/ceph', perms=0o755)
301
 
        mkdir(path)
 
367
        mkdir('/var/run/ceph', owner=ceph_user(),
 
368
              group=ceph_user(), perms=0o755)
 
369
        mkdir(path, owner=ceph_user(), group=ceph_user())
302
370
        # end changes for Ceph >= 0.61.3
303
371
        try:
304
372
            subprocess.check_call(['ceph-authtool', keyring,
309
377
            subprocess.check_call(['ceph-mon', '--mkfs',
310
378
                                   '-i', hostname,
311
379
                                   '--keyring', keyring])
312
 
 
 
380
            chownr(path, ceph_user(), ceph_user())
313
381
            with open(done, 'w'):
314
382
                pass
315
383
            with open(init_marker, 'w'):
367
435
        return
368
436
 
369
437
    status_set('maintenance', 'Initializing device {}'.format(dev))
370
 
    cmd = ['ceph-disk-prepare']
 
438
    cmd = ['ceph-disk', 'prepare']
371
439
    # Later versions of ceph support more options
372
440
    if cmp_pkgrevno('ceph', '0.48.3') >= 0:
373
441
        if osd_format:
405
473
            level=ERROR)
406
474
        raise
407
475
 
408
 
    mkdir(path)
 
476
    mkdir(path, owner=ceph_user(), group=ceph_user(), perms=0o755)
 
477
    chownr('/var/lib/ceph', ceph_user(), ceph_user())
409
478
    cmd = [
410
 
        'ceph-disk-prepare',
 
479
        'sudo', '-u', ceph_user(),
 
480
        'ceph-disk',
 
481
        'prepare',
411
482
        '--data-dir',
412
483
        path
413
484
    ]