Skip to content

Commit

Permalink
Adding MySQL Fabric wrapper script for rapid test deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
Jervin R committed Apr 29, 2014
1 parent 90bbcd3 commit 3e558bf
Show file tree
Hide file tree
Showing 2 changed files with 286 additions and 0 deletions.
52 changes: 52 additions & 0 deletions mysql-fabric/fabric.cfg-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[DEFAULT]
prefix = WORK_DIR
sysconfdir = FABRIC_SYSCONFDIR
logdir = WORK_DIR

[storage]
address = 127.0.0.1:FABRIC_STORE_PORT
user = fabric
password = fabric
database = fabric
connection_timeout = 6
connection_attempts = 6
connection_delay = 1

[servers]
user = fabric
password = fabric

[protocol.xmlrpc]
address = localhost:32274
threads = 5
user = admin
password = admin
disable_authentication = no
realm = MySQL Fabric
ssl_ca =
ssl_cert =
ssl_key =

[executor]
executors = 5

[logging]
level = INFO
url = file://WORK_DIR/fabric.log

[sharding]
mysqldump_program = /usr/bin/mysqldump
mysqlclient_program = /usr/bin/mysql

[connector]
ttl = 1

[failure_tracking]
notifications = 300
notification_clients = 50
notification_interval = 60
failover_interval = 0
detections = 3
detection_interval = 6
detection_timeout = 1
prune_time = 3600
234 changes: 234 additions & 0 deletions mysql-fabric/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
#!/bin/env bash

WORK_DIR=$1
MYSQL_UTILITIES_URL=http://mysql.mirrors.hoobly.com/Downloads/MySQLGUITools/mysql-utilities-1.4.2.tar.gz
MYSQL_CONNECTOR_URL=http://mysql.mirrors.hoobly.com/Downloads/Connector-Python/mysql-connector-python-1.2.1.tar.gz

_echo() {
echo "$(date +%Y-%m-%d_%H_%M_%S) fabric-init $1"
}

_die() {
_echo $1
kill -INT $$
}

_run() {
local _cmd=$1
local _ret=$2

$_cmd
_val=$?

if [ "x$_val" != "x$_ret" ]; then
_die "FATAL: '$_cmd' failed!"
fi
}

_workdir_valid() {
if [ "x$1" == "x" ]; then
_die 'FATAL: Specified WORK_DIR is empty!'
fi
}

_workon() {
local WORK_DIR=${1:-$FABRIC_WORK_DIR}
_workdir_valid $WORK_DIR
cd $WORK_DIR
> .vars
echo "export PYTHONPATH=$PYTHONPATH" >> .vars
echo "export PATH=$PATH" >> .vars
echo "PS1='$PS1'" >> .vars
echo "FABRIC_WORK_DIR=" >> .vars

export PATH=$WORK_DIR/utils/usr/bin:$PATH
export PYTHONPATH=.:$(find $WORK_DIR -type d -name *site-packages)
export PS1='fabric> '
export FABRIC_WORK_DIR=$WORK_DIR
}

_eod() {
source ./.vars
}

_init_mysql() {
local WORK_DIR=${1:-$FABRIC_WORK_DIR}
_workdir_valid $WORK_DIR
cd $WORK_DIR

local VERSION=$2
local FGROUPS=${3:-2}
local BASE_PORT=${4:-17600}

for sb in $(seq 1 $FGROUPS); do
make_replication_sandbox --replication_directory=fabric_group_$sb \
--sandbox_base_port=$BASE_PORT --how_many_slaves=1 \
--upper_directory=$WORK_DIR --node_options='--my_clause=gtid_mode=ON --my_clause=log-bin=mysql-bin --my_clause=log-slave-updates --my_clause=enforce-gtid-consistency' \
$VERSION

$WORK_DIR/fabric_group_$sb/m -uroot -pmsandbox \
-BNe "GRANT ALL ON *.* TO 'fabric'@'127.0.0.1' IDENTIFIED BY 'fabric'"
BASE_PORT=$(($BASE_PORT+2))
done

make_sandbox $VERSION -- --no_show --sandbox_directory=fabric_store \
--sandbox_port=$BASE_PORT --upper_directory=$WORK_DIR
$WORK_DIR/fabric_store/use -uroot -pmsandbox \
-BNe "GRANT ALL ON fabric.* TO 'fabric'@'127.0.0.1' IDENTIFIED BY 'fabric'"
}

_init_fabric() {
local WORK_DIR=${1:-$FABRIC_WORK_DIR}
_workdir_valid $WORK_DIR
cd $WORK_DIR

_teardown_fabric $WORK_DIR
FABRIC_PORT=$(cat $WORK_DIR/fabric_store/my.sandbox.cnf|grep -E '^port'|awk '{print $3}'|head -n1)

$WORK_DIR/fabric_store/use -uroot -pmsandbox \
-BNe "CREATE DATABASE IF NOT EXISTS fabric"

mkdir -p $WORK_DIR/utils/usr/etc/mysql && \
cat $WORK_DIR/fabric.cfg-template > $WORK_DIR/utils/usr/etc/mysql/fabric.cfg && \
sed -i 's,WORK_DIR,'"$WORK_DIR"',' $WORK_DIR/utils/usr/etc/mysql/fabric.cfg && \
sed -i 's,FABRIC_SYSCONFDIR,'"$WORK_DIR/utils/usr/etc/mysql"',' $WORK_DIR/utils/usr/etc/mysql/fabric.cfg && \
sed -i 's,FABRIC_STORE_PORT,'"$FABRIC_PORT"',' $WORK_DIR/utils/usr/etc/mysql/fabric.cfg && \
_echo "INFO: Setting up Fabric Storage System" && \
mysqlfabric manage setup && \
_echo "INFO: Starting Fabric server" && \
mysqlfabric manage start --daemonize && \
mysqlfabric manage ping

local fabric_is_running=$?
if [ "x$fabric_is_running" == "x0" ]; then
for d in $(ls $WORK_DIR|grep fabric_group_); do
mysqlfabric group create $d
mysqlfabric group add $d 127.0.0.1:$(cat $WORK_DIR/$d/master/my.sandbox.cnf|grep -E '^port'|awk '{print $3}'|head -n1)
mysqlfabric group add $d 127.0.0.1:$(cat $WORK_DIR/$d/node1/my.sandbox.cnf|grep -E '^port'|awk '{print $3}'|head -n1)
done

mysqlfabric group lookup_groups
fi
}

