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
=== modified file 'hooks/common.py'
--- hooks/common.py 2012-11-02 06:43:18 +0000
+++ hooks/common.py 2012-11-02 06:43:18 +0000
@@ -7,21 +7,22 @@
7import uuid7import uuid
88
9def get_service_user_file(service):9def get_service_user_file(service):
10 return '/var/lib/juju/%s.service_user' % service10 return '/var/lib/juju/%s.service_user2' % service
1111
1212
13def get_service_user(service):13def get_service_user(service):
14 if service == '':14 if service == '':
15 return ''15 return (None, None)
16 sfile = '/var/lib/juju/%s.service_user' % service16 sfile = get_service_user_file(service)
17 if os.path.exists(sfile):17 if os.path.exists(sfile):
18 with open(sfile, 'r') as f:18 with open(sfile, 'r') as f:
19 return f.readline().strip()19 return (f.readline().strip(), f.readline().strip())
20 suser = subprocess.check_output(['pwgen', '-N 1', '15']).strip().split("\n")[0]20 (suser, service_password) = subprocess.check_output(['pwgen', '-N 2', '15']).strip().split("\n")
21 with open(sfile, 'w') as f:21 with open(sfile, 'w') as f:
22 f.write("%s\n" % suser)22 f.write("%s\n" % suser)
23 f.write("%s\n" % service_password)
23 f.flush()24 f.flush()
24 return suser25 return (suser, service_password)
2526
2627
27def cleanup_service_user(service):28def cleanup_service_user(service):
@@ -37,13 +38,16 @@
37database_name = ''38database_name = ''
38if change_unit:39if change_unit:
39 database_name, _ = change_unit.split("/")40 database_name, _ = change_unit.split("/")
41 with open(database_name_file, 'w') as dbnf:
42 dbnf.write("%s\n" % database_name)
43 dbnf.flush()
40elif os.path.exists(database_name_file):44elif os.path.exists(database_name_file):
41 with open(database_name_file, 'r') as dbname:45 with open(database_name_file, 'r') as dbname:
42 database_name = dbname.readline()46 database_name = dbname.readline().strip()
43else:47else:
44 print 'No established database and no REMOTE_UNIT. Exitting gracefully.'48 print 'No established database and no REMOTE_UNIT.'
45# A user per service unit so we can deny access quickly49# A user per service unit so we can deny access quickly
46user = get_service_user(database_name)50user, service_password = get_service_user(database_name)
47connection = None51connection = None
48lastrun_path = '/var/lib/juju/%s.%s.lastrun' % (database_name,user)52lastrun_path = '/var/lib/juju/%s.%s.lastrun' % (database_name,user)
49slave_configured_path = '/var/lib/juju.slave.configured.for.%s' % database_name53slave_configured_path = '/var/lib/juju.slave.configured.for.%s' % database_name
5054
=== modified file 'hooks/db-relation-joined'
--- hooks/db-relation-joined 2012-04-25 08:23:44 +0000
+++ hooks/db-relation-joined 2012-11-02 06:43:18 +0000
@@ -18,24 +18,6 @@
18 print "[%s]" % sql18 print "[%s]" % sql
19 cursor.execute(sql)19 cursor.execute(sql)
2020
21# Determine if we need to create a new database
22if slave and slave_configured and not broken:
23 print "%s exists, assuming configuration done" % slave_configured_path
24 sys.exit(0)
25
26if not slave and not broken and not admin:
27 # Find existing databases
28 cursor.execute("show databases")
29 databases = [i[0] for i in cursor.fetchall()]
30 if database_name in databases:
31 print "database exists, assuming configuration has happened already"
32 sys.exit(0)
33
34# Database is created just before relation-set in case other steps fail
35
36# Create database user and grant access
37service_password = "".join(random.sample(string.letters, 10))
38
39runsql(21runsql(
40 "grant replication client on *.* to `%s` identified by '%s'" % (22 "grant replication client on *.* to `%s` identified by '%s'" % (
41 user,23 user,
@@ -78,11 +60,17 @@
78# Create new database or touch slave.configured file60# Create new database or touch slave.configured file
79if slave:61if slave:
80 open(slave_configured_path,'w').close()62 open(slave_configured_path,'w').close()
81else:63elif not broken and not admin:
82 if not broken and not admin:64 # Find existing databases
83 runsql("create database `%s` character set utf8" % database_name)65 cursor.execute("show databases")
84 with open(database_name_file, 'w') as dbname:66 databases = [i[0] for i in cursor.fetchall()]
85 dbname.write(database_name)67 if database_name in databases:
68 print "database exists already"
69 else:
70 if not broken and not admin:
71 runsql("create database `%s` character set utf8" % database_name)
72 with open(database_name_file, 'w') as dbname:
73 dbname.write(database_name)
8674
87if broken:75if broken:
88 os.unlink(broken_path)76 os.unlink(broken_path)
@@ -97,4 +85,3 @@
97 "password=%s" % service_password,85 "password=%s" % service_password,
98 'host=%s' % hostname,86 'host=%s' % hostname,
99 'slave=%s' % slave,])87 'slave=%s' % slave,])
100
10188
=== modified file 'hooks/master-relation-changed'
--- hooks/master-relation-changed 2012-11-02 06:43:18 +0000
+++ hooks/master-relation-changed 2012-11-02 06:43:18 +0000
@@ -21,7 +21,7 @@
21 fi21 fi
22 if [ "$runit" = "$JUJU_REMOTE_UNIT" ] ; then22 if [ "$runit" = "$JUJU_REMOTE_UNIT" ] ; then
23 remote_ip=$rhost23 remote_ip=$rhost
24 if ch_is_ip $remote_ip ; else24 if ! ch_is_ip $remote_ip ; then
25 remote_ip=$(ch_get_ip $remote_ip)25 remote_ip=$(ch_get_ip $remote_ip)
26 fi26 fi
27 fi27 fi
@@ -66,7 +66,8 @@
66chown -v -R www-data.www-data /var/www66chown -v -R www-data.www-data /var/www
6767
68user=${JUJU_REMOTE_UNIT%%/*}68user=${JUJU_REMOTE_UNIT%%/*}
69action=${0##master-relation-}69action=$(basename $0)
70action=${action##master-relation-}
70case "$action" in71case "$action" in
71changed)72changed)
72 mysql $ROOTARGS -e "GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO \`$user\`@\`$remote_ip\` IDENTIFIED BY '$pass'"73 mysql $ROOTARGS -e "GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO \`$user\`@\`$remote_ip\` IDENTIFIED BY '$pass'"
7374
=== modified file 'hooks/upgrade-charm'
--- hooks/upgrade-charm 2011-12-06 21:17:17 +0000
+++ hooks/upgrade-charm 2012-11-02 06:43:18 +0000
@@ -1,4 +1,11 @@
1#!/bin/sh1#!/bin/sh
2home=`dirname $0`2home=`dirname $0`
3# Remove any existing .service_user files, which will cause
4# new users/pw's to be generated, which is a good thing
5old_service_user_files=$(ls /var/lib/juju/$.service_user)
6if [ -n "$old_service_user_files" ] ; then
7 juju-log -l WARNING "Stale users left around, should be revoked: $(cat $old_service_user_files)"
8 rm -f $old_service_user_files
9fi
3$home/install10$home/install
4exec $home/config-changed11exec $home/config-changed
512
=== modified file 'revision'
--- revision 2012-11-02 06:43:18 +0000
+++ revision 2012-11-02 06:43:18 +0000
@@ -1,1 +1,1 @@
11561164

Subscribers

People subscribed via source and target branches

to all changes: