1#!/bin/bash -x
2
3# Copyright (C) 2009 Codership Oy <info@codership.com>
4
5PLUGIN_BASE_DIR=$(cd $(dirname $0); pwd -P)
6SELF=$PLUGIN_BASE_DIR/$(basename $0)
7
8WSREP_CLUSTER_NAME=${1:-"tor_galera_cluster"}
9WSREP_NODE_NAME=${2:-"$(hostname)"}
10PLUGIN_CONF=${3:-"$PLUGIN_BASE_DIR/plugin.cnf"}
11
12. $PLUGIN_CONF
13
14#=============== Fall back to reasonable defaults =======================
15
16# MySQL configuration file
17MYSQL_CNF=${MYSQL_CNF:-"$MYSQL_BASE_DIR/etc/my.cnf"}
18if test -s "$MYSQL_CNF"
19then
20    DEFAULTS_OPTION=" --defaults-file=$MYSQL_CNF "
21    my_cnf_datadir=$(grep ^datadir $MYSQL_CNF | sed s/[^/]*//)
22else
23    DEFAULTS_OPTION=" --no-defaults "
24fi
25
26# If it was not given explicitely, use the one from my.cnf
27MYSQL_DATA_DIR=${MYSQL_DATA_DIR:-"$my_cnf_datadir"}
28# If it was not found in my.cnf, use distribution default
29MYSQL_DATA_DIR=${MYSQL_DATA_DIR:-"$MYSQL_BASE_DIR/var"}
30
31# use mysqld server directly, better not have automatic restarting
32MYSQLD=${MYSQLD:-"$MYSQL_BASE_DIR/libexec/mysqld"}
33MYSQLADMIN=${MYSQLADMIN:-"$MYSQL_BASE_DIR/bin/mysqladmin"}
34# Port, socket and pid files
35MYSQL_PORT=${MYSQL_PORT:-3306}
36MYSQL_SOCKET=${MYSQL_SOCKET:-"$MYSQL_DATA_DIR/mysqld.sock"}
37MYSQL_PID=${MYSQL_PID:-"$MYSQL_DATA_DIR/mysqld.pid"}
38# Shutdown wait timeout.
39MYSQL_SHUTDOWN_WAIT=60
40
41#============= Nothing servicable below ================================
42
43# User to run as if started under superuser
44MYSQLD_USER=$(whoami)
45if test "$MYSQLD_USER" = "root"
46then
47    MYSQLD_USER=mysql
48fi
49
50#ROOT_USER=${ROOT_USER:-"-uroot"}
51#ROOT_PSWD=${ROOT_PSWD:-"-prootpass"}
52
53#mysql_log="$MYSQL_DATA_DIR/$(hostname).log"
54
55
56usage() { cat - << EOF
57
58usage: service <command>
59
60Commands:
61check                 : check cosistency either locally or through network
62start                 : start servers
63stop                  : stop servers
64restart               : stop and start servers
65status                : show running servers
66
67EOF
68}
69
70# Checks if a process with a given PID is still running
71find_pid()
72{
73    ps axc | grep mysqld | grep -w ^\ *$1 > /dev/null
74}
75
76galera_start()
77{
78    local failed
79
80    if ! test -x $MYSQLD
81    then
82	echo "$MYSQLD executable not found"
83	exit -1
84    fi
85
86    if test -f $MYSQL_PID
87    then
88        echo "Found existing '$MYSQL_PID'. Please run '$0 stop'"
89        exit -1;
90    fi
91
92    if test -f $WSREP_PROVIDER || test $WSREP_PROVIDER == "none"
93    then
94	WSREP_OPTS="$WSREP_OPTS --wsrep_provider=$WSREP_PROVIDER"
95    else
96	echo "WSREP provider '$WSREP_PROVIDER' not found"
97	exit -1
98    fi
99
100    WSREP_OPTS="$WSREP_OPTS \
101		--wsrep_cluster_name=$WSREP_CLUSTER_NAME \
102                --wsrep_cluster_address=$WSREP_CLUSTER_ADDRESS \
103		--wsrep_sst_method=$WSREP_SST_METHOD \
104		--wsrep_local_cache_size=$WSREP_LOCAL_CACHE_SIZE \
105		--wsrep_start_position=$WSREP_START_POSITION \
106		--wsrep_debug=$WSREP_DEBUG \
107		--wsrep_auto_increment_control=$WSREP_AUTO_INCREMENT_CONTROL \
108		--wsrep_retry_autocommit=$WSREP_RETRY_AUTOCOMMIT \
109		--wsrep_convert_LOCK_to_trx=$WSREP_CONVERT_LOCK_TO_TRX \
110		--wsrep_drupal_282555_workaround=$WSREP_DRUPAL_282555_WORKAROUND \
111		--wsrep_ws_persistency=$WSREP_WS_PERSISTENCY \
112                --wsrep_slave_threads=$WSREP_SLAVE_THREADS "
113
114    MYSQLD_OPTS=" --user=$MYSQLD_USER \
115	          --basedir=$MYSQL_BASE_DIR \
116	          --datadir=$MYSQL_DATA_DIR \
117	          --pid-file=$MYSQL_PID \
118	          --port=$MYSQL_PORT \
119	          --socket=$MYSQL_SOCKET \
120	          --skip-locking \
121                  --binlog_format=ROW \
122		  --default-storage-engine=InnoDB "
123
124    INNODB_OPTS=" --innodb_autoinc_lock_mode=2 \
125                  --innodb_flush_log_at_trx_commit=0 \
126		  --innodb_doublewrite=0"
127
128    err_log="$MYSQL_DATA_DIR/$(hostname).err"
129
130    echo -n "Starting mysqld instance with data dir $MYSQL_DATA_DIR and listening at port $MYSQL_PORT and socket $MYSQL_SOCKET..."
131
132    LD_LIBRARY_PATH=$(cd $(dirname $WSREP_PROVIDER) && pwd -P)
133    export LD_LIBRARY_PATH
134    export PATH=$MYSQL_BASE_DIR/bin:$PATH
135
136    nohup $MYSQLD $DEFAULTS_OPTION $MYSQLD_OPTS $INNODB_OPTS $WSREP_OPTS \
137          1>/dev/null 2>>$err_log &
138
139    my_pid=$!
140
141#    echo "Waiting for pid file"
142    while ! test -r $MYSQL_PID
143    do
144        sleep 1
145        if find_pid $my_pid
146        then
147            # process is alive, wait for pid file
148	    echo -n "."
149	else
150            failed="yes"
151	    break
152	fi
153    done
154
155    if test "$failed" != "yes"
156    then
157        echo " Done (PID:$(cat $MYSQL_PID))"
158    else
159        echo " Failed (PID:$my_pid)"
160    fi
161}
162
163galera_stop()
164{
165    # check pid file
166    if test -r $MYSQL_PID
167    then
168        # check if corresponding mysqld is running
169#        if ps axc | grep mysqld | grep $(cat $MYSQL_PID) >/dev/null 2>&1
170	local my_pid=$(cat $MYSQL_PID)
171        if find_pid $my_pid
172        then
173            echo -n "Killing PID $my_pid"
174	    kill $my_pid
175            # wait for pid file to disappear
176	    for second in $(seq 1 $MYSQL_SHUTDOWN_WAIT)
177	    do
178	        echo -n "."
179	        sleep 1
180	        if test ! -r $MYSQL_PID
181	        then
182	            break
183		fi
184	    done
185	    echo ""
186	    if test "$second" = "$MYSQL_SHUTDOWN_WAIT"
187	    then
188	        echo -n "Failed to stop mysqld safely. Killing with -9... "
189	        kill -9 $my_pid
190	    fi
191	else
192	    echo -n "Removing stale PID file $MYSQL_PID... "
193	fi
194
195	rm -rf $MYSQL_PID
196	echo "Done"
197    else
198	echo "PID file not found: $MYSQL_PID"
199    fi
200}
201
202galera_restart()
203{
204    galera_stop
205    galera_start
206}
207
208dump()
209{
210#local ROUTINES="--routines" # don't dump routines yet, will cause false err.
211local DUMP_OPTIONS=" --skip-opt --compact --flush-logs --lock-all-tables \
212           --quick --create-options --set-charset --skip-comments $ROUTINES "
213DB=${DB:-"--all-databases"}
214#set -x
215mysqldump $DUMP_OPTIONS $ROOT_USER $ROOT_PSWD -h127.0.0.1 -P$MYSQL_PORT  \
216          $IGNORE_TABLES $DB
217#set +x
218}
219
220checksum()
221{
222CS=`dump | md5sum`
223
224echo $CS
225}
226
227# write set level, SQL, RBR or ROW
228WS_LEVEL="RBR"
229
230#DB="test" # use 'test' database if none given
231IGNORE_TABLES=""
232
233case $(basename $0) in
234'dump')
235            COMMAND="dump"
236            ;;
237'check')
238            COMMAND="checksum"
239            ;;
240'online')
241            COMMAND=galera_start
242            ;;
243'stop'|'halt'|'kill'|'offline')
244            COMMAND=galera_stop
245            ;;
246'restart')
247            COMMAND=galera_restart
248            ;;
249'status')
250            COMMAND=status
251            ;;
252    *)
253            echo $0
254        # must be command
255            usage
256            exit 1
257            ;;
258esac
259
260$COMMAND
261
262#
263