#!/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 "MySQL processes:" >&2 ps x | grep mysql >&2 echo "Sandbox servers:" >&2 ls /tmp/1234? >&2 ls /tmp/290? >&2 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() { 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 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 [ "$MYSQL_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 xw | 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 sleep 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 xw | grep -v grep | grep mysqld_safe | grep /tmp/$p | awk '{print $1}'` pid2=`ps xw | grep -v grep | grep -v mysqld_safe | grep mysqld | grep /tmp/$p | awk '{print $1}'` [ "$pid1" ] && kill -9 $pid1 # Die, damn you, die! [ "$pid2" ] && 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 -ss -e "SELECT VERSION()" \ | 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 case $opt in start) cd $PERCONA_TOOLKIT_BRANCH/sandbox ./start-sandbox master 12345 exit_status=$((exit_status | $?)) set_mysql_version if [ $exit_status -eq 0 ]; then ./start-sandbox slave 12346 12345 exit_status=$((exit_status | $?)) ./start-sandbox slave 12347 12346 exit_status=$((exit_status | $?)) if [ $? -eq 0 -a "$MYSQL_VERSION" '>' "4.1" ]; then echo -n "Loading sakila database... " ./load-sakila-db 12345 exit_status=$((exit_status | $?)) if [ $exit_status -ne 0 ]; then echo "FAILED" else echo "OK" fi ../util/checksum-test-dataset ping=$(/tmp/12345/use -ss -e "SELECT MD5(RAND())") /tmp/12345/use -e "create table percona_test.sentinel(id int primary key, ping varchar(64) not null default '')" /tmp/12345/use -e "insert into percona_test.sentinel(id, ping) values(1, '$ping')"; echo -n "Waiting for replication to finish..." while true; do pong=$(/tmp/12347/use -ss -e 'select ping from percona_test.sentinel where id=1' 2>/dev/null) [ "$ping" = "$pong" ] && break echo -n '.' sleep 1 done echo ' OK' fi fi if [ $exit_status -eq 0 ]; then 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 12349 12348 12347 12346 12345 2903 2902 2901 2900; 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. echo "RESETTING SLAVE. This is DANGEROUS and DOESN'T WORK. FIXME." >&2 /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