Store shared-db information in the peer relation for clustered deploys and echo back to client service for each unit

This commit is contained in:
Liam Young 2014-08-15 14:16:23 +00:00
parent de3aa88b72
commit 1949112473
3 changed files with 40 additions and 8 deletions

View File

@ -0,0 +1 @@
percona_hooks.py

View File

@ -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})

View File

@ -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)