File: ctdb.init

package info (click to toggle)
ctdb 1.12%2Bgit20120201-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,656 kB
  • sloc: ansic: 61,736; sh: 18,367; xml: 3,887; python: 1,220; makefile: 554; perl: 319; awk: 118
file content (475 lines) | stat: -rw-r--r-- 11,766 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#!/bin/sh
#
##############################
# ctdb:                        Starts the clustered tdb daemon
#
# chkconfig:           - 90 01
#
# description:                 Starts and stops the clustered tdb daemon
# pidfile:             /var/run/ctdbd/ctdbd.pid
#

### BEGIN INIT INFO
# Provides:            ctdb
# Required-Start:      $network $local_fs $remote_fs
# Required-Stop:       $network $local_fs $remote_fs
# Default-Start:       2 3 4 5
# Default-Stop:        0 1 6
# Short-Description:   start and stop ctdb service
# Description:         initscript for the ctdb service
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

NAME=ctdb
DESC="Clustered TDB"

#LSB
if [ -f /lib/lsb/init-functions ] ; then
  . /lib/lsb/init-functions
fi
if ! type 'log_daemon_msg' >/dev/null; then
    log_daemon_msg () {
        if [ -z "${1:-}" ]; then
            return 1
        fi

        if [ -z "${2:-}" ]; then
            echo -n "$1:"
            return
        fi
        
        echo -n "$1: $2"
    }
fi
if ! type 'log_progress_msg' >/dev/null; then
    log_progress_msg () {
        if [ -z "${1:-}" ]; then
            return 1
        fi
        echo -n " $@"
    }
fi
if ! type 'log_end_msg' >/dev/null; then
    log_end_msg () {
        # If no arguments were passed, return
        if [ -z "${1:-}" ]; then
            return 1
        fi

        retval=$1

        if [ $1 -eq 0 ]; then
            echo "."
        elif [ $1 -eq 255 ]; then
            /bin/echo -e " (warning)."
        else
            /bin/echo -e " failed!"
        fi
        return $retval
    }
fi

# Source function library.
if [ -f /etc/init.d/functions ] ; then
    . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
    . /etc/rc.d/init.d/functions
fi

# rc.status
[ -f /etc/rc.status ] && {
    . /etc/rc.status
    rc_reset
    LC_ALL=en_US.UTF-8
}

# Avoid using root's TMPDIR
unset TMPDIR

[ -z "$CTDB_BASE" ] && {
    export CTDB_BASE="/etc/ctdb"
}

[ -z "$CTDB_VARDIR" ] && {
    export CTDB_VARDIR="/var/lib/ctdb"
}

. $CTDB_BASE/functions
loadconfig network
loadconfig ctdb

set -e

# check networking is up (for redhat)
[ "$NETWORKING" = "no" ] && exit 0

detect_init_style
export CTDB_INIT_STYLE

ctdbd=${CTDBD:-/usr/sbin/ctdbd}

test -x $ctdbd || exit 0

if [ "$CTDB_VALGRIND" = "yes" ]; then
    init_style="valgrind"
else
    init_style="$CTDB_INIT_STYLE"
fi

build_ctdb_options () {

    maybe_set () {
	# If the 2nd arg is null then return - don't set anything.
	# Else if the 3rd arg is set and it doesn't match the 2nd arg
	# then return
	[ -z "$2" -o \( -n "$3" -a "$3" != "$2" \) ] && return

	val="'$2'"
	case "$1" in
	    --*) sep="=" ;;
	    -*)  sep=" " ;;
	esac
	# For these options we're only passing a value-less flag.
	[ -n "$3" ] && {
	    val=""
	    sep=""
	}

	CTDB_OPTIONS="${CTDB_OPTIONS}${CTDB_OPTIONS:+ }${1}${sep}${val}"
    }

    [ -z "$CTDB_RECOVERY_LOCK" ] && {
        echo "No recovery lock specified. Starting CTDB without split brain prevention"
    }
    maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"

    # build up CTDB_OPTIONS variable from optional parameters
    maybe_set "--logfile"                "$CTDB_LOGFILE"
    maybe_set "--nlist"                  "$CTDB_NODES"
    maybe_set "--socket"                 "$CTDB_SOCKET"
    maybe_set "--public-addresses"       "$CTDB_PUBLIC_ADDRESSES"
    maybe_set "--public-interface"       "$CTDB_PUBLIC_INTERFACE"
    maybe_set "--dbdir"                  "$CTDB_DBDIR"
    maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
    maybe_set "--event-script-dir"       "$CTDB_EVENT_SCRIPT_DIR"
    maybe_set "--transport"              "$CTDB_TRANSPORT"
    maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
    maybe_set "--notification-script"    "$CTDB_NOTIFY_SCRIPT"
    maybe_set "--start-as-disabled"      "$CTDB_START_AS_DISABLED"    "yes"
    maybe_set "--start-as-stopped "      "$CTDB_START_AS_STOPPED"     "yes"
    maybe_set "--no-recmaster"           "$CTDB_CAPABILITY_RECMASTER" "no"
    maybe_set "--no-lmaster"             "$CTDB_CAPABILITY_LMASTER"   "no"
    maybe_set "--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
    maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
    maybe_set "--log-ringbuf-size"       "$CTDB_LOG_RINGBUF_SIZE"
    maybe_set "--syslog"                 "$CTDB_SYSLOG"               "yes"
    maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
}

