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 -p Run command in parallel on specified nodes.
38 -P Push given files to nodes instead of running commands.
39 -q Do not print node addresses (overrides -v).
40 -v Print node address even for a single node.
41 <NODES> "all", "any", "ok" (or "healthy"), "con" (or "connected") ; or
42 a node number (0 base); or
43 a hostname (if -n is specified); or
44 list (comma separated) of <NODES>; or
45 range (hyphen separated) of node numbers.
53 echo "Invalid <nodespec>" >&2 ; echo >&2
67 if [ -z "$CTDB_BASE" ] ; then
68 CTDB_BASE
="/usr/local/etc/ctdb"
75 while getopts "cf:hnpqvPi?" opt
; do
78 f
) ctdb_nodes_file
="$OPTARG" ;;
90 if [ $# -lt 2 ] ; then
102 # Note that this is 0-based
104 if [ "$n" -le $# ] ; then
109 if [ -n "$node" ] && [ "$node" != "#DEAD" ] ; then
112 echo "${prog}: \"node ${n}\" does not exist" >&2
119 # Subshell avoids hacks to restore $IFS.
124 *-*) seq "${i%-*}" "${i#*-}" 2>/dev
/null || invalid_nodespec
;;
125 all|any|ok|healthy|con|connected
) echo "$i" ;;
127 [ "$i" -gt -1 ] 2>/dev
/null ||
$names_ok || invalid_nodespec
134 ctdb_status_output
="" # cache
135 get_nodes_with_status
()
140 if [ -z "$ctdb_status_output" ] ; then
141 ctdb_status_output
=$
(ctdb
-X status
2>&1)
142 # No! Checking the exit code afterwards is actually clearer...
143 # shellcheck disable=SC2181
144 if [ $?
-ne 0 ] ; then
145 echo "${prog}: unable to get status of CTDB nodes" >&2
146 echo "$ctdb_status_output" >&2
151 ctdb_status_output
="${ctdb_status_output#*"${nl}"}"
157 while IFS
="" read -r i
; do
159 # Intentional word splitting
160 # shellcheck disable=SC2086
161 set -- $i # split line on colons
162 shift # line starts with : so 1st field is empty
163 local pnn
="$1" ; shift
164 shift # ignore IP address but need status bits below
168 # If any bit is 1, don't match this address.
171 [ "$s" != "1" ] ||
continue 2
175 # If disconnected bit is not 0, don't match this address.
176 [ "$1" = "0" ] ||
continue
182 # Intentional multi-word expansion
183 # shellcheck disable=SC2086
184 echo_nth
"$pnn" $all_nodes
185 done <<<"$ctdb_status_output"
189 get_any_available_node
()
193 # We do a recursive onnode to find which nodes are up and running.
195 out
=$
("$0" -pq all ctdb pnn
2>&1)
196 while read -r line
; do
197 if [[ "$line" =~ ^
[0-9]+$
]] ; then
199 # Intentional multi-word expansion
200 # shellcheck disable=SC2086
201 echo_nth
"$pnn" $all_nodes
204 # Else must be an error message from a down node.
213 local f
="${CTDB_BASE}/nodes"
214 if [ -n "$ctdb_nodes_file" ] ; then
216 if [ ! -e "$f" ] && [ "${f#/}" = "$f" ] ; then
217 # $f is relative, try in $CTDB_BASE
218 f
="${CTDB_BASE}/${f}"
222 if [ ! -r "$f" ] ; then
223 echo "${prog}: unable to open nodes file \"${f}\"" >&2
227 all_nodes
=$
(sed -e 's@#.*@@g' -e 's@ *@@g' -e 's@^$@#DEAD@' "$f")
230 nodes
=$
(parse_nodespec
"$1") ||
exit $?
234 echo "${all_nodes//#DEAD/}"
237 get_any_available_node
"$all_nodes" ||
exit 1
240 get_nodes_with_status
"$all_nodes" "healthy" ||
exit 1
243 get_nodes_with_status
"$all_nodes" "connected" ||
exit 1
245 [0-9]|
[0-9][0-9]|
[0-9][0-9][0-9])
246 # Intentional multi-word expansion
247 # shellcheck disable=SC2086
248 echo_nth
"$n" $all_nodes
251 $names_ok || invalid_nodespec
257 # shellcheck disable=SC2317
258 # push() called indirectly via $ONNODE_SSH
266 $verbose && echo "Pushing $f"
268 /*) rsync
"$f" "[${host}]:${f}" ;;
269 *) rsync
"${PWD}/${f}" "[${host}]:${PWD}/${f}" ;;
274 ######################################################################
280 if [ -n "$ONNODE_SSH" ] ; then
281 export RSYNC_RSH
="$ONNODE_SSH"
285 $current && command="cd $PWD && $command"
287 # Could "2>/dev/null || true" but want to see errors from typos in file.
288 [ -r "${CTDB_BASE}/onnode.conf" ] && .
"${CTDB_BASE}/onnode.conf"
289 [ -n "$ONNODE_SSH" ] || ONNODE_SSH
=ssh
290 # $ONNODE_SSH must accept the -n option - it can be ignored!
291 if $parallel ||
! $stdin ; then
296 ######################################################################
298 nodes
=$
(get_nodes
"$nodespec") ||
exit $?
303 # If $nodes contains a space or a newline then assume multiple nodes.
306 [ "$nodes" != "${nodes%[ "${nl}"]*}" ] && verbose
=true
310 # Intentional multi-word expansion
311 # shellcheck disable=SC2086
312 trap 'kill -TERM $pids 2>/dev/null' INT TERM
313 # There's a small race here where the kill can fail if no processes
314 # have been added to $pids and the script is interrupted. However,
315 # the part of the window where it matter is very small.
318 set -o pipefail
2>/dev
/null
320 ssh_cmd
="$ONNODE_SSH $ssh_opts"
323 $ssh_cmd "$n" "$command" 2>&1 |
sed -e "s@^@[$n] @"
325 $ssh_cmd "$n" "$command"
330 echo >&2 ; echo ">> NODE: $n <<" >&2
333 $ssh_cmd "$n" "$command"
340 wait "$p" || retcode
=$?