~corey.bryant/charms/trusty/ceph-radosgw/charm-proof

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/storage/linux/ceph.py

  • Committer: Liam Young
  • Date: 2015-01-16 16:18:04 UTC
  • mfrom: (30.2.22 ceph-radosgw)
  • Revision ID: liam.young@canonical.com-20150116161804-2jzipwa73t8gm6b1
[gnuoy,r=james-page] Add HA support

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright 2012 Canonical Ltd.
 
3
#
 
4
# This file is sourced from lp:openstack-charm-helpers
 
5
#
 
6
# Authors:
 
7
#  James Page <james.page@ubuntu.com>
 
8
#  Adam Gandelman <adamg@ubuntu.com>
 
9
#
 
10
 
 
11
import os
 
12
import shutil
 
13
import json
 
14
import time
 
15
 
 
16
from subprocess import (
 
17
    check_call,
 
18
    check_output,
 
19
    CalledProcessError,
 
20
)
 
21
from charmhelpers.core.hookenv import (
 
22
    relation_get,
 
23
    relation_ids,
 
24
    related_units,
 
25
    log,
 
26
    DEBUG,
 
27
    INFO,
 
28
    WARNING,
 
29
    ERROR,
 
30
)
 
31
from charmhelpers.core.host import (
 
32
    mount,
 
33
    mounts,
 
34
    service_start,
 
35
    service_stop,
 
36
    service_running,
 
37
    umount,
 
38
)
 
39
from charmhelpers.fetch import (
 
40
    apt_install,
 
41
)
 
42
 
 
43
KEYRING = '/etc/ceph/ceph.client.{}.keyring'
 
44
KEYFILE = '/etc/ceph/ceph.client.{}.key'
 
45
 
 
46
CEPH_CONF = """[global]
 
47
 auth supported = {auth}
 
48
 keyring = {keyring}
 
49
 mon host = {mon_hosts}
 
50
 log to syslog = {use_syslog}
 
51
 err to syslog = {use_syslog}
 
52
 clog to syslog = {use_syslog}
 
53
"""
 
54
 
 
55
 
 
56
def install():
 
57
    """Basic Ceph client installation."""
 
58
    ceph_dir = "/etc/ceph"
 
59
    if not os.path.exists(ceph_dir):
 
60
        os.mkdir(ceph_dir)
 
61
 
 
62
    apt_install('ceph-common', fatal=True)
 
63
 
 
64
 
 
65
def rbd_exists(service, pool, rbd_img):
 
66
    """Check to see if a RADOS block device exists."""
 
67
    try:
 
68
        out = check_output(['rbd', 'list', '--id',
 
69
                            service, '--pool', pool]).decode('UTF-8')
 
70
    except CalledProcessError:
 
71
        return False
 
72
 
 
73
    return rbd_img in out
 
74
 
 
75
 
 
76
def create_rbd_image(service, pool, image, sizemb):
 
77
    """Create a new RADOS block device."""
 
78
    cmd = ['rbd', 'create', image, '--size', str(sizemb), '--id', service,
 
79
           '--pool', pool]
 
80
    check_call(cmd)
 
81
 
 
82
 
 
83
def pool_exists(service, name):
 
84
    """Check to see if a RADOS pool already exists."""
 
85
    try:
 
86
        out = check_output(['rados', '--id', service,
 
87
                            'lspools']).decode('UTF-8')
 
88
    except CalledProcessError:
 
89
        return False
 
90
 
 
91
    return name in out
 
92
 
 
93
 
 
94
def get_osds(service):
 
95
    """Return a list of all Ceph Object Storage Daemons currently in the
 
96
    cluster.
 
97
    """
 
98
    version = ceph_version()
 
99
    if version and version >= '0.56':
 
100
        return json.loads(check_output(['ceph', '--id', service,
 
101
                                        'osd', 'ls',
 
102
                                        '--format=json']).decode('UTF-8'))
 
103
 
 
104
    return None
 
105
 
 
106
 
 
107
def create_pool(service, name, replicas=3):
 
108
    """Create a new RADOS pool."""
 
109
    if pool_exists(service, name):
 
110
        log("Ceph pool {} already exists, skipping creation".format(name),
 
111
            level=WARNING)
 
112
        return
 
113
 
 
114
    # Calculate the number of placement groups based
 