_teardown_mysql() {
local WORK_DIR=${1:-$FABRIC_WORK_DIR}
_workdir_valid $WORK_DIR
cd $WORK_DIR

_teardown_fabric $WORK_DIR

_echo "INFO: Tearing down Fabric nodes"
for sb in $(ls $WORK_DIR|grep fabric_group_); do
sbtool -o delete --source_dir=$WORK_DIR/$(basename $sb)
done

_echo "INFO: Tearing down Fabric store node"
sbtool -o delete --source_dir=$WORK_DIR/fabric_store
}

_teardown_fabric() {
local WORK_DIR=${1:-$FABRIC_WORK_DIR}
_workdir_valid $WORK_DIR
cd $WORK_DIR

_echo "INFO: Tearing down Fabric storage"

if [ -f $WORK_DIR/utils/usr/etc/mysql/fabric.cfg ]; then
mysqlfabric manage stop && \
mysqlfabric manage teardown && \
rm -rf $WORK_DIR/utils/usr/etc/mysql/fabric.cfg
fi
}

_init_utils() {
local WORK_DIR=${1:-$FABRIC_WORK_DIR}
_workdir_valid $WORK_DIR
cd $WORK_DIR
_echo "INFO: Preparing mysql-utilities"
local TARBALL=$(basename $MYSQL_UTILITIES_URL)
local UTILDIR=$(echo $TARBALL|sed -e 's/.tar.gz//g')

if [ ! -f $TARBALL ]; then
_run "wget $MYSQL_UTILITIES_URL" 0
fi

tar xzvf $TARBALL && \
mv -f $UTILDIR utils && \
cd utils && \
python setup.py build && \
python setup.py install --root=$WORK_DIR/utils && \
cd ../

_echo "INFO: Preparing MySQL Connector-Python"
local TARBALL=$(basename $MYSQL_CONNECTOR_URL)
local CONRDIR=$(echo $TARBALL|sed -e 's/.tar.gz//g')

if [ ! -f $TARBALL ]; then
_run "wget $MYSQL_CONNECTOR_URL" 0
fi

tar xzvf $TARBALL && \
cd $CONRDIR && \
python setup.py build && \
python setup.py install --root=$WORK_DIR/utils && \
cd ../

[ "x" != "x$FABRIC_WORK_DIR" ] && _eod && _workon $WORK_DIR
}

_teardown() {
local WORK_DIR=${1:-$FABRIC_WORK_DIR}
_workdir_valid $WORK_DIR
cd $WORK_DIR

_echo "INFO: Tearing down Fabric environment!"
_teardown_mysql $WORK_DIR
rm -rf $WORK_DIR/utils
rm -rf $WORK_DIR/*.log
rm -rf $WORK_DIR/mysql-connector-*
rm -rf $WORK_DIR/mysql-utilities-*
_eod
}

_init_all() {
local WORK_DIR=${1:-$FABRIC_WORK_DIR}
_workdir_valid $WORK_DIR
cd $WORK_DIR

_teardown $WORK_DIR
_workon $WORK_DIR
_init_utils $WORK_DIR
_init_mysql $WORK_DIR 5.6.17 2
_init_fabric $WORK_DIR
[ "x" == "x$FABRIC_WORK_DIR" ] && _eod && _workon $WORK_DIR
}

_echo "Fabric System Wrapper"
_echo "---------------------"
_echo ""
_echo "Initializer ready for these commands:"
_echo ""
_echo "_init_all <WORK_DIR>"
_echo " Meta function the bootstraps everything from 0 to a full running"
_echo " Fabric system using 5.6.17 and 2 groups. Your system should be able"
_echo " create MySQL sandboxes properly with 'make_sandbox 5.6.17' command"
_echo "_teardown [<WORK_DIR>]"
_echo " Teardown the whole Fabric system, WORK_DIR is optional if you are"
_echo " operating inside a Fabric environment via _workon"
_echo "_workon <WORK_DIR>"
_echo " Setup the environment for this Fabric cluster within WORK_DIR"
_echo "_init_utils [<WORK_DIR>]"
_echo " Setup MySQL Utilities inside WORK_DIR"
_echo "_init_mysql <WORK_DIR> <SANDBOX_VERSION> <NUMBER_OF_GROUPS>"
_echo " Setup the MySQL nodes that will consist of Fabric groups and storage."
_echo " This function uses MySQL Sandbox and accepts the MySQL version within"
_echo " you \$SANDBOX_BINARY directory"
_echo "_init_fabric [<WORK_DIR>]"
_echo " Setup and start the Fabric system then create and add the HA groups"
_echo " from _init_mysql"
_echo "_eod"
_echo " Exits the current Fabric environment which you have entered with '_workon'"
_echo "GOOD LUCK!"


0 comments on commit 3e558bf

Please sign in to comment.