1642009a69c9f18bb77b8bcc75460baa557ebc55
[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 }
73
74 export_debug_variables ()
75 {
76     [ -n "$CTDB_DEBUG_HUNG_SCRIPT" ] && export CTDB_DEBUG_HUNG_SCRIPT
77     [ -n "$CTDB_DEBUG_LOCKS" ] && export CTDB_DEBUG_LOCKS
78 }
79
80 ############################################################
81
82 start()
83 {
84     # build_ctdb_options() takes no arguments
85     # shellcheck disable=SC2119
86     build_ctdb_options
87
88     export_debug_variables
89
90     eval "$ctdbd" "$ctdb_options" || return 1
91
92     # Wait until ctdbd has started and is ready to respond to clients.
93     _timeout="${CTDB_STARTUP_TIMEOUT:-10}"
94     _count=0
95     while [ "$_count" -lt "$_timeout" ] ; do
96         if $CTDB runstate first_recovery startup running >/dev/null 2>&1 ; then
97             return 0
98         fi
99
100         _count=$((_count + 1))
101         sleep 1
102     done
103
104     echo "Timed out waiting for initialisation - check logs"
105     # Attempt a shutdown just in case things are still running
106     $CTDB shutdown >/dev/null 2>&1
107     drop_all_public_ips >/dev/null 2>&1
108     return 1
109 }
110
111 stop()
112 {
113         $CTDB shutdown
114
115         # The above command is important and needs to stand out, so
116         # post-check exit status
117         # shellcheck disable=SC2181
118         if [ $? -ne 0 ] ; then
119                 echo "Error while shutting down CTDB"
120                 drop_all_public_ips >/dev/null 2>&1
121                 return 1
122         fi
123
124         return 0
125 }
126
127 ############################################################
128
129 # Allow notifications for start/stop.
130 if [ -x "$CTDB_BASE/rc.ctdb" ] ; then
131     "$CTDB_BASE/rc.ctdb" "$action"
132 fi
133
134 case "$action" in
135     start) start ;;
136     stop)  stop  ;;
137     *)
138         echo "usage: $0 {start|stop}"
139         exit 1
140 esac