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