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