~lamont/charms/trusty/bind-resolver/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/hahelpers/ceph_utils.py

  • Committer: LaMont Jones
  • Date: 2014-10-04 05:19:46 UTC
  • Revision ID: lamont@mmjgroup.com-20141004051946-8fa32giu9qwpmcmu
initial commit

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 commands
 
12
import subprocess
 
13
import os
 
14
import shutil
 
15
import utils
 
16
 
 
17
KEYRING = '/etc/ceph/ceph.client.%s.keyring'
 
18
KEYFILE = '/etc/ceph/ceph.client.%s.key'
 
19
 
 
20
CEPH_CONF = """[global]
 
21
 auth supported = %(auth)s
 
22
 keyring = %(keyring)s
 
23
 mon host = %(mon_hosts)s
 
24
"""
 
25
 
 
26
 
 
27
def execute(cmd):
 
28
    subprocess.check_call(cmd)
 
29
 
 
30
 
 
31
def execute_shell(cmd):
 
32
    subprocess.check_call(cmd, shell=True)
 
33
 
 
34
 
 
35
def install():
 
36
    ceph_dir = "/etc/ceph"
 
37
    if not os.path.isdir(ceph_dir):
 
38
        os.mkdir(ceph_dir)
 
39
    utils.install('ceph-common')
 
40
 
 
41
 
 
42
def rbd_exists(service, pool, rbd_img):
 
43
    (rc, out) = commands.getstatusoutput('rbd list --id %s --pool %s' %\
 
44
                                         (service, pool))
 
45
    return rbd_img in out
 
46
 
 
47
 
 
48
def create_rbd_image(service, pool, image, sizemb):
 
49
    cmd = [
 
50
        'rbd',
 
51
        'create',
 
52
        image,
 
53
        '--size',
 
54
        str(sizemb),
 
55
        '--id',
 
56
        service,
 
57
        '--pool',
 
58
        pool
 
59
        ]
 
60
    execute(cmd)
 
61
 
 
62
 
 
63
def pool_exists(service, name):
 
64
    (rc, out) = commands.getstatusoutput("rados --id %s lspools" % service)
 
65
    return name in out
 
66
 
 
67
 
 
68
def create_pool(service, name):
 
69
    cmd = [
 
70
        'rados',
 
71
        '--id',
 
72
        service,
 
73
        'mkpool',
 
74
        name
 
75
        ]
 
76
    execute(cmd)
 
77
 
 
78
 
 
79
def keyfile_path(service):
 
80
    return KEYFILE % service
 
81
 
 
82
 
 
83
def keyring_path(service):
 
84
    return KEYRING % service
 
85
 
 
86
 
 
87
def create_keyring(service, key):
 
88
    keyring = keyring_path(service)
 
89
    if os.path.exists(keyring):
 
90
        utils.juju_log('INFO', 'ceph: Keyring exists at %s.' % keyring)
 
91
    cmd = [
 
92
        'ceph-authtool',
 
93
        keyring,
 
94
        '--create-keyring',
 
95
        '--name=client.%s' % service,
 
96
        '--add-key=%s' % key
 
97
        ]
 
98
    execute(cmd)
 
99
    utils.juju_log('INFO', 'ceph: Created new ring at %s.' % keyring)
 
100
 
 
101
 
 
102
def create_key_file(service, key):
 
103
    # create a file containing the key
 
104
    keyfile = keyfile_path(service)
 
105
    if os.path.exists(keyfile):
 
106
        utils.juju_log('INFO', 'ceph: Keyfile exists at %s.' % keyfile)
 
107
    fd = open(keyfile, 'w')
 
108
    fd.write(key)
 
109
    fd.close()
 
110
    utils.juju_log('INFO', 'ceph: Created new keyfile at %s.' % keyfile)
 
111
 
 
112
 
 
113
def get_ceph_nodes():
 
114
    hosts = []
 
115
    for r_id in utils.relation_ids('ceph'):
 
116
        for unit in utils.relation_list(r_id):
 
117
            hosts.append(utils.relation_get('private-address',
 
118
                                            unit=unit, rid=r_id))
 
119
    return hosts
 
120
 
 
121
 
 
122
def configure(service, key, auth):
 
123
    create_keyring(service, key)
 
124
    create_key_file(service, key)
 
125
    hosts = get_ceph_nodes()
 
126
    mon_hosts = ",".join(map(str, hosts))
 
127
    keyring = keyring_path(service)
 
128
    with open('/etc/ceph/ceph.conf', 'w') as ceph_conf:
 
129
        ceph_conf.write(CEPH_CONF % locals())
 
130
    modprobe_kernel_module('rbd')
 
131
 
 
132
 
 
133
def image_mapped(image_name):
 
134
    (rc, out) = commands.getstatusoutput('rbd showmapped')
 
135
    return image_name in out
 
136
 
 
137
 
 
138
def map_block_storage(service, pool, image):
 
