PageRenderTime 154ms CodeModel.GetById 121ms app.highlight 18ms RepoModel.GetById 12ms app.codeStats 0ms

/init.d/ctdb

http://github.com/brinkman83/bashrc
Shell | 366 lines | 282 code | 41 blank | 43 comment | 49 complexity | 730a31159683fc915d5df86f7a643e2a MD5 | raw file
  1#!/bin/sh
  2#
  3##############################
  4# ctdb:                        Starts the clustered tdb daemon
  5#
  6# chkconfig:           - 90 36
  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 $local_fs $remote_fs
 15# Required-Stop:     $network $local_fs $remote_fs
 16# Default-Start:     2 3 4 5
 17# Default-Stop:      0 1 6
 18# Short-Description: start and stop ctdb service
 19# Description:       initscript for the ctdb service
 20### END INIT INFO
 21
 22
 23PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 24
 25NAME=ctdb
 26DESC="Clustered TDB"
 27
 28#LSB
 29if [ -f /lib/lsb/init-functions ] ; then
 30  . /lib/lsb/init-functions
 31fi
 32if ! type 'log_daemon_msg' >/dev/null; then
 33    log_daemon_msg () {
 34        if [ -z "${1:-}" ]; then
 35            return 1
 36        fi
 37
 38        if [ -z "${2:-}" ]; then
 39            echo -n "$1:"
 40            return
 41        fi
 42        
 43        echo -n "$1: $2"
 44    }
 45fi
 46if ! type 'log_progress_msg' >/dev/null; then
 47    log_progress_msg () {
 48        if [ -z "${1:-}" ]; then
 49            return 1
 50        fi
 51        echo -n " $@"
 52    }
 53fi
 54if ! type 'log_end_msg' >/dev/null; then
 55    log_end_msg () {
 56        # If no arguments were passed, return
 57        if [ -z "${1:-}" ]; then
 58            return 1
 59        fi
 60
 61        retval=$1
 62
 63        if [ $1 -eq 0 ]; then
 64            echo "."
 65        elif [ $1 -eq 255 ]; then
 66            /bin/echo -e " (warning)."
 67        else
 68            /bin/echo -e " failed!"
 69        fi
 70        return $retval
 71    }
 72fi
 73
 74# Source function library.
 75if [ -f /etc/init.d/functions ] ; then
 76    . /etc/init.d/functions
 77elif [ -f /etc/rc.d/init.d/functions ] ; then
 78    . /etc/rc.d/init.d/functions
 79fi
 80
 81#gettext
 82if [ -f /usr/bin/gettext.sh ] ; then
 83    . /usr/bin/gettext.sh
 84fi
 85if ! type 'eval_gettext' >/dev/null; then
 86    eval_gettext() {
 87        echo -n "$1" | (export PATH `envsubst --variables "$1"`; envsubst "$1")
 88    }
 89fi
 90
 91[ -f /etc/rc.status ] && {
 92    . /etc/rc.status
 93    rc_reset
 94    LC_ALL=en_US.UTF-8
 95}
 96
 97# Avoid using root's TMPDIR
 98unset TMPDIR
 99
