~jorge/charms/precise/mysql/fix-metadata

« back to all changes in this revision

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

  • Committer: Clint Byrum
  • Date: 2011-03-17 20:43:05 UTC
  • mto: This revision was merged to the branch mainline in revision 38.
  • Revision ID: clint@ubuntu.com-20110317204305-8mfbh9bto61lndcs
- adding master/slave replication
- setting default storage engine to innodb to make replication snaps faster and provide better data integrity

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh -e
 
2
#
 
3
# Master side of master/slave replication
 
4
 
 
5
ROOTARGS="-uroot -p`cat /var/lib/ensemble/mysql.passwd`"
 
6
snapdir=/var/www/snaps
 
7
mkdir -p $snapdir
 
8
apt-get -y install apache2
 
9
# disable wide-open access (restrict to each db IP)
 
10
allowline=`augtool get /files/etc/apache2/sites-available/default/VirtualHost/Directory[2]/directive[4]/arg[2]`
 
11
if [ "$allowline" = "/files/etc/apache2/sites-available/default/VirtualHost/Directory[2]/directive[4]/arg[2] = all" ] ; then
 
12
  # disable allow from all
 
13
  augtool -b <<EOF
 
14
  defvar webroot /files/etc/apache2/sites-available/default/VirtualHost/Directory[2]
 
15
  rm \$webroot/directive[4]
 
16
  save
 
17
EOF
 
18
  service apache2 reload
 
19
fi
 
20
# TODO: settings.. make mmin tunable as it is highly subjective
 
21
recent_backup=`find $snapdir -name 'replication_seed.*.sql.gz' -mmin -60|head -1`
 
22
if [ -z "$recent_backup" ] ; then
 
23
    recent_backup=replication_seed.`date +%Y%m%d%H%M%S`.sql.gz
 
24
    echo `date`: Creating snapshot $recent_backup
 
25
    mysqldump $ROOTARGS --all-databases --single-transaction --master-data |gzip>$snapdir/$recent_backup
 
26
fi
 
27
 
 
28
case $ENSEMBLE_CHANGE in
 
29
joined)
 
30
  ;;
 
31
modified)
 
32
  remote_ip=`relation-get ip`
 
33
  local_ip=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'|head -n 1`
 
34
  augtool -b <<EOF
 
35
  defvar webroot /files/etc/apache2/sites-available/default/VirtualHost/Directory[2]
 
36
  set \$webroot/directive[last()+1] allow
 
37
  set \$webroot/directive[last()]/arg from
 
38
  set \$webroot/directive[last()]/arg $remote_ip/32
 
39
  save
 
40
EOF
 
41
  service apache2 reload
 
42
  mysql $ROOTARGS -e "GRANT REPLICATION_CLIENT ON *.* TO \`$ENSEMBLE_REMOTE_UNIT\`@\`$remote_ip\` IDENTIFIED BY '$pass'"
 
43
  relation-set dumpurl=/snaps/$name \
 
44
                       user=$ENSEMBLE_REMOTE_UNIT \
 
45
                       password=$pass \
 
46
                       ip=$local_ip
 
47
                       port=3306
 
48
  ;;
 
49
departed)
 
50
  remote_ip=`relation-get ip`
 
51
  mysql $ROOTARGS -e "REVOKE PRIVILEGES ON *.* FROM `$ENSEMBLE_REMOTE_UNIT`@`$remote_ip`"
 
52
  # find allow lines
 
53
  for aline in `augtool ls /files/etc/apache2/sites-available/default/VirtualHost/Directory[2]|grep " = allow"|cut -d' ' -f1` ; do
 
54
    ip=`augtool get /files/etc/apache2/sites-available/default/VirtualHost/Directory[2]/$aline/arg[2]`
 
55
    if [ "$ip" = "$remote_ip/32" ] ; then
 
56
      echo -e "rm /files/etc/apache2/sites-available/default/VirtualHost/Directory[2]/$aline\nsave" | augtool -b
 
57
      echo removed $aline from /files/etc/apache2/sites-available/default/VirtualHost/Directory[2]
 
58
      service apache2 reload
 
59
      break
 
60
    fi
 
61
  done
 
62
  ;;
 
63
*)
 
64
  exit 0
 
65
  ;;
 
66
esac