~charmers/charms/trusty/postgresql-psql/trunk

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Stuart Bishop
  • Date: 2013-04-05 11:20:41 UTC
  • Revision ID: stuart@stuartbishop.net-20130405112041-ol1tnsv3n0vnbcky
Work in progress

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
 
 
3
import sys
 
4
 
 
5
 
 
6
client_relation_types=['db', 'db-admin']
 
7
 
 
8
def relation_ids(relation_types):
 
9
    relids = []
 
10
    for reltype in relation_types:
 
11
        relid_cmd_line = ['relation-ids', '--format=json', reltype]
 
12
        json_relids = subprocess.check_output(relid_cmd_line).strip()
 
13
        if json_relids:
 
14
            relids.extend(json.loads(json_relids))
 
15
    return relids
 
16
 
 
17
 
 
18
def relation_list(relation_id):
 
19
    """Return the list of units participating in the relation."""
 
20
    cmd = ['relation-list', '--format=json', '-r', relation_id]
 
21
    json_units = subprocess.check_output(cmd).strip()
 
22
    if json_units:
 
23
        return json.loads(json_units)
 
24
    return []
 
25
 
 
26
 
 
27
def relation_get(relation_id, unit):
 
28
    cmd = ['relation-get', '--format=json', '-r', relation_id, '-', unit]
 
29
    json_relation = subprocess.check_output(cmd)
 
30
    if json_relation:
 
31
        return json.loads(json_relation)
 
32
    return {}
 
33
 
 
34
 
 
35
def all_relations(relation_types=client_relation_types):
 
36
    for reltype in relation_types:
 
37
        for relid in relation_ids(relation_types=[reltype]):
 
38
            for unit in relation_list(relation_id=relid):
 
39
                yield reltype, relid, unit, relation_get(relid, unit)
 
40
 
 
41
 
 
42
def rebuild_all_relations():
 
43
    for reltype, relid, unit, relation in all_relations():
 
44
        rebuild_relation(reltype, relid, unit, relation)
 
45
 
 
46
 
 
47
def rebuild_relation(reltype, relid, unit, relation):
 
48
    script_name = 'psql-{}-{}'.format(relid, unit)
 
49
    build_script(script_name, reltype, relid, unit, relation)
 
50
    if relation['state'] == 'master':
 
51
        script_name = 'psql-{}-master'.format(relid)
 
52
        build_script(script_name, reltype, relid, unit, relation)
 
53
    elif relation['state'] == 'slave':
 
54
        script_name = 'psql-{}-slave'.format(relid)
 
55
        build_script(script_name, reltype, relid, unit, relation)
 
56
 
 
57
 
 
58
def build_script(script_name, reltype, relid, unit, relation):
 
59
    bin_dir = os.path.expanduser('~ubuntu/bin')
 
60
    script_path = os.path.join(bin_dir, script_name)
 
61
    pgpass_path = os.path.abspath('pgpass.{}'.format(script_name))
 
62
    script = dedent(r"""\
 
63
        #!/bin/sh
 
64
        exec env \
 
65
            PGHOST={host} PGPORT={port} PGDATABASE={database} \
 
66
            PGPASSFILE={pgpass} psql $*
 
67
        """).format(dict(
 
68
            host=relation['host'],
 
69
            port=relation.get('port', 5432),
 
70
            database=relation['database'],
 
71
            pgpass=pgpass_path))
 
72
    pgpass = "*:*:*:{user}:{password}".format(
 
73
        relation['user'], relation['password'])
 
74
 
 
75
 
 
76
if __name__ == '__main__':
 
77
    raise SystemExit(rebuild_all_relations())