2efa91f76f40d265d6f6ca3858e2a88e2b803e0b
[vlendec/samba-autobuild/.git] / ctdb / tests / local_daemons.sh
1 #!/bin/sh
2
3 set -u
4
5 export CTDB_TEST_MODE="yes"
6
7 # Following 2 lines may be modified by installation script
8 CTDB_TESTS_ARE_INSTALLED=false
9 CTDB_TEST_DIR=$(dirname "$0")
10 export CTDB_TESTS_ARE_INSTALLED CTDB_TEST_DIR
11
12 export TEST_SCRIPTS_DIR="${CTDB_TEST_DIR}/scripts"
13
14 . "${TEST_SCRIPTS_DIR}/common.sh"
15
16 if ! $CTDB_TESTS_ARE_INSTALLED ; then
17         hdir="$CTDB_SCRIPTS_HELPER_BINDIR"
18         export CTDB_EVENTD="${hdir}/ctdb-eventd"
19         export CTDB_EVENT_HELPER="${hdir}/ctdb-event"
20         export CTDB_LOCK_HELPER="${hdir}/ctdb_lock_helper"
21         export CTDB_RECOVERY_HELPER="${hdir}/ctdb_recovery_helper"
22         export CTDB_TAKEOVER_HELPER="${hdir}/ctdb_takeover_helper"
23         export CTDB_CLUSTER_MUTEX_HELPER="${hdir}/ctdb_mutex_fcntl_helper"
24 fi
25
26 ########################################
27
28 # If the given IP is hosted then print 2 items: maskbits and iface
29 have_ip ()
30 {
31         _addr="$1"
32
33         case "$_addr" in
34         *:*) _bits=128 ;;
35         *)   _bits=32  ;;
36         esac
37
38         _t=$(ip addr show to "${_addr}/${_bits}")
39         [ -n "$_t" ]
40 }
41
42 setup_nodes ()
43 {
44         _num_nodes="$1"
45         _use_ipv6="$2"
46
47         _have_all_ips=true
48         for _i in $(seq 0 $((_num_nodes - 1)) ) ; do
49                 if $_use_ipv6 ; then
50                         _j=$(printf "%04x" $((0x5f00 + 1 + _i)) )
51                         _node_ip="fd00::5357:${_j}"
52                         if have_ip "$_node_ip" ; then
53                                 echo "$_node_ip"
54                         else
55                                 cat >&2 <<EOF
56 ERROR: ${_node_ip} not on an interface, please add it
57 EOF
58                                 _have_all_ips=false
59                         fi
60                 else
61                         _c=$(( _i / 100 ))
62                         _d=$(( 1 + (_i % 100) ))
63                         echo "127.0.${_c}.${_d}"
64                 fi
65         done
66
67         # Fail if we don't have all of the IPv6 addresses assigned
68         $_have_all_ips
69 }
70
71 setup_public_addresses ()
72 {
73         _num_nodes="$1"
74         _node_no_ips="$2"
75         _use_ipv6="$3"
76
77         for _i in $(seq 0 $((_num_nodes - 1)) ) ; do
78                 if  [ "$_i" -eq "$_node_no_ips" ] ; then
79                         continue
80                 fi
81
82                 # 2 public addresses on most nodes, just to make
83                 # things interesting
84                 if $_use_ipv6 ; then
85                         printf 'fc00:10::1:%x/64 lo\n' $((1 + _i))
86                         printf 'fc00:10::2:%x/64 lo\n' $((1 + _i))
87                 else
88                         _c1=$(( 100 + (_i / 100) ))
89                         _c2=$(( 200 + (_i / 100) ))
90                         _d=$(( 1 + (_i % 100) ))
91                         printf '192.168.%d.%d/24 lo\n' "$_c1" "$_d"
92                         printf '192.168.%d.%d/24 lo\n' "$_c2" "$_d"
93                 fi
94         done
95 }
96
97 setup_socket_wrapper ()
98 {
99         _socket_wrapper_so="$1"
100
101         _so="${directory}/libsocket-wrapper.so"
102         if [ ! -f "$_socket_wrapper_so" ] ; then
103                 die "$0 setup: Unable to find ${_socket_wrapper_so}"
104         fi
105
106         # Find absoluate path if only relative is given
107         case "$_socket_wrapper_so" in
108         /*) : ;;
109         *) _socket_wrapper_so="${PWD}/${_socket_wrapper_so}" ;;
110         esac
111
112         rm -f "$_so"
113         ln -s "$_socket_wrapper_so" "$_so"
114
115         _d="${directory}/sw"
116         rm -rf "$_d"
117         mkdir -p "$_d"
118 }
119
120 local_daemons_setup_usage ()
121 {
122         cat >&2 <<EOF
123 $0 <directory> setup [ <options>... ]
124
125 Options:
126   -F            Disable failover (default: failover enabled)
127   -N <file>     Nodes file (default: automatically generated)
128   -n <num>      Number of nodes (default: 3)
129   -P <file>     Public addresses file (default: automatically generated)
130   -R            Use a command for the recovery lock (default: use a file)
131   -S <library>  Socket wrapper shared library to preload (default: none)
132   -6            Generate IPv6 IPs for nodes, public addresses (default: IPv4)
133 EOF
134
135         exit 1
136 }
137
138 local_daemons_setup ()
139 {
140         _disable_failover=false
141         _nodes_file=""
142         _num_nodes=3
143         _public_addresses_file=""
144         _recovery_lock_use_command=false
145         _socket_wrapper=""
146         _use_ipv6=false
147
148         set -e
149
150         while getopts "FN:n:P:RS:6h?" _opt ; do
151                 case "$_opt" in
152                 F) _disable_failover=true ;;
153                 N) _nodes_file="$OPTARG" ;;
154                 n) _num_nodes="$OPTARG" ;;
155                 P) _public_addresses_file="$OPTARG" ;;
156                 R) _recovery_lock_use_command=true ;;
157                 S) _socket_wrapper="$OPTARG" ;;
158                 6) _use_ipv6=true ;;
159                 \?|h) local_daemons_setup_usage ;;
160                 esac
161         done
162         shift $((OPTIND - 1))
163
164         mkdir -p "$directory"
165
166         _nodes_all="${directory}/nodes"
167         if [ -n "$_nodes_file" ] ; then
168                 cp "$_nodes_file" "$_nodes_all"
169         else
170                 setup_nodes "$_num_nodes" $_use_ipv6 >"$_nodes_all"
171         fi
172
173         # If there are (strictly) greater than 2 nodes then we'll
174         # "randomly" choose a node to have no public addresses
175         _node_no_ips=-1
176         if [ "$_num_nodes" -gt 2 ] ; then
177                 _node_no_ips=$(($$ % _num_nodes))
178         fi
179
180         _public_addresses_all="${directory}/public_addresses"
181         if [ -n "$_public_addresses_file" ] ; then
182                 cp "$_public_addresses_file" "$_public_addresses_all"
183         else
184                 setup_public_addresses "$_num_nodes" \
185                                        $_node_no_ips \
186                                        $_use_ipv6 >"$_public_addresses_all"
187         fi
188
189         _recovery_lock="${directory}/rec.lock"
190         if $_recovery_lock_use_command ; then
191                 _helper="${CTDB_SCRIPTS_HELPER_BINDIR}/ctdb_mutex_fcntl_helper"
192                 _recovery_lock="! ${_helper} ${_recovery_lock}"
193         fi
194
195         if [ -n "$_socket_wrapper" ] ; then
196                 setup_socket_wrapper "$_socket_wrapper"
197         fi
198
199         for _n in $(seq 0 $((_num_nodes - 1))) ; do
200                 # common.sh will set CTDB_TEST_SUITE_DIR to a stupid
201                 # value when installed because common.sh is usually
202                 # sourced by a test.  CTDB_TEST_SUITE_DIR needs to be
203                 # correctly set so setup_ctdb_base() finds the
204                 # etc-ctdb/ subdirectory and the test event script is
205                 # correctly installed, so fix it.
206                 # shellcheck disable=SC2034
207                 CTDB_TEST_SUITE_DIR="$CTDB_TEST_DIR" \
208                            setup_ctdb_base "$directory" "node.${_n}" \
209                                 functions notify.sh debug-hung-script.sh
210
211                 cp "$_nodes_all" "${CTDB_BASE}/nodes"
212
213                 _public_addresses="${CTDB_BASE}/public_addresses"
214
215                 if  [ -z "$_public_addresses_file" ] && \
216                             [ $_node_no_ips -eq "$_n" ] ; then
217                         echo "Node ${_n} will have no public IPs."
218                         : >"$_public_addresses"
219                 else
220                         cp "$_public_addresses_all" "$_public_addresses"
221                 fi
222
223                 _node_ip=$(sed -n -e "$((_n + 1))p" "$_nodes_all")
224
225                 _db_dir="${CTDB_BASE}/db"
226                 for _d in "volatile" "persistent" "state" ; do
227                         mkdir -p "${_db_dir}/${_d}"
228                 done
229
230                 cat >"${CTDB_BASE}/ctdb.conf" <<EOF
231 [logging]
232         location = file:${CTDB_BASE}/log.ctdb
233         log level = INFO
234
235 [cluster]
236         recovery lock = ${_recovery_lock}
237         node address = ${_node_ip}
238
239 [database]
240         volatile database directory = ${_db_dir}/volatile
241         persistent database directory = ${_db_dir}/persistent
242         state database directory = ${_db_dir}/state
243
244 [failover]
245         disabled = ${_disable_failover}
246
247 [event]
248         debug script = debug-hung-script.sh
249 EOF
250         done
251 }
252
253 local_daemons_ssh_usage ()
254 {
255         cat >&2 <<EOF
256 usage: $0 <directory> ssh [ -n ] <ip> <command>
257 EOF
258
259         exit 1
260 }
261
262 local_daemons_ssh ()
263 {
264         if [ $# -lt 2 ] ; then
265                 local_daemons_ssh_usage
266         fi
267
268         # Only try to respect ssh -n option, others can't be used so discard them
269         _close_stdin=false
270         while getopts "nh?" _opt ; do
271                 case "$_opt" in
272                 n) _close_stdin=true ;;
273                 \?|h) local_daemons_ssh_usage ;;
274                 *) : ;;
275                 esac
276         done
277         shift $((OPTIND - 1))
278
279         if [ $# -lt 2 ] ; then
280                 local_daemons_ssh_usage
281         fi
282
283         _nodes="${directory}/nodes"
284
285         # IP adress of node. onnode can pass hostnames but not in these tests
286         _ip="$1" ; shift
287         # "$*" is command
288
289
290         # Determine the correct CTDB base directory
291         _num=$(awk -v ip="$_ip" '$1 == ip { print NR }' "$_nodes")
292         _node=$((_num - 1))
293         export CTDB_BASE="${directory}/node.${_node}"
294
295         if [ ! -d "$CTDB_BASE" ] ; then
296                 die "$0 ssh: Unable to find base for node ${_ip}"
297         fi
298
299         if $_close_stdin ; then
300                 exec sh -c "$*" <&-
301         else
302                 exec sh -c "$*"
303         fi
304 }
305
306 onnode_common ()
307 {
308         # onnode will execute this, which fakes ssh against local daemons
309         export ONNODE_SSH="${0} ${directory} ssh"
310
311         # onnode just needs the nodes file, so use the common one
312         export CTDB_BASE="$directory"
313 }
314
315 local_daemons_generic_usage ()
316 {
317         cat >&2 <<EOF
318 usage: $0 <directory> ${1} <nodes>
319
320 <nodes> can be  "all", a node number or any specification supported by onnode
321 EOF
322
323         exit 1
324 }
325
326 local_daemons_start_socket_wrapper ()
327 {
328         _so="${directory}/libsocket-wrapper.so"
329         _d="${directory}/sw"
330
331         if [ -d "$_d" ] && [ -f "$_so" ] ; then
332                 export SOCKET_WRAPPER_DIR="$_d"
333                 export LD_PRELOAD="$_so"
334         fi
335 }
336
337 local_daemons_start ()
338 {
339         if [ $# -ne 1 ] || [ "$1" = "-h" ] ; then
340                 local_daemons_generic_usage "start"
341         fi
342
343         local_daemons_start_socket_wrapper
344
345         _nodes="$1"
346
347         onnode_common
348
349         onnode "$_nodes" "${VALGRIND:-} ctdbd &"
350 }
351
352 local_daemons_stop ()
353 {
354         if [ $# -ne 1 ] || [ "$1" = "-h" ] ; then
355                 local_daemons_generic_usage "stop"
356         fi
357
358         _nodes="$1"
359
360         onnode_common
361
362         onnode -p "$_nodes" "${VALGRIND:-} ${CTDB:-ctdb} shutdown"
363 }
364
365 local_daemons_onnode_usage ()
366 {
367         cat >&2 <<EOF
368 usage: $0 <directory> onnode <nodes> <command>...
369
370 <nodes> can be  "all", a node number or any specification supported by onnode
371 EOF
372
373         exit 1
374 }
375
376 local_daemons_onnode ()
377 {
378         if [ $# -lt 2 ] || [ "$1" = "-h" ] ; then
379                 local_daemons_onnode_usage
380         fi
381
382         _nodes="$1"
383         shift
384
385         onnode_common
386
387         onnode "$_nodes" "$@"
388 }
389
390 local_daemons_print_socket ()
391 {
392         if [ $# -ne 1 ] || [ "$1" = "-h" ] ; then
393                 local_daemons_generic_usage "print-socket"
394         fi
395
396         _nodes="$1"
397         shift
398
399         onnode_common
400
401         _path="${CTDB_SCRIPTS_HELPER_BINDIR}/ctdb-path"
402         onnode -q "$_nodes" "${VALGRIND:-} ${_path} socket ctdbd"
403 }
404
405 local_daemons_print_log ()
406 {
407         if [ $# -ne 1 ] || [ "$1" = "-h" ] ; then
408                 local_daemons_generic_usage "print-log"
409         fi
410
411         _nodes="$1"
412         shift
413
414         onnode_common
415
416         # shellcheck disable=SC2016
417         # $CTDB_BASE must only be expanded under onnode, not in top-level shell
418         onnode -q "$_nodes" 'echo ${CTDB_BASE}/log.ctdb' |
419         while IFS='' read -r _l ; do
420                 _dir=$(dirname "$_l")
421                 _node=$(basename "$_dir")
422                 # Add fake hostname after date and time, which are the
423                 # first 2 words on each line
424                 sed -e "s|^\\([^ ][^ ]* [^ ][^ ]*\\)|\\1 ${_node}|" "$_l"
425         done |
426         sort
427
428 }
429
430 usage ()
431 {
432         cat <<EOF
433 usage: $0 <directory> <command> [ <options>... ]
434
435 Commands:
436   setup          Set up daemon configuration according to given options
437   start          Start specified daemon(s)
438   stop           Stop specified daemon(s)
439   onnode         Run a command in the environment of specified daemon(s)
440   print-socket   Print the Unix domain socket used by specified daemon(s)
441   print-log      Print logs for specified daemon(s) to stdout
442
443 All commands use <directory> for daemon configuration
444
445 Run command with -h option to see per-command usage
446 EOF
447
448         exit 1
449 }
450
451 if [ $# -lt 2 ] ; then
452         usage
453 fi
454
455 directory="$1"
456 command="$2"
457 shift 2
458
459 case "$command" in
460 setup) local_daemons_setup "$@" ;;
461 ssh) local_daemons_ssh "$@" ;; # Internal, not shown by usage()
462 start) local_daemons_start "$@" ;;
463 stop) local_daemons_stop "$@" ;;
464 onnode) local_daemons_onnode "$@" ;;
465 print-socket) local_daemons_print_socket "$@" ;;
466 print-log) local_daemons_print_log "$@" ;;
467 *) usage ;;
468 esac