change "ctdb pfetch" to take an optional third argument
[metze/ctdb/wip.git] / 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=$(POSIXLY_CORRECT=1 getopt -n "$prog" -o "cf:hno:pqv" -l help -- "$@")
74
75     [ $? != 0 ] && usage
76
77     eval set -- "$temp"
78
79     while true ; do
80         case "$1" in
81             -c) current=true ; shift ;;
82             -f) CTDB_NODES_FILE="$2" ; shift 2 ;;
83             -n) names_ok=true ; shift ;;
84             -o) prefix="$2" ; shift 2 ;;
85             -p) parallel=true ; shift ;;
86             -q) quiet=true ; shift ;;
87             -v) verbose=true ; shift ;;
88             --) shift ; break ;;
89             -h|--help|*) usage ;; # Shouldn't happen, so this is reasonable.
90         esac
91     done
92
93     [ $# -lt 2 ] && usage
94
95     nodespec="$1" ; shift
96     command="$@"
97 }
98
99 echo_nth ()
100 {
101     local n="$1" ; shift
102
103     shift $n
104     local node="$1"
105
106     if [ -n "$node" -a "$node" != "#DEAD" ] ; then
107         echo $node
108     else
109         echo "${prog}: \"node ${n}\" does not exist" >&2
110         exit 1
111     fi
112 }
113
114 parse_nodespec ()
115 {
116     # Subshell avoids hacks to restore $IFS.
117     (
118         IFS=","
119         for i in $1 ; do
120             case "$i" in
121                 *-*) seq "${i%-*}" "${i#*-}" 2>/dev/null || invalid_nodespec ;;
122                 # Separate lines for readability.
123                 all|any|ok|healthy|con|connected) echo "$i" ;;
124                 rm|recmaster|lvs|lvsmaster|natgw|natgwlist) echo "$i" ;;
125                 *)
126                     [ $i -gt -1 ] 2>/dev/null || $names_ok || invalid_nodespec
127                     echo $i
128             esac
129         done
130     )
131 }
132
133 ctdb_status_output="" # cache
134 get_nodes_with_status ()
135 {
136     local all_nodes="$1"
137     local status="$2"
138
139     local bits
140     case "$status" in
141         healthy)
142             bits="0:0:0:0:0"
143             ;;
144         connected)
145             bits="0:[0-1]:[0-1]:[0-1]:[0-1]"
146             ;;
147         *)
148             invalid_nodespec
149     esac
150
151     if [ -z "$ctdb_status_output" ] ; then
152         # FIXME: need to do something if $CTDB_NODES_SOCKETS is set.
153         ctdb_status_output=$(ctdb -Y status 2>/dev/null)
154         if [ $? -ne 0 ] ; then
155             echo "${prog}: unable to get status of CTDB nodes" >&2
156             exit 1
157         fi
158         ctdb_status_output="${ctdb_status_output#* }"
159     fi
160
161     local nodes=""
162     local i
163     for i in $ctdb_status_output ; do
164         # Try removing bits from end.
165         local t="${i%:${bits}:}"
166         if [ "$t" != "$i" ] ; then
167             # Succeeded.  Get address.  NOTE: this is an optimisation.
168             # It might be better to get the node number and then get
169             # the nth node to get the address.  This would make things
170             # more consistent if $ctdb_base/nodes actually contained
171             # hostnames.
172             nodes="${nodes} ${t##*:}"
173         fi
174     done
175
176     echo $nodes
177 }
178
179 ctdb_props="" # cache
180 get_node_with_property ()
181 {
182     local all_nodes="$1"
183     local prop="$2"
184
185     local prop_node=""
186     if [ "${ctdb_props##:${prop}:}" = "$ctdb_props" ] ; then
187         prop_node=$(ctdb "$prop" -Y 2>/dev/null)
188         # We only want the first line.
189         local nl="
190 "
191         prop_node="${prop_node%%${nl}*}"
192         if [ $? -eq 0 ] ; then
193             ctdb_props="${ctdb_props}${ctdb_props:+ }:${prop}:${prop_node}"
194         else
195             prop_node=""
196         fi
197     else
198         prop_node="${ctdb_props##:${prop}:}"
199         prop_node="${prop_node%% *}"
200     fi
201     if [ -n "$prop_node" ] ; then
202         echo_nth "$prop_node" $all_nodes
203     else
204         echo "${prog}: No ${prop} available" >&2
205         exit 1
206     fi
207 }
208
209 get_any_available_node ()
210 {
211     local all_nodes="$1"
212
213     # We do a recursive onnode to find which nodes are up and running.
214     local out=$($0 -pq all ctdb pnn 2>&1)
215     local line
216     while read line ; do 
217         local pnn="${line#PNN:}"
218         if [ "$pnn" != "$line" ] ; then
219             echo_nth "$pnn" $all_nodes
220             return 0
221         fi
222         # Else must be an error message from a down node.
223     done <<<"$out"
224     return 1
225 }
226
227 get_nodes ()
228 {
229     local all_nodes
230
231     if [ -n "$CTDB_NODES_SOCKETS" ] ; then 
232         all_nodes="$CTDB_NODES_SOCKETS"
233     else
234         local f="${ctdb_base}/nodes"
235         if [ -n "$CTDB_NODES_FILE" ] ; then
236             f="$CTDB_NODES_FILE"
237             if [ ! -e "$f" -a "${f#/}" = "$f" ] ; then
238                 # $f is relative, try in $ctdb_base
239                 f="${ctdb_base}/${f}"
240             fi
241         fi
242
243         if [ ! -r "$f" ] ; then
244             echo "${prog}: unable to open nodes file  \"${f}\"" >&2
245             exit 1
246         fi
247
248         all_nodes=$(sed -e 's@#.*@@g' -e 's@ *@@g' -e 's@^$@#DEAD@' "$f")
249     fi
250
251     local nodes=""
252     local n
253     for n in $(parse_nodespec "$1") ; do
254         [ $? != 0 ] && exit 1  # Required to catch exit in above subshell.
255         case "$n" in
256             all)
257                 echo "${all_nodes//#DEAD/}"
258                 ;;
259             any)
260                 get_any_available_node "$all_nodes" || exit 1
261                 ;;
262             ok|healthy) 
263                 get_nodes_with_status "$all_nodes" "healthy" || exit 1
264                 ;;
265             con|connected) 
266                 get_nodes_with_status "$all_nodes" "connected" || exit 1
267                 ;;
268             rm|recmaster)
269                 get_node_with_property "$all_nodes" "recmaster" || exit 1
270                 ;;
271             lvs|lvsmaster)
272                 get_node_with_property "$all_nodes" "lvsmaster" || exit 1
273                 ;;
274             natgw|natgwlist)
275                 get_node_with_property "$all_nodes" "natgwlist" || exit 1
276                 ;;
277             [0-9]|[0-9][0-9]|[0-9][0-9][0-9])
278                 echo_nth $n $all_nodes
279                 ;;
280             *)
281                 $names_ok || invalid_nodespec
282                 echo $n
283         esac
284     done
285 }
286
287 fakessh ()
288 {
289     CTDB_SOCKET="$1" sh -c "$2" 3>/dev/null
290 }
291
292 stdout_filter ()
293 {
294     if [ -n "$prefix" ] ; then
295         cat >"${prefix}.${n//\//_}"
296     elif $verbose && $parallel ; then
297         sed -e "s@^@[$n] @"
298     else
299         cat
300     fi
301 }
302
303 stderr_filter ()
304 {
305     if $verbose && $parallel ; then
306         sed -e "s@^@[$n] @"
307     else
308         cat
309     fi
310 }
311
312 ######################################################################
313
314 parse_options "$@"
315
316 $current && command="cd $PWD && $command"
317
318 ssh_opts=
319 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
320     SSH=fakessh
321 else 
322     # Could "2>/dev/null || true" but want to see errors from typos in file.
323     [ -r "${ctdb_base}/onnode.conf" ] && . "${ctdb_base}/onnode.conf"
324     [ -n "$SSH" ] || SSH=ssh
325     if [ "$SSH" = "ssh" ] ; then
326         ssh_opts="-n"
327     else
328         : # rsh? All bets are off!
329     fi
330 fi
331
332 ######################################################################
333
334 nodes=$(get_nodes "$nodespec")
335 [ $? != 0 ] && exit 1   # Required to catch exit in above subshell.
336
337 if $quiet ; then
338     verbose=false
339 else
340     # If $nodes contains a space or a newline then assume multiple nodes.
341     nl="
342 "
343     [ "$nodes" != "${nodes%[ ${nl}]*}" ] && verbose=true
344 fi
345
346 pids=""
347 trap 'kill -TERM $pids 2>/dev/null' INT TERM
348 # There's a small race here where the kill can fail if no processes
349 # have been added to $pids and the script is interrupted.  However,
350 # the part of the window where it matter is very small.
351 retcode=0
352 for n in $nodes ; do
353     set -o pipefail 2>/dev/null
354     if $parallel ; then
355         { exec 3>&1 ; { $SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command" | stdout_filter >&3 ; } 2>&1 | stderr_filter ; } &
356         pids="${pids} $!"
357     else
358         if $verbose ; then
359             echo >&2 ; echo ">> NODE: $n <<" >&2
360         fi
361
362         { exec 3>&1 ; { $SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command" | stdout_filter >&3 ; } 2>&1 | stderr_filter ; }
363         [ $? = 0 ] || retcode=$?
364     fi
365 done
366
367 $parallel && {
368     for p in $pids; do
369         wait $p
370         [ $? = 0 ] || retcode=$?
371     done
372 }
373
374 exit $retcode