ctdb-daemon: Remove ctdb_event_helper
[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 # Use in-tree binaries if running against local daemons.
5 # Otherwise CTDB need to be installed on all nodes.
6 if [ -n "$ctdb_dir" -a -d "${ctdb_dir}/bin" ] ; then
7         # ctdbd_wrapper is in config/ directory
8         PATH="${ctdb_dir}/bin:${ctdb_dir}/config:${PATH}"
9         hdir="${ctdb_dir}/bin"
10         export CTDB_EVENTD="${hdir}/ctdb_eventd"
11         export CTDB_EVENT_HELPER="${hdir}/ctdb_event"
12         export CTDB_LOCK_HELPER="${hdir}/ctdb_lock_helper"
13         export CTDB_RECOVERY_HELPER="${hdir}/ctdb_recovery_helper"
14         export CTDB_CLUSTER_MUTEX_HELPER="${hdir}/ctdb_mutex_fcntl_helper"
15 fi
16
17 export CTDB_NODES="${TEST_VAR_DIR}/nodes.txt"
18
19 #######################################
20
21 config_from_environment ()
22 {
23         # Override from the environment.  This would be easier if env was
24         # guaranteed to quote its output so it could be reused.
25         env |
26         grep '^CTDB_' |
27         sed -e 's@=\([^"]\)@="\1@' -e 's@[^"]$@&"@' -e 's@="$@&"@'
28 }
29
30 # If the given IP is hosted then print 2 items: maskbits and iface
31 have_ip ()
32 {
33         local addr="$1"
34         local bits t
35
36         case "$addr" in
37         *:*) bits=128 ;;
38         *)   bits=32  ;;
39         esac
40
41         t=$(ip addr show to "${addr}/${bits}")
42         [ -n "$t" ]
43 }
44
45 node_dir ()
46 {
47         local pnn="$1"
48
49         echo "${TEST_VAR_DIR}/node.${pnn}"
50 }
51
52 node_conf ()
53 {
54         local pnn="$1"
55
56         local node_dir=$(node_dir "$pnn")
57         echo "${node_dir}/ctdbd.conf"
58 }
59
60 node_pidfile ()
61 {
62         local pnn="$1"
63
64         local node_dir=$(node_dir "$pnn")
65         echo "${node_dir}/ctdbd.pid"
66 }
67
68 node_socket ()
69 {
70         local pnn="$1"
71
72         local node_dir=$(node_dir "$pnn")
73         echo "${node_dir}/ctdbd.socket"
74 }
75
76 setup_ctdb ()
77 {
78     local public_addresses_all="${TEST_VAR_DIR}/public_addresses_all"
79     rm -f $CTDB_NODES $public_addresses_all
80
81     # If there are (strictly) greater than 2 nodes then we'll randomly
82     # choose a node to have no public addresses.
83     local no_public_ips=-1
84     [ $TEST_LOCAL_DAEMONS -gt 2 ] && no_public_ips=$(($RANDOM % $TEST_LOCAL_DAEMONS))
85
86     # When running certain tests we add and remove eventscripts, so we
87     # need to be able to modify the events.d/ directory.  Therefore,
88     # we use a temporary events.d/ directory under $TEST_VAR_DIR.  We
89     # copy the actual test eventscript(s) in there from the original
90     # events.d/ directory that sits alongside $TEST_SCRIPT_DIR.
91     local top=$(dirname "$TEST_SCRIPTS_DIR")
92     local events_d="${top}/events.d"
93     mkdir -p "${TEST_VAR_DIR}/events.d"
94     cp -p "${events_d}/"* "${TEST_VAR_DIR}/events.d/"
95
96     local have_all_ips=true
97     local i
98     for i in $(seq 1 $TEST_LOCAL_DAEMONS) ; do
99         if [ -n "$CTDB_USE_IPV6" ]; then
100             local j=$(printf "%02x" $i)
101             local node_ip="fd00::5357:5f${j}"
102             if have_ip "$node_ip" ; then
103                 echo "$node_ip" >>"$CTDB_NODES"
104             else
105                 echo "ERROR: ${node_ip} not on an interface, please add it"
106                 have_all_ips=false
107             fi
108
109             # 2 public addresses on most nodes, just to make things interesting.
110             if [ $(($i - 1)) -ne $no_public_ips ] ; then
111                 echo "fc00:10::1:${i}/64 lo" >>"$public_addresses_all"
112                 echo "fc00:10::1:$(($i + $TEST_LOCAL_DAEMONS))/64 lo" >>"$public_addresses_all"
113             fi
114         else
115             j=$(( $i + 10))
116             echo 127.0.0.$j >>"$CTDB_NODES"
117             # 2 public addresses on most nodes, just to make things interesting.
118             if [ $(($i - 1)) -ne $no_public_ips ] ; then
119                 echo "192.168.234.$i/24 lo" >>"$public_addresses_all"
120                 echo "192.168.234.$(($i + $TEST_LOCAL_DAEMONS))/24 lo" >>"$public_addresses_all"
121             fi
122         fi
123     done
124
125     if ! $have_all_ips ; then
126             return 1
127     fi
128
129     local pnn
130     for pnn in $(seq 0 $(($TEST_LOCAL_DAEMONS - 1))) ; do
131         local node_dir=$(node_dir "$pnn")
132         mkdir -p "$node_dir"
133
134         local public_addresses_mine="${node_dir}/public_addresses"
135         local public_addresses
136
137         if  [ "$no_public_ips" = $pnn ] ; then
138             echo "Node $no_public_ips will have no public IPs."
139             public_addresses="/dev/null"
140         else
141             cp "$public_addresses_all" "$public_addresses_mine"
142             public_addresses="$public_addresses_mine"
143         fi
144
145         local node_ip=$(sed -n -e "$(($pnn + 1))p" "$CTDB_NODES")
146
147         local conf=$(node_conf "$pnn")
148         local socket=$(node_socket "$pnn")
149
150         local db_dir="${node_dir}/db"
151         mkdir -p "${db_dir}/persistent"
152
153         cat >"$conf" <<EOF
154 CTDB_RECOVERY_LOCK="${TEST_VAR_DIR}/rec.lock"
155 CTDB_NODES="$CTDB_NODES"
156 CTDB_NODE_ADDRESS="${node_ip}"
157 CTDB_EVENT_SCRIPT_DIR="${TEST_VAR_DIR}/events.d"
158 CTDB_LOGGING="file:${node_dir}/log.ctdb"
159 CTDB_DEBUGLEVEL=INFO
160 CTDB_DBDIR="${db_dir}"
161 CTDB_DBDIR_PERSISTENT="${db_dir}/persistent"
162 CTDB_DBDIR_STATE="${db_dir}/state"
163 CTDB_PUBLIC_ADDRESSES="${public_addresses}"
164 CTDB_SOCKET="${socket}"
165 CTDB_NOSETSCHED=yes
166 EOF
167
168         # Append any configuration variables set in environment to
169         # configuration file so they affect CTDB after each restart.
170         config_from_environment >>"$conf"
171     done
172 }
173
174 daemons_start ()
175 {
176     echo "Starting $TEST_LOCAL_DAEMONS ctdb daemons..."
177
178     local pnn
179     for pnn in $(seq 0 $(($TEST_LOCAL_DAEMONS - 1))) ; do
180         local pidfile=$(node_pidfile "$pnn")
181         local conf=$(node_conf "$pnn")
182
183         # If there is any CTDB configuration in the environment then
184         # append it to the regular configuration in a temporary
185         # configuration file and use it just this once.
186         local tmp_conf=""
187         local env_conf=$(config_from_environment)
188         if [ -n "$env_conf" ] ; then
189                 tmp_conf=$(mktemp --tmpdir="$TEST_VAR_DIR")
190                 cat "$conf" >"$tmp_conf"
191                 echo "$env_conf" >>"$tmp_conf"
192                 conf="$tmp_conf"
193         fi
194
195         CTDBD="${VALGRIND} ctdbd --sloppy-start --nopublicipcheck" \
196              CTDBD_CONF="$conf" \
197              ctdbd_wrapper "$pidfile" start
198
199         if [ -n "$tmp_conf" ] ; then
200                 rm -f "$tmp_conf"
201         fi
202     done
203 }
204
205 daemons_stop ()
206 {
207     echo "Stopping $TEST_LOCAL_DAEMONS ctdb daemons..."
208
209     local pnn
210     for pnn in $(seq 0 $(($TEST_LOCAL_DAEMONS - 1))) ; do
211         local pidfile=$(node_pidfile "$pnn")
212         local conf=$(node_conf "$pnn")
213
214         CTDBD_CONF="$conf" \
215              ctdbd_wrapper "$pidfile" stop
216     done
217
218     rm -rf "${TEST_VAR_DIR}/test.db"
219 }
220
221 maybe_stop_ctdb ()
222 {
223     if $TEST_CLEANUP ; then
224         daemons_stop
225     fi
226 }
227
228 _restart_ctdb_all ()
229 {
230     daemons_stop
231     daemons_start
232 }
233
234 ps_ctdbd ()
235 {
236         # If this fails to find processes then the tests fails, so
237         # look at full command-line so this will work with valgrind.
238         # Note that the output could be generated with pgrep's -a
239         # option but it doesn't exist in older versions.
240         ps -p $(pgrep -f '\<ctdbd\>' | xargs | sed -e 's| |,|g') -o args ww
241         echo
242 }
243
244 # onnode will use CTDB_NODES_SOCKETS to help the ctdb tool connection
245 # to each daemon
246 export CTDB_NODES_SOCKETS=""
247 for i in $(seq 0 $(($TEST_LOCAL_DAEMONS - 1))) ; do
248     socket=$(node_socket "$i")
249     CTDB_NODES_SOCKETS="${CTDB_NODES_SOCKETS}${CTDB_NODES_SOCKETS:+ }${socket}"
250 done