check_tdb () {
	local PDBASE=$1

	test x"$TDBTOOL_HAS_CHECK" = x"1" && {
		#
		# Note tdbtool always exits with 0
		#
		local OK=`tdbtool $PDBASE check | grep "Database integrity is OK" | wc -l`
		test x"$OK" = x"1" || {
			return 1;
		}

		return 0;
	}

	tdbdump $PDBASE >/dev/null 2>/dev/null || {
		return $?;
	}

	return 0;
}

check_persistent_databases () {
    PERSISTENT_DB_DIR="${CTDB_DBDIR:-$CTDB_VARDIR}/persistent"
    mkdir -p $PERSISTENT_DB_DIR 2>/dev/null
    local ERRCOUNT=$CTDB_MAX_PERSISTENT_CHECK_ERRORS

    test -z "$ERRCOUNT" && {
	ERRCOUNT="0"
    }
    test x"$ERRCOUNT" != x"0" && {
	return 0;
    }

    if command -v tdbtool >/dev/null 2>&1; then
        HAVE_TDBTOOL=1
    else
        HAVE_TDBTOOL=0
    fi

    if test x"$HAVE_TDBTOOL" = x"1" ; then
        TDBTOOL_HAS_CHECK=`echo "help" | tdbtool | grep check | wc -l`
    else
        TDBTOOL_HAS_CHECK=0
    fi

    if command -v tdbdump >/dev/null 2>&1; then
        HAVE_TDBDUMP=1
    else
        HAVE_TDBDUMP=0
    fi

    if test x"$HAVE_TDBDUMP" = x"0" -a x"$TDBTOOL_HAS_CHECK" = x"0" ; then
        echo "WARNING: Cannot check persistent databases since"
        echo "neither 'tdbdump' nor 'tdbtool check' is available."
        echo "Consider installing tdbtool or at least tdbdump!"
        return 0
    fi

    if test x"$HAVE_TDBDUMP" = x"1" -a x"$TDBTOOL_HAS_CHECK" = x"0" ; then
        if test x"$HAVE_TDBTOOL" = x"0"; then
            echo "WARNING: 'tdbtool' is not available. Using 'tdbdump' to"
            echo "check the persistent databases."
            echo "Consider installing a recent 'tdbtool' for better checks!"
        else
            echo "WARNING: The installed 'tdbtool' does not offer the 'check'"
            echo "subcommand. Using 'tdbdump' for persistent database checks."
            echo "Consider updating 'tdbtool' for better checks!"
        fi
    fi

    for PDBASE in `ls $PERSISTENT_DB_DIR/*.tdb.[0-9] 2>/dev/null`; do
	check_tdb $PDBASE || {
	    echo "Persistent database $PDBASE is corrupted! CTDB will not start."
	    return 1
	}
    done
}

set_ctdb_variables () {
    # set any tunables from the config file
    set | grep ^CTDB_SET_ | cut -d_ -f3- | 
    while read v; do
	varname=`echo $v | cut -d= -f1`
	value=`echo $v | cut -d= -f2`
	ctdb setvar $varname $value || RETVAL=1
    done || exit 1
}

set_retval() {
    return $1
}

wait_until_ready () {
    _timeout="${1:-10}" # default is 10 seconds

    _count=0
    while ! is_ready ; do
	if [ $_count -ge $_timeout ] ; then
	    return 1
	fi
	sleep 1
	_count=$(($_count + 1))
    done
}

drop_all_public_ips() {
    [ -z "$CTDB_PUBLIC_ADDRESSES" ] && {
	return
    }

    cat $CTDB_PUBLIC_ADDRESSES | while read IP IFACE REST; do
	ip addr del $IP dev $IFACE >/dev/null 2>/dev/null ||:
    done
}

is_running() {
    pkill -0 -f $ctdbd || return 1
    return 0
}