100[ -z "$CTDB_BASE" ] && {
101    export CTDB_BASE="/etc/ctdb"
102}
103
104. $CTDB_BASE/functions
105loadconfig network
106loadconfig ctdb
107
108set -e
109
110# check networking is up (for redhat)
111[ "$NETWORKING" = "no" ] && exit 0
112
113detect_init_style
114export CTDB_INIT_STYLE
115
116ctdbd=${CTDBD:-/usr/sbin/ctdbd}
117
118test -x $ctdbd || exit 0
119
120if [ "$CTDB_VALGRIND" = "yes" ]; then
121    init_style="valgrind"
122else
123    init_style="$CTDB_INIT_STYLE"
124fi
125
126build_ctdb_options () {
127    [ -z "$CTDB_RECOVERY_LOCK" ] && {
128        echo "You must configure the location of the CTDB_RECOVERY_LOCK"
129        exit 1
130    }
131
132    maybe_set () {
133        # If the 2nd arg is null then return - don't set anything.
134        # Else if the 3rd arg is set and it doesn't match the 2nd arg
135        # then return
136        [ -z "$2" -o \( -n "$3" -a "$3" != "$2" \) ] && return
137
138        val="$2"
139        case "$1" in
140            --*) sep="=" ;;
141            -*)  sep=" " ;;
142        esac
143        # For these options we're only passing a value-less flag.
144        [ -n "$3" ] && {
145            val=""
146            sep=""
147        }
148
149        CTDB_OPTIONS="${CTDB_OPTIONS}${CTDB_OPTIONS:+ }${1}${sep}${val}"
150    }
151
152    [ -z "$CTDB_RECOVERY_LOCK" ] && {
153        echo "No recovery lock specified. Starting CTDB without split brain prevention"
154    }
155    maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"
156
157    # build up CTDB_OPTIONS variable from optional parameters
158    maybe_set "--logfile"                "$CTDB_LOGFILE"
159    maybe_set "--nlist"                  "$CTDB_NODES"
160    maybe_set "--socket"                 "$CTDB_SOCKET"
161    maybe_set "--public-addresses"       "$CTDB_PUBLIC_ADDRESSES"
162    maybe_set "--public-interface"       "$CTDB_PUBLIC_INTERFACE"
163    maybe_set "--dbdir"                  "$CTDB_DBDIR"
164    maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
165    maybe_set "--event-script-dir"       "$CTDB_EVENT_SCRIPT_DIR"
166    maybe_set "--transport"              "$CTDB_TRANSPORT"
167    maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
168    maybe_set "--notification-script"    "$CTDB_NOTIFY_SCRIPT"
169    maybe_set "--start-as-disabled"      "$CTDB_START_AS_DISABLED"    "yes"
170    maybe_set "--start-as-stopped "      "$CTDB_START_AS_STOPPED"     "yes"
171    maybe_set "--no-recmaster"           "$CTDB_CAPABILITY_RECMASTER" "no"
172    maybe_set "--no-lmaster"             "$CTDB_CAPABILITY_LMASTER"   "no"
173    maybe_set "--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
174    maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
175}
176
177check_persistent_databases () {
178    case $init_style in
179        debian) PERSISTENT_DB_DIR="${CTDB_DBDIR:-/var/lib/ctdb}/persistent" ;;
180        *) PERSISTENT_DB_DIR="${CTDB_DBDIR:-/var/ctdb}/persistent" ;;
181    esac
182    mkdir -p $PERSISTENT_DB_DIR 2>/dev/null
183    for PDBASE in `ls $PERSISTENT_DB_DIR/*.tdb.[0-9] 2>/dev/null`; do
184        /usr/bin/tdbdump $PDBASE >/dev/null 2>/dev/null || {
185        echo "Persistent database $PDBASE is corrupted! CTDB will not start."
186        return 1
187    }
188    done
189}
190
191set_ctdb_variables () {
192    # set any tunables from the config file
193    set | grep ^CTDB_SET_ | cut -d_ -f3- | 
194    while read v; do
195        varname=`echo $v | cut -d= -f1`
196        value=`echo $v | cut -d= -f2`
197        ctdb setvar $varname $value || RETVAL=1
198    done || exit 1
199}
200
201set_retval() {
202    return $1
203}
204
205running() {
206    pkill -0 -f $ctdbd || return 1
207    return 0
208}
209
210start_server() {
211    build_ctdb_options
212
213    check_persistent_databases || return $?
214
215    if [ yes == "$CTDB_SUPPRESS_COREFILE" ]; then
216	ulimit -c 0
217    else
218	ulimit -c unlimited
219    fi
220
221    case $init_style in
222        valgrind)
223	        valgrind -q --log-file=/var/log/ctdb_valgrind \
224	            $ctdbd --nosetsched $CTDB_OPTIONS 
225	        RETVAL=$?
226	        echo
227	    ;;
228        suse)
229	        startproc $ctdbd $CTDB_OPTIONS
230	        rc_status -v
231	        RETVAL=$?
232	    ;;
233        redhat)
234	        daemon $ctdbd $CTDB_OPTIONS
235	        RETVAL=$?
236	        echo
237	        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ctdb || RETVAL=1
238	    ;;
239        debian)
240	        start-stop-daemon --start --quiet --background \
241	            --exec $ctdbd -- $CTDB_OPTIONS
242	        RETVAL=$?
243	    ;;
244    esac
245
246    sleep 1
247
248    set_ctdb_variables
249
250    return $RETVAL
251}	
252
253stop_server() {
254    ctdb shutdown >/dev/null 2>&1
255    RETVAL=$?
256    count=0
257    if [ "$init_style" = "debian" ]; then
258        start-stop-daemon --stop --quiet --exec $ctdbd
259        RETVAL=$?
260    fi
261    while pkill -0 -f $ctdbd ; do
262        sleep 1
263        count=`expr $count + 1`
264        [ $count -gt 10 ] && {
265	        eval_gettext "killing ctdbd "
266	        pkill -9 -f $ctdbd
267	        pkill -9 -f $CTDB_BASE/events.d/
268        }
269    done
270    case $init_style in
271        suse)
272	        # re-set the return code to the recorded RETVAL in order
273	        # to print the correct status message
274	        set_retval $RETVAL
275	        rc_status -v
276	    ;;
277        redhat)
278	        [ $RETVAL -eq 0 ] && success || failure
279	        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ctdb
280	        echo ""
281	    ;;
282    esac
283    return $RETVAL
284}	
285
286case "$1" in
287    start)
288        log_daemon_msg "Starting $DESC " "$NAME"
289        # Check if it's running first
290        if running ;  then
291            log_progress_msg "already running"
292            log_end_msg 0
293            exit 0
294        fi
295        if start_server && running ;  then
296            # It's ok, the server started and is running
297            log_end_msg 0
298        else
299            # Either we could not start it or it is not running
300            # after we did
301            log_end_msg 1
302        fi
303        ;;
304    stop)
305        log_daemon_msg "Stopping $DESC" "$NAME"
306        if running ; then
307            # Only stop the server if we see it running
308            stop_server
309            log_end_msg $?
310        else
311            # If it's not running don't do anything
312            log_progress_msg "not running"
313            log_end_msg 0
314            exit 0
315        fi
316        ;;
317    restart|reload|force-reload)
318        log_daemon_msg "Restarting $DESC" "$NAME"
319        running && stop_server
320        start_server
321        running
322        log_end_msg $?
323        ;;
324    status)
325        log_daemon_msg "Checking status of $DESC" "$NAME"
326        if running ;  then
327            log_progress_msg "running"
328            echo
329            ctdb status
330            log_end_msg 0
331        else
332            log_progress_msg "not running"
333            log_end_msg 1
334            exit 1
335        fi
336 	    ;;
337    condrestart)
338        if running ;  then
339            log_daemon_msg "Restarting $DESC" "$NAME"
340            stop_server
341            start_server
342            log_end_msg $?
343        fi
344        ;;
345    cron)
346        # used from cron to auto-restart ctdb
347        if ! running ; then
348            log_daemon_msg "Starting $DESC " "$NAME"
349            if start_server && running ;  then
350                # It's ok, the server started and is running
351                log_end_msg 0
352            else
353                # Either we could not start it or it is not running
354                # after we did
355                log_end_msg 1
356            fi
357        fi
358        ;;
359    *)
360        eval_gettext "Usage: $0 {start|stop|restart|status|cron|condrestart}" >&2
361        echo >&2
362        exit 1
363        ;;
364esac
365
366exit $?