onnode changes. "ok" is an alias for "healthy", "con" is an alias for
[obnox/samba/samba-obnox.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     -p         Run command in parallel on specified nodes.
35     -q         Do not print node addresses (overrides -v).
36     -v         Print node address even for a single node.
37   <NODES>      "all", "ok" (or "healthy"), "con" (or "connected"),
38                "rm" (or "recmaster");
39                or a node number (0 base); or
40                list (comma separated) of <NODES>; or
41                range (hyphen separated) of node numbers.
42 EOF
43     exit 1
44
45 }
46
47 invalid_nodespec ()
48 {
49     echo "Invalid <nodespec>" >&2 ; echo >&2
50     usage
51 }
52
53 # Defaults.
54 current=false
55 parallel=false
56 verbose=false
57 quiet=false
58
59 parse_options ()
60 {
61     # $POSIXLY_CORRECT means that the command passed to onnode can
62     # take options and getopt won't reorder things to make them
63     # options ot onnode.
64     local temp=$(POSIXLY_CORRECT=1 getopt -n "$prog" -o "chpqv" -l help -- "$@")
65
66     [ $? != 0 ] && usage
67
68     eval set -- "$temp"
69
70     while true ; do
71         case "$1" in
72             -c) current=true ; shift ;;
73             -p) parallel=true ; shift ;;
74             -q) quiet=true ; shift ;;
75             -v) verbose=true ; shift ;;
76             --) shift ; break ;;
77             -h|--help|*) usage ;; # Shouldn't happen, so this is reasonable.
78         esac
79     done
80
81     [ $# -lt 2 ] && usage
82
83     nodespec="$1" ; shift
84     command="$@"
85 }
86
87 # Can probably be avoided if we use bash?
88 get_nth ()
89 {
90     local n="$1" ; shift
91
92     local c=0
93     local j
94     for j ; do
95         if [ $n -eq $c ] ; then
96             echo $j
97             break
98         fi
99         c=$(($c + 1))
100     done
101 }
102
103 echo_nth ()
104 {
105     local node=$(get_nth "$@")
106     if [ -n "$node" ] ; 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                 all|ok|healthy|con|connected|rm|recmaster) echo "$i" ;;
123                 *)
124                     [ $i -gt -1 ] 2>/dev/null || invalid_nodespec
125                     echo $i
126             esac
127         done
128     )
129 }
130
131 # Cache
132 ctdb_status_output=""
133 get_nodes_with_status ()
134 {
135     local all_nodes="$1"
136     local status="$2"
137
138     local bits
139     case "$status" in
140         ok|healthy)
141             bits="0:0:0:0"
142             ;;
143         con|connected)
144             bits="0:[0-1]:[0-1]:[0-1]"
145             ;;
146         *)
147             invalid_nodespec
148     esac
149
150     if [ -z "$ctdb_status_output" ] ; then
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 use
167             # get_nth() 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_recmaster=""
178 get_nodes ()
179 {
180     [ -f "$CTDB_NODES_FILE" ] || CTDB_NODES_FILE=/etc/ctdb/nodes
181     local all_nodes=$(egrep '^[[:alnum:]]' $CTDB_NODES_FILE)
182
183     local nodes=""
184     local n
185     for n in $(parse_nodespec "$1") ; do
186         [ $? != 0 ] && exit 1  # Required to catch exit in above subshell.
187         case "$n" in
188             all)
189                 echo $all_nodes ;;
190             ok|healthy|con|connected) 
191                 get_nodes_with_status "$all_nodes" "$n" || exit 1
192                 ;;
193             rm|recmaster)
194                 if [ -z "$ctdb_recmaster" ] ; then
195                     ctdb_recmaster=$(ctdb recmaster 2>/dev/null)
196                     [ $? -eq 0 ] || ctdb_recmaster=""
197                 fi
198                 if [ -n "$ctdb_recmaster" ] ; then
199                     echo_nth "$ctdb_recmaster" $all_nodes
200                 else
201                     echo "${prog}: No recmaster available" >&2
202                     exit 1
203                 fi
204                 
205                 ;;
206             *)
207                 echo_nth $n $all_nodes
208         esac
209         
210     done
211 }
212
213 ######################################################################
214
215 parse_options "$@"
216
217 $current && command="cd $PWD && $command"
218
219 SSH_OPTS=
220 # Could "2>/dev/null || true" but want to see errors from typos in file.
221 [ -r /etc/ctdb/onnode.conf ] && . /etc/ctdb/onnode.conf
222 [ -n "$SSH" ] || SSH=ssh
223 if [ "$SSH" = "ssh" ] ; then
224     ssh_opts="-n"
225 else
226     : # rsh? All bets are off!
227 fi
228
229 ######################################################################
230
231 nodes=$(get_nodes "$nodespec")
232 [ $? != 0 ] && exit 1   # Required to catch exit in above subshell.
233
234 if $quiet ; then
235     verbose=false
236 else
237     # If $nodes contains a space or a newline then assume multiple nodes.
238     nl="
239 "
240     [ "$nodes" != "${nodes%[ ${nl}]*}" ] && verbose=true
241 fi
242
243 pids=""
244 trap 'kill -TERM $pids 2>/dev/null' INT TERM
245 # There's a small race here where the kill can fail if no processes
246 # have been added to $pids and the script is interrupted.  However,
247 # the part of the window where it matter is very small.
248 retcode=0
249 for n in $nodes ; do
250     if $parallel ; then
251         if $verbose ; then
252             # pipefail is a bashism - is there some way to do this with plain sh?
253             set -o pipefail 2>/dev/null
254             ($SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command" 2>&1 | sed -e "s@^@[$n] @" )&
255         else
256             $SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command" &
257         fi
258         pids="${pids} $!"
259     else
260         if $verbose ; then
261             echo >&2 ; echo ">> NODE: $n <<" >&2
262         fi
263
264         $SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command" 
265         [ $? = 0 ] || retcode=$?
266     fi
267 done
268
269 $parallel && {
270     for p in $pids; do
271         wait $p
272         [ $? = 0 ] || retcode=$?
273     done
274 }
275
276 exit $retcode