2
# Copyright 2012 Canonical Ltd.
4
# This file is sourced from lp:openstack-charm-helpers
7
# James Page <james.page@ubuntu.com>
8
# Adam Gandelman <adamg@ubuntu.com>
16
from subprocess import (
22
from charmhelpers.core.hookenv import (
32
from charmhelpers.core.host import (
41
from charmhelpers.fetch import (
45
KEYRING = '/etc/ceph/ceph.client.{}.keyring'
46
KEYFILE = '/etc/ceph/ceph.client.{}.key'
48
CEPH_CONF = """[global]
49
auth supported = {auth}
51
mon host = {mon_hosts}
56
''' Basic Ceph client installation '''
57
ceph_dir = "/etc/ceph"
58
if not os.path.exists(ceph_dir):
60
apt_install('ceph-common', fatal=True)
63
def rbd_exists(service, pool, rbd_img):
64
''' Check to see if a RADOS block device exists '''
66
out = check_output(['rbd', 'list', '--id', service,
68
except CalledProcessError:
74
def create_rbd_image(service, pool, image, sizemb):
75
''' Create a new RADOS block device '''
90
def pool_exists(service, name):
91
''' Check to see if a RADOS pool already exists '''
93
out = check_output(['rados', '--id', service, 'lspools'])
94
except CalledProcessError:
100
def get_osds(service):
102
Return a list of all Ceph Object Storage Daemons
103
currently in the cluster
105
return json.loads(check_output(['ceph', '--id', service,
106
'osd', 'ls', '--format=json']))
109
def create_pool(service, name, replicas=2):
110
''' Create a new RADOS pool '''
111
if pool_exists(service, name):
112
log("Ceph pool {} already exists, skipping creation".format(name),
115
# Calculate the number of placement groups based
116
# on upstream recommended best practices.
117
pgnum = (len(get_osds(service)) * 100 / replicas)
119
'ceph', '--id', service,
120
'osd', 'pool', 'create',
125
'ceph', '--id', service,
126
'osd', 'pool', 'set', name,
127
'size', str(replicas)
132
def delete_pool(service, name):
133
''' Delete a RADOS pool from ceph '''
135
'ceph', '--id', service,
136
'osd', 'pool', 'delete',
137
name, '--yes-i-really-really-mean-it'
142
def _keyfile_path(service):
143
return KEYFILE.format(service)
146
def _keyring_path(service):
147
return KEYRING.format(service)
150
def create_keyring(service, key):
151
''' Create a new Ceph keyring containing key'''
152
keyring = _keyring_path(service)
153
if os.path.exists(keyring):
154
log('ceph: Keyring exists at %s.' % keyring, level=WARNING)
160
'--name=client.{}'.format(service),
161
'--add-key={}'.format(key)
164
log('ceph: Created new ring at %s.' % keyring, level=INFO)
167
def create_key_file(service, key):
168
''' Create a file containing key '''
169
keyfile = _keyfile_path(service)
170
if os.path.exists(keyfile):
171
log('ceph: Keyfile exists at %s.' % keyfile, level=WARNING)
173
with open(keyfile, 'w') as fd:
175
log('ceph: Created new keyfile at %s.' % keyfile, level=INFO)
178
def get_ceph_nodes():
179
''' Query named relation 'ceph' to detemine current nodes '''
181
for r_id in relation_ids('ceph'):
182
for unit in related_units(r_id):
183
hosts.append(relation_get('private-address', unit=unit, rid=r_id))
187
def configure(service, key, auth):
188
''' Perform basic configuration of Ceph '''
189
create_keyring(service, key)
190
create_key_file(service, key)
191
hosts = get_ceph_nodes()
192
with open('/etc/ceph/ceph.conf', 'w') as ceph_conf:
193
ceph_conf.write(CEPH_CONF.format(auth=auth,
194
keyring=_keyring_path(service),
195
mon_hosts=",".join(map(str, hosts))))
199
def image_mapped(name):
200
''' Determine whether a RADOS block device is mapped locally '''
202
out = check_output(['rbd', 'showmapped'])
203
except CalledProcessError:
209
def map_block_storage(service, pool, image):
210
''' Map a RADOS block device for local use '''
214
'{}/{}'.format(pool, image),
218
_keyfile_path(service),
223
def filesystem_mounted(fs):
224
''' Determine whether a filesytems is already mounted '''
225
return fs in [f for f, m in mounts()]
228
def make_filesystem(blk_device, fstype='ext4', timeout=10):
229
''' Make a new filesystem on the specified block device '''
231
e_noent = os.errno.ENOENT
232
while not os.path.exists(blk_device):
234
log('ceph: gave up waiting on block device %s' % blk_device,
236
raise IOError(e_noent, os.strerror(e_noent), blk_device)
237
log('ceph: waiting for block device %s to appear' % blk_device,
242
log('ceph: Formatting block device %s as filesystem %s.' %
243
(blk_device, fstype), level=INFO)
244
check_call(['mkfs', '-t', fstype, blk_device])
247
def place_data_on_block_device(blk_device, data_src_dst):
248
''' Migrate data in data_src_dst to blk_device and then remount '''
249
# mount block device into /mnt
250
mount(blk_device, '/mnt')
252
copy_files(data_src_dst, '/mnt')
253
# umount block device
255
# Grab user/group ID's from original source
256
_dir = os.stat(data_src_dst)
259
# re-mount where the data should originally be
260
# TODO: persist is currently a NO-OP in core.host
261
mount(blk_device, data_src_dst, persist=True)
262
# ensure original ownership of new mount.
263
os.chown(data_src_dst, uid, gid)
267
def modprobe(module):
268
''' Load a kernel module and configure for auto-load on reboot '''
269
log('ceph: Loading kernel module', level=INFO)
270
cmd = ['modprobe', module]
272
with open('/etc/modules', 'r+') as modules:
273
if module not in modules.read():
274
modules.write(module)
277
def copy_files(src, dst, symlinks=False, ignore=None):
278
''' Copy files from src to dst '''
279
for item in os.listdir(src):
280
s = os.path.join(src, item)
281
d = os.path.join(dst, item)
283
shutil.copytree(s, d, symlinks, ignore)
288
def ensure_ceph_storage(service, pool, rbd_img, sizemb, mount_point,
289
blk_device, fstype, system_services=[]):
291
NOTE: This function must only be called from a single service unit for
292
the same rbd_img otherwise data loss will occur.
294
Ensures given pool and RBD image exists, is mapped to a block device,
295
and the device is formatted and mounted at the given mount_point.
297
If formatting a device for the first time, data existing at mount_point
298
will be migrated to the RBD device before being re-mounted.
300
All services listed in system_services will be stopped prior to data
301
migration and restarted when complete.
303
# Ensure pool, RBD image, RBD mappings are in place.
304
if not pool_exists(service, pool):
305
log('ceph: Creating new pool {}.'.format(pool))
306
create_pool(service, pool)
308
if not rbd_exists(service, pool, rbd_img):
309
log('ceph: Creating RBD image ({}).'.format(rbd_img))
310
create_rbd_image(service, pool, rbd_img, sizemb)
312
if not image_mapped(rbd_img):
313
log('ceph: Mapping RBD Image {} as a Block Device.'.format(rbd_img))
314
map_block_storage(service, pool, rbd_img)
317
# TODO: What happens if for whatever reason this is run again and
318
# the data is already in the rbd device and/or is mounted??
319
# When it is mounted already, it will fail to make the fs
320
# XXX: This is really sketchy! Need to at least add an fstab entry
321
# otherwise this hook will blow away existing data if its executed
323
if not filesystem_mounted(mount_point):
324
make_filesystem(blk_device, fstype)
326
for svc in system_services:
327
if service_running(svc):
328
log('ceph: Stopping services {} prior to migrating data.'
332
place_data_on_block_device(blk_device, mount_point)
334
for svc in system_services:
335
log('ceph: Starting service {} after migrating data.'
340
def ensure_ceph_keyring(service, user=None, group=None):
342
Ensures a ceph keyring is created for a named service
343
and optionally ensures user and group ownership.
345
Returns False if no ceph key is available in relation state.
348
for rid in relation_ids('ceph'):
349
for unit in related_units(rid):
350
key = relation_get('key', rid=rid, unit=unit)
355
create_keyring(service=service, key=key)
356
keyring = _keyring_path(service)
358
check_call(['chown', '%s.%s' % (user, group), keyring])