Merge branch 'master' of 10.1.1.27:/shared/ctdb/ctdb-master
[vlendec/samba-autobuild/.git] / ctdb / tools / onnode
1 #!/bin/bash
2
3 # Run commands on CTDB nodes.
4
5 # See http://ctdb.samba.org/ for more information about CTDB.
6
7 # Copyright (C) Martin Schwenke  2008
8
9 # Based on an earlier script by Andrew Tridgell and Ronnie Sahlberg.
10
11 # Copyright (C) Andrew Tridgell  2007
12
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 3 of the License, or
16 # (at your option) any later version.
17    
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22    
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, see <http://www.gnu.org/licenses/>.
25
26 prog=$(basename $0)
27
28 usage ()
29 {
30     cat >&2 <<EOF
31 Usage: onnode [OPTION] ... <NODES> <COMMAND> ...
32   options:
33     -c          Run in current working directory on specified nodes.
34     -o <prefix> Save standard output from each node to file <prefix>.<ip>
35     -p          Run command in parallel on specified nodes.
36     -q          Do not print node addresses (overrides -v).
37     -n          Allow nodes to be specified by name.
38     -f          Specify nodes file, overrides CTDB_NODES_FILE.
39     -v          Print node address even for a single node.
40   <NODES>       "all", "any", "ok" (or "healthy"), "con" (or "connected"),
41                 "rm" (or "recmaster"), "lvs" (or "lvsmaster"),
42                 "natgw" (or "natgwlist"); or
43                 a node number (0 base); or
44                 a hostname (if -n is specified); or
45                 list (comma separated) of <NODES>; or
46                 range (hyphen separated) of node numbers.
47 EOF
48     exit 1
49
50 }
51
52 invalid_nodespec ()
53 {
54     echo "Invalid <nodespec>" >&2 ; echo >&2
55     usage
56 }
57
58 # Defaults.
59 current=false
60 parallel=false
61 verbose=false
62 quiet=false
63 prefix=""
64 names_ok=false
65
66 ctdb_base="${CTDB_BASE:-/etc/ctdb}"
67
68 parse_options ()
69 {
70     # $POSIXLY_CORRECT means that the command passed to onnode can
71     # take options and getopt won't reorder things to make them
72     # options ot onnode.
73     local temp
74     # Not on the previous line - local returns 0!
75     temp=$(POSIXLY_CORRECT=1 getopt -n "$prog" -o "cf:hno:pqv" -l help -- "$@")
76
77     [ $? != 0 ] && usage
78
79     eval set -- "$temp"
80
81     while true ; do
82         case "$1" in
83             -c) current=true ; shift ;;
84             -f) CTDB_NODES_FILE="$2" ; shift 2 ;;
85             -n) names_ok=true ; shift ;;
86             -o) prefix="$2" ; shift 2 ;;
87             -p) parallel=true ; shift ;;
88             -q) quiet=true ; shift ;;
89             -v) verbose=true ; shift ;;
90             --) shift ; break ;;
91             -h|--help|*) usage ;; # Shouldn't happen, so this is reasonable.
92         esac
93     done
94
95     [ $# -lt 2 ] && usage
96
97     nodespec="$1" ; shift
98     command="$@"
99 }
100
101 echo_nth ()
102 {
103     local n="$1" ; shift
104
105     shift $n
106     local node="$1"
107
108     if [ -n "$node" -a "$node" != "#DEAD" ] ; then
109         echo $node
110     else
111         echo "${prog}: \"node ${n}\" does not exist" >&2
112         exit 1
113     fi
114 }
115
116 parse_nodespec ()
117 {
118     # Subshell avoids hacks to restore $IFS.
119     (
120         IFS=","
121         for i in $1 ; do
122             case "$i" in
123                 *-*) seq "${i%-*}" "${i#*-}" 2>/dev/null || invalid_nodespec ;;
124                 # Separate lines for readability.
125                 all|any|ok|healthy|con|connected) echo "$i" ;;
126                 rm|recmaster|lvs|lvsmaster|natgw|natgwlist) echo "$i" ;;
127                 *)
128                     [ $i -gt -1 ] 2>/dev/null || $names_ok || invalid_nodespec
129                     echo $i
130             esac
131         done
132     )
133 }
134
135 ctdb_status_output="" # cache
136 get_nodes_with_status ()
137 {
138     local all_nodes="$1"
139     local status="$2"
140
141     if [ -z "$ctdb_status_output" ] ; then
142         ctdb_status_output=$(ctdb -Y status 2>&1)
143         if [ $? -ne 0 ] ; then
144             echo "${prog}: unable to get status of CTDB nodes" >&2
145             echo "$ctdb_status_output" >&2
146             exit 1
147         fi
148         local nl="
149 "
150         ctdb_status_output="${ctdb_status_output#*${nl}}"
151     fi
152
153     (
154         local i
155         IFS="${IFS}:"
156         while IFS="" read i ; do
157
158             set -- $i # split line on colons
159             shift     # line starts with : so 1st field is empty
160             local pnn="$1" ; shift
161             local ip="$1" ; shift
162
163             case "$status" in
164                 healthy)
165                     # If any bit is not 0, don't match this address.
166                     local s
167                     for s ; do
168                         [ "$s" = "0" ] || continue 2
169                     done
170                     ;;
171                 connected)
172                     # If disconnected bit is not 0, don't match this address.
173                     [ "$1" = "0" ] || continue
174                     ;;
175                 *)
176                     invalid_nodespec
177             esac
178
179             echo_nth "$pnn" $all_nodes
180         done <<<"$ctdb_status_output"
181     )
182 }
183
184 ctdb_props="" # cache
185 get_node_with_property ()
186 {
187     local all_nodes="$1"
188     local prop="$2"
189
190     local prop_node=""
191     if [ "${ctdb_props##:${prop}:}" = "$ctdb_props" ] ; then
192         # Not in cache.
193         prop_node=$(ctdb "$prop" -Y 2>/dev/null)
194         if [ $? -eq 0 ] ; then
195             if [ "$prop" = "natgwlist" ] ; then
196                 prop_node="${prop_node%% *}" # 1st word
197                 if [ "$prop_node" = "-1" ] ; then
198                     # This works around natgwlist returning 0 even
199                     # when there's no natgw.
200                     prop_node=""
201                 fi
202             else
203                 # We only want the first line.
204                 local nl="
205 "
206                 prop_node="${prop_node%%${nl}*}"
207             fi
208         else
209             prop_node=""
210         fi
211
212         if [ -n "$prop_node" ] ; then
213             # Add to cache.
214             ctdb_props="${ctdb_props}${ctdb_props:+ }:${prop}:${prop_node}"
215         fi
216     else
217         # Get from cache.
218         prop_node="${ctdb_props##:${prop}:}"
219         prop_node="${prop_node%% *}"
220     fi
221
222     if [ -n "$prop_node" ] ; then
223         echo_nth "$prop_node" $all_nodes
224     else
225         echo "${prog}: No ${prop} available" >&2
226         exit 1
227     fi
228 }
229
230 get_any_available_node ()
231 {
232     local all_nodes="$1"
233
234     # We do a recursive onnode to find which nodes are up and running.
235     local out=$($0 -pq all ctdb pnn 2>&1)
236     local line
237     while read line ; do 
238         local pnn="${line#PNN:}"
239         if [ "$pnn" != "$line" ] ; then
240             echo_nth "$pnn" $all_nodes
241             return 0
242         fi
243         # Else must be an error message from a down node.
244     done <<<"$out"
245     return 1
246 }
247
248 get_nodes ()
249 {
250     local all_nodes
251
252     if [ -n "$CTDB_NODES_SOCKETS" ] ; then 
253         all_nodes="$CTDB_NODES_SOCKETS"
254     else
255         local f="${ctdb_base}/nodes"
256         if [ -n "$CTDB_NODES_FILE" ] ; then
257             f="$CTDB_NODES_FILE"
258             if [ ! -e "$f" -a "${f#/}" = "$f" ] ; then
259                 # $f is relative, try in $ctdb_base
260                 f="${ctdb_base}/${f}"
261             fi
262         fi
263
264         if [ ! -r "$f" ] ; then
265             echo "${prog}: unable to open nodes file  \"${f}\"" >&2
266             exit 1
267         fi
268
269         all_nodes=$(sed -e 's@#.*@@g' -e 's@ *@@g' -e 's@^$@#DEAD@' "$f")
270     fi
271
272     local nodes=""
273     local n
274     for n in $(parse_nodespec "$1") ; do
275         [ $? != 0 ] && exit 1  # Required to catch exit in above subshell.
276         case "$n" in
277             all)
278                 echo "${all_nodes//#DEAD/}"
279                 ;;
280             any)
281                 get_any_available_node "$all_nodes" || exit 1
282                 ;;
283             ok|healthy) 
284                 get_nodes_with_status "$all_nodes" "healthy" || exit 1
285                 ;;
286             con|connected) 
287                 get_nodes_with_status "$all_nodes" "connected" || exit 1
288                 ;;
289             rm|recmaster)
290                 get_node_with_property "$all_nodes" "recmaster" || exit 1
291                 ;;
292             lvs|lvsmaster)
293                 get_node_with_property "$all_nodes" "lvsmaster" || exit 1
294                 ;;
295             natgw|natgwlist)
296                 get_node_with_property "$all_nodes" "natgwlist" || exit 1
297                 ;;
298             [0-9]|[0-9][0-9]|[0-9][0-9][0-9])
299                 echo_nth $n $all_nodes
300                 ;;
301             *)
302                 $names_ok || invalid_nodespec
303                 echo $n
304         esac
305     done
306 }
307
308 fakessh ()
309 {
310     CTDB_SOCKET="$1" sh -c "$2" 3>/dev/null
311 }
312
313 stdout_filter ()
314 {
315     if [ -n "$prefix" ] ; then
316         cat >"${prefix}.${n//\//_}"
317     elif $verbose && $parallel ; then
318         sed -e "s@^@[$n] @"
319     else
320         cat
321     fi
322 }
323
324 stderr_filter ()
325 {
326     if $verbose && $parallel ; then
327         sed -e "s@^@[$n] @"
328     else
329         cat
330     fi
331 }
332
333 ######################################################################
334
335 parse_options "$@"
336
337 $current && command="cd $PWD && $command"
338
339 ssh_opts=
340 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
341     SSH=fakessh
342     EXTRA_SSH_OPTS=""
343 else 
344     # Could "2>/dev/null || true" but want to see errors from typos in file.
345     [ -r "${ctdb_base}/onnode.conf" ] && . "${ctdb_base}/onnode.conf"
346     [ -n "$SSH" ] || SSH=ssh
347     if [ "$SSH" = "ssh" ] ; then
348         ssh_opts="-n"
349     else
350         : # rsh? All bets are off!
351     fi
352 fi
353
354 ######################################################################
355
356 nodes=$(get_nodes "$nodespec")
357 [ $? != 0 ] && exit 1   # Required to catch exit in above subshell.
358
359 if $quiet ; then
360     verbose=false
361 else
362     # If $nodes contains a space or a newline then assume multiple nodes.
363     nl="
364 "
365     [ "$nodes" != "${nodes%[ ${nl}]*}" ] && verbose=true
366 fi
367
368 pids=""
369 trap 'kill -TERM $pids 2>/dev/null' INT TERM
370 # There's a small race here where the kill can fail if no processes
371 # have been added to $pids and the script is interrupted.  However,
372 # the part of the window where it matter is very small.
373 retcode=0
374 for n in $nodes ; do
375     set -o pipefail 2>/dev/null
376     if $parallel ; then
377         { exec 3>&1 ; { $SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command" | stdout_filter >&3 ; } 2>&1 | stderr_filter ; } &
378         pids="${pids} $!"
379     else
380         if $verbose ; then
381             echo >&2 ; echo ">> NODE: $n <<" >&2
382         fi
383
384         { exec 3>&1 ; { $SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command" | stdout_filter >&3 ; } 2>&1 | stderr_filter ; }
385         [ $? = 0 ] || retcode=$?
386     fi
387 done
388
389 $parallel && {
390     for p in $pids; do
391         wait $p
392         [ $? = 0 ] || retcode=$?
393     done
394 }
395
396 exit $retcode