ctdb-tests: Simplify handling of public addresses for local daemons
[amitay/samba.git] / ctdb / tests / simple / scripts / local_daemons.bash
1 # If we're not running on a real cluster then we need a local copy of
2 # ctdb (and other stuff) in $PATH and we will use local daemons.
3
4 export CTDB_NODES_SOCKETS=""
5 for i in $(seq 0 $(($TEST_LOCAL_DAEMONS - 1))) ; do
6     CTDB_NODES_SOCKETS="${CTDB_NODES_SOCKETS}${CTDB_NODES_SOCKETS:+ }${TEST_VAR_DIR}/sock.${i}"
7 done
8
9 # Use in-tree binaries if running against local daemons.
10 # Otherwise CTDB need to be installed on all nodes.
11 if [ -n "$ctdb_dir" -a -d "${ctdb_dir}/bin" ] ; then
12     PATH="${ctdb_dir}/bin:${PATH}"
13     export CTDB_LOCK_HELPER="${ctdb_dir}/bin/ctdb_lock_helper"
14     export CTDB_EVENT_HELPER="${ctdb_dir}/bin/ctdb_event_helper"
15     export CTDB_RECOVERY_HELPER="${ctdb_dir}/bin/ctdb_recovery_helper"
16 fi
17
18 export CTDB_NODES="${TEST_VAR_DIR}/nodes.txt"
19
20 #######################################
21
22 daemons_stop ()
23 {
24     echo "Attempting to politely shutdown daemons..."
25     onnode -q all $CTDB shutdown || true
26
27     echo "Sleeping for a while..."
28     sleep_for 1
29
30     local pat="ctdbd --socket=${TEST_VAR_DIR}/.* --nlist .* --nopublicipcheck"
31     if pgrep -f "$pat" >/dev/null ; then
32         echo "Killing remaining daemons..."
33         pkill -f "$pat"
34
35         if pgrep -f "$pat" >/dev/null ; then
36             echo "Once more with feeling.."
37             pkill -9 -f "$pat"
38         fi
39     fi
40
41     rm -rf "${TEST_VAR_DIR}/test.db"
42 }
43
44 setup_ctdb ()
45 {
46     mkdir -p "${TEST_VAR_DIR}/test.db/persistent"
47
48     local public_addresses_all="${TEST_VAR_DIR}/public_addresses_all"
49     local no_public_addresses="${TEST_VAR_DIR}/no_public_addresses.txt"
50     rm -f $CTDB_NODES $public_addresses_all $no_public_addresses
51
52     # If there are (strictly) greater than 2 nodes then we'll randomly
53     # choose a node to have no public addresses.
54     local no_public_ips=-1
55     [ $TEST_LOCAL_DAEMONS -gt 2 ] && no_public_ips=$(($RANDOM % $TEST_LOCAL_DAEMONS))
56     echo "$no_public_ips" >$no_public_addresses
57
58     # When running certain tests we add and remove eventscripts, so we
59     # need to be able to modify the events.d/ directory.  Therefore,
60     # we use a temporary events.d/ directory under $TEST_VAR_DIR.  We
61     # copy the actual test eventscript(s) in there from the original
62     # events.d/ directory that sits alongside $TEST_SCRIPT_DIR.
63     local top=$(dirname "$TEST_SCRIPTS_DIR")
64     local events_d="${top}/events.d"
65     mkdir -p "${TEST_VAR_DIR}/events.d"
66     cp -p "${events_d}/"* "${TEST_VAR_DIR}/events.d/"
67
68     local i
69     for i in $(seq 1 $TEST_LOCAL_DAEMONS) ; do
70         if [ "${CTDB_USE_IPV6}x" != "x" ]; then
71             j=$((printf "%02x" $i))
72             echo "fd00::5357:5f${j}" >>"$CTDB_NODES"
73             # FIXME: need to add addresses to lo as root before running :-(
74             # ip addr add "fc00:10::${i}/64" dev lo
75             # 2 public addresses on most nodes, just to make things interesting.
76             if [ $(($i - 1)) -ne $no_public_ips ] ; then
77                 echo "fc00:10::1:${i}/64 lo" >>"$public_addresses_all"
78                 echo "fc00:10::1:$(($i + $TEST_LOCAL_DAEMONS))/64 lo" >>"$public_addresses_all"
79             fi
80         else
81             j=$(( $i + 10))
82             echo 127.0.0.$j >>"$CTDB_NODES"
83             # 2 public addresses on most nodes, just to make things interesting.
84             if [ $(($i - 1)) -ne $no_public_ips ] ; then
85                 echo "192.168.234.$i/24 lo" >>"$public_addresses_all"
86                 echo "192.168.234.$(($i + $TEST_LOCAL_DAEMONS))/24 lo" >>"$public_addresses_all"
87             fi
88         fi
89     done
90 }
91
92 daemons_start_1 ()
93 {
94     local pnn="$1"
95     shift # "$@" gets passed to ctdbd
96
97     local public_addresses_all="${TEST_VAR_DIR}/public_addresses_all"
98     local public_addresses_mine="${TEST_VAR_DIR}/public_addresses.${pnn}"
99     local no_public_addresses="${TEST_VAR_DIR}/no_public_addresses.txt"
100     local public_addresses
101
102     local no_public_ips=-1
103     [ -r $no_public_addresses ] && read no_public_ips <$no_public_addresses
104
105     if  [ "$no_public_ips" = $pnn ] ; then
106             echo "Node $no_public_ips will have no public IPs."
107             public_addresses="/dev/null"
108     else
109             cp "$public_addresses_all" "$public_addresses_mine"
110             public_addresses="$public_addresses_mine"
111     fi
112
113     local node_ip=$(sed -n -e "$(($pnn + 1))p" "$CTDB_NODES")
114     local ctdb_options="--sloppy-start --reclock=${TEST_VAR_DIR}/rec.lock --nlist $CTDB_NODES --nopublicipcheck --listen=${node_ip} --event-script-dir=${TEST_VAR_DIR}/events.d --logging=file:${TEST_VAR_DIR}/daemon.${pnn}.log -d 3 --dbdir=${TEST_VAR_DIR}/test.db --dbdir-persistent=${TEST_VAR_DIR}/test.db/persistent --dbdir-state=${TEST_VAR_DIR}/test.db/state --nosetsched --public-addresses=${public_addresses}"
115
116     # We'll use "pkill -f" to kill the daemons with
117     # "--socket=.* --nlist .* --nopublicipcheck" as context.
118     $VALGRIND ctdbd --socket="${TEST_VAR_DIR}/sock.$pnn" $ctdb_options "$@" ||return 1
119 }
120
121 daemons_start ()
122 {
123     # "$@" gets passed to ctdbd
124
125     echo "Starting $TEST_LOCAL_DAEMONS ctdb daemons..."
126
127     for i in $(seq 0 $(($TEST_LOCAL_DAEMONS - 1))) ; do
128         daemons_start_1 $i "$@"
129     done
130 }
131
132 maybe_stop_ctdb ()
133 {
134     if $TEST_CLEANUP ; then
135         daemons_stop
136     fi
137 }
138
139 _restart_ctdb_all ()
140 {
141     daemons_stop
142     daemons_start "$@"
143 }