Merge lp:~mthaddon/charms/precise/pgbouncer/dbproxy-relation-ids into lp:charms/pgbouncer

Proposed by Tom Haddon
Status: Merged
Merge reported by: Juan L. Negron
Merged at revision: not available
Proposed branch: lp:~mthaddon/charms/precise/pgbouncer/dbproxy-relation-ids
Merge into: lp:charms/pgbouncer
Diff against target: 240 lines (+101/-89)
2 files modified
hooks/backend-db-admin-relation-changed (+11/-6)
hooks/db-proxy-relation-changed (+90/-83)
To merge this branch: bzr merge lp:~mthaddon/charms/precise/pgbouncer/dbproxy-relation-ids
Reviewer Review Type Date Requested Status
Juan L. Negron (community) Approve
Review via email: mp+155205@code.launchpad.net

Description of the change

If we have more than one db-proxy relation, the charm fails because we get the output of "relation-ids db-proxy" and pass that to "relation-list -r", which only accepts one relation id at a time. So, this fixes that problem.

To post a comment you must log in.
Revision history for this message
Juan L. Negron (negronjl) wrote :

Reviewing now.

-Juan

Revision history for this message
Juan L. Negron (negronjl) wrote :

Approved.

-Juan

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/backend-db-admin-relation-changed'
2--- hooks/backend-db-admin-relation-changed 2012-11-09 18:40:54 +0000
3+++ hooks/backend-db-admin-relation-changed 2013-03-25 11:34:23 +0000
4@@ -30,10 +30,15 @@
5 /usr/sbin/service pgbouncer reload
6
7 # Trigger db-proxy-relation-changed if it exists already
8-dbproxyid=$(relation-ids db-proxy)
9-if [ -n "$dbproxyid" ]; then
10- dbproxyremoteunit=$(relation-list -r $dbproxyid)
11- if [ -n "$dbproxyremoteunit" ]; then
12- ./hooks/db-proxy-relation-changed
13- fi
14+dbproxy_relation_ids=$(relation-ids db-proxy)
15+if [ -n "${dbproxy_relation_ids}" ]; then
16+ for dbproxy_relation_id in $(echo ${dbproxy_relation_ids}); do
17+ dbproxyremoteunit=$(relation-list -r ${dbproxy_relation_id})
18+ if [ -n "${dbproxyremoteunit}" ]; then
19+ ./hooks/db-proxy-relation-changed
20+ # We only need to do this once, so we can break out of the for loop
21+ # now
22+ break
23+ fi
24+ done
25 fi
26
27=== modified file 'hooks/db-proxy-relation-changed'
28--- hooks/db-proxy-relation-changed 2013-02-25 07:15:10 +0000
29+++ hooks/db-proxy-relation-changed 2013-03-25 11:34:23 +0000
30@@ -8,8 +8,8 @@
31 # Find the backend-db-admin relationship
32 backendid=$(relation-ids backend-db-admin)
33 if [ -z "$backendid" ]; then
34- juju-log "No backend-db-admin relation yet, exiting"
35- exit 0
36+ juju-log "No backend-db-admin relation yet, exiting"
37+ exit 0
38 fi
39 backendunit=$(relation-list -r $backendid)
40 # TODO: Handle multple backend units
41@@ -18,111 +18,118 @@
42 export PGPASSWORD=$(relation-get -r $backendid password $backendunit)
43 export PGDATABASE='template1'
44 if [ -z "$PGPASSWORD" ]; then
45- juju-log "No backend-db-admin relation yet, exiting"
46- exit 0
47-fi
48-
49-# If called out of context
50-if [ "$JUJU_RELATION" != "db-proxy" ]; then
51- juju-log "Out of db-proxy relation context, manually setting JUJU vars for db-proxy relation"
52- JUJU_RELATION="db-proxy"
53- export JUJU_RELATION_ID=$(relation-ids db-proxy)
54- # TODO: This must handle multiple db-proxy relationships
55- # for JUJU_RELATION_ID in $JUJU_RELATION_IDS; do
56- export JUJU_REMOTE_UNIT=$(relation-list -r $JUJU_RELATION_ID)
57- if [ -z "$JUJU_REMOTE_UNIT" ]; then
58- juju-log "No db-proxy relation remote-unit yet, exiting"
59- exit 0
60- fi
61-fi
62-
63-# Get remote relationship
64-database=$(relation-get database)
65-# A comma seperated list of roles can be passed in by the joining charm which will be granted
66-# th the db user
67-roles=$(relation-get roles)
68-user=${JUJU_REMOTE_UNIT///*}
69-if [[ -z ${database} ]]; then
70- database=${JUJU_REMOTE_UNIT///*}
71- database=${database//-/_}
72- user=${user//-/_}
73-else
74- user="${user//-/_}_${database}"
75-fi
76-
77-host=$(unit-get private-address)
78-port=$(config-get listen_port)
79-
80-service_password_file="/var/lib/juju/${user}.password"
81+ juju-log "No backend-db-admin relation yet, exiting"
82+ exit 0
83+fi
84
85 create_password() {
86- password=`pwgen -s 16`
87- echo ${password} > ${service_password_file}
88+ password=`pwgen -s 16`
89+ echo ${password} > ${service_password_file}
90 }
91 has_password() {
92- [ -f ${service_password_file} ] && password=`cat ${service_password_file}`
93+ [ -f ${service_password_file} ] && password=`cat ${service_password_file}`
94 }
95-
96 create_role() {
97- local l_role=${1}
98- psql -c"create user ${l_role}"
99+ local l_role=${1}
100+ psql -c"create user ${l_role}"
101 }
102 create_user() {
103- psql -c"create user ${user} password '${password}'"
104+ psql -c"create user ${user} password '${password}'"
105 }
106 has_user() {
107- local l_role=${1:-$user}
108- psql -c"select rolname from pg_roles;" | grep -wq ${l_role}
109+ local l_role=${1:-$user}
110+ psql -c"select rolname from pg_roles;" | grep -wq ${l_role}
111 }
112 set_password() {
113- psql -c"alter user ${user} password '${password}'"
114+ psql -c"alter user ${user} password '${password}'"
115 }
116-
117-# If we point pgbouncer at an existing db then the user may exist but the
118-# password is unknown. In this case reset the password
119-if has_user && ! has_password ; then
120- create_password && set_password
121-fi
122-has_password || create_password
123-has_user || create_user
124-
125 create_database() {
126- psql -c"create database ${database} owner ${user}"
127- psql -c"grant all privileges on database ${database} to ${user}"
128+ psql -c"create database ${database} owner ${user}"
129+ psql -c"grant all privileges on database ${database} to ${user}"
130 }
131 has_database() {
132- psql -c"select datname from pg_database;" | grep -wq ${database}
133+ psql -c"select datname from pg_database;" | grep -wq ${database}
134 }
135-
136-has_database || create_database
137-
138 grant_role() {
139- local l_role=$1
140- has_user $l_role || create_role $l_role
141- echo "Granting $l_role to ${user};"
142- psql -c"grant ${l_role} to ${user};"
143+ local l_role=$1
144+ has_user $l_role || create_role $l_role
145+ echo "Granting $l_role to ${user};"
146+ psql -c"grant ${l_role} to ${user};"
147 }
148
149-db_owner=`psql -t -c"SELECT rolname FROM pg_database JOIN pg_authid ON pg_database.datdba = pg_authid.oid where datname = '${database}'" | tr -d ' '`
150-if [ "${db_owner}" != "${user}" ]; then
151- grant_role $db_owner
152-fi
153-
154-if [ -n "${roles}" ]; then
155- echo "Roles: $roles"
156- allroles=(${roles//,/ })
157-
158- for role in "${allroles[@]}"; do
159- grant_role $role
160- done
161-fi
162-
163-relation-set -r $JUJU_RELATION_ID host=${host} \
164+function update_db_credentials() {
165+ # Get remote relationship
166+ database=$(relation-get database)
167+ # A comma seperated list of roles can be passed in by the joining charm
168+ # which will be granted to the db user
169+ roles=$(relation-get roles)
170+ user=${JUJU_REMOTE_UNIT///*}
171+ if [[ -z ${database} ]]; then
172+ database=${JUJU_REMOTE_UNIT///*}
173+ database=${database//-/_}
174+ user=${user//-/_}
175+ else
176+ user="${user//-/_}_${database}"
177+ fi
178+
179+ host=$(unit-get private-address)
180+ port=$(config-get listen_port)
181+
182+ service_password_file="/var/lib/juju/${user}.password"
183+
184+ # If we point pgbouncer at an existing db then the user may exist but the
185+ # password is unknown. In this case reset the password
186+ if has_user && ! has_password ; then
187+ create_password && set_password
188+ fi
189+ has_password || create_password
190+ has_user || create_user
191+
192+ has_database || create_database
193+
194+ db_owner=`psql -t -c"SELECT rolname FROM pg_database JOIN pg_authid ON pg_database.datdba = pg_authid.oid where datname = '${database}'" | tr -d ' '`
195+ if [ "${db_owner}" != "${user}" ]; then
196+ grant_role $db_owner
197+ fi
198+
199+ if [ -n "${roles}" ]; then
200+ echo "Roles: $roles"
201+ allroles=(${roles//,/ })
202+
203+ for role in "${allroles[@]}"; do
204+ grant_role $role
205+ done
206+ fi
207+
208+ relation-set -r $JUJU_RELATION_ID host=${host} \
209 port=${port} \
210 user=${user} \
211 password=${password} \
212 database=${database}
213
214-echo "${database} = host=${PGHOST} port=5432 dbname=${database}" > /var/lib/juju/db-${database}
215+ echo "${database} = host=${PGHOST} port=5432 dbname=${database}" > /var/lib/juju/db-${database}
216+}
217+
218+if [ "$JUJU_RELATION" != "db-proxy" ]; then
219+ # If called out of context
220+ juju-log "Out of db-proxy relation context, manually setting JUJU vars for db-proxy relation"
221+ JUJU_RELATION="db-proxy"
222+ export JUJU_RELATION_IDS=$(relation-ids db-proxy)
223+ UPDATED="False"
224+ for JUJU_RELATION_ID in $(echo ${JUJU_RELATION_ID}); do
225+ export JUJU_REMOTE_UNIT=$(relation-list -r $JUJU_RELATION_ID)
226+ if [ -n "$JUJU_REMOTE_UNIT" ]; then
227+ update_db_credentials
228+ UPDATED="True"
229+ fi
230+ done
231+ if [ "$UPDATED" = "False" ]; then
232+ juju-log "Nothing updated, exiting"
233+ exit 0
234+ fi
235+else
236+ # We're being called normally
237+ update_db_credentials
238+fi
239
240 update_config

Subscribers

People subscribed via source and target branches

to all changes: