~niedbalski/charms/trusty/rabbitmq-server/fix-1378263

« back to all changes in this revision

Viewing changes to hooks/rabbitmq_server_relations.py

  • Committer: James Page
  • Date: 2014-03-05 12:57:20 UTC
  • mto: This revision was merged to the branch mainline in revision 52.
  • Revision ID: james.page@canonical.com-20140305125720-fekpo1kpvh5tl93h
Redux take 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import subprocess
7
7
import glob
8
8
 
9
 
 
10
9
import rabbit_utils as rabbit
11
 
import lib.utils as utils
12
 
import lib.cluster_utils as cluster
13
 
import lib.ceph_utils as ceph
14
 
import lib.openstack_common as openstack
 
10
from lib.utils import (
 
11
    chown, chmod,
 
12
    is_newer,
 
13
)
 
14
from charmhelpers.contrib.hahelpers.cluster import (
 
15
    is_clustered,
 
16
    eligible_leader
 
17
)
15
18
 
16
 
import _pythonpath
17
 
_ = _pythonpath
 
19
import charmhelpers.contrib.storage.linux.ceph as ceph
 
20
from charmhelpers.contrib.openstack.utils import save_script_rc
18
21
 
19
22
from charmhelpers.fetch import (
20
23
    add_source,
21
 
    apt_update)
22
 
from charmhelpers.core import hookenv
23
 
from charmhelpers.core.host import rsync, mkdir, pwgen
 
24
    apt_update,
 
25
    apt_install)
 
26
 
 
27
from charmhelpers.core.hookenv import (
 
28
    open_port, close_port,
 
29
    log, ERROR,
 
30
    relation_get,
 
31
    relation_set,
 
32
    relation_ids,
 
33
    related_units,
 
34
    local_unit,
 
35
    config,
 
36
    unit_get,
 
37
    is_relation_made,
 
38
    Hooks, UnregisteredHookError
 
39
)
 
40
from charmhelpers.core.host import (
 
41
    rsync, pwgen,
 
42
    service_stop, service_restart
 
43
)
24
44
from charmhelpers.contrib.charmsupport.nrpe import NRPE
25
45
from charmhelpers.contrib.ssl.service import ServiceCA
26
46
 
 
47
hooks = Hooks()
27
48
 
28
49
SERVICE_NAME = os.getenv('JUJU_UNIT_NAME').split('/')[0]
29
50
POOL_NAME = SERVICE_NAME
31
52
NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins'
32
53
 
33
54
 
 
55
@hooks.hook('install')
34
56
def install():
35
57
    pre_install_hooks()
36
 
    add_source(utils.config_get('source'), utils.config_get('key'))
 
58
    add_source(config('source'), config('key'))
37
59
    apt_update(fatal=True)
38
 
    utils.install(*rabbit.PACKAGES)
39
 
    utils.expose(5672)
40
 
    utils.chown(RABBIT_DIR, rabbit.RABBIT_USER, rabbit.RABBIT_USER)
41
 
    utils.chmod(RABBIT_DIR, 0775)
 
60
    apt_install(rabbit.PACKAGES, fatal=True)
 
61
    open_port(5672)
 
62
    chown(RABBIT_DIR, rabbit.RABBIT_USER, rabbit.RABBIT_USER)
 
63
    chmod(RABBIT_DIR, 0o775)
42
64
 
43
65
 
44
66
def configure_amqp(username, vhost):
56
78
 
57
79
    return password
58
80
 
 
81
 
 
82
@hooks.hook('amqp-relation-changed')
59
83
def amqp_changed(relation_id=None, remote_unit=None):
60
 
    if not cluster.eligible_leader('res_rabbitmq_vip'):
61
 
        utils.juju_log('INFO',
62
 
                       'amqp_changed(): Deferring amqp_changed'
63
 
                       ' to eligible_leader.')
 
84
    if not eligible_leader('res_rabbitmq_vip'):
 
85
        log('amqp_changed(): Deferring amqp_changed'
 
86
            ' to eligible_leader.')
64
87
        return
65
88
 
66
89
    relation_settings = {}
67
 
    settings = hookenv.relation_get(rid=relation_id, unit=remote_unit)
 
