e8f0a23a2575a4027a4189a3a3cce6b9c283ae3b
[metze/samba/wip.git] / ctdb / config / ctdbd_wrapper
1 #!/bin/sh
2
3 # ctdbd wrapper - start or stop CTDB
4
5 usage ()
6 {
7     echo "usage: ctdbd_wrapper { start | stop }"
8     exit 1
9 }
10
11 [ $# -eq 1 ] || usage
12
13 action="$1"
14
15 ############################################################
16
17 if [ -z "$CTDB_BASE" ] ; then
18     export CTDB_BASE="/usr/local/etc/ctdb"
19 fi
20
21 . "${CTDB_BASE}/functions"
22
23 loadconfig
24
25 ctdbd="${CTDBD:-/usr/local/sbin/ctdbd}"
26
27 ############################################################
28
29 # Only the nested function references its arguments
30 # shellcheck disable=SC2120
31 build_ctdb_options ()
32 {
33     ctdb_options=""
34
35     maybe_set ()
36     {
37         # If the given variable isn't set then do nothing
38         [ -n "$2" ] || return
39         # If a required value for the variable and it doesn't match,
40         # then do nothing
41         [ -z "$3" -o "$3" = "$2" ] || return
42
43         val="'$2'"
44         case "$1" in
45             --*) sep="=" ;;
46             -*)  sep=" " ;;
47         esac
48         # For these options we're only passing a value-less flag.
49         if [ -n "$3" ] ; then
50             val=""
51             sep=""
52         fi
53
54         ctdb_options="${ctdb_options}${ctdb_options:+ }${1}${sep}${val}"
55     }
56
57     # build up ctdb_options variable from optional parameters
58     maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"
59     maybe_set "--logging"                "$CTDB_LOGGING"
60     maybe_set "--listen"                 "$CTDB_NODE_ADDRESS"
61     maybe_set "--dbdir"                  "$CTDB_DBDIR"
62     maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
63     maybe_set "--dbdir-state"            "$CTDB_DBDIR_STATE"
64     maybe_set "--transport"              "$CTDB_TRANSPORT"
65     maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
66     maybe_set "--start-as-disabled"      "$CTDB_START_AS_DISABLED"    "yes"
67     maybe_set "--start-as-stopped "      "$CTDB_START_AS_STOPPED"     "yes"
68     maybe_set "--no-recmaster"           "$CTDB_CAPABILITY_RECMASTER" "no"
69     maybe_set "--no-lmaster"             "$CTDB_CAPABILITY_LMASTER"   "no"
70     maybe_set "--nosetsched"             "$CTDB_NOSETSCHED"           "yes"
71     maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
72     maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
73 }
74
75 export_debug_variables ()
76 {
77     [ -n "$CTDB_DEBUG_HUNG_SCRIPT" ] && export CTDB_DEBUG_HUNG_SCRIPT
78     [ -n "$CTDB_DEBUG_LOCKS" ] && export CTDB_DEBUG_LOCKS
79 }
80
81 ############################################################
82
83 start()
84 {
85     # build_ctdb_options() takes no arguments
86     # shellcheck disable=SC2119
87     build_ctdb_options
88
89     export_debug_variables
90
91     eval "$ctdbd" "$ctdb_options" || return 1
92
93     # Wait until ctdbd has started and is ready to respond to clients.
94     _timeout="${CTDB_STARTUP_TIMEOUT:-10}"
95     _count=0
96     while [ "$_count" -lt "$_timeout" ] ; do
97         if $CTDB runstate first_recovery startup running >/dev/null 2>&1 ; then
98             return 0
99         fi
100
101         _count=$((_count + 1))
102         sleep 1
103     done
104
105     echo "Timed out waiting for initialisation - check logs"
106     # Attempt a shutdown just in case things are still running
107     $CTDB shutdown >/dev/null 2>&1
108     drop_all_public_ips >/dev/null 2>&1
109     return 1
110 }
111
112 stop()
113 {
114         $CTDB shutdown
115
116         # The above command is important and needs to stand out, so
117         # post-check exit status
118         # shellcheck disable=SC2181
119         if [ $? -ne 0 ] ; then
120                 echo "Error while shutting down CTDB"
121                 drop_all_public_ips >/dev/null 2>&1
122                 return 1
123         fi
124
125         return 0
126 }
127
128 ############################################################
129
130 # Allow notifications for start/stop.
131 if [ -x "$CTDB_BASE/rc.ctdb" ] ; then
132     "$CTDB_BASE/rc.ctdb" "$action"
133 fi
134
135 case "$action" in
136     start) start ;;
137     stop)  stop  ;;
138     *)
139         echo "usage: $0 {start|stop}"
140         exit 1
141 esac