231
231
return unit_get('private-address')
234
def configure_db_for_hosts(hosts, database, username):
235
"""Hosts may be a json-encoded list of hosts or a single hostname."""
237
hosts = json.loads(hosts)
238
log("Multiple hostnames provided by relation: %s" % (', '.join(hosts)),
241
log("Single hostname provided by relation: %s" % (hosts),
246
password = configure_db(host, database, username)
234
251
# TODO: This could be a hook common between mysql and percona-cluster
235
252
@hooks.hook('shared-db-relation-changed')
236
253
def shared_db_changed(relation_id=None, unit=None):
270
287
database = settings['database']
271
288
username = settings['username']
273
# Hostname can be json-encoded list of hostnames
275
hostname = json.loads(hostname)
279
if isinstance(hostname, list):
280
for host in hostname:
281
password = configure_db(host, database, username)
283
password = configure_db(hostname, database, username)
285
allowed_units = unit_sorted(get_allowed_units(database, username))
290
# NOTE: do this before querying access grants
291
password = configure_db_for_hosts(hostname, database, username)
293
allowed_units = unit_sorted(get_allowed_units(database, username,
294
relation_id=relation_id))
286
295
allowed_units = ' '.join(allowed_units)
296
relation_set(relation_id=relation_id, allowed_units=allowed_units)
287
298
db_host = get_db_host(hostname)
288
299
peer_store_and_set(relation_id=relation_id,
291
allowed_units=allowed_units)
293
303
# Process multiple database setup requests.
294
304
# from incoming relation data:
316
326
databases[db] = {}
317
327
databases[db][x] = v
320
331
for db in databases:
321
332
if singleset.issubset(databases[db]):
322
333
database = databases[db]['database']
323
334
hostname = databases[db]['hostname']
324
335
username = databases[db]['username']
326
hostname = json.loads(hostname)
330
if isinstance(hostname, list):
331
for host in hostname:
332
password = configure_db(host, database, username)
334
password = configure_db(hostname, database, username)
336
return_data['_'.join([db, 'password'])] = password
337
return_data['_'.join([db, 'allowed_units'])] = \
338
" ".join(unit_sorted(get_allowed_units(database,
337
# NOTE: do this before querying access grants
338
password = configure_db_for_hosts(hostname, database, username)
340
a_units = get_allowed_units(database, username,
341
relation_id=relation_id)
342
a_units = ' '.join(unit_sorted(a_units))
343
allowed_units['%s_allowed_units' % (db)] = a_units
345
return_data['%s_password' % (db)] = password
340
346
db_host = get_db_host(hostname)
342
if len(return_data) > 0:
343
peer_store_and_set(relation_id=relation_id,
349
relation_set(relation_id=relation_id, **allowed_units)
351
log("No allowed_units - not setting relation settings",
355
peer_store_and_set(relation_id=relation_id, db_host=db_host,
345
peer_store_and_set(relation_id=relation_id,
358
log("No return data - not setting relation settings", level=DEBUG)
348
360
peer_store_and_set(relation_id=relation_id,
349
361
relation_settings={'access-network': access_network})