90
    settings = relation_get(rid=relation_id, unit=remote_unit)
68
91
 
69
92
    singleset = set(['username', 'vhost'])
70
93
 
71
94
    if singleset.issubset(settings):
72
95
        if None in [settings['username'], settings['vhost']]:
73
 
            utils.juju_log('INFO', 'amqp_changed(): Relation not ready.')
 
96
            log('amqp_changed(): Relation not ready.')
74
97
            return
75
98
 
76
99
        relation_settings['password'] = configure_amqp(
88
111
            if singleset.issubset(queues[amqp]):
89
112
                relation_settings[
90
113
                    '_'.join([amqp, 'password'])] = configure_amqp(
91
 
                        queues[amqp]['username'],
92
 
                        queues[amqp]['vhost'])
 
114
                    queues[amqp]['username'],
 
115
                    queues[amqp]['vhost'])
93
116
 
94
 
    relation_settings['hostname'] = utils.unit_get('private-address')
 
117
    relation_settings['hostname'] = unit_get('private-address')
95
118
    configure_client_ssl(relation_settings)
96
119
 
97
 
    if cluster.is_clustered():
 
120
    if is_clustered():
98
121
        relation_settings['clustered'] = 'true'
99
 
        if utils.is_relation_made('ha'):
 
122
        if is_relation_made('ha'):
100
123
            # active/passive settings
101
 
            relation_settings['vip'] = utils.config_get('vip')
102
 
            relation_settings['ha-vip-only'] = utils.config_get('ha-vip-only')
 
124
            relation_settings['vip'] = config('vip')
 
125
            relation_settings['ha-vip-only'] = config('ha-vip-only')
103
126
 
104
127
    if relation_id:
105
128
        relation_settings['rid'] = relation_id
106
129
 
107
130
    # set if need HA queues or not
108
131
    relation_settings['ha_queues'] = (rabbit.compare_version('3.0.1') >= 0)
109
 
    utils.relation_set(**relation_settings)
110
 
 
111
 
 
 
132
    relation_set(relation_settings=relation_settings)
 
133
 
 
134
 
 
135
@hooks.hook('cluster-relation-joined')
112
136
def cluster_joined():
113
 
    if utils.is_relation_made('ha') and \
114
 
            utils.config_get('ha-vip-only') is False:
115
 
        utils.juju_log('INFO',
116
 
                       'hacluster relation is present, skipping native '
117
 
                       'rabbitmq cluster config.')
 
137
    if is_relation_made('ha') and \
 
138
            config('ha-vip-only') is False:
 
139
        log('hacluster relation is present, skipping native '
 
140
            'rabbitmq cluster config.')
118
141
        return
119
142
 
120
 
    if utils.is_newer():
121
 
        utils.juju_log('INFO', 'cluster_joined: Relation greater.')
 
143
    if is_newer():
 
144
        log('cluster_joined: Relation greater.')
122
145
        return
123
146
 
124
147
    rabbit.COOKIE_PATH = '/var/lib/rabbitmq/.erlang.cookie'
125
148
    if not os.path.isfile(rabbit.COOKIE_PATH):
126
 
        utils.juju_log('ERROR', 'erlang cookie missing from %s' %
127
 
                       rabbit.COOKIE_PATH)
 
149
        log('erlang cookie missing from %s' % rabbit.COOKIE_PATH,
 
150
            level=ERROR)
128
151
        return
129
152
    cookie = open(rabbit.COOKIE_PATH, 'r').read().strip()
130
153
    rabbit.set_clustered_attribute('cookie', cookie)
131
154
 
132
155
 
 
156
@hooks.hook('cluster-relation-changed')
133
157
def cluster_changed():
134
158
    # sync passwords
135
 
    rdata = hookenv.relation_get()
 
159
    rdata = relation_get()
136
160
    echo_data = {}
137
161
    for attribute, value in rdata.iteritems():
138
162
        if '.passwd' in attribute or attribute == 'cookie':
139
163
            echo_data[attribute] = value
140
164
    if len(echo_data) > 0:
141
 
        hookenv.relation_set(relation_settings=echo_data)
 
165
        relation_set(relation_settings=echo_data)
142
166
 
143
167
    if 'cookie' not in echo_data:
