Merge lp:~stub/charms/precise/postgresql/bug-1084263-fix-broken-hooks into lp:charms/postgresql

Proposed by Stuart Bishop
Status: Merged
Approved by: Mark Mims
Approved revision: 62
Merged at revision: 51
Proposed branch: lp:~stub/charms/precise/postgresql/bug-1084263-fix-broken-hooks
Merge into: lp:charms/postgresql
Prerequisite: lp:~stub/charms/precise/postgresql/cleanups
Diff against target: 144 lines (+45/-18)
1 file modified
hooks/hooks.py (+45/-18)
To merge this branch: bzr merge lp:~stub/charms/precise/postgresql/bug-1084263-fix-broken-hooks
Reviewer Review Type Date Requested Status
charmers Pending
Review via email: mp+169486@code.launchpad.net

Description of the change

Fix the relation broken hooks, which juju now requires to actually work.

To post a comment you must log in.
56. By Stuart Bishop

Merged cleanups into bug-1084263-fix-broken-hooks.

57. By Stuart Bishop

Fix and reorder imports, charmhelpers moved

58. By Stuart Bishop

Merged cleanups into bug-1084263-fix-broken-hooks.

59. By Stuart Bishop

Merged cleanups into bug-1084263-fix-broken-hooks.

60. By Stuart Bishop

Merged cleanups into bug-1084263-fix-broken-hooks.

61. By Stuart Bishop

Merged cleanups into bug-1084263-fix-broken-hooks.

62. By Stuart Bishop

Merged cleanups into bug-1084263-fix-broken-hooks.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/hooks.py'
2--- hooks/hooks.py 2013-05-29 11:42:28 +0000
3+++ hooks/hooks.py 2013-06-20 14:25:32 +0000
4@@ -1,24 +1,26 @@
5 #!/usr/bin/env python
6 # vim: et ai ts=4 sw=4:
7
8+import commands
9 import cPickle as pickle
10+import glob
11+from grp import getgrnam
12 import json
13-import yaml
14-import os
15-import glob
16+import os.path
17+from pwd import getpwnam
18 import random
19 import re
20 import shutil
21+import socket
22 import string
23-import socket
24 import subprocess
25 import sys
26 from textwrap import dedent
27 import time
28+import yaml
29 from yaml.constructor import ConstructorError
30-import commands
31-from pwd import getpwnam
32-from grp import getgrnam
33+
34+from charmhelpers.core import hookenv
35
36
37 # jinja2 may not be importable until the install hook has installed the
38@@ -1088,6 +1090,8 @@
39 postgresql_restart()
40 break
41
42+ snapshot_relations()
43+
44
45 def quote_identifier(identifier):
46 r'''Quote an identifier, such as a table or role name.
47@@ -1244,6 +1248,16 @@
48 return this_host.strip()
49
50
51+def snapshot_relations():
52+ '''Snapshot our relation information into local state.
53+
54+ We need this information to be available in -broken
55+ hooks letting us actually clean up properly. Bug #1190996.
56+ '''
57+ local_state['relations'] = hookenv.relations()
58+ local_state.save()
59+
60+
61 def db_relation_joined_changed(user, database, roles):
62 if not user_exists(user):
63 password = create_user(user)
64@@ -1263,6 +1277,8 @@
65 schema_user=schema_user,
66 database=database)
67
68+ snapshot_relations()
69+
70
71 def db_admin_relation_joined_changed(user, database='all'):
72 if not user_exists(user):
73@@ -1274,22 +1290,37 @@
74 host, config_data["listen_port"]))
75 generate_postgresql_hba(postgresql_hba)
76
77-
78-def db_relation_broken(user, database):
79+ snapshot_relations()
80+
81+
82+def db_relation_broken():
83 from psycopg2.extensions import AsIs
84
85- sql = "REVOKE ALL PRIVILEGES ON %s FROM %s"
86+ # The relation no longer exists, so we can't pull the database name
87+ # we used from there. Instead, we have to persist this information
88+ # ourselves.
89+ relation = local_state['relations']['db'][os.environ['JUJU_RELATION_ID']]
90+ unit_relation_data = relation[os.environ['JUJU_UNIT_NAME']]
91+
92+ user = unit_relation_data['user']
93+ database = unit_relation_data['database']
94+
95+ sql = "REVOKE ALL PRIVILEGES ON DATABASE %s FROM %s"
96 run_sql_as_postgres(sql, AsIs(quote_identifier(database)),
97 AsIs(quote_identifier(user)))
98 run_sql_as_postgres(sql, AsIs(quote_identifier(database)),
99 AsIs(quote_identifier(user + "_schema")))
100
101+ # Cleanup our local state.
102+ snapshot_relations()
103+
104
105 def db_admin_relation_broken(user):
106 from psycopg2.extensions import AsIs
107
108- sql = "ALTER USER %s NOSUPERUSER"
109- run_sql_as_postgres(sql, AsIs(quote_identifier(user)))
110+ if user_exists(user):
111+ sql = "ALTER USER %s NOSUPERUSER"
112+ run_sql_as_postgres(sql, AsIs(quote_identifier(user)))
113 generate_postgresql_hba(postgresql_hba)
114
115
116@@ -1671,7 +1702,6 @@
117
118 def replication_relation_broken():
119 config_changed(postgresql_config)
120- authorize_remote_ssh()
121
122
123 def clone(master_unit, master_host):
124@@ -1898,7 +1928,7 @@
125 db_relation_joined_changed(user, database, []) # No roles yet.
126
127 elif hook_name == "db-relation-changed":
128- roles = filter(None, relation_get('roles').split(","))
129+ roles = filter(None, (relation_get('roles') or '').split(","))
130
131 # If the remote service has requested we use a particular database
132 # name, honour that request.
133@@ -1913,10 +1943,7 @@
134 db_relation_joined_changed(user, database, roles)
135
136 elif hook_name == "db-relation-broken":
137- database = relation_get('database')
138- user = user_name(
139- os.environ['JUJU_RELATION_ID'], os.environ['JUJU_REMOTE_UNIT'])
140- db_relation_broken(user, database)
141+ db_relation_broken()
142
143 elif hook_name in ("db-admin-relation-joined",
144 "db-admin-relation-changed"):

Subscribers

People subscribed via source and target branches