~le-charmers/charms/trusty/rabbitmq-server/leadership-election

« back to all changes in this revision

Viewing changes to hooks/rabbitmq_server_relations.py

  • Committer: Liam Young
  • Date: 2015-05-11 08:03:57 UTC
  • mfrom: (83.1.14 rabbitmq-server)
  • Revision ID: liam.young@canonical.com-20150511080357-3ftop9kxb6o0e3mq
Merged trunk in + LE charmhelper sync

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
import glob
8
8
import socket
9
9
 
 
10
try:
 
11
    import yaml  # flake8: noqa
 
12
except ImportError:
 
13
    if sys.version_info.major == 2:
 
14
        subprocess.check_call(['apt-get', 'install', '-y', 'python-yaml'])
 
15
    else:
 
16
        subprocess.check_call(['apt-get', 'install', '-y', 'python3-yaml'])
 
17
    import yaml  # flake8: noqa
 
18
 
10
19
import rabbit_utils as rabbit
11
20
from lib.utils import (
12
21
    chown, chmod,
52
61
    is_relation_made,
53
62
    Hooks,
54
63
    UnregisteredHookError,
55
 
    is_leader
 
64
    is_leader,
 
65
    charm_dir,
56
66
)
57
67
from charmhelpers.core.host import (
58
68
    cmp_pkgrevno,
60
70
    rsync,
61
71
    service_stop,
62
72
    service_restart,
 
73
    write_file,
63
74
)
64
75
from charmhelpers.contrib.charmsupport import nrpe
65
76
from charmhelpers.contrib.ssl.service import ServiceCA
82
93
RABBIT_USER = 'rabbitmq'
83
94
RABBIT_GROUP = 'rabbitmq'
84
95
NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins'
 
96
SCRIPTS_DIR = '/usr/local/bin'
 
97
STATS_CRONFILE = '/etc/cron.d/rabbitmq-stats'
 
98
STATS_DATAFILE = os.path.join(RABBIT_DIR, 'data',
 
99
                              '{}_queue_stats.dat'
 
100
                              ''.format(socket.gethostname()))
85
101
 
86
102
 
87
103
@hooks.hook('install')
229
245
                       relation_settings=relation_settings)
230
246
 
231
247
 
 
248
def is_sufficient_peers():
 
249
    """If min-cluster-size has been provided, check that we have sufficient
 
250
    number of peers to proceed with creating rabbitmq cluster.
 
251
    """
 
252
    min_size = config('min-cluster-size')
 
253
    if min_size:
 
254
        size = 0
 
255
        for rid in relation_ids('cluster'):
 
256
            size = len(related_units(rid))
 
257
 
 
258
        # Include this unit
 
259
        size += 1
 
260
        if min_size > size:
 
261
            log("Insufficient number of peer units to form cluster "
 
262
                "(expected=%s, got=%s)" % (min_size, size), level=INFO)
 
263
            return False
 
264
 
 
265
    return True
 
266
 
 
267
 
232
268
@hooks.hook('cluster-relation-joined')
233
269
def cluster_joined(relation_id=None):
234
270
    if config('prefer-ipv6'):
256
292
        log('erlang cookie missing from %s' % rabbit.COOKIE_PATH,
257
293
            level=ERROR)
258
294
        return
259
 
    cookie = open(rabbit.COOKIE_PATH, 'r').read().strip()
260
 
    peer_store('cookie', cookie)
 
295
 
 
296
    if not is_sufficient_peers():
 
297
        return
 
298
 
 
299
    if is_elected_leader('res_rabbitmq_vip'):
 
300
        cookie = open(rabbit.COOKIE_PATH, 'r').read().strip()
 
301
        peer_store('cookie', cookie)
261
302
 
262
303
 
263
304
@hooks.hook('cluster-relation-changed')
279
320
    whitelist = [a for a in rdata.keys() if a not in blacklist]
280
321
    peer_echo(includes=whitelist)
281
322
 
 
323
    if not is_sufficient_peers():
 
324
        # Stop rabbit until leader has finished configuring
 
325
        service_stop('rabbitmq-server')
 
326
        return
 
327
 
282
328
    # sync the cookie with peers if necessary
283
329
    update_cookie()
284
330
 
292
338
    try:
293
339
        if not is_leader():
294
340
            rabbit.cluster_with()
 
341
            update_nrpe_checks()
295
342
    except NotImplementedError:
296
343
        if is_newer():
297
344
            rabbit.cluster_with()
 
345
            update_nrpe_checks()
298
346
 
299
347
    # If cluster has changed peer db may have changed so run amqp_changed
300
348
    # to sync any changes
478
526
        rsync(os.path.join(os.getenv('CHARM_DIR'), 'scripts',
479
527
                           'check_rabbitmq.py'),
480
528
              os.path.join(NAGIOS_PLUGINS, 'check_rabbitmq.py'))
 
529
        rsync(os.path.join(os.getenv('CHARM_DIR'), 'scripts',
 
530
                           'check_rabbitmq_queues.py'),
 
531
              os.path.join(NAGIOS_PLUGINS, 'check_rabbitmq_queues.py'))
 
532
    if config('stats_cron_schedule'):
 
533
        script = os.path.join(SCRIPTS_DIR, 'collect_rabbitmq_stats.sh')
 
534
        cronjob = "{} root {}\n".format(config('stats_cron_schedule'), script)
 
535
        rsync(os.path.join(charm_dir(), 'scripts',
 
536
                           'collect_rabbitmq_stats.sh'), script)
 
537
        write_file(STATS_CRONFILE, cronjob)
 
538
    elif os.path.isfile(STATS_CRONFILE):
 
539
        os.remove(STATS_CRONFILE)
481
540
 
482
541
    # Find out if nrpe set nagios_hostname
483
542
    hostname = nrpe.get_nagios_hostname()
500
559
        check_cmd='{}/check_rabbitmq.py --user {} --password {} --vhost {}'
501
560
                  ''.format(NAGIOS_PLUGINS, user, password, vhost)
502
561
    )
 
562
    if config('queue_thresholds'):
 
563
        cmd = ""
 
564
        # If value of queue_thresholds is incorrect we want the hook to fail
 
565
        for item in yaml.safe_load(config('queue_thresholds')):
 
566
            cmd += ' -c "{}" "{}" {} {}'.format(*item)
 
567
        nrpe_compat.add_check(
 
568
            shortname=rabbit.RABBIT_USER + '_queue',
 
569
            description='Check RabbitMQ Queues',
 
570
            check_cmd='{}/check_rabbitmq_queues.py{} {}'.format(
 
571
                        NAGIOS_PLUGINS, cmd, STATS_DATAFILE)
 
572
        )
503
573
    nrpe_compat.write()
504
574
 
505
575