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 -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" or a node number (0 base); or
38 list (comma separated) of <NODES>; or
39 range (hyphen separated) of node numbers.
47 echo "Invalid <nodespec>" >&2 ; echo >&2
59 # $POSIXLY_CORRECT means that the command passed to onnode can
60 # take options and getopt won't reorder things to make them
62 temp=$(POSIXLY_CORRECT=1 getopt -n "$prog" -o "chpqv" -l help -- "$@")
70 -c) current=true ; shift ;;
71 -p) parallel=true ; shift ;;
72 -q) quiet=true ; shift ;;
73 -v) verbose=true ; shift ;;
75 -h|--help|*) usage ;; # Shouldn't happen, so this is reasonable.
85 # Can probably be avoided if we use bash?
92 if [ $n -eq $c ] ; then
102 # Subshell avoids hacks to restore $IFS.
107 *-*) seq "${i%-*}" "${i#*-}" 2>/dev/null || invalid_nodespec ;;
110 [ $i -gt -1 ] 2>/dev/null || invalid_nodespec
119 [ -f "$CTDB_NODES_FILE" ] || CTDB_NODES_FILE=/etc/ctdb/nodes
120 all_nodes=$(egrep '^[[:alnum:]]' $CTDB_NODES_FILE)
123 for n in $(parse_nodespec "$1") ; do
124 [ $? != 0 ] && exit 1 # Required to catch exit in above subshell.
126 all) echo $all_nodes ;;
127 *) node=$(get_nth $n $all_nodes)
128 if [ -n "$node" ] ; then
131 echo "${prog}: \"node ${n}\" does not exist" >&2
139 ######################################################################
143 $current && command="cd $PWD && $command"
146 # Could "2>/dev/null || true" but want to see errors from typos in file.
147 [ -r /etc/ctdb/onnode.conf ] && . /etc/ctdb/onnode.conf
148 [ -n "$SSH" ] || SSH=ssh
149 if [ "$SSH" = "ssh" ] ; then
152 : # rsh? All bets are off!
155 ######################################################################
157 nodes=$(get_nodes "$nodespec")
158 [ $? != 0 ] && exit 1 # Required to catch exit in above subshell.
163 # If $nodes contains a space or a newline then assume multiple nodes.
166 [ "$nodes" != "${nodes%[ ${nl}]*}" ] && verbose=true
170 trap 'kill -TERM $pids 2>/dev/null' INT TERM
171 # There's a small race here where the kill can fail if no processes
172 # have been added to $pids and the script is interrupted. However,
173 # the part of the window where it matter is very small.
178 # pipefail is a bashism - is there some way to do this with plain sh?
179 set -o pipefail 2>/dev/null
180 ($SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command" 2>&1 | sed -e "s@^@[$n] @" )&
182 $SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command" &
187 echo >&2 ; echo ">> NODE: $n <<" >&2
190 $SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command"
191 [ $? = 0 ] || retcode=$?
198 [ $? = 0 ] || retcode=$?