Files
percona-toolkit/sandbox/test-env

380 lines
11 KiB
Bash
Executable File

#!/bin/sh
# This script controls the Percona Toolkit test environment. The basic
# environment is a master on port 12345 in /tmp/12345 and a slave on port
# 12346 in /tmp/12346. This script attempts to ensure that all environment
# vars like PERCONA_TOOLKIT_BRANCH and PERCONA_TOOLKIT_SANDBOX are correct.
# Exist 0 on success/no errors, or 1 on any warnings or errors.
err() {
echo
for msg; do
echo "$msg" >&2
done
}
usage() {
err "Usage: test-env start|stop|restart|status|checkconfig|reset|kill" \
"" \
" start Start test servers in /tmp/PORT" \
" stop Stop test servers and remvoe all /tmp/PORT" \
" kill Kill test servers (use if stop fails)" \
" restart Stop and start test servers" \
" status Print status of test servers" \
" checkconfig Check test env and test servers" \
" reset Reset test server binary logs" \
" version Print MySQL version of running test servers" \
""
}
mysql_basedir_ok() {
local basedir=$1
if [ ! -d "$basedir" ] || [ ! -d "$basedir/bin" ]; then
return 0
fi
if [ ! -x "$basedir/bin/mysqld_safe" ]; then
return 0
fi
return 1 # basedir is ok
}
set_mysql_basedir() {
# Auto-detect basedir if its not explicitly set.
if [ -z "$PERCONA_TOOLKIT_SANDBOX" ]; then
BASEDIR_AUTO_DETECTED=1
mysqld=`which mysqld 2>/dev/null`
if [ -n "$mysqld" ]; then
PERCONA_TOOLKIT_SANDBOX=`$mysqld --verbose --help 2>/dev/null | grep 'basedir ' | awk '{print $2}'`
if [ -z "$PERCONA_TOOLKIT_SANDBOX" ]; then
return 0
fi
else
return 0
fi
else
if [ -x "$PERCONA_TOOLKIT_SANDBOX/bin/mysqld" ]; then
mysqld="$PERCONA_TOOLKIT_SANDBOX/bin/mysqld"
elif [ -x "$PERCONA_TOOLKIT_SANDBOX/sbin/mysqld" ]; then
mysqld="$PERCONA_TOOLKIT_SANDBOX/sbin/mysqld"
elif [ -x "$PERCONA_TOOLKIT_SANDBOX/libexec/mysqld" ]; then
mysqld="$PERCONA_TOOLKIT_SANDBOX/libexec/mysqld"
else
err "Cannot find executable mysqld in $PERCONA_TOOLKIT_SANDBOX/bin, $PERCONA_TOOLKIT_SANDBOX/sbin or $PERCONA_TOOLKIT_SANDBOX/libexec."
return 0
fi
fi
mysql_basedir_ok $PERCONA_TOOLKIT_SANDBOX
local basedir_ok=$?
if [ $basedir_ok -eq 1 ]; then
export PERCONA_TOOLKIT_SANDBOX=$PERCONA_TOOLKIT_SANDBOX
fi
return $basedir_ok
}
checkconfig() {
local print_conf=$1
local stat=""
conf_err=0
if [ -z "$PERCONA_TOOLKIT_BRANCH" ] || [ ! -d "$PERCONA_TOOLKIT_BRANCH" ]; then
conf_err=1
stat="INVALID"
else
stat="ok"
fi
if [ $print_conf ]; then
echo "PERCONA_TOOLKIT_BRANCH=$PERCONA_TOOLKIT_BRANCH - $stat"
fi
set_mysql_basedir
if [ $? -ne 1 ]; then
conf_err=1
stat="INVALID"
else
stat="ok"
fi
if [ $print_conf ]; then
echo -n "PERCONA_TOOLKIT_SANDBOX=$PERCONA_TOOLKIT_SANDBOX - $stat"
if [ -n "$BASEDIR_AUTO_DETECTED" ]; then
echo " (auto-detected)"
else
echo
fi
fi
return $conf_err
}
sandbox_status() {
local type=$1
local port=$2
local master_port=$3
local status=0 # sandbox is ok, no problems
echo "MySQL $type test server on port $port:"
echo -n " PID file exists - "
if [ -f "/tmp/$port/data/mysql_sandbox$port.pid" ]; then
echo "yes"
echo -n " PID file has a PID - "
local pid=`cat /tmp/$port/data/mysql_sandbox$port.pid 2>/dev/null`
if [ -n "$pid" ]; then
echo "yes"
echo -n " process $pid is alive - "
kill -0 $pid >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "yes"
else
echo "NO"
status=1
fi
else
echo "NO"
status=1
fi
else
echo "NO"
status=1
fi
echo -n " MySQL is alive - "
$PERCONA_TOOLKIT_SANDBOX/bin/mysqladmin --defaults-file="/tmp/$port/my.sandbox.cnf" ping >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "yes"
if [ "$version" != "4.0" ] && [ "$version" != "4.1" ]; then
echo -n " sakila db is loaded - "
/tmp/$port/use -e 'show databases like "sakila"' 2>/dev/null | grep sakila >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "yes"
else
echo "NO"
status=1
fi
fi
if [ "$type" = "slave" ]; then
echo -n " slave is running - "
# Slave status should show:
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes
local slave_running=`/tmp/$port/use -e 'show slave status\G' 2>/dev/null | grep Running | grep -c Yes`
if [ $slave_running -eq 2 ]; then
echo "yes"
else
echo "NO"
status=1
fi
if [ -n "$master_port" ]; then
echo -n " slave to master $master_port - "
local mp=`/tmp/$port/use -e 'show slave status\G' 2>/dev/null | grep Master_Port | awk '{print $2}'`
if [ "$mp" = "$master_port" ]; then
echo "yes"
else
echo "NO"
status=1
fi
fi
fi
else
echo "NO"
status=1
fi
return $status
}
sandbox_is_running() {
local p=$1
ps axw | grep mysqld | grep -v grep | grep /tmp/$p >/dev/null
}
kill_sandbox() {
local p=$1
local rmdir=1
# See if the sandbox server is running.
sandbox_is_running $p
if [ $? -eq 0 ]; then
# Try to kill it with mysqladmin shutdown. We try different
# user/pass because sometimes a test can bork acct privs.
mysqladmin -h127.1 -P$p -umsandbox -pmsandbox shutdown >/dev/null 2>&1
mysqladmin -h127.1 -P$p -uroot -pmsandbox shutdown >/dev/null 2>&1
mysqladmin -h127.1 -P$p -uroot shutdown >/dev/null 2>&1
# See if the sandbox server is still running.
sandbox_is_running $p
if [ $? -eq 0 ]; then
# Kill both mysqld_safe and mysqld.
pid1=`ps axw | grep mysqld_safe | grep /tmp/$p | awk '{print $1}'`
pid2=`ps axw | grep mysqld | grep -v mysqld_safe | grep /tmp/$p | awk '{print $1}'`
kill -9 $pid1 # Die, damn you, die!
kill -9 $pid2
sleep 1
# Third and finaly check if the sandbox server is running.
sandbox_is_running $p
if [ $? -eq 0 ]; then
err "Failed to kill MySQL test server on port $p (PID $pid1, $pid2)"
rmdir=0
else
echo "Killed MySQL test server on port $p (PID $pid1, $pid2)"
fi
else
echo "Killed MySQL test server on port $p"
fi
fi
if [ $rmdir -eq 1 ] && [ -d "/tmp/$p" ]; then
rm -rf /tmp/$p
echo "Removed /tmp/$p"
fi
return
}
MYSQL_VERSION=""
set_mysql_version() {
if [ -d /tmp/12345 ] && [ -f /tmp/12345/use ]; then
MYSQL_VERSION=$(/tmp/12345/use -N -e "select version()" | head -n 2 | tail -n 1 | cut -d'.' -f1,2)
fi
}
# ###########################################################################
# Sanity check the cmd line options.
# ###########################################################################
if [ $# -lt 1 ]; then
usage
exit 1
fi
opt=$1
# ###########################################################################
# Process the option.
# ###########################################################################
exit_status=0
if [ $opt = 'checkconfig' ]; then
checkconfig 1
echo -n "Percona Toolkit test environment config is "
if [ $conf_err -eq 0 ]; then
echo "ok!"
exit 0
else
echo "invalid."
exit 1
fi
else
checkconfig
if [ $conf_err -eq 1 ]; then
err "The Percona Toolkit test environment config is invalid." \
"Run '$0 checkconfig' to see the current configuration."
exit 1
fi
fi
# Determine the MySQL version.
# bin/mysqld -V should print something like:
# /usr/bin/mysqld Ver 5.1.34 for linux-gnu on x86_64 (MySQL Community Server)
version=`$mysqld -V | awk '{print $3}' | cut -d. -f 1,2`;
if [ ! -d "$PERCONA_TOOLKIT_BRANCH/sandbox/servers/$version" ]; then
err "Percona Toolkit sandbox server version $version does not exists."
exit 1
fi
case $opt in
start)
cd $PERCONA_TOOLKIT_BRANCH/sandbox
./start-sandbox master 12345
exit_status=$?
if [ $exit_status -eq 0 ]; then
./start-sandbox slave 12346 12345
./start-sandbox slave 12347 12346
exit_status=$?
if [ "$version" != "4.0" ] && [ "$version" != "4.1" ]; then
if [ $? -eq 0 ]; then
echo -n "Loading sakila database... "
./load-sakila-db 12345
exit_status=$?
if [ $? -ne 0 ]; then
echo "FAILED"
else
echo "OK"
fi
fi
fi
fi
if [ $exit_status -eq 0 ]; then
set_mysql_version
echo "Percona Toolkit test environment started with MySQL v$MYSQL_VERSION."
else
err "There was an error starting the Percona Toolkit test environment."
fi
;;
stop)
cd $PERCONA_TOOLKIT_BRANCH/sandbox
./stop-sandbox 12349 12348 12347 12346 12345
exit_status=$((exit_status | $?))
./stop-sandbox 2903 2902 2901 2900
exit_status=$((exit_status | $?))
if [ $exit_status -eq 0 ]; then
echo "Percona Toolkit test environment stopped."
else
err "Error stopping the Percona Toolkit test environment."
fi
;;
kill)
# This is a blunt approach for killing the entire mk test env
# when a polite stop fails. It uses kill -9 as a last resort.
for port in 12345 12346 12347 12348 12349 2900 2901 2902 2903; do
kill_sandbox $port
done
;;
restart)
$0 stop
$0 start
;;
status)
sandbox_status 'master' '12345'
master_status=$?
sandbox_status 'slave' '12346' '12345'
slave_status=$?
echo -n "Percona Test test environment is "
if [ $master_status -eq 0 ] && [ $slave_status -eq 0 ]; then
echo "ok!"
else
echo "invalid."
exit_status=1
fi
;;
reset)
# Several tests reset the bin logs so that queries from prior tests
# don't replicate to new sandbox servers. This makes creating new
# sandbox servers a lot faster. There's no check if this works or
# not, so... yeah.
/tmp/12347/use -e "STOP SLAVE; FLUSH SLAVE;"
/tmp/12346/use -e "STOP SLAVE; FLUSH SLAVE; FLUSH MASTER;"
/tmp/12345/use -e "FLUSH MASTER"
/tmp/12346/use -e "CHANGE MASTER TO master_host='127.0.0.1', master_user='msandbox', master_password='msandbox', master_port=12345, master_log_file='mysql-bin.000001', master_log_pos=0"
/tmp/12346/use -e "START SLAVE"
/tmp/12347/use -e "CHANGE MASTER TO master_host='127.0.0.1', master_user='msandbox', master_password='msandbox', master_port=12346, master_log_file='mysql-bin.000001', master_log_pos=0"
/tmp/12347/use -e "START SLAVE"
exit_status=0
;;
version)
set_mysql_version
echo $MYSQL_VERSION
;;
*)
usage
exit_status=1
;;
esac
exit $exit_status