Merge lp:~clint-fewbar/charms/precise/mysql/add-binlog-format into lp:charms/mysql

Proposed by Clint Byrum
Status: Merged
Approved by: Marco Ceppi
Approved revision: 73
Merged at revision: 73
Proposed branch: lp:~clint-fewbar/charms/precise/mysql/add-binlog-format
Merge into: lp:charms/mysql
Diff against target: 174 lines (+71/-42)
6 files modified
config.yaml (+4/-0)
hooks/common.py (+13/-9)
hooks/config-changed (+51/-18)
hooks/db-relation-joined (+2/-0)
hooks/install (+0/-14)
revision (+1/-1)
To merge this branch: bzr merge lp:~clint-fewbar/charms/precise/mysql/add-binlog-format
Reviewer Review Type Date Requested Status
Marco Ceppi (community) Approve
Review via email: mp+103427@code.launchpad.net

Description of the change

Adds the ability to set the binlog format, and will also turn off binlogging in 'fast' tuning.

To post a comment you must log in.
Revision history for this message
Marco Ceppi (marcoceppi) wrote :

This looks great!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'config.yaml'
2--- config.yaml 2011-12-06 02:01:20 +0000
3+++ config.yaml 2012-04-25 09:05:26 +0000
4@@ -23,3 +23,7 @@
5 default: -1
6 type: int
7 description: Maximum connections to allow. -1 means use the server's compiled in default.
8+ binlog-format:
9+ default: 'MIXED'
10+ type: string
11+ description: If binlogging is enabled, this is the format that will be used. Ignored when tuning-level == fast.
12
13=== modified file 'hooks/common.py'
14--- hooks/common.py 2011-12-05 20:17:48 +0000
15+++ hooks/common.py 2012-04-25 09:05:26 +0000
16@@ -1,6 +1,7 @@
17 # vim: syntax=python
18
19 import os
20+import sys
21 import MySQLdb
22 import subprocess
23 import uuid
24@@ -25,17 +26,20 @@
25 os.unlink(get_service_user_file(service))
26
27
28-try:
29- change_unit = os.environ['JUJU_REMOTE_UNIT']
30-except KeyError:
31- pass
32-
33-if len(change_unit) == 0:
34- # XXX hack to work around https://launchpad.net/bugs/791042
35- change_unit = subprocess.check_output(['relation-list']).strip().split("\n")[0]
36+relation_id = os.environ.get('JUJU_RELATION_ID')
37+change_unit = os.environ.get('JUJU_REMOTE_UNIT')
38
39 # We'll name the database the same as the service.
40-database_name, _ = change_unit.split("/")
41+database_name_file = '.%s_database_name' % (relation_id)
42+# change_unit will be None on broken hooks
43+if change_unit:
44+ database_name, _ = change_unit.split("/")
45+elif os.path.exists(database_name_file):
46+ with open(database_name_file, 'r') as dbname:
47+ database_name = dbname.readline()
48+else:
49+ print 'No established database and no REMOTE_UNIT. Exitting gracefully.'
50+ sys.exit(0)
51 # A user per service unit so we can deny access quickly
52 user = get_service_user(database_name)
53 connection = None
54
55=== modified file 'hooks/config-changed'
56--- hooks/config-changed 2011-12-06 21:17:17 +0000
57+++ hooks/config-changed 2012-04-25 09:05:26 +0000
58@@ -211,23 +211,56 @@
59 !includedir /etc/mysql/conf.d/
60 """
61
62+i_am_a_slave = os.path.isfile('/var/lib/juju/i.am.a.slave')
63+unit_id = os.environ['JUJU_UNIT_NAME'].split('/')[1]
64+
65+if not i_am_a_slave and configs['tuning-level'] == 'fast':
66+ binlog_cnf = ''
67+else:
68+ # On slaves, this gets overwritten
69+ binlog_template = """
70+[mysqld]
71+server_id = %s
72+log_bin = /var/log/mysql/mysql-bin.log
73+binlog_format = %s
74+"""
75+
76+ binlog_cnf = binlog_template % (unit_id,
77+ configs.get('binlog_format','MIXED'))
78+
79 mycnf=template % configs
80-with tempfile.NamedTemporaryFile(mode='w',dir='/etc/mysql',delete=False) as t:
81- t.write(mycnf)
82- t.flush()
83- tmd5 = hashlib.md5()
84- tmd5.update(mycnf)
85- with open('/etc/mysql/my.cnf','r') as old:
86- md5=hashlib.md5()
87- md5.update(old.read())
88- oldhash = md5.digest()
89- if oldhash != tmd5.digest():
90- os.rename('/etc/mysql/my.cnf','/etc/mysql/my.cnf.%s' % md5.hexdigest())
91- os.rename(t.name, '/etc/mysql/my.cnf')
92- try:
93- check_call(['service','mysql','stop'])
94- except subprocess.CalledProcessError:
95- pass
96- check_call(['service','mysql','start'])
97+
98+targets = {'/etc/mysql/conf.d/binlog.cnf': binlog_cnf,
99+ '/etc/mysql/my.cnf': mycnf,
100+ }
101+
102+need_restart = False
103+for target,content in targets.iteritems():
104+ tdir = os.path.dirname(target)
105+ if len(content) == 0 and os.path.exists(target):
106+ os.unlink(target)
107+ need_restart = True
108+ continue
109+ with tempfile.NamedTemporaryFile(mode='w',dir=tdir,delete=False) as t:
110+ t.write(content)
111+ t.flush()
112+ tmd5 = hashlib.md5()
113+ tmd5.update(content)
114+ if os.path.exists(target):
115+ with open(target,'r') as old:
116+ md5=hashlib.md5()
117+ md5.update(old.read())
118+ oldhash = md5.digest()
119+ if oldhash != tmd5.digest():
120+ os.rename(target,'%s.%s' % (target, md5.hexdigest()))
121+ need_restart = True
122 else:
123- check_call(['juju-log','my.cnf not changed, skipping restart/replace'])
124+ need_restart = True
125+ os.rename(t.name, target)
126+
127+if need_restart:
128+ try:
129+ check_call(['service','mysql','stop'])
130+ except subprocess.CalledProcessError:
131+ pass
132+ check_call(['service','mysql','start'])
133
134=== modified file 'hooks/db-relation-joined'
135--- hooks/db-relation-joined 2011-11-29 01:36:56 +0000
136+++ hooks/db-relation-joined 2012-04-25 09:05:26 +0000
137@@ -81,6 +81,8 @@
138 else:
139 if not broken and not admin:
140 runsql("create database `%s` character set utf8" % database_name)
141+ with open(database_name_file, 'w') as dbname:
142+ dbname.write(database_name)
143
144 if broken:
145 os.unlink(broken_path)
146
147=== modified file 'hooks/install'
148--- hooks/install 2011-12-20 06:13:08 +0000
149+++ hooks/install 2012-04-25 09:05:26 +0000
150@@ -13,17 +13,3 @@
151 echo mysql-server-5.1 mysql-server/root_password_again password $PASSWORD | debconf-set-selections
152
153 DEBIAN_FRONTEND=noninteractive apt-get -y install -qq mysql-server
154-
155-unit_id=`echo $JUJU_UNIT_NAME | cut -d/ -f2`
156-
157-# On slaves, this gets overwritten
158-if [ ! -f /var/lib/juju/i.am.a.slave ] ; then
159- cat > /etc/mysql/conf.d/binlog.cnf <<EOF
160-[mysqld]
161-server_id = $unit_id
162-log_bin = /var/log/mysql/mysql-bin.log
163-EOF
164-fi
165-
166-service mysql stop || :
167-service mysql start
168
169=== modified file 'revision'
170--- revision 2011-12-06 21:17:17 +0000
171+++ revision 2012-04-25 09:05:26 +0000
172@@ -1,1 +1,1 @@
173-118
174+121

Subscribers

People subscribed via source and target branches

to all changes: