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