6
client_relation_types=['db', 'db-admin']
8
def relation_ids(relation_types):
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()
14
relids.extend(json.loads(json_relids))
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()
23
return json.loads(json_units)
27
def relation_get(relation_id, unit):
28
cmd = ['relation-get', '--format=json', '-r', relation_id, '-', unit]
29
json_relation = subprocess.check_output(cmd)
31
return json.loads(json_relation)
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)
42
def rebuild_all_relations():
43
for reltype, relid, unit, relation in all_relations():
44
rebuild_relation(reltype, relid, unit, relation)
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)
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))
65
PGHOST={host} PGPORT={port} PGDATABASE={database} \
66
PGPASSFILE={pgpass} psql $*
68
host=relation['host'],
69
port=relation.get('port', 5432),
70
database=relation['database'],
72
pgpass = "*:*:*:{user}:{password}".format(
73
relation['user'], relation['password'])
76
if __name__ == '__main__':
77
raise SystemExit(rebuild_all_relations())