144
 
        utils.juju_log('INFO',
145
 
                       'cluster_joined: cookie not yet set.')
 
168
        log('cluster_joined: cookie not yet set.')
146
169
        return
147
170
 
148
171
    # sync cookie
149
172
    cookie = echo_data['cookie']
150
173
    if open(rabbit.COOKIE_PATH, 'r').read().strip() == cookie:
151
 
        utils.juju_log('INFO', 'Cookie already synchronized with peer.')
 
174
        log('Cookie already synchronized with peer.')
152
175
    else:
153
 
        utils.juju_log('INFO', 'Synchronizing erlang cookie from peer.')
 
176
        log('Synchronizing erlang cookie from peer.')
154
177
        rabbit.service('stop')
155
178
        with open(rabbit.COOKIE_PATH, 'wb') as out:
156
179
            out.write(cookie)
157
180
        rabbit.service('start')
158
181
 
159
 
    if utils.is_relation_made('ha') and \
160
 
            utils.config_get('ha-vip-only') is False:
161
 
        utils.juju_log('INFO',
162
 
                       'hacluster relation is present, skipping native '
163
 
                       'rabbitmq cluster config.')
 
182
    if is_relation_made('ha') and \
 
183
            config('ha-vip-only') is False:
 
184
        log('hacluster relation is present, skipping native '
 
185
            'rabbitmq cluster config.')
164
186
        return
165
187
 
166
188
    # cluster with node
167
 
    if utils.is_newer():
 
189
    if is_newer():
168
190
        if rabbit.cluster_with():
169
191
            # resync nrpe user after clustering
170
192
            update_nrpe_checks()
171
193
 
172
194
 
 
195
@hooks.hook('cluster-relation-departed')
173
196
def cluster_departed():
174
 
    if utils.is_relation_made('ha') and \
175
 
            utils.config_get('ha-vip-only') is False:
176
 
        utils.juju_log('INFO',
177
 
                       'hacluster relation is present, skipping native '
178
 
                       'rabbitmq cluster config.')
 
197
    if is_relation_made('ha') and \
 
198
            config('ha-vip-only') is False:
 
199
        log('hacluster relation is present, skipping native '
 
200
            'rabbitmq cluster config.')
179
201
        return
180
 
    if not utils.is_newer():
181
 
        utils.juju_log('INFO', 'cluster_joined: Relation lesser.')
 
202
    if not is_newer():
 
203
        log('cluster_joined: Relation lesser.')
182
204
        return
183
205
    rabbit.break_cluster()
184
206
 
185
207
 
 
208
@hooks.hook('ha-relation-joined')
186
209
def ha_joined():
187
 
    corosync_bindiface = utils.config_get('ha-bindiface')
188
 
    corosync_mcastport = utils.config_get('ha-mcastport')
189
 
    vip = utils.config_get('vip')
190
 
    vip_iface = utils.config_get('vip_iface')
191
 
    vip_cidr = utils.config_get('vip_cidr')
192
 
    rbd_name = utils.config_get('rbd-name')
193
 
    vip_only = utils.config_get('ha-vip-only')
 
210
    corosync_bindiface = config('ha-bindiface')
 
211
    corosync_mcastport = config('ha-mcastport')
 
212
    vip = config('vip')
 
213
    vip_iface = config('vip_iface')
 
214
    vip_cidr = config('vip_cidr')
 
215
    rbd_name = config('rbd-name')
 
216
    vip_only = config('ha-vip-only')
194
217
 
195
218
    if None in [corosync_bindiface, corosync_mcastport, vip, vip_iface,
196
219
                vip_cidr, rbd_name] and vip_only is False:
197
 
        utils.juju_log('ERROR', 'Insufficient configuration data to '
198
 
                       'configure hacluster.')
 
220
        log('Insufficient configuration data to configure hacluster.',
 
221
            level=ERROR)
199
222
        sys.exit(1)
200
223
    elif None in [corosync_bindiface, corosync_mcastport, vip, vip_iface,
201
224
                  vip_cidr] and vip_only is True:
202
 
        utils.juju_log('ERROR', 'Insufficient configuration data to '
203
 
                       'configure VIP-only hacluster.')
 
225
        log('Insufficient configuration data to configure VIP-only hacluster.',
 
226
            level=ERROR)
