ctdb-scripts: Drop CTDB_PUBLIC_ADDRESSES configuration option
[vlendec/samba-autobuild/.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 loadconfig "ctdb"
23
24 ctdbd="${CTDBD:-/usr/local/sbin/ctdbd}"
25
26 ############################################################
27
28 # If necessary, mount volatile database directory on tmpfs
29 dbdir_tmpfs_start ()
30 {
31     if [ -z "$CTDB_DBDIR_TMPFS_OPTIONS" ] ; then
32         return
33     fi
34
35     # Shortcut for readability
36     _opts="$CTDB_DBDIR_TMPFS_OPTIONS"
37
38     mkdir -p "$CTDB_DBDIR" || exit $?
39
40     # If already mounted then remount, otherwise mount
41     if findmnt -t tmpfs "$CTDB_DBDIR" >/dev/null ; then
42         mount -t tmpfs -o "remount,$_opts" none "$CTDB_DBDIR" || \
43             exit $?
44     else
45         mount -t tmpfs -o "$_opts" none "$CTDB_DBDIR" || exit $?
46     fi
47 }
48
49 # If necessary, unmount volatile database tmpfs directory on exit
50 dbdir_tmpfs_stop ()
51 {
52     if [ -z "$CTDB_DBDIR_TMPFS_OPTIONS" ] ; then
53         return
54     fi
55
56     if [ -d "$CTDB_DBDIR" ] && findmnt -t tmpfs "$CTDB_DBDIR" >/dev/null ; then
57         umount "$CTDB_DBDIR"
58     fi
59 }
60
61 # Only the nested function references its arguments
62 # shellcheck disable=SC2120
63 build_ctdb_options ()
64 {
65     ctdb_options=""
66
67     maybe_set ()
68     {
69         # If the given variable isn't set then do nothing
70         [ -n "$2" ] || return
71         # If a required value for the variable and it doesn't match,
72         # then do nothing
73         [ -z "$3" -o "$3" = "$2" ] || return
74
75         val="'$2'"
76         case "$1" in
77             --*) sep="=" ;;
78             -*)  sep=" " ;;
79         esac
80         # For these options we're only passing a value-less flag.
81         if [ -n "$3" ] ; then
82             val=""
83             sep=""
84         fi
85
86         ctdb_options="${ctdb_options}${ctdb_options:+ }${1}${sep}${val}"
87     }
88
89     if [ -z "$CTDB_RECOVERY_LOCK" ] ; then
90         echo "No recovery lock specified. Starting CTDB without split brain prevention."
91     fi
92     maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"
93
94     # build up ctdb_options variable from optional parameters
95     maybe_set "--logging"                "$CTDB_LOGGING"
96     maybe_set "--nlist"                  "$CTDB_NODES"
97     maybe_set "--listen"                 "$CTDB_NODE_ADDRESS"
98     maybe_set "--public-interface"       "$CTDB_PUBLIC_INTERFACE"
99     maybe_set "--dbdir"                  "$CTDB_DBDIR"
100     maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
101     maybe_set "--dbdir-state"            "$CTDB_DBDIR_STATE"
102     maybe_set "--transport"              "$CTDB_TRANSPORT"
103     maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
104     maybe_set "--notification-script"    "$CTDB_NOTIFY_SCRIPT"
105     maybe_set "--start-as-disabled"      "$CTDB_START_AS_DISABLED"    "yes"
106     maybe_set "--start-as-stopped "      "$CTDB_START_AS_STOPPED"     "yes"
107     maybe_set "--no-recmaster"           "$CTDB_CAPABILITY_RECMASTER" "no"
108     maybe_set "--no-lmaster"             "$CTDB_CAPABILITY_LMASTER"   "no"
109     maybe_set "--nosetsched"             "$CTDB_NOSETSCHED"           "yes"
110     maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
111     maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
112 }
113
114 export_debug_variables ()
115 {
116     [ -n "$CTDB_DEBUG_HUNG_SCRIPT" ] && export CTDB_DEBUG_HUNG_SCRIPT
117     [ -n "$CTDB_DEBUG_LOCKS" ] && export CTDB_DEBUG_LOCKS
118 }
119
120 ############################################################
121
122 start()
123 {
124     dbdir_tmpfs_start
125
126     # build_ctdb_options() takes no arguments
127     # shellcheck disable=SC2119
128     build_ctdb_options
129
130     export_debug_variables
131
132     # Explicitly trying to disable core files, no other way
133     # shellcheck disable=SC2039
134     if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
135         ulimit -c 0
136     else
137         ulimit -c unlimited
138     fi
139
140     # Unsupported option easily avoided by not using configuration variable
141     # shellcheck disable=SC2039
142     if [ -n "$CTDB_MAX_OPEN_FILES" ]; then
143         ulimit -n "$CTDB_MAX_OPEN_FILES"
144     fi
145
146     if [ -n "$CTDB_VALGRIND" -a "$CTDB_VALGRIND" != "no" ] ; then
147         if [ "$CTDB_VALGRIND" = "yes" ] ; then
148             ctdbd="valgrind -q --log-file=/usr/local/var/log/ctdb_valgrind ${ctdbd}"
149         else
150             ctdbd="${CTDB_VALGRIND} ${ctdbd}"
151         fi
152         ctdb_options="${ctdb_options} --valgrinding"
153     fi
154
155     case "$CTDB_LOGGING" in
156         syslog:udp|syslog:udp-rfc5424)
157             logger -t ctdbd "CTDB is being run with ${CTDB_LOGGING}.  If nothing is logged then check your syslogd configuration"
158             ;;
159         syslog|syslog:*) : ;;
160         file:*)
161             logger -t ctdbd "CTDB is being run without syslog enabled.  Logs will be in ${CTDB_LOGGING#file:}"
162             ;;
163         *)
164             logger -t ctdbd "CTDB is being run without syslog enabled.  Logs will be in log.ctdb"
165     esac
166
167     eval "$ctdbd" "$ctdb_options" || return 1
168
169     # Wait until ctdbd has started and is ready to respond to clients.
170     _timeout="${CTDB_STARTUP_TIMEOUT:-10}"
171     _count=0
172     while [ "$_count" -lt "$_timeout" ] ; do
173         if $CTDB runstate first_recovery startup running >/dev/null 2>&1 ; then
174             return 0
175         fi
176
177         _count=$((_count + 1))
178         sleep 1
179     done
180
181     echo "Timed out waiting for initialisation - check logs"
182     # Attempt a shutdown just in case things are still running
183     $CTDB shutdown >/dev/null 2>&1
184     drop_all_public_ips >/dev/null 2>&1
185     return 1
186 }
187
188 stop()
189 {
190         $CTDB shutdown
191
192         # The above command is important and needs to stand out, so
193         # post-check exit status
194         # shellcheck disable=SC2181
195         if [ $? -ne 0 ] ; then
196                 echo "Error while shutting down CTDB"
197                 drop_all_public_ips >/dev/null 2>&1
198                 return 1
199         fi
200
201         dbdir_tmpfs_stop
202
203         return 0
204 }
205
206 ############################################################
207
208 # Allow notifications for start/stop.
209 if [ -x "$CTDB_BASE/rc.ctdb" ] ; then
210     "$CTDB_BASE/rc.ctdb" "$action"
211 fi
212
213 case "$action" in
214     start) start ;;
215     stop)  stop  ;;
216     *)
217         echo "usage: $0 {start|stop}"
218         exit 1
219 esac