~charmers/charms/oneiric/mediawiki/trunk

« back to all changes in this revision

Viewing changes to formulas/mysql/hooks/master-relation-changed

  • Committer: Clint Byrum
  • Date: 2011-05-11 12:54:21 UTC
  • mfrom: (35.2.5 add-mysql-replication)
  • Revision ID: clint@ubuntu.com-20110511125421-f8jqbf1ihxd15j4a
Merging mysql one-way-replication relations

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
# Master side of master/slave replication
 
4
 
 
5
set -e
 
6
 
 
7
ROOTARGS="-uroot -p`cat /var/lib/ensemble/mysql.passwd`"
 
8
snapdir=/var/www/snaps
 
9
mkdir -p $snapdir
 
10
apt-get -y install apache2
 
11
# disable wide-open access (restrict to each db IP)
 
12
allowline=`augtool get /files/etc/apache2/sites-available/default/VirtualHost/Directory[2]/directive[4]/arg[2]`
 
13
if [ "$allowline" = "/files/etc/apache2/sites-available/default/VirtualHost/Directory[2]/directive[4]/arg[2] = all" ] ; then
 
14
  # disable allow from all
 
15
  augtool -b <<EOF
 
16
  defvar webroot /files/etc/apache2/sites-available/default/VirtualHost/Directory[2]
 
17
  rm \$webroot/directive[4]
 
18
  save
 
19
EOF
 
20
  service apache2 reload
 
21
fi
 
22
 
 
23
# TODO: settings.. make mmin tunable as it is highly subjective
 
24
recent_backup=`find $snapdir -name 'replication_seed.*.sql.gz' -mmin -60|head -1`
 
25
if [ -z "$recent_backup" ] ; then
 
26
    name=replication_seed.`date +%Y%m%d%H%M%S`.sql.gz
 
27
    echo `date`: Creating snapshot $recent_backup
 
28
    # Assumes transactional storage -- MyISAM please GO AWAY
 
29
    mysqldump $ROOTARGS --all-databases --single-transaction --master-data |gzip>$snapdir/$name
 
30
    # Make sure webserver can serve it
 
31
else
 
32
    name=`basename $recent_backup`
 
33
fi
 
34
chown -v -R www-data.www-data /var/www
 
35
 
 
36
remote_ip=`relation-get ip`
 
37
test -n "$remote_ip" || exit 1
 
38
local_ip=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'|head -n 1`
 
39
pass=`pwgen -s 16`
 
40
augtool -b <<EOF
 
41
defvar webroot /files/etc/apache2/sites-available/default/VirtualHost/Directory[2]
 
42
set \$webroot/directive[last()+1] allow
 
43
set \$webroot/directive[last()]/arg[1] from
 
44
set \$webroot/directive[last()]/arg[2] $remote_ip/32
 
45
save
 
46
EOF
 
47
service apache2 reload
 
48
mysql $ROOTARGS -e "GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO \`$ENSEMBLE_REMOTE_UNIT\`@\`$remote_ip\` IDENTIFIED BY '$pass'"
 
49
relation-set dumpurl=/snaps/$name \
 
50
                     user=$ENSEMBLE_REMOTE_UNIT \
 
51
                     password=$pass \
 
52
                     ip=$local_ip \
 
53
                     port=3306