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