third_party: Link uid_wrapper against pthread
[Samba.git] / ctdb / tests / local_daemons.sh
blob6217e54332d2aa7375548eb598544438323b58a8
1 #!/bin/sh
3 set -u
5 export CTDB_TEST_MODE="yes"
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
12 export TEST_SCRIPTS_DIR="${CTDB_TEST_DIR}/scripts"
14 . "${TEST_SCRIPTS_DIR}/common.sh"
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"
26 ########################################
28 # If the given IP is hosted then print 2 items: maskbits and iface
29 have_ip ()
31 _addr="$1"
33 case "$_addr" in
34 *:*) _bits=128 ;;
35 *) _bits=32 ;;
36 esac
38 _t=$(ip addr show to "${_addr}/${_bits}")
39 [ -n "$_t" ]
42 setup_nodes ()
44 _num_nodes="$1"
45 _use_ipv6="$2"
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
60 else
61 _c=$(( _i / 100 ))
62 _d=$(( 1 + (_i % 100) ))
63 echo "127.0.${_c}.${_d}"
65 done
67 # Fail if we don't have all of the IPv6 addresses assigned
68 $_have_all_ips
71 setup_public_addresses ()
73 _num_nodes="$1"
74 _node_no_ips="$2"
75 _use_ipv6="$3"
77 for _i in $(seq 0 $((_num_nodes - 1)) ) ; do
78 if [ "$_i" -eq "$_node_no_ips" ] ; then
79 continue
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"
94 done
97 setup_socket_wrapper ()
99 _socket_wrapper_so="$1"
101 _so="${directory}/libsocket-wrapper.so"
102 if [ ! -f "$_socket_wrapper_so" ] ; then
103 die "$0 setup: Unable to find ${_socket_wrapper_so}"
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
112 rm -f "$_so"
113 ln -s "$_socket_wrapper_so" "$_so"
115 _d="${directory}/sw"
116 rm -rf "$_d"
117 mkdir -p "$_d"
120 local_daemons_setup_usage ()
122 cat >&2 <<EOF
123 $0 <directory> setup [ <options>... ]
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)
135 exit 1
138 local_daemons_setup ()
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
148 set -e
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))
164 mkdir -p "$directory"
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"
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))
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"
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}"
195 if [ -n "$_socket_wrapper" ] ; then
196 setup_socket_wrapper "$_socket_wrapper"
199 for _n in $(seq 0 $((_num_nodes - 1))) ; do
200 # CTDB_TEST_SUITE_DIR needs to be correctly set so
201 # setup_ctdb_base() finds the etc-ctdb/ subdirectory
202 # and the test event script is correctly installed
203 # shellcheck disable=SC2034
204 CTDB_TEST_SUITE_DIR="$CTDB_TEST_DIR" \
205 setup_ctdb_base "$directory" "node.${_n}" \
206 functions notify.sh debug-hung-script.sh
208 cp "$_nodes_all" "${CTDB_BASE}/nodes"
210 _public_addresses="${CTDB_BASE}/public_addresses"
212 if [ -z "$_public_addresses_file" ] && \
213 [ $_node_no_ips -eq "$_n" ] ; then
214 echo "Node ${_n} will have no public IPs."
215 : >"$_public_addresses"
216 else
217 cp "$_public_addresses_all" "$_public_addresses"
220 _node_ip=$(sed -n -e "$((_n + 1))p" "$_nodes_all")
222 _db_dir="${CTDB_BASE}/db"
223 for _d in "volatile" "persistent" "state" ; do
224 mkdir -p "${_db_dir}/${_d}"
225 done
227 cat >"${CTDB_BASE}/ctdb.conf" <<EOF
228 [logging]
229 location = file:${CTDB_BASE}/log.ctdb
230 log level = INFO
232 [cluster]
233 recovery lock = ${_recovery_lock}
234 node address = ${_node_ip}
236 [database]
237 volatile database directory = ${_db_dir}/volatile
238 persistent database directory = ${_db_dir}/persistent
239 state database directory = ${_db_dir}/state
241 [failover]
242 disabled = ${_disable_failover}
244 [event]
245 debug script = debug-hung-script.sh
247 done
250 local_daemons_ssh_usage ()
252 cat >&2 <<EOF
253 usage: $0 <directory> ssh [ -n ] <ip> <command>
256 exit 1
259 local_daemons_ssh ()
261 if [ $# -lt 2 ] ; then
262 local_daemons_ssh_usage
265 # Only try to respect ssh -n option, others can't be used so discard them
266 _close_stdin=false
267 while getopts "nh?" _opt ; do
268 case "$_opt" in
269 n) _close_stdin=true ;;
270 \?|h) local_daemons_ssh_usage ;;
271 *) : ;;
272 esac
273 done
274 shift $((OPTIND - 1))
276 if [ $# -lt 2 ] ; then
277 local_daemons_ssh_usage
280 _nodes="${directory}/nodes"
282 # IP adress of node. onnode can pass hostnames but not in these tests
283 _ip="$1" ; shift
284 # "$*" is command
287 # Determine the correct CTDB base directory
288 _num=$(awk -v ip="$_ip" '$1 == ip { print NR }' "$_nodes")
289 _node=$((_num - 1))
290 export CTDB_BASE="${directory}/node.${_node}"
292 if [ ! -d "$CTDB_BASE" ] ; then
293 die "$0 ssh: Unable to find base for node ${_ip}"
296 if $_close_stdin ; then
297 exec sh -c "$*" <&-
298 else
299 exec sh -c "$*"
303 onnode_common ()
305 # onnode will execute this, which fakes ssh against local daemons
306 export ONNODE_SSH="${0} ${directory} ssh"
308 # onnode just needs the nodes file, so use the common one
309 export CTDB_BASE="$directory"
312 local_daemons_generic_usage ()
314 cat >&2 <<EOF
315 usage: $0 <directory> ${1} <nodes>
317 <nodes> can be "all", a node number or any specification supported by onnode
320 exit 1
323 local_daemons_start_socket_wrapper ()
325 _so="${directory}/libsocket-wrapper.so"
326 _d="${directory}/sw"
328 if [ -d "$_d" ] && [ -f "$_so" ] ; then
329 export SOCKET_WRAPPER_DIR="$_d"
330 export LD_PRELOAD="$_so"
334 local_daemons_start ()
336 if [ $# -ne 1 ] || [ "$1" = "-h" ] ; then
337 local_daemons_generic_usage "start"
340 local_daemons_start_socket_wrapper
342 _nodes="$1"
344 onnode_common
346 onnode "$_nodes" "${VALGRIND:-} ctdbd &"
349 local_daemons_stop ()
351 if [ $# -ne 1 ] || [ "$1" = "-h" ] ; then
352 local_daemons_generic_usage "stop"
355 _nodes="$1"
357 onnode_common
359 onnode -p "$_nodes" "${VALGRIND:-} ${CTDB:-ctdb} shutdown"
362 local_daemons_onnode_usage ()
364 cat >&2 <<EOF
365 usage: $0 <directory> onnode <nodes> <command>...
367 <nodes> can be "all", a node number or any specification supported by onnode
370 exit 1
373 local_daemons_onnode ()
375 if [ $# -lt 2 ] || [ "$1" = "-h" ] ; then
376 local_daemons_onnode_usage
379 _nodes="$1"
380 shift
382 onnode_common
384 onnode "$_nodes" "$@"
387 local_daemons_print_socket ()
389 if [ $# -ne 1 ] || [ "$1" = "-h" ] ; then
390 local_daemons_generic_usage "print-socket"
393 _nodes="$1"
394 shift
396 onnode_common
398 _path="${CTDB_SCRIPTS_HELPER_BINDIR}/ctdb-path"
399 onnode -q "$_nodes" "${VALGRIND:-} ${_path} socket ctdbd"
402 local_daemons_print_log ()
404 if [ $# -ne 1 ] || [ "$1" = "-h" ] ; then
405 local_daemons_generic_usage "print-log"
408 _nodes="$1"
409 shift
411 onnode_common
413 # shellcheck disable=SC2016
414 # $CTDB_BASE must only be expanded under onnode, not in top-level shell
415 onnode -q "$_nodes" 'echo ${CTDB_BASE}/log.ctdb' |
416 while IFS='' read -r _l ; do
417 _dir=$(dirname "$_l")
418 _node=$(basename "$_dir")
419 # Add fake hostname after date and time, which are the
420 # first 2 words on each line
421 sed -e "s|^\\([^ ][^ ]* [^ ][^ ]*\\)|\\1 ${_node}|" "$_l"
422 done |
423 sort
427 usage ()
429 cat <<EOF
430 usage: $0 <directory> <command> [ <options>... ]
432 Commands:
433 setup Set up daemon configuration according to given options
434 start Start specified daemon(s)
435 stop Stop specified daemon(s)
436 onnode Run a command in the environment of specified daemon(s)
437 print-socket Print the Unix domain socket used by specified daemon(s)
438 print-log Print logs for specified daemon(s) to stdout
440 All commands use <directory> for daemon configuration
442 Run command with -h option to see per-command usage
445 exit 1
448 if [ $# -lt 2 ] ; then
449 usage
452 directory="$1"
453 command="$2"
454 shift 2
456 case "$command" in
457 setup) local_daemons_setup "$@" ;;
458 ssh) local_daemons_ssh "$@" ;; # Internal, not shown by usage()
459 start) local_daemons_start "$@" ;;
460 stop) local_daemons_stop "$@" ;;
461 onnode) local_daemons_onnode "$@" ;;
462 print-socket) local_daemons_print_socket "$@" ;;
463 print-log) local_daemons_print_log "$@" ;;
464 *) usage ;;
465 esac