204
227
        sys.exit(1)
205
228
 
206
 
    if not utils.is_relation_made('ceph', 'auth') and vip_only is False:
207
 
        utils.juju_log('INFO',
208
 
                       'ha_joined: No ceph relation yet, deferring.')
 
229
    if not is_relation_made('ceph', 'auth') and vip_only is False:
 
230
        log('ha_joined: No ceph relation yet, deferring.')
209
231
        return
210
232
 
211
233
    name = '%s@localhost' % SERVICE_NAME
212
234
    if rabbit.get_node_name() != name and vip_only is False:
213
 
        utils.juju_log('INFO', 'Stopping rabbitmq-server.')
214
 
        utils.stop('rabbitmq-server')
 
235
        log('Stopping rabbitmq-server.')
 
236
        service_stop('rabbitmq-server')
215
237
        rabbit.set_node_name('%s@localhost' % SERVICE_NAME)
216
238
    else:
217
 
        utils.juju_log('INFO', 'Node name already set to %s.' % name)
 
239
        log('Node name already set to %s.' % name)
218
240
 
219
241
    relation_settings = {}
220
242
    relation_settings['corosync_bindiface'] = corosync_bindiface
240
262
            'res_rabbitmq_rbd': 'params name="%s" pool="%s" user="%s" '
241
263
                                'secret="%s"' %
242
264
                                (rbd_name, POOL_NAME,
243
 
                                 SERVICE_NAME, ceph.keyfile_path(SERVICE_NAME)),
 
265
                                 SERVICE_NAME, ceph._keyfile_path(
 
266
                                     SERVICE_NAME)),
244
267
            'res_rabbitmq_fs': 'params device="/dev/rbd/%s/%s" directory="%s" '
245
268
                               'fstype="ext4" op start start-delay="10s"' %
246
269
                               (POOL_NAME, rbd_name, RABBIT_DIR),
251
274
        }
252
275
 
253
276
        relation_settings['groups'] = {
254
 
            'grp_rabbitmq': 'res_rabbitmq_rbd res_rabbitmq_fs res_rabbitmq_vip '
255
 
                            'res_rabbitmq-server',
 
277
            'grp_rabbitmq':
 
278
            'res_rabbitmq_rbd res_rabbitmq_fs res_rabbitmq_vip '
 
279
            'res_rabbitmq-server',
256
280
        }
257
281
 
258
 
    for rel_id in utils.relation_ids('ha'):
259
 
        utils.relation_set(rid=rel_id, **relation_settings)
 
282
    for rel_id in relation_ids('ha'):
 
283
        relation_set(relation_id=rel_id, relation_settings=relation_settings)
260
284
 
261
285
    env_vars = {
262
286
        'OPENSTACK_PORT_EPMD': 4369,
263
 
        'OPENSTACK_PORT_MCASTPORT': utils.config_get('ha-mcastport'),
 
287
        'OPENSTACK_PORT_MCASTPORT': config('ha-mcastport'),
264
288
    }
265
 
    openstack.save_script_rc(**env_vars)
266
 
 
267
 
 
 
289
    save_script_rc(**env_vars)
 
290
 
 
291
 
 
292
@hooks.hook('ha-relation-changed')
268
293
def ha_changed():
269
 
    if not cluster.is_clustered():
 
294
    if not is_clustered():
270
295
        return
271
 
    vip = utils.config_get('vip')