115
    # on upstream recommended best practices.
 
116
    osds = get_osds(service)
 
117
    if osds:
 
118
        pgnum = (len(osds) * 100 // replicas)
 
119
    else:
 
120
        # NOTE(james-page): Default to 200 for older ceph versions
 
121
        # which don't support OSD query from cli
 
122
        pgnum = 200
 
123
 
 
124
    cmd = ['ceph', '--id', service, 'osd', 'pool', 'create', name, str(pgnum)]
 
125
    check_call(cmd)
 
126
 
 
127
    cmd = ['ceph', '--id', service, 'osd', 'pool', 'set', name, 'size',
 
128
           str(replicas)]
 
129
    check_call(cmd)
 
130
 
 
131
 
 
132
def delete_pool(service, name):
 
133
    """Delete a RADOS pool from ceph."""
 
134
    cmd = ['ceph', '--id', service, 'osd', 'pool', 'delete', name,
 
135
           '--yes-i-really-really-mean-it']
 
136
    check_call(cmd)
 
137
 
 
138
 
 
139
def _keyfile_path(service):
 
140
    return KEYFILE.format(service)
 
141
 
 
142
 
 
143
def _keyring_path(service):
 
144
    return KEYRING.format(service)
 
145
 
 
146
 
 
147
def create_keyring(service, key):
 
148
    """Create a new Ceph keyring containing key."""
 
149
    keyring = _keyring_path(service)
 
150
    if os.path.exists(keyring):
 
151
        log('Ceph keyring exists at %s.' % keyring, level=WARNING)
 
152
        return
 
153
 
 
154
    cmd = ['ceph-authtool', keyring, '--create-keyring',
 
155
           '--name=client.{}'.format(service), '--add-key={}'.format(key)]
 
156
    check_call(cmd)
 
157
    log('Created new ceph keyring at %s.' % keyring, level=DEBUG)
 
158
 
 
159
 
 
160
def delete_keyring(service):
 
161
    """Delete an existing Ceph keyring."""
 
162
    keyring = _keyring_path(service)
 
163
    if not os.path.exists(keyring):
 
164
        log('Keyring does not exist at %s' % keyring, level=WARNING)
 
165
        return
 
166
 
 
167
    os.remove(keyring)
 
168
    log('Deleted ring at %s.' % keyring, level=INFO)
 
169
 
 
170
 
 
171
def create_key_file(service, key):
 
172
    """Create a file containing key."""
 
173
    keyfile = _keyfile_path(service)
 
174
    if os.path.exists(keyfile):
 
175
        log('Keyfile exists at %s.' % keyfile, level=WARNING)
 
176
        return
 
177
 
 
178
    with open(keyfile, 'w') as fd:
 
179
        fd.write(key)
 
180
 
 
181
    log('Created new keyfile at %s.' % keyfile, level=INFO)
 
182
 
 
183
 
 
184
def get_ceph_nodes():
 
185
    """Query named relation 'ceph' to determine current nodes."""
 
186
    hosts = []
 
187
    for r_id in relation_ids('ceph'):
 
188
        for unit in related_units(r_id):
 
189
            hosts.append(relation_get('private-address', unit=unit, rid=r_id))
 
190
 
 
191
    return hosts
 
192
 
 
193
 
 
194
def configure(service, key, auth, use_syslog):
 
195
    """Perform basic configuration of Ceph."""
 
196
    create_keyring(service, key)
 
197
    create_key_file(service, key)
 
198
    hosts = get_ceph_nodes()
 
199
    with open('/etc/ceph/ceph.conf', 'w') as ceph_conf:
 
200
        ceph_conf.write(CEPH_CONF.format(auth=auth,
 
201
                                         keyring=_keyring_path(service),
 
202
                                         mon_hosts=",".join(map(str, hosts)),
 
203
                                         use_syslog=use_syslog))
 
204
    modprobe('rbd')
 
205
 
 
206
 
 
207
def image_mapped(name):
 
208
    """Determine whether a RADOS block device is mapped locally."""
 
209
    try:
 
210
        out = check_output(['rbd', 'showmapped']).decode('UTF-8')
 
211
    except CalledProcessError:
 
212
        return False
 
213
 
 
214
    return name in out
 
215
 
 
216
 
 
217
def map_block_storage(service, pool, image):
 
218
    """Map a RADOS block device for local use."""
 
219
    cmd = [
 
220
        'rbd',
 
221
        'map',
 
222
        '{}/{}'.format(pool, image),
 
223
        '--user',
 
224
        service,
 
225
        '--secret',
 
226
        _keyfile_path(service),
 
227
    ]
 
228
    check_call(cmd)
 
229
 
 
230
 
 
231
def filesystem_mounted(fs):
 
232
    """Determine whether a filesytems is already mounted."""
 
233
    return fs in [f for f, m in mounts()]
 
234
 
 
235
 
 
236
def make_filesystem(blk_device, fstype='ext4', timeout=10):
 
237
    """Make a new filesystem on the specified block device."""
 
238
    count = 0
 
239
    e_noent = os.errno.ENOENT
 
240
    while not os.path.exists(blk_device):
 
241
        if count >= timeout:
 
242
            log('Gave up waiting on block device %s' % blk_device,
 
243
                level=ERROR)
 
244
            raise IOError(e_noent, os.strerror(e_noent), blk_device)
 
245
 
 
246
        log('Waiting for block device %s to appear' % blk_device,
 
247
            level=DEBUG)
 
248
        count += 1
 
249
        time.sleep(1)
 
250
    else:
 
251
        log('Formatting block device %s as filesystem %s.' %
 
252
            (blk_device, fstype), level=INFO)
 
253
        check_call(['mkfs', '-t', fstype, blk_device])
 
254
 
 
255
 
 
256
def place_data_on_block_device(blk_device, data_src_dst):
 
257
    """Migrate data in data_src_dst to blk_device and then remount."""
 
258
    # mount block device into /mnt
 
259
    mount(blk_device, '/mnt')
 
260
    # copy data to /mnt
 
261
    copy_files(data_src_dst, '/mnt')
 
262
    # umount block device
 
263
    umount('/mnt')
 
264
    # Grab user/group ID's from original source
 
265
    _dir = os.stat(data_src_dst)
 
266
    uid = _dir.st_uid
 
267
    gid = _dir.st_gid
 
268
    # re-mount where the data should originally be
 
269
    # TODO: persist is currently a NO-OP in core.host
 
270
    mount(blk_device, data_src_dst, persist=True)
 
271
    # ensure original ownership of new mount.
 
272
    os.chown(data_src_dst, uid, gid)
 
273
 
 
274
 
 
275
# TODO: re-use
 
276
def modprobe(module):
 
277
    """Load a kernel module and configure for auto-load on reboot."""
 
278
    log('Loading kernel module', level=INFO)
 
279
    cmd = ['modprobe', module]
 
280
    check_call(cmd)
 
281
    with open('/etc/modules', 'r+') as modules:
 
282
        if module not in modules.read():
 
283
            modules.write(module)
 
284
 
 
285
 
 
286
def copy_files(src, dst, symlinks=False, ignore=None):
 
287
    """Copy files from src to dst."""
 
288
    for item in os.listdir(src):
 
289
        s = os.path.join(src, item)
 
290
        d = os.path.join(dst, item)
 
291
        if os.path.isdir(s):
 
292
            shutil.copytree(s, d, symlinks, ignore)
 
293
        else:
 
294
            shutil.copy2(s, d)
 
295
 
 
296
 
 
297
def ensure_ceph_storage(service, pool, rbd_img, sizemb, mount_point,
 
298
                        blk_device, fstype, system_services=[],
 
299
                        replicas=3):
 
300
    """NOTE: This function must only be called from a single service unit for
 
301
    the same rbd_img otherwise data loss will occur.
 
302
 
 
303
    Ensures given pool and RBD image exists, is mapped to a block device,
 
304
    and the device is formatted and mounted at the given mount_point.
 
305
 
 
306
    If formatting a device for the first time, data existing at mount_point
 
307
    will be migrated to the RBD device before being re-mounted.
 
308
 
 
309
    All services listed in system_services will be stopped prior to data
 
310
    migration and restarted when complete.
 
311
    """
 
312
    # Ensure pool, RBD image, RBD mappings are in place.
 
313
    if not pool_exists(service, pool):
 
314
        log('Creating new pool {}.'.format(pool), level=INFO)
 
315
        create_pool(service, pool, replicas=replicas)
 
316
 
 
317
    if not rbd_exists(service, pool, rbd_img):
 
318
        log('Creating RBD image ({}).'.format(rbd_img), level=INFO)
 
319
        create_rbd_image(service, pool, rbd_img, sizemb)
 
320
 
 
321
    if not image_mapped(rbd_img):
 
322
        log('Mapping RBD Image {} as a Block Device.'.format(rbd_img),
 
323
            level=INFO)
 
324
        map_block_storage(service, pool, rbd_img)
 
325
 
 
326
    # make file system
 
327
    # TODO: What happens if for whatever reason this is run again and
 
328
    # the data is already in the rbd device and/or is mounted??
 
329
    # When it is mounted already, it will fail to make the fs
 
330
    # XXX: This is really sketchy!  Need to at least add an fstab entry
 
331
    #      otherwise this hook will blow away existing data if its executed
 
332
    #      after a reboot.
 
333
    if not filesystem_mounted(mount_point):
 
334
        make_filesystem(blk_device, fstype)
 
335
 
 
336
        for svc in system_services:
 
337
            if service_running(svc):
 
338
                log('Stopping services {} prior to migrating data.'
 
339
                    .format(svc), level=DEBUG)
 
340
                service_stop(svc)
 
341
 
 
342
        place_data_on_block_device(blk_device, mount_point)
 
343
 
 
344
        for svc in system_services:
 
345
            log('Starting service {} after migrating data.'
 
346
                .format(svc), level=DEBUG)
 
347
            service_start(svc)
 
348
 
 
349
 
 
350
def ensure_ceph_keyring(service, user=None, group=None):
 
351
    """Ensures a ceph keyring is created for a named service and optionally
 
352
    ensures user and group ownership.
 
353
 
 
354
    Returns False if no ceph key is available in relation state.
 
355
    """
 
356
    key = None
 
357
    for rid in relation_ids('ceph'):
 
358
        for unit in related_units(rid):
 
359
            key = relation_get('key', rid=rid, unit=unit)
 
360
            if key:
 
361
                break
 
362
 
 
363
    if not key:
 
364
        return False
 
365
 
 
366
    create_keyring(service=service, key=key)
 
367
    keyring = _keyring_path(service)
 
368
    if user and group:
 
369
        check_call(['chown', '%s.%s' % (user, group), keyring])
 
370
 
 
371
    return True
 
372
 
 
373
 
 
374
def ceph_version():
 
375
    """Retrieve the local version of ceph."""
 
376
    if os.path.exists('/usr/bin/ceph'):
 
377
        cmd = ['ceph', '-v']
 
378
        output = check_output(cmd).decode('US-ASCII')
 
379
        output = output.split()
 
380
        if len(output) > 3:
 
381
            return output[2]
 
382
        else:
 
383
            return None
 
384
    else:
 
385
        return None
 
386
 
 
387
 
 
388
class CephBrokerRq(object):
 
389
    """Ceph broker request.
 
390
 
 
391
    Multiple operations can be added to a request and sent to the Ceph broker
 
392
    to be executed.
 
393
 
 
394
    Request is json-encoded for sending over the wire.
 
395
 
 
396
    The API is versioned and defaults to version 1.
 
397
    """
 
398
    def __init__(self, api_version=1):
 
399
        self.api_version = api_version
 
400
        self.ops = []
 
401
 
 
402
    def add_op_create_pool(self, name, replica_count=3):
 
403
        self.ops.append({'op': 'create-pool', 'name': name,
 
404
                         'replicas': replica_count})
 
405
 
 
406
    @property
 
407
    def request(self):
 
408
        return json.dumps({'api-version': self.api_version, 'ops': self.ops})
 
409
 
 
410
 
 
411
class CephBrokerRsp(object):
 
412
    """Ceph broker response.
 
413
 
 
414
    Response is json-decoded and contents provided as methods/properties.
 
415
 
 
416
    The API is versioned and defaults to version 1.
 
417
    """
 
418
    def __init__(self, encoded_rsp):
 
419
        self.api_version = None
 
420
        self.rsp = json.loads(encoded_rsp)
 
421
 
 
422
    @property
 
423
    def exit_code(self):
 
424
        return self.rsp.get('exit-code')
 
425
 
 
426
    @property
 
427
    def exit_msg(self):
 
428
        return self.rsp.get('stderr')