diff --git a/hooks/rabbit_utils.py b/hooks/rabbit_utils.py index 9e1e5b90..7da0d069 100644 --- a/hooks/rabbit_utils.py +++ b/hooks/rabbit_utils.py @@ -37,6 +37,8 @@ from charmhelpers.contrib.peerstorage import ( peer_retrieve ) +from collections import OrderedDict + PACKAGES = ['rabbitmq-server', 'python-amqplib'] RABBITMQ_CTL = '/usr/sbin/rabbitmqctl' @@ -49,6 +51,24 @@ HOSTS_FILE = '/etc/hosts' _named_passwd = '/var/lib/charm/{}/{}.passwd' +# hook_contexts are used as a convenient mechanism to render +# templates logically, consider building a hook_context for +# template rendering so the charm doesn't concern itself with +CONFIG_FILES = OrderedDict([ + (RABBITMQ_CONF, { + 'hook_contexts': [], + 'services': ['rabbitmq-server'] + }), + (ENV_CONF, { + 'hook_contexts': [], + 'services': ['rabbitmq-server'] + }), + (COOKIE_PATH, { + 'hook_contexts': [], + 'services': ['rabbitmq-server'] + }), +]) + def vhost_exists(vhost): try: @@ -441,3 +461,19 @@ def assert_charm_supports_ipv6(): if lsb_release()['DISTRIB_CODENAME'].lower() < "trusty": raise Exception("IPv6 is not supported in the charms for Ubuntu " "versions less than Trusty 14.04") + +def restart_map(): + '''Determine the correct resource map to be passed to + charmhelpers.core.restart_on_change() based on the services configured. + + :returns: dict: A dictionary mapping config file to lists of services + that should be restarted when file changes. + ''' + _map = [] + for f, ctxt in CONFIG_FILES.iteritems(): + svcs = [] + for svc in ctxt['services']: + svcs.append(svc) + if svcs: + _map.append((f, svcs)) + return OrderedDict(_map) \ No newline at end of file diff --git a/hooks/rabbitmq_server_relations.py b/hooks/rabbitmq_server_relations.py index d4525f49..473a94a0 100755 --- a/hooks/rabbitmq_server_relations.py +++ b/hooks/rabbitmq_server_relations.py @@ -50,7 +50,10 @@ from charmhelpers.core.hookenv import ( UnregisteredHookError ) from charmhelpers.core.host import ( - rsync, service_stop, service_restart, cmp_pkgrevno + cmp_pkgrevno, + restart_on_change, + rsync, + service_stop, ) from charmhelpers.contrib.charmsupport.nrpe import NRPE from charmhelpers.contrib.ssl.service import ServiceCA @@ -166,6 +169,7 @@ def amqp_changed(relation_id=None, remote_unit=None): @hooks.hook('cluster-relation-joined') +@restart_on_change(rabbit.restart_map()) def cluster_joined(relation_id=None): if config('prefer-ipv6'): relation_settings = {'hostname': socket.gethostname(), @@ -197,16 +201,13 @@ def cluster_joined(relation_id=None): if nodename: log('forcing nodename=%s' % nodename) # need to stop it under current nodename - service_stop('rabbitmq-server') rabbit.update_rmq_env_conf(hostname='rabbit@%s' % nodename, ipv6=config('prefer-ipv6')) - service_restart('rabbitmq-server') if is_newer(): log('cluster_joined: Relation greater.') return - rabbit.COOKIE_PATH = '/var/lib/rabbitmq/.erlang.cookie' if not os.path.isfile(rabbit.COOKIE_PATH): log('erlang cookie missing from %s' % rabbit.COOKIE_PATH, level=ERROR) @@ -233,17 +234,9 @@ def cluster_changed(): whitelist = [a for a in rdata.keys() if a not in blacklist] peer_echo(includes=whitelist) - # sync cookie - cookie = peer_retrieve('cookie') - if open(rabbit.COOKIE_PATH, 'r').read().strip() == cookie: - log('Cookie already synchronized with peer.') - else: - log('Synchronizing erlang cookie from peer.') - rabbit.service('stop') - with open(rabbit.COOKIE_PATH, 'wb') as out: - out.write(cookie) - rabbit.service('start') - + # sync the cookie with peers if necessary + update_cookie() + if is_relation_made('ha') and \ config('ha-vip-only') is False: log('hacluster relation is present, skipping native ' @@ -262,6 +255,18 @@ def cluster_changed(): amqp_changed(relation_id=rid, remote_unit=unit) +@restart_on_change(rabbit.restart_map()) +def update_cookie(): + # sync cookie + cookie = peer_retrieve('cookie') + if open(rabbit.COOKIE_PATH, 'r').read().strip() == cookie: + log('Cookie already synchronized with peer.') + else: + log('Synchronizing erlang cookie from peer.') + with open(rabbit.COOKIE_PATH, 'wb') as out: + out.write(cookie) + + @hooks.hook('cluster-relation-departed') def cluster_departed(): if is_relation_made('ha') and \ @@ -589,12 +594,8 @@ def configure_rabbit_ssl(): open_port(ssl_port) -def restart_rabbit_update_nrpe(): - service_restart('rabbitmq-server') - update_nrpe_checks() - - @hooks.hook('config-changed') +@restart_on_change(rabbit.restart_map()) def config_changed(): if config('prefer-ipv6'): rabbit.assert_charm_supports_ipv6() @@ -616,9 +617,7 @@ def config_changed(): chmod(RABBIT_DIR, 0o775) if config('prefer-ipv6'): - service_stop('rabbitmq-server') rabbit.update_rmq_env_conf(ipv6=config('prefer-ipv6')) - service_restart('rabbitmq-server') if config('management_plugin') is True: rabbit.enable_plugin(MAN_PLUGIN) @@ -633,15 +632,15 @@ def config_changed(): ha_is_active_active = config("ha-vip-only") if ha_is_active_active: - restart_rabbit_update_nrpe() + update_nrpe_checks() else: if is_elected_leader('res_rabbitmq_vip'): - restart_rabbit_update_nrpe() + update_nrpe_checks() else: log("hacluster relation is present but this node is not active" " skipping update nrpe checks") else: - restart_rabbit_update_nrpe() + update_nrpe_checks() def pre_install_hooks():