272
 
    utils.juju_log('INFO', 'ha_changed(): We are now HA clustered. '
 
296
    vip = config('vip')
 
297
    log('ha_changed(): We are now HA clustered. '
273
298
                   'Advertising our VIP (%s) to all AMQP clients.' %
274
299
                   vip)
275
300
    # need to re-authenticate all clients since node-name changed.
276
 
    for rid in utils.relation_ids('amqp'):
277
 
        for unit in utils.relation_list(rid):
 
301
    for rid in relation_ids('amqp'):
 
302
        for unit in related_units(rid):
278
303
            amqp_changed(relation_id=rid, remote_unit=unit)
279
304
 
280
305
 
 
306
@hooks.hook('ceph-relation-joined')
281
307
def ceph_joined():
282
 
    utils.juju_log('INFO', 'Start Ceph Relation Joined')
283
 
    utils.configure_source()
 
308
    log('Start Ceph Relation Joined')
 
309
    #NOTE fixup
 
310
    #utils.configure_source()
284
311
    ceph.install()
285
 
    utils.juju_log('INFO', 'Finish Ceph Relation Joined')
286
 
 
287
 
 
 
312
    log('Finish Ceph Relation Joined')
 
313
 
 
314
 
 
315
@hooks.hook('ceph-relation-changed')
288
316
def ceph_changed():
289
 
    utils.juju_log('INFO', 'Start Ceph Relation Changed')
290
 
    auth = utils.relation_get('auth')
291
 
    key = utils.relation_get('key')
292
 
    use_syslog = str(utils.config_get('use-syslog')).lower()
 
317
    log('Start Ceph Relation Changed')
 
318
    auth = relation_get('auth')
 
319
    key = relation_get('key')
 
320
    use_syslog = str(config('use-syslog')).lower()
293
321
    if None in [auth, key]:
294
 
        utils.juju_log('INFO', 'Missing key or auth in relation')
 
322
        log('Missing key or auth in relation')
295
323
        sys.exit(0)
296
324
 
297
325
    ceph.configure(service=SERVICE_NAME, key=key, auth=auth,
298
326
                   use_syslog=use_syslog)
299
327
 
300
 
    if cluster.eligible_leader('res_rabbitmq_vip'):
301
 
        rbd_img = utils.config_get('rbd-name')
302
 
        rbd_size = utils.config_get('rbd-size')
 
328
    if eligible_leader('res_rabbitmq_vip'):
 
329
        rbd_img = config('rbd-name')
 
330
        rbd_size = config('rbd-size')
303
331
        sizemb = int(rbd_size.split('G')[0]) * 1024
304
332
        blk_device = '/dev/rbd/%s/%s' % (POOL_NAME, rbd_img)
305
 
        rbd_pool_rep_count = utils.config_get('ceph-osd-replication-count')
 
333
        rbd_pool_rep_count = config('ceph-osd-replication-count')
306
334
        ceph.ensure_ceph_storage(service=SERVICE_NAME, pool=POOL_NAME,
307
335
                                 rbd_img=rbd_img, sizemb=sizemb,
308
336
                                 fstype='ext4', mount_point=RABBIT_DIR,
310
338
                                 system_services=['rabbitmq-server'],
311
339
                                 rbd_pool_replicas=rbd_pool_rep_count)
312
340
    else:
313
 
        utils.juju_log('INFO',
314
 
                       'This is not the peer leader. Not configuring RBD.')
315
 
        utils.juju_log('INFO', 'Stopping rabbitmq-server.')
316
 
        utils.stop('rabbitmq-server')
 
341
        log('This is not the peer leader. Not configuring RBD.')
 
342
        log('Stopping rabbitmq-server.')
 
343
        service_stop('rabbitmq-server')
317
344
 
318
345
    # If 'ha' relation has been made before the 'ceph' relation
319
346
    # it is important to make sure the ha-relation data is being
320
347
    # sent.
321
 
    if utils.is_relation_made('ha'):
322
 
        utils.juju_log('INFO', '*ha* relation exists. Triggering ha_joined()')
 
348
    if is_relation_made('ha'):
 
349
        log('*ha* relation exists. Triggering ha_joined()')
323
350
        ha_joined()
324
351
    else:
325
 
        utils.juju_log('INFO', '*ha* relation does not exist.')
326
 
    utils.juju_log('INFO', 'Finish Ceph Relation Changed')
327
 
 
328
 
 
 
352
        log('*ha* relation does not exist.')
 
353
    log('Finish Ceph Relation Changed')
 
354
 
 
355
 
 
356
@hooks.hook('nrpe-external-master-relation-changed')
329
357
def update_nrpe_checks():
330
358
    if os.path.isdir(NAGIOS_PLUGINS):
331
359
        rsync(os.path.join(os.getenv('CHARM_DIR'), 'scripts',
333
361
              os.path.join(NAGIOS_PLUGINS, 'check_rabbitmq.py'))
334
362
 
335
363
    # create unique user and vhost for each unit
336
 
    current_unit = hookenv.local_unit().replace('/', '-')
 
364
    current_unit = local_unit().replace('/', '-')
337
365
    user = 'nagios-%s' % current_unit
338
366
    vhost = 'nagios-%s' % current_unit
339
367
    password = rabbit.get_clustered_attribute('%s.passwd' % user)
340
368
    if not password:
341
 
        utils.juju_log('INFO', 'Setting password for nagios unit: %s' % user)
 
369
        log('Setting password for nagios unit: %s' % user)
342
370
        password = pwgen(length=64)
343
371
        rabbit.set_clustered_attribute('%s.passwd' % user, password)
344
372
 
356
384
    nrpe_compat.write()
357
385
 
358
386
 
 
387
@hooks.hook('upgrade-charm')
359
388
def upgrade_charm():
360
389
    pre_install_hooks()
361
 
    add_source(utils.config_get('source'), utils.config_get('key'))
 
390
    add_source(config('source'), config('key'))
362
391
    apt_update(fatal=True)
363
392
 
364
393
    # Ensure older passwd files in /var/lib/juju are moved to
378
407
                if stored_password:
379
408
                    rabbit.set_clustered_attribute(username, stored_password)
380
409
 
381
 
            utils.juju_log('INFO',
382
 
                           'upgrade_charm: Migrating stored passwd'
383
 
                           ' from %s to %s.' % (s, d))
 
410
            log('upgrade_charm: Migrating stored passwd'
 
411
                ' from %s to %s.' % (s, d))
384
412
            shutil.move(s, d)
385
413
 
386
414
    # explicitly update buggy file name naigos.passwd
399
427
    ssl_mode, external_ca = _get_ssl_mode()
400
428
    if ssl_mode == 'off':
401
429
        return
402
 
    relation_data['ssl_port'] = utils.config_get('ssl_port')
 
430
    relation_data['ssl_port'] = config('ssl_port')
403
431
    if external_ca:
404
 
        if utils.config_get('ssl_ca'):
 
432
        if config('ssl_ca'):
405
433
            relation_data['ssl_ca'] = base64.b64encode(
406
 
                utils.config_get('ssl_ca'))
 
434
                config('ssl_ca'))
407
435
        return
408
436
    ca = ServiceCA.get_ca()
409
437
    relation_data['ssl_ca'] = base64.b64encode(ca.get_ca_bundle())
410
438
 
411
439
 
412
440
def _get_ssl_mode():
413
 
    config = utils.config_get()
 
441
    config = config()
414
442
    ssl_mode = config.get('ssl')
415
443
    external_ca = False
416
444
    # Legacy config boolean option
419
447
        ssl_mode = 'off'
420
448
    elif ssl_mode == 'off' and ssl_on:
421
449
        ssl_mode = 'on'
422
 
    ssl_key = utils.config_get('ssl_key')
423
 
    ssl_cert = utils.config_get('ssl_cert')
 
450
    ssl_key = config('ssl_key')
 
451
    ssl_cert = config('ssl_cert')
424
452
    if all((ssl_key, ssl_cert)):
425
453
        external_ca = True
426
454
    return ssl_mode, external_ca
441
469
 
442
470
def reconfigure_client_ssl(ssl_enabled=False):
443
471
    ssl_config_keys = set(('ssl_key', 'ssl_cert', 'ssl_ca'))
444
 
    for rid in hookenv.relation_ids('amqp'):
445
 
        rdata = hookenv.relation_get(
446
 
            rid=rid, unit=os.environ['JUJU_UNIT_NAME'])
 
472
    for rid in relation_ids('amqp'):
 
473
        rdata = relation_get(rid=rid, unit=os.environ['JUJU_UNIT_NAME'])
447
474
        if not ssl_enabled and ssl_config_keys.intersection(rdata):
448
475
            # No clean way to remove entirely, but blank them.
449
 
            utils.relation_set(
450
 
                rid=rid, ssl_key='', ssl_cert='', ssl_ca='')
 
476
            relation_set(relation_id=rid, ssl_key='', ssl_cert='', ssl_ca='')
451
477
        elif ssl_enabled and not ssl_config_keys.intersection(rdata):
452
478
            configure_client_ssl(rdata)
453
 
            utils.relation_set(rid=rid, **rdata)
 
479
            relation_set(relation_id=rid, **rdata)
454
480
 
455
481
 
456
482
def configure_rabbit_ssl():
465
491
    if ssl_mode == 'off':
466
492
        if os.path.exists(rabbit.RABBITMQ_CONF):
467
493
            os.remove(rabbit.RABBITMQ_CONF)
468
 
        utils.close_port(utils.config_get('ssl_port'))
 
494
        close_port(config('ssl_port'))
469
495
        reconfigure_client_ssl()
470
496
        return
471
 
    ssl_key = _convert_from_base64(utils.config_get('ssl_key'))
472
 
    ssl_cert = _convert_from_base64(utils.config_get('ssl_cert'))
473
 
    ssl_ca = _convert_from_base64(utils.config_get('ssl_ca'))
474
 
    ssl_port = utils.config_get('ssl_port')
 
497
    ssl_key = _convert_from_base64(config('ssl_key'))
 
498
    ssl_cert = _convert_from_base64(config('ssl_cert'))
 
499
    ssl_ca = _convert_from_base64(config('ssl_ca'))
 
500
    ssl_port = config('ssl_port')
475
501
 
476
502
    # If external managed certs then we need all the fields.
477
503
    if (ssl_mode in ('on', 'only') and any((ssl_key, ssl_cert)) and
478
504
            not all((ssl_key, ssl_cert))):
479
 
        utils.juju_log(
480
 
            'ERROR',
481
 
            'If ssl_key or ssl_cert are specified both are required.')
 
505
        log('If ssl_key or ssl_cert are specified both are required.',
 
506
            level=ERROR)
482
507
        sys.exit(1)
483
508
 
484
509
    if not external_ca:
488
513
        ssl_key, ssl_cert, ssl_port, ssl_ca,
489
514
        ssl_only=(ssl_mode == "only"), ssl_client=False)
