From 19491124731ee38337d07df2a33de05732fbca3d Mon Sep 17 00:00:00 2001 From: Liam Young Date: Fri, 15 Aug 2014 14:16:23 +0000 Subject: [PATCH] Store shared-db information in the peer relation for clustered deploys and echo back to client service for each unit --- hooks/cluster-relation-departed | 1 + hooks/percona_hooks.py | 32 ++++++++++++++++++++++++-------- hooks/percona_utils.py | 15 +++++++++++++++ 3 files changed, 40 insertions(+), 8 deletions(-) create mode 120000 hooks/cluster-relation-departed diff --git a/hooks/cluster-relation-departed b/hooks/cluster-relation-departed new file mode 120000 index 0000000..2af5208 --- /dev/null +++ b/hooks/cluster-relation-departed @@ -0,0 +1 @@ +percona_hooks.py \ No newline at end of file diff --git a/hooks/percona_hooks.py b/hooks/percona_hooks.py index 6a4e164..edeffbe 100755 --- a/hooks/percona_hooks.py +++ b/hooks/percona_hooks.py @@ -3,8 +3,10 @@ import sys import os +from charmhelpers.core.hookenv import relation_id as current_relation_id from charmhelpers.core.hookenv import ( Hooks, UnregisteredHookError, + is_relation_made, log, relation_get, relation_set, @@ -27,6 +29,7 @@ from charmhelpers.fetch import ( add_source, ) from charmhelpers.contrib.peerstorage import ( + peer_retrieve, peer_echo ) from percona_utils import ( @@ -40,6 +43,7 @@ from percona_utils import ( seeded, mark_seeded, configure_mysql_root_password, relation_clear, + relation_set_and_store, ) from mysql import ( get_mysql_password, @@ -116,6 +120,7 @@ def config_changed(): shared_db_changed(r_id, unit) +@hooks.hook('cluster-relation-departed') @hooks.hook('cluster-relation-changed') def cluster_changed(): peer_echo() @@ -165,9 +170,20 @@ def db_changed(relation_id=None, unit=None, admin=None): @hooks.hook('shared-db-relation-changed') def shared_db_changed(relation_id=None, unit=None): if not eligible_leader(LEADER_RES): + relation_clear(relation_id) + # Each unit needs to set the db information otherwise if the unit + # with the info dies the settings die with it Bug# 1355848 + if is_relation_made('cluster'): + if not relation_id: + relation_id = current_relation_id() + rel_settings = { + 'db_host': config('vip'), + 'password': peer_retrieve(relation_id + '_password'), + 'access-network': config('access-network'), + } + relation_set(relation_id=relation_id, **rel_settings) log('Service is peered, clearing shared-db relation' ' as this service unit is not the leader') - relation_clear(relation_id) return settings = relation_get(unit=unit, @@ -194,9 +210,9 @@ def shared_db_changed(relation_id=None, unit=None): is_address_in_network(access_network, get_host_ip(settings['hostname']))): db_host = get_address_in_network(access_network) - relation_set(relation_id=relation_id, - db_host=db_host, - password=password) + relation_set_and_store(relation_id=relation_id, + db_host=db_host, + password=password) else: # Process multiple database setup requests. # from incoming relation data: @@ -236,10 +252,10 @@ def shared_db_changed(relation_id=None, unit=None): get_host_ip(databases[db]['hostname']))): db_host = get_address_in_network(access_network) if len(return_data) > 0: - relation_set(relation_id=relation_id, - **return_data) - relation_set(relation_id=relation_id, - db_host=db_host) + relation_set_and_store(relation_id=relation_id, + **return_data) + relation_set_and_store(relation_id=relation_id, + db_host=db_host) relation_set(relation_id=relation_id, relation_settings={'access-network': access_network}) diff --git a/hooks/percona_utils.py b/hooks/percona_utils.py index f0a13f4..4c60fef 100644 --- a/hooks/percona_utils.py +++ b/hooks/percona_utils.py @@ -6,8 +6,10 @@ import os from charmhelpers.core.host import ( lsb_release, ) +from charmhelpers.core.hookenv import relation_id as current_relation_id from charmhelpers.core.hookenv import ( unit_get, + is_relation_made, relation_ids, related_units, relation_get, @@ -19,6 +21,9 @@ from charmhelpers.fetch import ( filter_installed_packages, ) from mysql import get_mysql_root_password, MySQLHelper +from charmhelpers.contrib.peerstorage import ( + peer_store, +) try: import jinja2 @@ -133,3 +138,13 @@ def relation_clear(r_id=None): settings[setting] = None relation_set(relation_id=r_id, **settings) + + +def relation_set_and_store(relation_id, **kwargs): + ''' For each pair set them in the relation but also store in peer db''' + if not relation_id: + relation_id = current_relation_id() + relation_set(relation_id=relation_id, **kwargs) + if is_relation_made('cluster'): + for key, value in kwargs.iteritems(): + peer_store('_'.join([relation_id, key]), value)