ctdb-tools: Reindent parts of onnode
[samba.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     -f          Specify nodes file, overrides CTDB_NODES_FILE.
35     -i          Keep standard input open - the default is to close it.
36     -n          Allow nodes to be specified by name.
37     -o <prefix> Save standard output from each node to file <prefix>.<ip>
38     -p          Run command in parallel on specified nodes.
39     -P          Push given files to nodes instead of running commands.
40     -q          Do not print node addresses (overrides -v).
41     -v          Print node address even for a single node.
42   <NODES>       "all", "any", "ok" (or "healthy"), "con" (or "connected") ; 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 push=false
66 stdin=false
67
68 if [ -z "$CTDB_BASE" ] ; then
69     CTDB_BASE="/usr/local/etc/ctdb"
70 fi
71
72 . "${CTDB_BASE}/functions"
73 loadconfig "ctdb"
74
75 parse_options ()
76 {
77     # $POSIXLY_CORRECT means that the command passed to onnode can
78     # take options and getopt won't reorder things to make them
79     # options ot onnode.
80     local temp
81     # Not on the previous line - local returns 0!
82     temp=$(POSIXLY_CORRECT=1 getopt -n "$prog" -o "cf:hno:pqvPi" -l help -- "$@")
83
84     # No! Checking the exit code afterwards is actually clearer...
85     # shellcheck disable=SC2181
86     [ $? -eq 0 ] || usage
87
88     eval set -- "$temp"
89
90     while true ; do
91         case "$1" in
92             -c) current=true ; shift ;;
93             -f) CTDB_NODES_FILE="$2" ; shift 2 ;;
94             -n) names_ok=true ; shift ;;
95             -o) prefix="$2" ; shift 2 ;;
96             -p) parallel=true ; shift ;;
97             -q) quiet=true ; shift ;;
98             -v) verbose=true ; shift ;;
99             -P) push=true ; shift ;;
100             -i) stdin=true ; shift ;;
101             --) shift ; break ;;
102             -h|--help|*) usage ;; # Shouldn't happen, so this is reasonable.
103         esac
104     done
105
106     [ $# -lt 2 ] && usage
107
108     nodespec="$1" ; shift
109     command="$*"
110 }
111
112 echo_nth ()
113 {
114     local n="$1" ; shift
115
116     shift "$n"
117     local node="$1"
118
119     if [ -n "$node" -a "$node" != "#DEAD" ] ; then
120         echo "$node"
121     else
122         echo "${prog}: \"node ${n}\" does not exist" >&2
123         exit 1
124     fi
125 }
126
127 parse_nodespec ()
128 {
129     # Subshell avoids hacks to restore $IFS.
130     (
131         IFS=","
132         for i in $1 ; do
133             case "$i" in
134                 *-*) seq "${i%-*}" "${i#*-}" 2>/dev/null || invalid_nodespec ;;
135                 all|any|ok|healthy|con|connected) echo "$i" ;;
136                 *)
137                     [ "$i" -gt -1 ] 2>/dev/null || $names_ok || invalid_nodespec
138                     echo "$i"
139             esac
140         done
141     )
142 }
143
144 ctdb_status_output="" # cache
145 get_nodes_with_status ()
146 {
147     local all_nodes="$1"
148     local status="$2"
149
150     if [ -z "$ctdb_status_output" ] ; then
151         ctdb_status_output=$(ctdb -X status 2>&1)
152         # No! Checking the exit code afterwards is actually clearer...
153         # shellcheck disable=SC2181
154         if [ $? -ne 0 ] ; then
155             echo "${prog}: unable to get status of CTDB nodes" >&2
156             echo "$ctdb_status_output" >&2
157             exit 1
158         fi
159         local nl="
160 "
161         ctdb_status_output="${ctdb_status_output#*${nl}}"
162     fi
163
164     (
165         local i
166         IFS="${IFS}|"
167         while IFS="" read i ; do
168
169             # Intentional word splitting
170             # shellcheck disable=SC2086
171             set -- $i # split line on colons
172             shift     # line starts with : so 1st field is empty
173             local pnn="$1" ; shift
174             shift # ignore IP address but need status bits below
175
176             case "$status" in
177                 healthy)
178                     # If any bit is 1, don't match this address.
179                     local s
180                     for s ; do
181                         [ "$s" != "1" ] || continue 2
182                     done
183                     ;;
184                 connected)
185                     # If disconnected bit is not 0, don't match this address.
186                     [ "$1" = "0" ] || continue
187                     ;;
188                 *)
189                     invalid_nodespec
190             esac
191
192             # Intentional multi-word expansion
193             # shellcheck disable=SC2086
194             echo_nth "$pnn" $all_nodes
195         done <<<"$ctdb_status_output"
196     )
197 }
198
199 get_any_available_node ()
200 {
201     local all_nodes="$1"
202
203     # We do a recursive onnode to find which nodes are up and running.
204     local out line
205     out=$("$0" -pq all ctdb pnn 2>&1)
206     while read line ; do
207         if [[ "$line" =~ ^[0-9]+$ ]] ; then
208             local pnn="$line"
209             # Intentional multi-word expansion
210             # shellcheck disable=SC2086
211             echo_nth "$pnn" $all_nodes
212             return 0
213         fi
214         # Else must be an error message from a down node.
215     done <<<"$out"
216     return 1
217 }
218
219 get_nodes ()
220 {
221         local all_nodes
222
223         local f="${CTDB_BASE}/nodes"
224         if [ -n "$CTDB_NODES_FILE" ] ; then
225                 f="$CTDB_NODES_FILE"
226                 if [ ! -e "$f" -a "${f#/}" = "$f" ] ; then
227                         # $f is relative, try in $CTDB_BASE
228                         f="${CTDB_BASE}/${f}"
229                 fi
230         elif [ -n "$CTDB_NODES" ] ; then
231                 f="$CTDB_NODES"
232         fi
233
234         if [ ! -r "$f" ] ; then
235                 echo "${prog}: unable to open nodes file  \"${f}\"" >&2
236                 exit 1
237         fi
238
239         all_nodes=$(sed -e 's@#.*@@g' -e 's@ *@@g' -e 's@^$@#DEAD@' "$f")
240
241         local n nodes
242         nodes=$(parse_nodespec "$1") || exit $?
243         for n in $nodes ; do
244                 case "$n" in
245                 all)
246                         echo "${all_nodes//#DEAD/}"
247                         ;;
248                 any)
249                         get_any_available_node "$all_nodes" || exit 1
250                         ;;
251                 ok|healthy)
252                         get_nodes_with_status "$all_nodes" "healthy" || exit 1
253                         ;;
254                 con|connected)
255                         get_nodes_with_status "$all_nodes" "connected" || exit 1
256                         ;;
257                 [0-9]|[0-9][0-9]|[0-9][0-9][0-9])
258                         # Intentional multi-word expansion
259                         # shellcheck disable=SC2086
260                         echo_nth "$n" $all_nodes
261                         ;;
262                 *)
263                         $names_ok || invalid_nodespec
264                         echo "$n"
265                 esac
266         done
267 }
268
269 push()
270 {
271     local host="$1"
272     local files="$2"
273
274     local f
275     for f in $files ; do
276         $verbose && echo "Pushing $f"
277         case "$f" in
278             /*) rsync "$f" "[${host}]:${f}" ;;
279             *)  rsync "${PWD}/${f}" "[${host}]:${PWD}/${f}" ;;
280         esac
281     done
282 }
283
284 stdout_filter ()
285 {
286     if [ -n "$prefix" ] ; then
287         cat >"${prefix}.${n//\//_}"
288     elif $verbose && $parallel ; then
289         sed -e "s@^@[$n] @"
290     else
291         cat
292     fi
293 }
294
295 stderr_filter ()
296 {
297     if $verbose && $parallel ; then
298         sed -e "s@^@[$n] @"
299     else
300         cat
301     fi
302 }
303
304 ######################################################################
305
306 parse_options "$@"
307
308 ssh_opts=
309 if $push ; then
310         ONNODE_SSH=push
311         ONNODE_SSH_OPTS=""
312 else
313         $current && command="cd $PWD && $command"
314
315         # Could "2>/dev/null || true" but want to see errors from typos in file.
316         [ -r "${CTDB_BASE}/onnode.conf" ] && . "${CTDB_BASE}/onnode.conf"
317         [ -n "$ONNODE_SSH" ] || ONNODE_SSH=ssh
318         if [ "$ONNODE_SSH" = "ssh" ] ; then
319                 if $parallel || ! $stdin ; then
320                         ssh_opts="-n"
321                 fi
322         else
323                 : # rsh? All bets are off!
324         fi
325 fi
326
327 ######################################################################
328
329 nodes=$(get_nodes "$nodespec") || exit $?
330
331 if $quiet ; then
332     verbose=false
333 else
334     # If $nodes contains a space or a newline then assume multiple nodes.
335     nl="
336 "
337     [ "$nodes" != "${nodes%[ ${nl}]*}" ] && verbose=true
338 fi
339
340 pids=""
341 # Intentional multi-word expansion
342 # shellcheck disable=SC2086
343 trap 'kill -TERM $pids 2>/dev/null' INT TERM
344 # There's a small race here where the kill can fail if no processes
345 # have been added to $pids and the script is interrupted.  However,
346 # the part of the window where it matter is very small.
347 retcode=0
348 for n in $nodes ; do
349         set -o pipefail 2>/dev/null
350
351         # The following code applies stdout_filter and stderr_filter to
352         # the relevant streams.  Both filters are at the end of pipes so
353         # they read from stdin and (by default) write to stdout.  To allow
354         # the filters to operate independently, the output of
355         # stdout_filter is sent to a temporary file descriptor (3), which
356         # is redirected back to stdout at the outermost level.
357         ssh_cmd="$ONNODE_SSH $ssh_opts $ONNODE_SSH_OPTS"
358         if $parallel ; then
359                 {
360                         exec 3>&1
361                         {
362                                 $ssh_cmd "$n" "$command" 3>&- |
363                                         stdout_filter >&3
364                         } 2>&1 | stderr_filter
365                 } &
366                 pids="${pids} $!"
367         else
368                 if $verbose ; then
369                         echo >&2 ; echo ">> NODE: $n <<" >&2
370                 fi
371
372                 {
373                         exec 3>&1
374                         {
375                                 $ssh_cmd "$n" "$command" 3>&- |
376                                         stdout_filter >&3
377                         } 2>&1 | stderr_filter
378                 } || retcode=$?
379         fi
380 done
381
382 if $parallel ; then
383         for p in $pids; do
384                 wait "$p" || retcode=$?
385         done
386 fi
387
388 exit $retcode