~openstack-charmers-archive/charms/trusty/keystone/next

« back to all changes in this revision

Viewing changes to hooks/keystone_utils.py

  • Committer: Edward Hope-Morley
  • Date: 2015-01-22 18:44:33 UTC
  • mto: This revision was merged to the branch mainline in revision 110.
  • Revision ID: edward.hope-morley@canonical.com-20150122184433-wimphdybuuzy897d
[hopem, r=]

Wait until DB ready before performing Keystone api ops.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
    local_unit,
63
63
    relation_get,
64
64
    relation_set,
 
65
    relation_id,
65
66
    relation_ids,
66
67
    related_units,
67
68
    DEBUG,
1356
1357
        level=DEBUG)
1357
1358
    for rid in rel_ids:
1358
1359
        relation_set(relation_id=rid, relation_settings=_notifications)
 
1360
 
 
1361
 
 
1362
def is_db_ready(use_current_context=False, db_rel=None):
 
1363
    """Database relations are expected to provide a list of 'allowed' units to
 
1364
    confirm that the database is ready for use by those units.
 
1365
 
 
1366
    If db relation has provided this information and local unit is a member,
 
1367
    returns True otherwise False.
 
1368
    """
 
1369
    key = 'allowed_units'
 
1370
    db_rels = ['shared-db', 'pgsql-db']
 
1371
    if db_rel:
 
1372
        db_rels = [db_rel]
 
1373
 
 
1374
    rel_has_units = False
 
1375
 
 
1376
    if use_current_context:
 
1377
        if not any([relation_id() in relation_ids(r) for r in db_rels]):
 
1378
            raise Exception("use_current_context=True but not in one of %s "
 
1379
                            "rel hook contexts (currently in %s)." %
 
1380
                            (', '.join(db_rels), relation_id()))
 
1381
 
 
1382
        allowed_units = relation_get(attribute=key)
 
1383
        if allowed_units and local_unit() in allowed_units.split():
 
1384
            return True
 
1385
    else:
 
1386
        for rel in db_rels:
 
1387
            for rid in relation_ids(rel):
 
1388
                for unit in related_units(rid):
 
1389
                    allowed_units = relation_get(rid=rid, unit=unit,
 
1390
                                                 attribute=key)
 
1391
                    if allowed_units and local_unit() in allowed_units.split():
 
1392
                        return True
 
1393
 
 
1394
                    # If relation has units
 
1395
                    return False
 
1396
 
 
1397
    # If neither relation has units then we are probably in sqllite mode return
 
1398
    # True.
 
1399
    return not rel_has_units