ctdb-tests: Update local daemons tests to use CTDB_NOSETSCHED
[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         # ctdbd_wrapper is in config/ directory
13         PATH="${ctdb_dir}/bin:${ctdb_dir}/config:${PATH}"
14         hdir="${ctdb_dir}/bin"
15         export CTDB_LOCK_HELPER="${hdir}/ctdb_lock_helper"
16         export CTDB_EVENT_HELPER="${hdir}/ctdb_event_helper"
17         export CTDB_RECOVERY_HELPER="${hdir}/ctdb_recovery_helper"
18         export CTDB_CLUSTER_MUTEX_HELPER="${hdir}/ctdb_mutex_fcntl_helper"
19 fi
20
21 export CTDB_NODES="${TEST_VAR_DIR}/nodes.txt"
22
23 #######################################
24
25 daemons_stop ()
26 {
27     echo "Attempting to politely shutdown daemons..."
28     onnode -q all $CTDB shutdown || true
29
30     echo "Sleeping for a while..."
31     sleep_for 1
32
33     local pat="ctdbd --sloppy-start --nopublicipcheck"
34     if pgrep -f "$pat" >/dev/null ; then
35         echo "Killing remaining daemons..."
36         pkill -f "$pat"
37
38         if pgrep -f "$pat" >/dev/null ; then
39             echo "Once more with feeling.."
40             pkill -9 -f "$pat"
41         fi
42     fi
43
44     rm -rf "${TEST_VAR_DIR}/test.db"
45 }
46
47 setup_ctdb ()
48 {
49     mkdir -p "${TEST_VAR_DIR}/test.db/persistent"
50
51     local public_addresses_all="${TEST_VAR_DIR}/public_addresses_all"
52     local no_public_addresses="${TEST_VAR_DIR}/no_public_addresses.txt"
53     rm -f $CTDB_NODES $public_addresses_all $no_public_addresses
54
55     # If there are (strictly) greater than 2 nodes then we'll randomly
56     # choose a node to have no public addresses.
57     local no_public_ips=-1
58     [ $TEST_LOCAL_DAEMONS -gt 2 ] && no_public_ips=$(($RANDOM % $TEST_LOCAL_DAEMONS))
59     echo "$no_public_ips" >$no_public_addresses
60
61     # When running certain tests we add and remove eventscripts, so we
62     # need to be able to modify the events.d/ directory.  Therefore,
63     # we use a temporary events.d/ directory under $TEST_VAR_DIR.  We
64     # copy the actual test eventscript(s) in there from the original
65     # events.d/ directory that sits alongside $TEST_SCRIPT_DIR.
66     local top=$(dirname "$TEST_SCRIPTS_DIR")
67     local events_d="${top}/events.d"
68     mkdir -p "${TEST_VAR_DIR}/events.d"
69     cp -p "${events_d}/"* "${TEST_VAR_DIR}/events.d/"
70
71     local i
72     for i in $(seq 1 $TEST_LOCAL_DAEMONS) ; do
73         if [ "${CTDB_USE_IPV6}x" != "x" ]; then
74             j=$((printf "%02x" $i))
75             echo "fd00::5357:5f${j}" >>"$CTDB_NODES"
76             # FIXME: need to add addresses to lo as root before running :-(
77             # ip addr add "fc00:10::${i}/64" dev lo
78             # 2 public addresses on most nodes, just to make things interesting.
79             if [ $(($i - 1)) -ne $no_public_ips ] ; then
80                 echo "fc00:10::1:${i}/64 lo" >>"$public_addresses_all"
81                 echo "fc00:10::1:$(($i + $TEST_LOCAL_DAEMONS))/64 lo" >>"$public_addresses_all"
82             fi
83         else
84             j=$(( $i + 10))
85             echo 127.0.0.$j >>"$CTDB_NODES"
86             # 2 public addresses on most nodes, just to make things interesting.
87             if [ $(($i - 1)) -ne $no_public_ips ] ; then
88                 echo "192.168.234.$i/24 lo" >>"$public_addresses_all"
89                 echo "192.168.234.$(($i + $TEST_LOCAL_DAEMONS))/24 lo" >>"$public_addresses_all"
90             fi
91         fi
92     done
93 }
94
95 daemons_start_1 ()
96 {
97     local pnn="$1"
98     shift # "$@" gets passed to ctdbd
99
100     local public_addresses_all="${TEST_VAR_DIR}/public_addresses_all"
101     local public_addresses_mine="${TEST_VAR_DIR}/public_addresses.${pnn}"
102     local no_public_addresses="${TEST_VAR_DIR}/no_public_addresses.txt"
103     local public_addresses
104
105     local no_public_ips=-1
106     [ -r $no_public_addresses ] && read no_public_ips <$no_public_addresses
107
108     if  [ "$no_public_ips" = $pnn ] ; then
109             echo "Node $no_public_ips will have no public IPs."
110             public_addresses="/dev/null"
111     else
112             cp "$public_addresses_all" "$public_addresses_mine"
113             public_addresses="$public_addresses_mine"
114     fi
115
116     local node_ip=$(sed -n -e "$(($pnn + 1))p" "$CTDB_NODES")
117
118     local pidfile="${TEST_VAR_DIR}/ctdbd.${pnn}.pid"
119     local conf="${TEST_VAR_DIR}/ctdbd.${pnn}.conf"
120     cat >"$conf" <<EOF
121 CTDB_RECOVERY_LOCK="${TEST_VAR_DIR}/rec.lock"
122 CTDB_NODES="$CTDB_NODES"
123 CTDB_NODE_ADDRESS="${node_ip}"
124 CTDB_EVENT_SCRIPT_DIR="${TEST_VAR_DIR}/events.d"
125 CTDB_LOGGING="file:${TEST_VAR_DIR}/daemon.${pnn}.log"
126 CTDB_DEBUGLEVEL=3
127 CTDB_DBDIR="${TEST_VAR_DIR}/test.db"
128 CTDB_DBDIR_PERSISTENT="${TEST_VAR_DIR}/test.db/persistent"
129 CTDB_DBDIR_STATE="${TEST_VAR_DIR}/test.db/state"
130 CTDB_PUBLIC_ADDRESSES="${public_addresses}"
131 CTDB_SOCKET="${TEST_VAR_DIR}/sock.$pnn"
132 CTDB_NOSETSCHED=yes
133 EOF
134
135     # Override from the environment.  This would be easier if env was
136     # guaranteed to quote its output so it could be reused.
137     env |
138     grep '^CTDB_' |
139     sed -e 's@=\([^"]\)@="\1@' -e 's@[^"]$@&"@' -e 's@="$@&"@' >>"$conf"
140
141     # We'll use "pkill -f" to kill the daemons with
142     # "ctdbd --sloppy-start --nopublicipcheck" as context.
143     CTDBD="${VALGRIND} ctdbd --sloppy-start --nopublicipcheck" \
144          CTDBD_CONF="$conf" \
145          ctdbd_wrapper "$pidfile" start
146 }
147
148 daemons_start ()
149 {
150     # "$@" gets passed to ctdbd
151
152     echo "Starting $TEST_LOCAL_DAEMONS ctdb daemons..."
153
154     for i in $(seq 0 $(($TEST_LOCAL_DAEMONS - 1))) ; do
155         daemons_start_1 $i "$@"
156     done
157 }
158
159 maybe_stop_ctdb ()
160 {
161     if $TEST_CLEANUP ; then
162         daemons_stop
163     fi
164 }
165
166 _restart_ctdb_all ()
167 {
168     daemons_stop
169     daemons_start "$@"
170 }