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