is_ready() {
    ctdb ping >/dev/null 2>&1 || return 1
}

start_ctdbd() {
    build_ctdb_options

    # make sure we drop any ips that might still be held if previous
    # instance of ctdb got killed with -9 or similar
    drop_all_public_ips

    check_persistent_databases || return $?

    if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
	ulimit -c 0
    else
	ulimit -c unlimited
    fi

    case $init_style in
	valgrind)
	    eval valgrind -q --log-file=/var/log/ctdb/ctdb_valgrind.log \
		$ctdbd --valgrinding "$CTDB_OPTIONS"
	    RETVAL=$?
	    echo
	    ;;
	suse)
	    eval startproc $ctdbd "$CTDB_OPTIONS"
	    RETVAL=$?
	    ;;
	redhat)
	    eval $ctdbd "$CTDB_OPTIONS"
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ctdb || RETVAL=1
	    ;;
	debian)
	    eval start-stop-daemon --start --quiet --background \
		--exec $ctdbd -- "$CTDB_OPTIONS"
	    RETVAL=$?
	    ;;
    esac

    if [ $RETVAL -eq 0 ] ; then
	if wait_until_ready ; then
	    set_ctdb_variables
	else
	    RETVAL=1
	    pkill -9 -f $ctdbd >/dev/null 2>&1
	fi
    fi

    case $init_style in
	suse)
	    set_retval $RETVAL
	    rc_status -v
	    ;;
	redhat)
	    [ $RETVAL -eq 0 ] && success || failure
	    echo
	    ;;
    esac

    return $RETVAL
}	

stop_ctdbd() {
    ctdb shutdown >/dev/null 2>&1
    RETVAL=$?
    count=0
    if [ "$init_style" = "debian" ]; then
        start-stop-daemon --stop --quiet --exec $ctdbd
        RETVAL=$?
    fi
    while is_running ; do
        sleep 1
        count=$(($count + 1))
        [ $count -gt 10 ] && {
	        echo -n "killing ctdbd "
	        pkill -9 -f $ctdbd
	        pkill -9 -f $CTDB_BASE/events.d/
        }
    done
    # make sure all ips are dropped, pfkill -9 might leave them hanging around
    drop_all_public_ips

    case $init_style in
        suse)
	        # re-set the return code to the recorded RETVAL in order
	        # to print the correct status message
	        set_retval $RETVAL
	        rc_status -v
	    ;;
        redhat)
	        [ $RETVAL -eq 0 ] && success || failure
	        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ctdb
	        echo ""
	    ;;
    esac

    return $RETVAL
}	

case "$1" in
    start)
        log_daemon_msg "Starting $DESC " "$NAME"
        # Check if it's running first
        if is_running ;  then
            log_progress_msg "already running"
            log_end_msg 0
            exit 0
        fi
        if start_ctdbd && is_running ;  then
            # It's ok, the server started and is running
            log_end_msg 0
        else
            # Either we could not start it or it is not running
            # after we did
            log_end_msg 1
        fi
        ;;
    stop)
        log_daemon_msg "Stopping $DESC" "$NAME"
        if is_running ; then
            # Only stop the server if we see it running
            stop_ctdbd
            log_end_msg $?
        else
            # If it's not running don't do anything
            log_progress_msg "not running"
            log_end_msg 0
            exit 0
        fi
        ;;
    restart|force-reload)
        log_daemon_msg "Restarting $DESC" "$NAME"
        is_running && stop_ctdbd
        start_ctdbd
        is_running
        log_end_msg $?
        ;;
    status)
        log_daemon_msg "Checking status of $DESC" "$NAME"
        if is_running ;  then
            log_progress_msg "running"
            echo
            ctdb status
            log_end_msg 0
        else
            log_progress_msg "not running"
            log_end_msg 1
            exit 1
        fi
 	    ;;
    condrestart|try-restart)
        if is_running ;  then
            log_daemon_msg "Restarting $DESC" "$NAME"
            stop_ctdbd
            start_ctdbd
            log_end_msg $?
        fi
        ;;
    cron)
        # used from cron to auto-restart ctdb
        if ! is_running ; then
            log_daemon_msg "Starting $DESC " "$NAME"
            if start_ctdbd && is_running ;  then
                # It's ok, the server started and is running
                log_end_msg 0
            else
                # Either we could not start it or it is not running
                # after we did
                log_end_msg 1
            fi
        fi
        ;;
    reload)
	# do nothing
	;;
    *)
        echo "Usage: \$0 {start|stop|restart|force-reload|status|cron|condrestart|try-restart}"
        exit 2
        ;;
esac

exit $?