Initscript fixes, mostly for "stop" action.
[obnox/ctdb.git] / config / ctdb.init
1 #!/bin/sh
2 #
3 ##############################
4 # ctdb:                        Starts the clustered tdb daemon
5 #
6 # chkconfig:           - 90 01
7 #
8 # description:                 Starts and stops the clustered tdb daemon
9 # pidfile:             /var/run/ctdbd/ctdbd.pid
10 #
11
12 ### BEGIN INIT INFO
13 # Provides:            ctdb
14 # Required-Start:      $network
15 # Required-Stop:       $network
16 # Default-Stop:
17 # Default-Start:       3 5
18 # Short-Description:   start and stop ctdb service
19 # Description:         initscript for the ctdb service
20 ### END INIT INFO
21
22 # Source function library.
23 if [ -f /etc/init.d/functions ] ; then
24   . /etc/init.d/functions
25 elif [ -f /etc/rc.d/init.d/functions ] ; then
26   . /etc/rc.d/init.d/functions
27 fi
28
29 [ -f /etc/rc.status ] && {
30     . /etc/rc.status
31     rc_reset
32     LC_ALL=en_US.UTF-8
33 }
34
35 # Avoid using root's TMPDIR
36 unset TMPDIR
37
38 [ -z "$CTDB_BASE" ] && {
39     export CTDB_BASE="/etc/ctdb"
40 }
41
42 . $CTDB_BASE/functions
43 loadconfig network
44 loadconfig ctdb
45
46 # check networking is up (for redhat)
47 [ "${NETWORKING}" = "no" ] && exit 0
48
49 [ -z "$CTDB_RECOVERY_LOCK" ] && {
50     echo "You must configure the location of the CTDB_RECOVERY_LOCK"
51     exit 1
52 }
53 CTDB_OPTIONS="$CTDB_OPTIONS --reclock=$CTDB_RECOVERY_LOCK"
54
55 # build up CTDB_OPTIONS variable from optional parameters
56 [ -z "$CTDB_LOGFILE" ]          || CTDB_OPTIONS="$CTDB_OPTIONS --logfile=$CTDB_LOGFILE"
57 [ -z "$CTDB_NODES" ]            || CTDB_OPTIONS="$CTDB_OPTIONS --nlist=$CTDB_NODES"
58 [ -z "$CTDB_SOCKET" ]           || CTDB_OPTIONS="$CTDB_OPTIONS --socket=$CTDB_SOCKET"
59 [ -z "$CTDB_PUBLIC_ADDRESSES" ] || CTDB_OPTIONS="$CTDB_OPTIONS --public-addresses=$CTDB_PUBLIC_ADDRESSES"
60 [ -z "$CTDB_PUBLIC_INTERFACE" ] || CTDB_OPTIONS="$CTDB_OPTIONS --public-interface=$CTDB_PUBLIC_INTERFACE"
61 [ -z "$CTDB_DBDIR" ]            || CTDB_OPTIONS="$CTDB_OPTIONS --dbdir=$CTDB_DBDIR"
62 [ -z "$CTDB_DBDIR_PERSISTENT" ] || CTDB_OPTIONS="$CTDB_OPTIONS --dbdir-persistent=$CTDB_DBDIR_PERSISTENT"
63 [ -z "$CTDB_EVENT_SCRIPT_DIR" ] || CTDB_OPTIONS="$CTDB_OPTIONS --event-script-dir $CTDB_EVENT_SCRIPT_DIR"
64 [ -z "$CTDB_TRANSPORT" ]        || CTDB_OPTIONS="$CTDB_OPTIONS --transport $CTDB_TRANSPORT"
65 [ -z "$CTDB_DEBUGLEVEL" ]       || CTDB_OPTIONS="$CTDB_OPTIONS -d $CTDB_DEBUGLEVEL"
66 [ -z "$CTDB_NOTIFY_SCRIPT" ]       || CTDB_OPTIONS="$CTDB_OPTIONS --notification-script=$CTDB_NOTIFY_SCRIPT"
67 [ -z "$CTDB_START_AS_DISABLED" ] || [ "$CTDB_START_AS_DISABLED" != "yes" ] || {
68         CTDB_OPTIONS="$CTDB_OPTIONS --start-as-disabled"
69 }
70 [ -z "$CTDB_CAPABILITY_RECMASTER" ] || [ "$CTDB_CAPABILITY_RECMASTER" != "no" ] || {
71         CTDB_OPTIONS="$CTDB_OPTIONS --no-recmaster"
72 }
73 [ -z "$CTDB_CAPABILITY_LMASTER" ] || [ "$CTDB_CAPABILITY_LMASTER" != "no" ] || {
74         CTDB_OPTIONS="$CTDB_OPTIONS --no-lmaster"
75 }
76 [ -z "$CTDB_LVS_PUBLIC_IP" ] || {
77         CTDB_OPTIONS="$CTDB_OPTIONS --lvs --single-public-ip=$CTDB_LVS_PUBLIC_IP"
78 }
79 [ -z "$CTDB_SCRIPT_LOG_LEVEL" ] || {
80         CTDB_OPTIONS="$CTDB_OPTIONS --script-log-level=$CTDB_SCRIPT_LOG_LEVEL"
81 }
82
83 detect_init_style
84 export CTDB_INIT_STYLE
85
86 if [ "x$CTDB_VALGRIND" = "xyes" ]; then
87         init_style="valgrind"
88 else
89         init_style="$CTDB_INIT_STYLE"
90 fi
91
92 set_retval() {
93         return $1
94 }
95
96 ctdbd=${CTDBD:-/usr/sbin/ctdbd}
97
98 start() {
99         echo -n $"Starting ctdbd service: "
100
101         ctdb ping >& /dev/null
102         if [ $? == "0" ] ; then
103                 echo $"CTDB is already running"
104                 RETVAL=1
105                 return $RETVAL
106         fi
107
108         # check all persistent databases that they look ok
109         PERSISTENT_DB_DIR="/var/ctdb/persistent"
110         [ -z "$CTDB_DBDIR" ] || {
111                 PERSISTENT_DB_DIR="$CTDB_DBDIR/persistent"
112         }
113         mkdir -p $PERSISTENT_DB_DIR 2>/dev/null
114         for PDBASE in `ls $PERSISTENT_DB_DIR/*.tdb.[0-9] 2>/dev/null`; do
115                 /usr/bin/tdbdump $PDBASE >/dev/null 2>/dev/null || {
116                         echo "Persistent database $PDBASE is corrupted! CTDB will not start."
117                         return 1
118                 }
119         done
120
121         case $init_style in
122             valgrind)
123                 daemon valgrind -q --log-file=/var/log/ctdb_valgrind \
124                     $ctdbd --nosetsched $CTDB_OPTIONS 
125                 RETVAL=0
126                 ;;
127             suse)
128                 startproc $ctdbd $CTDB_OPTIONS
129                 rc_status -v
130                 RETVAL=$?
131                 ;;
132             redhat)
133                 daemon $ctdbd $CTDB_OPTIONS
134                 RETVAL=$?
135                 echo
136                 [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ctdb || RETVAL=1
137                 ;;
138             ubuntu)
139                 start-stop-daemon --start --quiet --background \
140                     --exec $ctdbd -- $CTDB_OPTIONS
141                 RETVAL=$?
142                 ;;
143         esac
144
145         sleep 1
146         # set any tunables from the config file
147         set | grep ^CTDB_SET_ | cut -d_ -f3- | 
148         while read v; do
149             varname=`echo $v | cut -d= -f1`
150             value=`echo $v | cut -d= -f2`
151             ctdb setvar $varname $value || RETVAL=1
152         done || exit 1
153
154         return $RETVAL
155 }       
156
157 stop() {
158         echo -n $"Shutting down ctdbd service: "
159         pkill -0 -f $ctdbd || {
160             echo -n "  Warning: ctdbd not running ! "
161             case $init_style in
162                 suse)
163                     rc_status -v
164                     ;;
165                 redhat)
166                     echo ""
167                     ;;
168             esac
169             return 0
170         }
171         ctdb shutdown >/dev/null 2>&1
172         RETVAL=$?
173         count=0
174         while pkill -0 -f $ctdbd ; do
175             sleep 1
176             count=$(($count + 1))
177             [ $count -gt 10 ] && {
178                 echo -n $"killing ctdbd "
179                 pkill -9 -f $ctdbd
180                 pkill -9 -f $CTDB_BASE/events.d/
181             }
182         done
183         case $init_style in
184             suse)
185                 # re-set the return code to the recorded RETVAL
186                 # in order to print the correct status message
187                 set_retval $RETVAL
188                 rc_status -v
189                 ;;
190             redhat)
191                 echo
192                 [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ctdb
193                 echo ""
194                 ;;
195         esac
196         return $RETVAL
197 }       
198
199 restart() {
200         stop
201         start
202 }       
203
204 status() {
205         echo -n $"Checking for ctdbd service: "
206         ctdb ping >& /dev/null || {
207             RETVAL=$?
208             echo -n "  ctdbd not running. "
209             case $init_style in
210                 suse)
211                     set_retval $RETVAL
212                     rc_status -v
213                     ;;
214                 redhat)
215                     echo ""
216                     ;;
217             esac
218             return $RETVAL
219         }
220         echo ""
221         ctdb status
222 }       
223
224
225 case "$1" in
226   start)
227         start
228         ;;
229   stop)
230         stop
231         ;;
232   restart|reload)
233         restart
234         ;;
235   status)
236         status
237         ;;
238   condrestart)
239         ctdb status > /dev/null && restart || :
240         ;;
241   cron)
242         # used from cron to auto-restart ctdb
243         ctdb status > /dev/null || restart
244         ;;
245   *)
246         echo $"Usage: $0 {start|stop|restart|status|cron|condrestart}"
247         exit 1
248 esac
249
250 exit $?