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", "ok" (or "healthy"), "con" (or "connected"),
38 "rm" (or "recmaster");
39 or a node number (0 base); or
40 list (comma separated) of <NODES>; or
41 range (hyphen separated) of node numbers.
49 echo "Invalid <nodespec>" >&2 ; echo >&2
61 # $POSIXLY_CORRECT means that the command passed to onnode can
62 # take options and getopt won't reorder things to make them
64 local temp=$(POSIXLY_CORRECT=1 getopt -n "$prog" -o "chpqv" -l help -- "$@")
72 -c) current=true ; shift ;;
73 -p) parallel=true ; shift ;;
74 -q) quiet=true ; shift ;;
75 -v) verbose=true ; shift ;;
77 -h|--help|*) usage ;; # Shouldn't happen, so this is reasonable.
87 # Can probably be avoided if we use bash?
95 if [ $n -eq $c ] ; then
105 local node=$(get_nth "$@")
106 if [ -n "$node" ] ; then
109 echo "${prog}: \"node ${n}\" does not exist" >&2
116 # Subshell avoids hacks to restore $IFS.
121 *-*) seq "${i%-*}" "${i#*-}" 2>/dev/null || invalid_nodespec ;;
122 all|ok|healthy|con|connected|rm|recmaster) echo "$i" ;;
124 [ $i -gt -1 ] 2>/dev/null || invalid_nodespec
132 ctdb_status_output=""
133 get_nodes_with_status ()
144 bits="0:[0-1]:[0-1]:[0-1]"
150 if [ -z "$ctdb_status_output" ] ; then
151 # FIXME: need to do something if $CTDB_NODES_SOCKETS is set.
152 ctdb_status_output=$(ctdb -Y status 2>/dev/null)
153 if [ $? -ne 0 ] ; then
154 echo "${prog}: unable to get status of CTDB nodes" >&2
157 ctdb_status_output="${ctdb_status_output#* }"
162 for i in $ctdb_status_output ; do
163 # Try removing bits from end.
164 local t="${i%:${bits}:}"
165 if [ "$t" != "$i" ] ; then
166 # Succeeded. Get address. NOTE: this is an optimisation.
167 # It might be better to get the node number and then use
168 # get_nth() to get the address. This would make things
169 # more consistent if /etc/ctdb/nodes actually contained
171 nodes="${nodes} ${t##*:}"
183 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
184 all_nodes="$CTDB_NODES_SOCKETS"
186 [ -f "$CTDB_NODES_FILE" ] || CTDB_NODES_FILE=/etc/ctdb/nodes
187 all_nodes=$(egrep '^[[:alnum:]]' $CTDB_NODES_FILE)
192 for n in $(parse_nodespec "$1") ; do
193 [ $? != 0 ] && exit 1 # Required to catch exit in above subshell.
197 ok|healthy|con|connected)
198 get_nodes_with_status "$all_nodes" "$n" || exit 1
201 if [ -z "$ctdb_recmaster" ] ; then
202 ctdb_recmaster=$(ctdb recmaster 2>/dev/null)
203 [ $? -eq 0 ] || ctdb_recmaster=""
205 if [ -n "$ctdb_recmaster" ] ; then
206 echo_nth "$ctdb_recmaster" $all_nodes
208 echo "${prog}: No recmaster available" >&2
214 echo_nth $n $all_nodes
222 CTDB_SOCKET="$1" sh -c "$2"
225 ######################################################################
229 $current && command="cd $PWD && $command"
232 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
235 # Could "2>/dev/null || true" but want to see errors from typos in file.
236 [ -r /etc/ctdb/onnode.conf ] && . /etc/ctdb/onnode.conf
237 [ -n "$SSH" ] || SSH=ssh
238 if [ "$SSH" = "ssh" ] ; then
241 : # rsh? All bets are off!
245 ######################################################################
247 nodes=$(get_nodes "$nodespec")
248 [ $? != 0 ] && exit 1 # Required to catch exit in above subshell.
253 # If $nodes contains a space or a newline then assume multiple nodes.
256 [ "$nodes" != "${nodes%[ ${nl}]*}" ] && verbose=true
260 trap 'kill -TERM $pids 2>/dev/null' INT TERM
261 # There's a small race here where the kill can fail if no processes
262 # have been added to $pids and the script is interrupted. However,
263 # the part of the window where it matter is very small.
268 # pipefail is a bashism - is there some way to do this with plain sh?
269 set -o pipefail 2>/dev/null
270 ($SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command" 2>&1 | sed -e "s@^@[$n] @" )&
272 $SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command" &
277 echo >&2 ; echo ">> NODE: $n <<" >&2
280 $SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command"
281 [ $? = 0 ] || retcode=$?
288 [ $? = 0 ] || retcode=$?