139
    cmd = [
 
140
        'rbd',
 
141
        'map',
 
142
        '%s/%s' % (pool, image),
 
143
        '--user',
 
144
        service,
 
145
        '--secret',
 
146
        keyfile_path(service),
 
147
        ]
 
148
    execute(cmd)
 
149
 
 
150
 
 
151
def filesystem_mounted(fs):
 
152
    return subprocess.call(['grep', '-wqs', fs, '/proc/mounts']) == 0
 
153
 
 
154
 
 
155
def make_filesystem(blk_device, fstype='ext4'):
 
156
    utils.juju_log('INFO',
 
157
                   'ceph: Formatting block device %s as filesystem %s.' %\
 
158
                   (blk_device, fstype))
 
159
    cmd = ['mkfs', '-t', fstype, blk_device]
 
160
    execute(cmd)
 
161
 
 
162
 
 
163
def place_data_on_ceph(service, blk_device, data_src_dst, fstype='ext4'):
 
164
    # mount block device into /mnt
 
165
    cmd = ['mount', '-t', fstype, blk_device, '/mnt']
 
166
    execute(cmd)
 
167
 
 
168
    # copy data to /mnt
 
169
    try:
 
170
        copy_files(data_src_dst, '/mnt')
 
171
    except:
 
172
        pass
 
173
 
 
174
    # umount block device
 
175
    cmd = ['umount', '/mnt']
 
176
    execute(cmd)
 
177
 
 
178
    _dir = os.stat(data_src_dst)
 
179
    uid = _dir.st_uid
 
180
    gid = _dir.st_gid
 
181
 
 
182
    # re-mount where the data should originally be
 
183
    cmd = ['mount', '-t', fstype, blk_device, data_src_dst]
 
184
    execute(cmd)
 
185
 
 
186
    # ensure original ownership of new mount.
 
187
    cmd = ['chown', '-R', '%s:%s' % (uid, gid), data_src_dst]
 
188
    execute(cmd)
 
189
 
 
190
 
 
191
# TODO: re-use
 
192
def modprobe_kernel_module(module):
 
193
    utils.juju_log('INFO', 'Loading kernel module')
 
194
    cmd = ['modprobe', module]
 
195
    execute(cmd)
 
196
    cmd = 'echo %s >> /etc/modules' % module
 
197
    execute_shell(cmd)
 
198
 
 
199
 
 
200
def copy_files(src, dst, symlinks=False, ignore=None):
 
201
    for item in os.listdir(src):
 
202
        s = os.path.join(src, item)
 
203
        d = os.path.join(dst, item)
 
204
        if os.path.isdir(s):
 
205
            shutil.copytree(s, d, symlinks, ignore)
 
206
        else:
 
207
            shutil.copy2(s, d)
 
208
 
 
209
 
 
210
def ensure_ceph_storage(service, pool, rbd_img, sizemb, mount_point,
 
211
                        blk_device, fstype, system_services=[]):
 
212
    """
 
213
    To be called from the current cluster leader.
 
214
    Ensures given pool and RBD image exists, is mapped to a block device,
 
215
    and the device is formatted and mounted at the given mount_point.
 
216
 
 
217
    If formatting a device for the first time, data existing at mount_point
 
218
    will be migrated to the RBD device before being remounted.
 
219
 
 
220
    All services listed in system_services will be stopped prior to data
 
221
    migration and restarted when complete.
 
222
    """
 
223
    # Ensure pool, RBD image, RBD mappings are in place.
 
224
    if not pool_exists(service, pool):
 
225
        utils.juju_log('INFO', 'ceph: Creating new pool %s.' % pool)
 
226
        create_pool(service, pool)
 
227
 
 
228
    if not rbd_exists(service, pool, rbd_img):
 
229
        utils.juju_log('INFO', 'ceph: Creating RBD image (%s).' % rbd_img)
 
230
        create_rbd_image(service, pool, rbd_img, sizemb)
 
231
 
 
232
    if not image_mapped(rbd_img):
 
233
        utils.juju_log('INFO', 'ceph: Mapping RBD Image as a Block Device.')
 
234
        map_block_storage(service, pool, rbd_img)
 
235
 
 
236
    # make file system
 
237
    # TODO: What happens if for whatever reason this is run again and
 
238
    # the data is already in the rbd device and/or is mounted??
 
239
    # When it is mounted already, it will fail to make the fs
 
240
    # XXX: This is really sketchy!  Need to at least add an fstab entry
 
241
    #      otherwise this hook will blow away existing data if its executed
 
242
    #      after a reboot.
 
243
    if not filesystem_mounted(mount_point):
 
244
        make_filesystem(blk_device, fstype)
 
245
 
 
246
        for svc in system_services:
 
247
            if utils.running(svc):
 
248
                utils.juju_log('INFO',
 
249
                               'Stopping services %s prior to migrating '\
 
250
                               'data' % svc)
 
251
                utils.stop(svc)
 
252
 
 
253
        place_data_on_ceph(service, blk_device, mount_point, fstype)
 
254
 
 
255
        for svc in system_services:
 
256
            utils.start(svc)