Initscript cleanups.
[metze/ctdb/wip.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 [ -z "$CTDB_RECOVERY_LOCK" ] && {
47     echo "No recovery lock specified. Starting CTDB without split brain prevention"
48 }
49
50 # check networking is up (for redhat)
51 [ "$NETWORKING" = "no" ] && exit 0
52
53 detect_init_style
54 export CTDB_INIT_STYLE
55
56 ctdbd=${CTDBD:-/usr/sbin/ctdbd}
57
58 if [ "$CTDB_VALGRIND" = "yes" ]; then
59     init_style="valgrind"
60 else
61     init_style="$CTDB_INIT_STYLE"
62 fi
63
64 build_ctdb_options () {
65     [ -z "$CTDB_RECOVERY_LOCK" ] && {
66         echo "You must configure the location of the CTDB_RECOVERY_LOCK"
67         exit 1
68     }
69
70     maybe_set () {
71         # If the 2nd arg is null then return - don't set anything.
72         # Else if the 3rd arg is set and it doesn't match the 2nd arg
73         # then return
74         [ -z "$2" -o \( -n "$3" -a "$3" != "$2" \) ] && return
75
76         val="$2"
77         case "$1" in
78             --*) sep="=" ;;
79             -*)  sep=" " ;;
80         esac
81         # For these options we're only passing a value-less flag.
82         [ -n "$3" ] && {
83             val=""
84             sep=""
85         }
86
87         CTDB_OPTIONS="${CTDB_OPTIONS}${CTDB_OPTIONS:+ }${1}${sep}${val}"
88     }
89
90     maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"
91
92     # build up CTDB_OPTIONS variable from optional parameters
93     maybe_set "--logfile"                "$CTDB_LOGFILE"
94     maybe_set "--nlist"                  "$CTDB_NODES"
95     maybe_set "--socket"                 "$CTDB_SOCKET"
96     maybe_set "--public-addresses"       "$CTDB_PUBLIC_ADDRESSES"
97     maybe_set "--public-interface"       "$CTDB_PUBLIC_INTERFACE"
98     maybe_set "--dbdir"                  "$CTDB_DBDIR"
99     maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
100     maybe_set "--event-script-dir"       "$CTDB_EVENT_SCRIPT_DIR"
101     maybe_set "--transport"              "$CTDB_TRANSPORT"
102     maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
103     maybe_set "--notification-script"    "$CTDB_NOTIFY_SCRIPT"
104     maybe_set "--start-as-disabled"      "$CTDB_START_AS_DISABLED"    "yes"
105     maybe_set "--no-recmaster"           "$CTDB_CAPABILITY_RECMASTER" "no"
106     maybe_set "--no-lmaster"             "$CTDB_CAPABILITY_LMASTER"   "no"
107     maybe_set "--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
108     maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
109 }
110
111 check_persistent_databases () {
112     PERSISTENT_DB_DIR="${CTDB_DBDIR:-/var/ctdb}/persistent"
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
122 set_ctdb_variables () {
123     # set any tunables from the config file
124     set | grep ^CTDB_SET_ | cut -d_ -f3- | 
125     while read v; do
126         varname=`echo $v | cut -d= -f1`
127         value=`echo $v | cut -d= -f2`
128         ctdb setvar $varname $value || RETVAL=1
129     done || exit 1
130 }
131
132 set_retval() {
133     return $1
134 }
135
136 ctdbd=${CTDBD:-/usr/sbin/ctdbd}
137
138 start() {
139     echo -n $"Starting ctdbd service: "
140
141     ctdb ping >/dev/null 2>&1 && {
142         echo $"CTDB is already running"
143         return 1
144     }
145
146     build_ctdb_options
147
148     check_persistent_databases || return $?
149
150     case $init_style in
151         valgrind)
152             valgrind -q --log-file=/var/log/ctdb_valgrind \
153                 $ctdbd --nosetsched $CTDB_OPTIONS 
154             RETVAL=$?
155             echo
156             ;;
157         suse)
158             startproc $ctdbd $CTDB_OPTIONS
159             rc_status -v
160             RETVAL=$?
161             ;;
162         redhat)
163             daemon $ctdbd $CTDB_OPTIONS
164             RETVAL=$?
165             echo
166             [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ctdb || RETVAL=1
167             ;;
168         ubuntu)
169             start-stop-daemon --start --quiet --background \
170                 --exec $ctdbd -- $CTDB_OPTIONS
171             RETVAL=$?
172             ;;
173     esac
174
175     sleep 1
176
177     set_ctdb_variables
178
179     return $RETVAL
180 }       
181
182 stop() {
183     echo -n $"Shutting down ctdbd service: "
184     pkill -0 -f $ctdbd || {
185         echo -n "  Warning: ctdbd not running ! "
186         case $init_style in
187             suse)
188                 rc_status -v
189                 ;;
190             redhat)
191                 echo ""
192                 ;;
193         esac
194         return 0
195     }
196     ctdb shutdown >/dev/null 2>&1
197     RETVAL=$?
198     count=0
199     while pkill -0 -f $ctdbd ; do
200         sleep 1
201         count=$(($count + 1))
202         [ $count -gt 10 ] && {
203             echo -n $"killing ctdbd "
204             pkill -9 -f $ctdbd
205             pkill -9 -f $CTDB_BASE/events.d/
206         }
207     done
208     case $init_style in
209         suse)
210             # re-set the return code to the recorded RETVAL in order
211             # to print the correct status message
212             set_retval $RETVAL
213             rc_status -v
214             ;;
215         redhat)
216             echo
217             [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ctdb
218             echo ""
219             ;;
220     esac
221     return $RETVAL
222 }
223
224 restart() {
225     stop
226     start
227 }       
228
229 status() {
230     echo -n $"Checking for ctdbd service: "
231     ctdb ping >/dev/null 2>&1 || {
232         RETVAL=$?
233         echo -n "  ctdbd not running. "
234         case $init_style in
235             suse)
236                 set_retval $RETVAL
237                 rc_status -v
238                 ;;
239             redhat)
240                 echo ""
241                 ;;
242         esac
243         return $RETVAL
244     }
245     echo ""
246     ctdb status
247 }
248
249
250 case "$1" in
251     start)
252         start
253         ;;
254     stop)
255         stop
256         ;;
257     restart|reload)
258         restart
259         ;;
260     status)
261         status
262         ;;
263     condrestart)
264         ctdb status > /dev/null && restart || :
265         ;;
266     cron)
267         # used from cron to auto-restart ctdb
268         ctdb status > /dev/null || restart
269         ;;
270     *)
271         echo $"Usage: $0 {start|stop|restart|status|cron|condrestart}"
272         exit 1
273 esac
274
275 exit $?