summaryrefslogtreecommitdiff
path: root/ctdb/config/ctdbd_wrapper
blob: e8f0a23a2575a4027a4189a3a3cce6b9c283ae3b (plain)
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
#!/bin/sh

# ctdbd wrapper - start or stop CTDB

usage ()
{
    echo "usage: ctdbd_wrapper { start | stop }"
    exit 1
}

[ $# -eq 1 ] || usage

action="$1"

############################################################

if [ -z "$CTDB_BASE" ] ; then
    export CTDB_BASE="/usr/local/etc/ctdb"
fi

. "${CTDB_BASE}/functions"

loadconfig

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

############################################################

# Only the nested function references its arguments
# shellcheck disable=SC2120
build_ctdb_options ()
{
    ctdb_options=""

    maybe_set ()
    {
	# If the given variable isn't set then do nothing
	[ -n "$2" ] || return
	# If a required value for the variable and it doesn't match,
	# then do nothing
	[ -z "$3" -o "$3" = "$2" ] || return

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

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

    # build up ctdb_options variable from optional parameters
    maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"
    maybe_set "--logging"                "$CTDB_LOGGING"
    maybe_set "--listen"                 "$CTDB_NODE_ADDRESS"
    maybe_set "--dbdir"                  "$CTDB_DBDIR"
    maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
    maybe_set "--dbdir-state"            "$CTDB_DBDIR_STATE"
    maybe_set "--transport"              "$CTDB_TRANSPORT"
    maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
    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 "--nosetsched"             "$CTDB_NOSETSCHED"           "yes"
    maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
    maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
}

export_debug_variables ()
{
    [ -n "$CTDB_DEBUG_HUNG_SCRIPT" ] && export CTDB_DEBUG_HUNG_SCRIPT
    [ -n "$CTDB_DEBUG_LOCKS" ] && export CTDB_DEBUG_LOCKS
}

############################################################

start()
{
    # build_ctdb_options() takes no arguments
    # shellcheck disable=SC2119
    build_ctdb_options

    export_debug_variables

    eval "$ctdbd" "$ctdb_options" || return 1

    # Wait until ctdbd has started and is ready to respond to clients.
    _timeout="${CTDB_STARTUP_TIMEOUT:-10}"
    _count=0
    while [ "$_count" -lt "$_timeout" ] ; do
	if $CTDB runstate first_recovery startup running >/dev/null 2>&1 ; then
	    return 0
	fi

	_count=$((_count + 1))
	sleep 1
    done

    echo "Timed out waiting for initialisation - check logs"
    # Attempt a shutdown just in case things are still running
    $CTDB shutdown >/dev/null 2>&1
    drop_all_public_ips >/dev/null 2>&1
    return 1
}

stop()
{
	$CTDB shutdown

	# The above command is important and needs to stand out, so
	# post-check exit status
	# shellcheck disable=SC2181
	if [ $? -ne 0 ] ; then
		echo "Error while shutting down CTDB"
		drop_all_public_ips >/dev/null 2>&1
		return 1
	fi

	return 0
}

############################################################

# Allow notifications for start/stop.
if [ -x "$CTDB_BASE/rc.ctdb" ] ; then
    "$CTDB_BASE/rc.ctdb" "$action"
fi

case "$action" in
    start) start ;;
    stop)  stop  ;;
    *)
	echo "usage: $0 {start|stop}"
	exit 1
esac