~brad-marshall/charms/trusty/percona-cluster/fix-nagios

« back to all changes in this revision

Viewing changes to hooks/percona_hooks.py

  • Committer: Edward Hope-Morley
  • Date: 2015-02-04 17:47:15 UTC
  • mfrom: (42.1.5 percona-cluster.fix-1389670)
  • Revision ID: edward.hope-morley@canonical.com-20150204174715-p5dhe9pq021v0xom
[hopem,r=]

Don't put allowed units onto the cluster relation.

Also fixes support for non-prefixed db relation
settings.

Closes-Bug: 1389670

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
    config,
19
19
    remote_unit,
20
20
    relation_type,
 
21
    DEBUG,
21
22
    INFO,
22
23
)
23
24
from charmhelpers.core.host import (
24
25
    service_restart,
25
26
    file_hash,
26
 
    write_file,
27
27
    lsb_release,
28
28
)
29
29
from charmhelpers.core.templating import render
231
231
    return unit_get('private-address')
232
232
 
233
233
 
 
234
def configure_db_for_hosts(hosts, database, username):
 
235
    """Hosts may be a json-encoded list of hosts or a single hostname."""
 
236
    try:
 
237
        hosts = json.loads(hosts)
 
238
        log("Multiple hostnames provided by relation: %s" % (', '.join(hosts)),
 
239
            level=DEBUG)
 
240
    except ValueError:
 
241
        log("Single hostname provided by relation: %s" % (hosts),
 
242
            level=DEBUG)
 
243
        hosts = [hosts]
 
244
 
 
245
    for host in hosts:
 
246
        password = configure_db(host, database, username)
 
247
 
 
248
    return password
 
249
 
 
250
 
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']
272
289
 
273
 
        # Hostname can be json-encoded list of hostnames
274
 
        try:
275
 
            hostname = json.loads(hostname)
276
 
        except ValueError:
277
 
            pass
278
 
 
279
 
        if isinstance(hostname, list):
280
 
            for host in hostname:
281
 
                password = configure_db(host, database, username)
282
 
        else:
283
 
            password = configure_db(hostname, database, username)
284
 
 
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)
 
292
 
 
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)
 
297
 
287
298
        db_host = get_db_host(hostname)
288
299
        peer_store_and_set(relation_id=relation_id,
289
300
                           db_host=db_host,
290
 
                           password=password,
291
 
                           allowed_units=allowed_units)
 
301
                           password=password)
292
302
    else:
293
303
        # Process multiple database setup requests.
294
304
        # from incoming relation data:
316
326
                databases[db] = {}
317
327
            databases[db][x] = v
318
328
 
 
329
        allowed_units = {}
319
330
        return_data = {}
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']
325
 
                try:
326
 
                    hostname = json.loads(hostname)
327
 
                except ValueError:
328
 
                    pass
329
 
 
330
 
                if isinstance(hostname, list):
331
 
                    for host in hostname:
332
 
                        password = configure_db(host, database, username)
333
 
                else:
334
 
                    password = configure_db(hostname, database, username)
335
 
 
336
 
                return_data['_'.join([db, 'password'])] = password
337
 
                return_data['_'.join([db, 'allowed_units'])] = \
338
 
                    " ".join(unit_sorted(get_allowed_units(database,
339
 
                                                           username)))
 
336
 
 
337
                # NOTE: do this before querying access grants
 
338
                password = configure_db_for_hosts(hostname, database, username)
 
339
 
 
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
 
344
 
 
345
                return_data['%s_password' % (db)] = password
340
346
                db_host = get_db_host(hostname)
341
347
 
342
 
        if len(return_data) > 0:
343
 
            peer_store_and_set(relation_id=relation_id,
 
348
        if allowed_units:
 
349
            relation_set(relation_id=relation_id, **allowed_units)
 
350
        else:
 
351
            log("No allowed_units - not setting relation settings",
 
352
                level=DEBUG)
 
353
 
 
354
        if return_data:
 
355
            peer_store_and_set(relation_id=relation_id, db_host=db_host,
344
356
                               **return_data)
345
 
            peer_store_and_set(relation_id=relation_id,
346
 
                               db_host=db_host)
 
357
        else:
 
358
            log("No return data - not setting relation settings", level=DEBUG)
347
359
 
348
360
    peer_store_and_set(relation_id=relation_id,
349
361
                       relation_settings={'access-network': access_network})