3 # Run commands on CTDB nodes.
5 # See http://ctdb.samba.org/ for more information about CTDB.
7 # Copyright (C) Martin Schwenke 2008
9 # Based on an earlier script by Andrew Tridgell and Ronnie Sahlberg.
11 # Copyright (C) Andrew Tridgell 2007
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.
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.
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/>.
31 Usage: onnode [OPTION] ... <NODES> <COMMAND> ...
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.
54 echo "Invalid <nodespec>" >&2 ; echo >&2
69 if [ -z "$CTDB_BASE" ] ; then
70 CTDB_BASE="/usr/local/etc/ctdb"
77 while getopts "cf:hno:pqvPi?" opt ; do
80 f) ctdb_nodes_file="$OPTARG" ;;
82 o) prefix="$OPTARG" ;;
93 if [ $# -lt 2 ] ; then
108 if [ -n "$node" -a "$node" != "#DEAD" ] ; then
111 echo "${prog}: \"node ${n}\" does not exist" >&2
118 # Subshell avoids hacks to restore $IFS.
123 *-*) seq "${i%-*}" "${i#*-}" 2>/dev/null || invalid_nodespec ;;
124 all|any|ok|healthy|con|connected) echo "$i" ;;
126 [ "$i" -gt -1 ] 2>/dev/null || $names_ok || invalid_nodespec
133 ctdb_status_output="" # cache
134 get_nodes_with_status ()
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
150 ctdb_status_output="${ctdb_status_output#*${nl}}"
156 while IFS="" read i ; do
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
167 # If any bit is 1, don't match this address.
170 [ "$s" != "1" ] || continue 2
174 # If disconnected bit is not 0, don't match this address.
175 [ "$1" = "0" ] || continue
181 # Intentional multi-word expansion
182 # shellcheck disable=SC2086
183 echo_nth "$pnn" $all_nodes
184 done <<<"$ctdb_status_output"
188 get_any_available_node ()
192 # We do a recursive onnode to find which nodes are up and running.
194 out=$("$0" -pq all ctdb pnn 2>&1)
196 if [[ "$line" =~ ^[0-9]+$ ]] ; then
198 # Intentional multi-word expansion
199 # shellcheck disable=SC2086
200 echo_nth "$pnn" $all_nodes
203 # Else must be an error message from a down node.
212 local f="${CTDB_BASE}/nodes"
213 if [ -n "$ctdb_nodes_file" ] ; then
215 if [ ! -e "$f" -a "${f#/}" = "$f" ] ; then
216 # $f is relative, try in $CTDB_BASE
217 f="${CTDB_BASE}/${f}"
221 if [ ! -r "$f" ] ; then
222 echo "${prog}: unable to open nodes file \"${f}\"" >&2
226 all_nodes=$(sed -e 's@#.*@@g' -e 's@ *@@g' -e 's@^$@#DEAD@' "$f")
229 nodes=$(parse_nodespec "$1") || exit $?
233 echo "${all_nodes//#DEAD/}"
236 get_any_available_node "$all_nodes" || exit 1
239 get_nodes_with_status "$all_nodes" "healthy" || exit 1
242 get_nodes_with_status "$all_nodes" "connected" || exit 1
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
250 $names_ok || invalid_nodespec
263 $verbose && echo "Pushing $f"
265 /*) rsync "$f" "[${host}]:${f}" ;;
266 *) rsync "${PWD}/${f}" "[${host}]:${PWD}/${f}" ;;
273 if [ -n "$prefix" ] ; then
274 cat >"${prefix}.${n//\//_}"
275 elif $verbose && $parallel ; then
284 if $verbose && $parallel ; then
291 ######################################################################
300 $current && command="cd $PWD && $command"
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
311 ######################################################################
313 nodes=$(get_nodes "$nodespec") || exit $?
318 # If $nodes contains a space or a newline then assume multiple nodes.
321 [ "$nodes" != "${nodes%[ ${nl}]*}" ] && verbose=true
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.
333 set -o pipefail 2>/dev/null
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"
346 $ssh_cmd "$n" "$command" 3>&- |
348 } 2>&1 | stderr_filter
353 echo >&2 ; echo ">> NODE: $n <<" >&2
356 $ssh_cmd "$n" "$command" | stdout_filter
363 wait "$p" || retcode=$?