25
53
shutil.copy(x, '/etc/init/')
56
@hooks.hook('install')
29
utils.juju_log('INFO', 'Begin install hook.')
30
utils.configure_source()
31
utils.install('ceph', 'gdisk', 'ntp', 'btrfs-tools', 'python-ceph', 'xfsprogs')
58
log('Begin install hook.')
60
apt_update(fatal=True)
61
apt_install(packages=ceph.PACKAGES, fatal=True)
32
62
install_upstart_scripts()
33
utils.juju_log('INFO', 'End install hook.')
63
log('End install hook.')
36
66
def emit_cephconf():
38
'auth_supported': utils.config_get('auth-supported'),
68
'auth_supported': config('auth-supported'),
39
69
'mon_hosts': ' '.join(get_mon_hosts()),
40
'fsid': utils.config_get('fsid'),
70
'fsid': config('fsid'),
41
71
'version': ceph.get_ceph_version()
44
74
with open('/etc/ceph/ceph.conf', 'w') as cephconf:
45
cephconf.write(utils.render_template('ceph.conf', cephcontext))
75
cephconf.write(render_template('ceph.conf', cephcontext))
47
77
JOURNAL_ZAPPED = '/var/lib/ceph/journal_zapped'
80
@hooks.hook('config-changed')
50
81
def config_changed():
51
utils.juju_log('INFO', 'Begin config-changed hook.')
82
log('Begin config-changed hook.')
53
utils.juju_log('INFO', 'Monitor hosts are ' + repr(get_mon_hosts()))
84
log('Monitor hosts are ' + repr(get_mon_hosts()))
55
86
# Pre-flight checks
56
if not utils.config_get('fsid'):
57
utils.juju_log('CRITICAL', 'No fsid supplied, cannot proceed.')
59
if not utils.config_get('monitor-secret'):
60
utils.juju_log('CRITICAL',
61
'No monitor-secret supplied, cannot proceed.')
63
if utils.config_get('osd-format') not in ceph.DISK_FORMATS:
64
utils.juju_log('CRITICAL',
65
'Invalid OSD disk format configuration specified')
87
if not config('fsid'):
88
log('No fsid supplied, cannot proceed.', level=ERROR)
90
if not config('monitor-secret'):
91
log('No monitor-secret supplied, cannot proceed.', level=ERROR)
93
if config('osd-format') not in ceph.DISK_FORMATS:
94
log('Invalid OSD disk format configuration specified', level=ERROR)
70
e_mountpoint = utils.config_get('ephemeral-unmount')
99
e_mountpoint = config('ephemeral-unmount')
71
100
if (e_mountpoint and
72
101
filesystem_mounted(e_mountpoint)):
73
102
subprocess.call(['umount', e_mountpoint])
75
osd_journal = utils.config_get('osd-journal')
104
osd_journal = config('osd-journal')
76
105
if (osd_journal and
77
106
not os.path.exists(JOURNAL_ZAPPED) and
78
107
os.path.exists(osd_journal)):
80
109
with open(JOURNAL_ZAPPED, 'w') as zapped:
81
110
zapped.write('DONE')
83
for dev in utils.config_get('osd-devices').split(' '):
112
for dev in config('osd-devices').split(' '):
86
115
# Support use of single node ceph
87
116
if (not ceph.is_bootstrapped() and
88
int(utils.config_get('monitor-count')) == 1):
117
int(config('monitor-count')) == 1):
89
118
bootstrap_monitor_cluster()
90
119
ceph.wait_for_bootstrap()
92
121
if ceph.is_bootstrapped():
93
122
ceph.rescan_osd_devices()
95
utils.juju_log('INFO', 'End config-changed hook.')
124
log('End config-changed hook.')
98
127
def get_mon_hosts():
100
hosts.append('{}:6789'.format(utils.get_host_ip()))
129
hosts.append('{}:6789'.format(get_host_ip()))
102
for relid in utils.relation_ids('mon'):
103
for unit in utils.relation_list(relid):
131
for relid in relation_ids('mon'):
132
for unit in related_units(relid):
105
'{}:6789'.format(utils.get_host_ip(
106
utils.relation_get('private-address',
134
'{}:6789'.format(get_host_ip(
135
relation_get('private-address',
226
253
notify_radosgws()
229
utils.juju_log('INFO',
230
'Not enough mons ({}), punting.'.format(
256
log('Not enough mons ({}), punting.'.format(
231
257
len(get_mon_hosts())))
233
utils.juju_log('INFO', 'End mon-relation hook.')
259
log('End mon-relation hook.')
236
262
def notify_osds():
237
utils.juju_log('INFO', 'Begin notify_osds.')
239
for relid in utils.relation_ids('osd'):
240
utils.relation_set(fsid=utils.config_get('fsid'),
241
osd_bootstrap_key=ceph.get_osd_bootstrap_key(),
242
auth=utils.config_get('auth-supported'),
245
utils.juju_log('INFO', 'End notify_osds.')
263
log('Begin notify_osds.')
265
for relid in relation_ids('osd'):
266
relation_set(relation_id=relid,
268
osd_bootstrap_key=ceph.get_osd_bootstrap_key(),
269
auth=config('auth-supported'))
271
log('End notify_osds.')
248
274
def notify_radosgws():
249
utils.juju_log('INFO', 'Begin notify_radosgws.')
251
for relid in utils.relation_ids('radosgw'):
252
utils.relation_set(radosgw_key=ceph.get_radosgw_key(),
253
auth=utils.config_get('auth-supported'),
256
utils.juju_log('INFO', 'End notify_radosgws.')
275
log('Begin notify_radosgws.')
277
for relid in relation_ids('radosgw'):
278
relation_set(relation_id=relid,
279
radosgw_key=ceph.get_radosgw_key(),
280
auth=config('auth-supported'))
282
log('End notify_radosgws.')
259
285
def notify_client():
260
utils.juju_log('INFO', 'Begin notify_client.')
286
log('Begin notify_client.')
262
for relid in utils.relation_ids('client'):
263
units = utils.relation_list(relid)
288
for relid in relation_ids('client'):
289
units = related_units(relid)
264
290
if len(units) > 0:
265
291
service_name = units[0].split('/')[0]
266
utils.relation_set(key=ceph.get_named_key(service_name),
267
auth=utils.config_get('auth-supported'),
270
utils.juju_log('INFO', 'End notify_client.')
292
relation_set(relation_id=relid,
293
key=ceph.get_named_key(service_name),
294
auth=config('auth-supported'))
296
log('End notify_client.')
299
@hooks.hook('osd-relation-joined')
273
300
def osd_relation():
274
utils.juju_log('INFO', 'Begin osd-relation hook.')
301
log('Begin osd-relation hook.')
276
303
if ceph.is_quorum():
277
utils.juju_log('INFO',
278
'mon cluster in quorum - providing fsid & keys')
279
utils.relation_set(fsid=utils.config_get('fsid'),
280
osd_bootstrap_key=ceph.get_osd_bootstrap_key(),
281
auth=utils.config_get('auth-supported'))
304
log('mon cluster in quorum - providing fsid & keys')
305
relation_set(fsid=config('fsid'),
306
osd_bootstrap_key=ceph.get_osd_bootstrap_key(),
307
auth=config('auth-supported'))
283
utils.juju_log('INFO',
284
'mon cluster not in quorum - deferring fsid provision')
286
utils.juju_log('INFO', 'End osd-relation hook.')
309
log('mon cluster not in quorum - deferring fsid provision')
311
log('End osd-relation hook.')
314
@hooks.hook('radosgw-relation-joined')
289
315
def radosgw_relation():
290
utils.juju_log('INFO', 'Begin radosgw-relation hook.')
316
log('Begin radosgw-relation hook.')
292
utils.install('radosgw') # Install radosgw for admin tools
318
# Install radosgw for admin tools
319
apt_install(packages=filter_installed_packages(['radosgw']))
294
321
if ceph.is_quorum():
295
utils.juju_log('INFO',
296
'mon cluster in quorum - \
297
providing radosgw with keys')
298
utils.relation_set(radosgw_key=ceph.get_radosgw_key(),
299
auth=utils.config_get('auth-supported'))
322
log('mon cluster in quorum - providing radosgw with keys')
323
relation_set(radosgw_key=ceph.get_radosgw_key(),
324
auth=config('auth-supported'))
301
utils.juju_log('INFO',
302
'mon cluster not in quorum - deferring key provision')
304
utils.juju_log('INFO', 'End radosgw-relation hook.')
326
log('mon cluster not in quorum - deferring key provision')
328
log('End radosgw-relation hook.')
331
@hooks.hook('client-relation-joined')
307
332
def client_relation():
308
utils.juju_log('INFO', 'Begin client-relation hook.')
333
log('Begin client-relation hook.')
310
335
if ceph.is_quorum():
311
utils.juju_log('INFO',
312
'mon cluster in quorum - \
313
providing client with keys')
314
service_name = os.environ['JUJU_REMOTE_UNIT'].split('/')[0]
315
utils.relation_set(key=ceph.get_named_key(service_name),
316
auth=utils.config_get('auth-supported'))
336
log('mon cluster in quorum - providing client with keys')
337
service_name = remote_unit().split('/')[0]
338
relation_set(key=ceph.get_named_key(service_name),
339
auth=config('auth-supported'))
318
utils.juju_log('INFO',
319
'mon cluster not in quorum - deferring key provision')
321
utils.juju_log('INFO', 'End client-relation hook.')
341
log('mon cluster not in quorum - deferring key provision')
343
log('End client-relation hook.')
346
@hooks.hook('upgrade-charm')
324
347
def upgrade_charm():
325
utils.juju_log('INFO', 'Begin upgrade-charm hook.')
348
log('Begin upgrade-charm hook.')
327
utils.install('xfsprogs')
350
apt_install(packages=filter_installed_packages(ceph.PACKAGES), fatal=True)
328
351
install_upstart_scripts()
330
utils.juju_log('INFO', 'End upgrade-charm hook.')
353
log('End upgrade-charm hook.')
334
358
# In case we're being redeployed to the same machines, try
335
359
# to make sure everything is running as soon as possible.
336
subprocess.call(['start', 'ceph-mon-all-starter'])
360
subprocess.call(['start', 'ceph-mon-all'])
337
361
ceph.rescan_osd_devices()
341
'config-changed': config_changed,
343
'mon-relation-departed': mon_relation,
344
'mon-relation-joined': mon_relation,
345
'osd-relation-joined': osd_relation,
346
'radosgw-relation-joined': radosgw_relation,
347
'client-relation-joined': client_relation,
349
'upgrade-charm': upgrade_charm,
365
hooks.execute(sys.argv)
366
except UnregisteredHookError as e:
367
log('Unknown hook {} - skipping.'.format(e))