490
515
    reconfigure_client_ssl(True)
491
 
    utils.open_port(ssl_port)
492
 
 
493
 
 
 
516
    open_port(ssl_port)
 
517
 
 
518
 
 
519
@hooks.hook('config-changed')
494
520
def config_changed():
495
 
    if utils.config_get('management_plugin') is True:
 
521
    if config('management_plugin') is True:
496
522
        rabbit.enable_plugin(MAN_PLUGIN)
497
 
        utils.open_port(55672)
 
523
        open_port(55672)
498
524
    else:
499
525
        rabbit.disable_plugin(MAN_PLUGIN)
500
 
        utils.close_port(55672)
 
526
        close_port(55672)
501
527
 
502
528
    configure_rabbit_ssl()
503
529
 
504
 
    if cluster.eligible_leader('res_rabbitmq_vip') or \
505
 
       utils.config_get('ha-vip-only') is True:
506
 
        utils.restart('rabbitmq-server')
 
530
    if eligible_leader('res_rabbitmq_vip') or \
 
531
       config('ha-vip-only') is True:
 
532
        service_restart('rabbitmq-server')
507
533
 
508
534
    update_nrpe_checks()
509
535
 
513
539
        if os.path.isfile(f) and os.access(f, os.X_OK):
514
540
            subprocess.check_call(['sh', '-c', f])
515
541
 
516
 
hooks = {
517
 
    'install': install,
518
 
    'amqp-relation-changed': amqp_changed,
519
 
    'cluster-relation-joined': cluster_joined,
520
 
    'cluster-relation-changed': cluster_changed,
521
 
    'cluster-relation-departed': cluster_departed,
522
 
    'ha-relation-joined': ha_joined,
523
 
    'ha-relation-changed': ha_changed,
524
 
    'ceph-relation-joined': ceph_joined,
525
 
    'ceph-relation-changed': ceph_changed,
526
 
    'upgrade-charm': upgrade_charm,
527
 
    'config-changed': config_changed,
528
 
    'nrpe-external-master-relation-changed': update_nrpe_checks
529
 
}
530
542
 
531
 
utils.do_hooks(hooks)
 
543
if __name__ == '__main__':
 
544
    try:
 
545
        hooks.execute(sys.argv)
 
546
    except UnregisteredHookError as e:
 
547
        log('Unknown hook {} - skipping.'.format(e))