Merge lp:~clint-fewbar/charms/precise/mysql/fix-db-relation-idempotency into lp:charms/mysql

Proposed by Clint Byrum
Status: Merged
Merged at revision: 87
Proposed branch: lp:~clint-fewbar/charms/precise/mysql/fix-db-relation-idempotency
Merge into: lp:charms/mysql
Prerequisite: lp:~clint-fewbar/charms/precise/mysql/fix-master-hooks
Diff against target: 155 lines (+35/-36)
5 files modified
hooks/common.py (+13/-9)
hooks/db-relation-joined (+11/-24)
hooks/master-relation-changed (+3/-2)
hooks/upgrade-charm (+7/-0)
revision (+1/-1)
To merge this branch: bzr merge lp:~clint-fewbar/charms/precise/mysql/fix-db-relation-idempotency
Reviewer Review Type Date Requested Status
charmers Pending
Review via email: mp+132630@code.launchpad.net

Description of the change

Often when one removes/adds the db relations from mysql, the add fails to work because the joined hook considers an existing database a reason to not do grants/relation-set. This is a big problem though, as it makes using another database service with the same data in it fail. This fix corrects that.

To post a comment you must log in.
Revision history for this message
Clint Byrum (clint-fewbar) wrote :

Note that this was retracted the first time a bit while I figured out a better scheme.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/common.py'
2--- hooks/common.py 2012-11-02 06:43:18 +0000
3+++ hooks/common.py 2012-11-02 06:43:18 +0000
4@@ -7,21 +7,22 @@
5 import uuid
6
7 def get_service_user_file(service):
8- return '/var/lib/juju/%s.service_user' % service
9+ return '/var/lib/juju/%s.service_user2' % service
10
11
12 def get_service_user(service):
13 if service == '':
14- return ''
15- sfile = '/var/lib/juju/%s.service_user' % service
16+ return (None, None)
17+ sfile = get_service_user_file(service)
18 if os.path.exists(sfile):
19 with open(sfile, 'r') as f:
20- return f.readline().strip()
21- suser = subprocess.check_output(['pwgen', '-N 1', '15']).strip().split("\n")[0]
22+ return (f.readline().strip(), f.readline().strip())
23+ (suser, service_password) = subprocess.check_output(['pwgen', '-N 2', '15']).strip().split("\n")
24 with open(sfile, 'w') as f:
25 f.write("%s\n" % suser)
26+ f.write("%s\n" % service_password)
27 f.flush()
28- return suser
29+ return (suser, service_password)
30
31
32 def cleanup_service_user(service):
33@@ -37,13 +38,16 @@
34 database_name = ''
35 if change_unit:
36 database_name, _ = change_unit.split("/")
37+ with open(database_name_file, 'w') as dbnf:
38+ dbnf.write("%s\n" % database_name)
39+ dbnf.flush()
40 elif os.path.exists(database_name_file):
41 with open(database_name_file, 'r') as dbname:
42- database_name = dbname.readline()
43+ database_name = dbname.readline().strip()
44 else:
45- print 'No established database and no REMOTE_UNIT. Exitting gracefully.'
46+ print 'No established database and no REMOTE_UNIT.'
47 # A user per service unit so we can deny access quickly
48-user = get_service_user(database_name)
49+user, service_password = get_service_user(database_name)
50 connection = None
51 lastrun_path = '/var/lib/juju/%s.%s.lastrun' % (database_name,user)
52 slave_configured_path = '/var/lib/juju.slave.configured.for.%s' % database_name
53
54=== modified file 'hooks/db-relation-joined'
55--- hooks/db-relation-joined 2012-04-25 08:23:44 +0000
56+++ hooks/db-relation-joined 2012-11-02 06:43:18 +0000
57@@ -18,24 +18,6 @@
58 print "[%s]" % sql
59 cursor.execute(sql)
60
61-# Determine if we need to create a new database
62-if slave and slave_configured and not broken:
63- print "%s exists, assuming configuration done" % slave_configured_path
64- sys.exit(0)
65-
66-if not slave and not broken and not admin:
67- # Find existing databases
68- cursor.execute("show databases")
69- databases = [i[0] for i in cursor.fetchall()]
70- if database_name in databases:
71- print "database exists, assuming configuration has happened already"
72- sys.exit(0)
73-
74-# Database is created just before relation-set in case other steps fail
75-
76-# Create database user and grant access
77-service_password = "".join(random.sample(string.letters, 10))
78-
79 runsql(
80 "grant replication client on *.* to `%s` identified by '%s'" % (
81 user,
82@@ -78,11 +60,17 @@
83 # Create new database or touch slave.configured file
84 if slave:
85 open(slave_configured_path,'w').close()
86-else:
87- if not broken and not admin:
88- runsql("create database `%s` character set utf8" % database_name)
89- with open(database_name_file, 'w') as dbname:
90- dbname.write(database_name)
91+elif not broken and not admin:
92+ # Find existing databases
93+ cursor.execute("show databases")
94+ databases = [i[0] for i in cursor.fetchall()]
95+ if database_name in databases:
96+ print "database exists already"
97+ else:
98+ if not broken and not admin:
99+ runsql("create database `%s` character set utf8" % database_name)
100+ with open(database_name_file, 'w') as dbname:
101+ dbname.write(database_name)
102
103 if broken:
104 os.unlink(broken_path)
105@@ -97,4 +85,3 @@
106 "password=%s" % service_password,
107 'host=%s' % hostname,
108 'slave=%s' % slave,])
109-
110
111=== modified file 'hooks/master-relation-changed'
112--- hooks/master-relation-changed 2012-11-02 06:43:18 +0000
113+++ hooks/master-relation-changed 2012-11-02 06:43:18 +0000
114@@ -21,7 +21,7 @@
115 fi
116 if [ "$runit" = "$JUJU_REMOTE_UNIT" ] ; then
117 remote_ip=$rhost
118- if ch_is_ip $remote_ip ; else
119+ if ! ch_is_ip $remote_ip ; then
120 remote_ip=$(ch_get_ip $remote_ip)
121 fi
122 fi
123@@ -66,7 +66,8 @@
124 chown -v -R www-data.www-data /var/www
125
126 user=${JUJU_REMOTE_UNIT%%/*}
127-action=${0##master-relation-}
128+action=$(basename $0)
129+action=${action##master-relation-}
130 case "$action" in
131 changed)
132 mysql $ROOTARGS -e "GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO \`$user\`@\`$remote_ip\` IDENTIFIED BY '$pass'"
133
134=== modified file 'hooks/upgrade-charm'
135--- hooks/upgrade-charm 2011-12-06 21:17:17 +0000
136+++ hooks/upgrade-charm 2012-11-02 06:43:18 +0000
137@@ -1,4 +1,11 @@
138 #!/bin/sh
139 home=`dirname $0`
140+# Remove any existing .service_user files, which will cause
141+# new users/pw's to be generated, which is a good thing
142+old_service_user_files=$(ls /var/lib/juju/$.service_user)
143+if [ -n "$old_service_user_files" ] ; then
144+ juju-log -l WARNING "Stale users left around, should be revoked: $(cat $old_service_user_files)"
145+ rm -f $old_service_user_files
146+fi
147 $home/install
148 exec $home/config-changed
149
150=== modified file 'revision'
151--- revision 2012-11-02 06:43:18 +0000
152+++ revision 2012-11-02 06:43:18 +0000
153@@ -1,1 +1,1 @@
154-156
155+164

Subscribers

People subscribed via source and target branches

to all changes: