~james-page/charms/trusty/percona-cluster/fqdn-fix

« back to all changes in this revision

Viewing changes to hooks/mysql.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:
11
11
from charmhelpers.core.host import pwgen, mkdir, write_file
12
12
from charmhelpers.core.hookenv import (
13
13
    relation_get,
14
 
    relation_ids,
15
14
    related_units,
16
15
    service_name,
17
16
    unit_get,
18
17
    log,
 
18
    DEBUG,
19
19
    INFO,
20
20
)
21
21
from charmhelpers.core.hookenv import config as config_get
331
331
    return mysql_config
332
332
 
333
333
 
334
 
def get_allowed_units(database, username):
 
334
def get_allowed_units(database, username, relation_id=None):
335
335
    m_helper = MySQLHelper()
336
336
    m_helper.connect(password=get_mysql_root_password())
337
337
    allowed_units = set()
338
 
    for relid in relation_ids('shared-db'):
339
 
        for unit in related_units(relid):
340
 
            hosts = relation_get(attribute="%s_%s" % (database, 'hostname'),
341
 
                                 unit=unit, rid=relid)
342
 
            if not hosts:
343
 
                hosts = [relation_get(attribute='private-address', unit=unit,
344
 
                                      rid=relid)]
345
 
            else:
346
 
                # hostname can be json-encoded list of hostnames
347
 
                try:
348
 
                    _hosts = json.loads(hosts)
349
 
                except ValueError:
350
 
                    pass
 
338
    for unit in related_units(relation_id):
 
339
        settings = relation_get(rid=relation_id, unit=unit)
 
340
        # First check for setting with prefix, then without
 
341
        for attr in ["%s_hostname" % (database), 'hostname']:
 
342
            hosts = settings.get(attr, None)
 
343
            if hosts:
 
344
                break
 
345
 
 
346
        if hosts:
 
347
            # hostname can be json-encoded list of hostnames
 
348
            try:
 
349
                hosts = json.loads(hosts)
 
350
            except ValueError:
 
351
                hosts = [hosts]
 
352
        else:
 
353
            hosts = [settings['private-address']]
 
354
 
 
355
        if hosts:
 
356
            for host in hosts:
 
357
                if m_helper.grant_exists(database, username, host):
 
358
                    log("Grant exists for host '%s' on db '%s'" %
 
359
                        (host, database), level=DEBUG)
 
360
                    if unit not in allowed_units:
 
361
                        allowed_units.add(unit)
351
362
                else:
352
 
                    hosts = _hosts
353
 
 
354
 
            if not isinstance(hosts, list):
355
 
                hosts = [hosts]
356
 
 
357
 
            if hosts:
358
 
                for host in hosts:
359
 
                    log("Checking host '%s' grant" % (host), level=INFO)
360
 
                    if m_helper.grant_exists(database, username, host):
361
 
                        if unit not in allowed_units:
362
 
                            allowed_units.add(unit)
363
 
            else:
364
 
                log("No hosts found for grant check", level=INFO)
 
363
                    log("Grant does NOT exist for host '%s' on db '%s'" %
 
364
                        (host, database), level=DEBUG)
 
365
        else:
 
366
            log("No hosts found for grant check", level=INFO)
365
367
 
366
368
    return allowed_units