~sinzui/juju-ci-tools/repository

« back to all changes in this revision

Viewing changes to xenial/mysql/hooks/master-relation-departed

  • Committer: Curtis Hovey
  • Date: 2015-11-06 15:43:36 UTC
  • Revision ID: curtis@hovey.name-20151106154336-soncn5peue27jtr0
Added xenial.

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
. /usr/share/charm-helper/sh/net.sh
 
8
 
 
9
ROOTARGS="-uroot -p`cat /var/lib/mysql/mysql.passwd`"
 
10
snapdir=/var/www/snaps
 
11
mkdir -p $snapdir
 
12
apt-get -y install apache2
 
13
# disable wide-open access (restrict to each db IP)
 
14
rhosts=""
 
15
remote_ip=""
 
16
for r in $(relation-ids master) ; do
 
17
    for runit in $(relation-list -r $r) ; do
 
18
        rhost=$(relation-get -r $r hostname $runit)
 
19
        if [ -n "$rhost" ] ; then
 
20
            rhosts=${rhosts},$rhost
 
21
            if [ "$runit" = "$JUJU_REMOTE_UNIT" ] ; then
 
22
                remote_ip=$rhost
 
23
                if ! ch_is_ip $remote_ip ; then
 
24
                    remote_ip=$(ch_get_ip $remote_ip)
 
25
                fi
 
26
            fi
 
27
        fi
 
28
    done
 
29
done
 
30
# remove any leading comma
 
31
rhosts=${rhosts##,}
 
32
 
 
33
# if there are no hosts, we simply disable all access
 
34
target=/etc/apache2/sites-available/mysql-dumps
 
35
cat > $target <<EOF
 
36
DocumentRoot /var/www
 
37
<Directory /var/www>
 
38
  Order deny,allow
 
39
  Deny from all
 
40
EOF
 
41
if [ -n "$rhosts" ] ; then
 
42
cat >> $target <<EOF
 
43
  allow from ${rhosts}
 
44
EOF
 
45
fi
 
46
cat >> $target <<EOF
 
47
  Options -Indexes
 
48
</Directory>
 
49
EOF
 
50
a2ensite mysql-dumps
 
51
a2dissite default
 
52
service apache2 reload
 
53
 
 
54
pass=`pwgen -s 16`
 
55
# TODO: settings.. make mmin tunable as it is highly subjective
 
56
recent_backup=`find $snapdir -name 'replication_seed.*.sql.gz' -mmin -60|head -1`
 
57
if [ -z "$recent_backup" ] ; then
 
58
    name=replication_seed.`date +%Y%m%d%H%M%S`.sql.gz
 
59
    echo `date`: Creating snapshot $recent_backup
 
60
    # Assumes transactional storage -- MyISAM please GO AWAY
 
61
    mysqldump $ROOTARGS --add-drop-database --all-databases --single-transaction --master-data |gzip>$snapdir/$name
 
62
    # Make sure webserver can serve it
 
63
else
 
64
    name=`basename $recent_backup`
 
65
fi
 
66
chown -v -R www-data.www-data /var/www
 
67
 
 
68
user=${JUJU_REMOTE_UNIT%%/*}
 
69
action=$(basename $0)
 
70
action=${action##master-relation-}
 
71
case "$action" in
 
72
changed)
 
73
    mysql $ROOTARGS -e "GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO \`$user\`@\`$remote_ip\` IDENTIFIED BY '$pass'"
 
74
    relation-set dumpurl=/snaps/$name \
 
75
                         user=$user \
 
76
                         password=$pass \
 
77
                         hostname=`unit-get private-address` \
 
78
                         port=3306
 
79
    ;;
 
80
departed)
 
81
    mysql $ROOTARGS -e "REVOKE REPLICATION SLAVE, REPLICATION CLIENT ON *.* FROM `$user`@`$remote_ip`"
 
82
    ;;
 
83
broken)
 
84
    # XXX Need some way to tie users to relation ID
 
85
    ;;
 
86
esac