2018-07-01 19:47:37 +10:00
#!/usr/bin/env bash
2008-07-09 14:18:15 +10:00
# Run commands on CTDB nodes.
# See http://ctdb.samba.org/ for more information about CTDB.
# Copyright (C) Martin Schwenke 2008
# Based on an earlier script by Andrew Tridgell and Ronnie Sahlberg.
2008-07-10 14:19:52 +10:00
# Copyright (C) Andrew Tridgell 2007
2008-07-09 14:18:15 +10:00
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
2020-06-04 09:48:03 +10:00
2008-07-09 14:18:15 +10:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2020-06-04 09:48:03 +10:00
2008-07-09 14:18:15 +10:00
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
2016-07-06 17:31:51 +10:00
prog=$(basename "$0")
2008-07-09 14:18:15 +10:00
usage ()
{
cat >&2 <<EOF
2008-07-10 14:19:52 +10:00
Usage: onnode [OPTION] ... <NODES> <COMMAND> ...
options:
2009-05-06 13:17:34 +10:00
-c Run in current working directory on specified nodes.
2018-03-09 16:36:39 +11:00
-f Specify nodes file, overriding default.
2013-11-13 14:04:17 +11:00
-i Keep standard input open - the default is to close it.
-n Allow nodes to be specified by name.
2009-05-06 13:17:34 +10:00
-p Run command in parallel on specified nodes.
2013-11-13 14:04:17 +11:00
-P Push given files to nodes instead of running commands.
2009-05-06 13:17:34 +10:00
-q Do not print node addresses (overrides -v).
-v Print node address even for a single node.
2016-04-18 16:59:53 +10:00
<NODES> "all", "any", "ok" (or "healthy"), "con" (or "connected") ; or
2009-10-14 13:44:57 +11:00
a node number (0 base); or
a hostname (if -n is specified); or
2009-05-06 13:17:34 +10:00
list (comma separated) of <NODES>; or
range (hyphen separated) of node numbers.
2008-07-09 14:18:15 +10:00
EOF
exit 1
}
invalid_nodespec ()
{
echo "Invalid <nodespec>" >&2 ; echo >&2
usage
}
# Defaults.
current=false
2018-03-09 16:36:39 +11:00
ctdb_nodes_file=""
2008-07-09 14:18:15 +10:00
parallel=false
verbose=false
quiet=false
2009-10-14 13:44:57 +11:00
names_ok=false
2012-07-17 16:45:55 +10:00
push=false
2013-11-13 14:04:17 +11:00
stdin=false
2008-07-09 14:18:15 +10:00
2015-08-17 20:47:58 +10:00
if [ -z "$CTDB_BASE" ] ; then
CTDB_BASE="/usr/local/etc/ctdb"
fi
2008-07-09 14:18:15 +10:00
parse_options ()
{
2018-07-01 18:43:06 +10:00
local opt
2019-08-12 16:11:13 +10:00
while getopts "cf:hnpqvPi?" opt ; do
2018-07-01 18:43:06 +10:00
case "$opt" in
c) current=true ;;
f) ctdb_nodes_file="$OPTARG" ;;
n) names_ok=true ;;
p) parallel=true ;;
q) quiet=true ;;
v) verbose=true ;;
P) push=true ;;
i) stdin=true ;;
\?|h) usage ;;
esac
done
shift $((OPTIND - 1))
if [ $# -lt 2 ] ; then
usage
fi
nodespec="$1" ; shift
command="$*"
2008-07-09 14:18:15 +10:00
}
2009-05-11 13:39:31 +10:00
echo_nth ()
2008-07-09 14:18:15 +10:00
{
2008-09-12 11:22:50 +10:00
local n="$1" ; shift
2008-07-09 14:18:15 +10:00
2018-10-12 14:41:42 +11:00
# Note that this is 0-based
local node=""
if [ "$n" -le $# ] ; then
shift "$n"
node="$1"
fi
2008-07-09 14:18:15 +10:00
2019-09-02 14:58:22 +10:00
if [ -n "$node" ] && [ "$node" != "#DEAD" ] ; then
2016-07-06 17:31:51 +10:00
echo "$node"
2008-09-12 16:55:18 +10:00
else
echo "${prog}: \"node ${n}\" does not exist" >&2
exit 1
fi
}
2008-07-09 14:18:15 +10:00
parse_nodespec ()
{
# Subshell avoids hacks to restore $IFS.
(
IFS=","
for i in $1 ; do
case "$i" in
*-*) seq "${i%-*}" "${i#*-}" 2>/dev/null || invalid_nodespec ;;
2009-09-15 19:33:35 +10:00
all|any|ok|healthy|con|connected) echo "$i" ;;
2008-07-09 14:18:15 +10:00
*)
2016-07-06 17:31:51 +10:00
[ "$i" -gt -1 ] 2>/dev/null || $names_ok || invalid_nodespec
echo "$i"
2008-07-09 14:18:15 +10:00
esac
done
)
}
2009-05-11 13:39:31 +10:00
ctdb_status_output="" # cache
2008-09-12 11:22:50 +10:00
get_nodes_with_status ()
{
local all_nodes="$1"
local status="$2"
if [ -z "$ctdb_status_output" ] ; then
2014-11-20 14:39:59 +11:00
ctdb_status_output=$(ctdb -X status 2>&1)
2017-08-11 12:49:32 +10:00
# No! Checking the exit code afterwards is actually clearer...
# shellcheck disable=SC2181
2008-09-12 16:55:18 +10:00
if [ $? -ne 0 ] ; then
echo "${prog}: unable to get status of CTDB nodes" >&2
2011-05-23 15:24:52 +10:00
echo "$ctdb_status_output" >&2
2008-09-12 16:55:18 +10:00
exit 1
fi
2011-05-23 15:24:52 +10:00
local nl="
"
2022-05-27 23:23:48 +10:00
ctdb_status_output="${ctdb_status_output#*"${nl}"}"
2008-09-12 11:22:50 +10:00
fi
2011-05-17 14:24:30 +10:00
(
local i
2014-11-20 14:39:59 +11:00
IFS="${IFS}|"
2020-06-04 09:59:59 +10:00
while IFS="" read -r i ; do
2011-05-17 14:24:30 +10:00
2016-07-06 17:31:51 +10:00
# Intentional word splitting
# shellcheck disable=SC2086
2011-05-23 15:24:52 +10:00
set -- $i # split line on colons
shift # line starts with : so 1st field is empty
2011-05-17 14:24:30 +10:00
local pnn="$1" ; shift
2016-07-06 17:16:44 +10:00
shift # ignore IP address but need status bits below
2011-05-17 14:24:30 +10:00
case "$status" in
healthy)
2013-10-24 14:15:53 +11:00
# If any bit is 1, don't match this address.
2011-05-17 14:24:30 +10:00
local s
for s ; do
2013-10-24 14:15:53 +11:00
[ "$s" != "1" ] || continue 2
2011-05-17 14:24:30 +10:00
done
;;
connected)
# If disconnected bit is not 0, don't match this address.
[ "$1" = "0" ] || continue
;;
*)
invalid_nodespec
esac
2011-05-23 15:24:52 +10:00
2016-07-06 17:31:51 +10:00
# Intentional multi-word expansion
# shellcheck disable=SC2086
2011-05-17 14:24:30 +10:00
echo_nth "$pnn" $all_nodes
2011-05-23 15:24:52 +10:00
done <<<"$ctdb_status_output"
2011-05-17 14:24:30 +10:00
)
2008-09-12 11:22:50 +10:00
}
2009-09-15 19:33:35 +10:00
get_any_available_node ()
{
local all_nodes="$1"
# We do a recursive onnode to find which nodes are up and running.
2016-07-14 12:27:05 +10:00
local out line
out=$("$0" -pq all ctdb pnn 2>&1)
2020-06-04 09:59:59 +10:00
while read -r line ; do
2016-07-22 16:19:23 +10:00
if [[ "$line" =~ ^[0-9]+$ ]] ; then
local pnn="$line"
2016-07-06 17:31:51 +10:00
# Intentional multi-word expansion
# shellcheck disable=SC2086
2009-09-15 19:33:35 +10:00
echo_nth "$pnn" $all_nodes
return 0
fi
# Else must be an error message from a down node.
done <<<"$out"
return 1
}
2008-07-09 14:18:15 +10:00
get_nodes ()
{
2018-02-27 13:59:50 +11:00
local all_nodes
2008-11-20 20:40:01 +11:00
2015-08-17 20:47:58 +10:00
local f="${CTDB_BASE}/nodes"
2018-03-09 16:36:39 +11:00
if [ -n "$ctdb_nodes_file" ] ; then
f="$ctdb_nodes_file"
2019-09-02 14:58:22 +10:00
if [ ! -e "$f" ] && [ "${f#/}" = "$f" ] ; then
2018-02-27 13:59:50 +11:00
# $f is relative, try in $CTDB_BASE
f="${CTDB_BASE}/${f}"
fi
2010-01-21 13:40:03 +11:00
fi
if [ ! -r "$f" ] ; then
2018-02-27 13:59:50 +11:00
echo "${prog}: unable to open nodes file \"${f}\"" >&2
exit 1
2010-01-21 13:40:03 +11:00
fi
all_nodes=$(sed -e 's@#.*@@g' -e 's@ *@@g' -e 's@^$@#DEAD@' "$f")
2008-07-09 14:18:15 +10:00
2018-02-27 13:59:50 +11:00
local n nodes
nodes=$(parse_nodespec "$1") || exit $?
for n in $nodes ; do
case "$n" in
all)
echo "${all_nodes//#DEAD/}"
;;
any)
get_any_available_node "$all_nodes" || exit 1
;;
ok|healthy)
get_nodes_with_status "$all_nodes" "healthy" || exit 1
;;
con|connected)
get_nodes_with_status "$all_nodes" "connected" || exit 1
;;
[0-9]|[0-9][0-9]|[0-9][0-9][0-9])
# Intentional multi-word expansion
# shellcheck disable=SC2086
echo_nth "$n" $all_nodes
;;
*)
$names_ok || invalid_nodespec
echo "$n"
esac
done
2008-07-09 14:18:15 +10:00
}
2023-07-03 13:26:20 +10:00
# shellcheck disable=SC2317
# push() called indirectly via $ONNODE_SSH
2020-06-04 09:48:03 +10:00
push ()
2012-07-17 16:45:55 +10:00
{
2020-06-04 09:48:03 +10:00
local host="$1"
local files="$2"
local f
for f in $files ; do
$verbose && echo "Pushing $f"
case "$f" in
/*) rsync "$f" "[${host}]:${f}" ;;
*) rsync "${PWD}/${f}" "[${host}]:${PWD}/${f}" ;;
esac
done
2012-07-17 16:45:55 +10:00
}
2008-07-09 14:18:15 +10:00
######################################################################
parse_options "$@"
2008-11-20 20:40:01 +11:00
ssh_opts=
2012-07-17 16:45:55 +10:00
if $push ; then
2020-06-04 09:58:41 +10:00
if [ -n "$ONNODE_SSH" ] ; then
export RSYNC_RSH="$ONNODE_SSH"
fi
2018-02-27 13:59:50 +11:00
ONNODE_SSH=push
2012-07-17 16:45:55 +10:00
else
2018-02-27 13:59:50 +11:00
$current && command="cd $PWD && $command"
2012-07-17 16:45:55 +10:00
# Could "2>/dev/null || true" but want to see errors from typos in file.
2015-08-17 20:47:58 +10:00
[ -r "${CTDB_BASE}/onnode.conf" ] && . "${CTDB_BASE}/onnode.conf"
2018-02-27 12:11:54 +11:00
[ -n "$ONNODE_SSH" ] || ONNODE_SSH=ssh
2018-10-03 19:13:57 +10:00
# $ONNODE_SSH must accept the -n option - it can be ignored!
if $parallel || ! $stdin ; then
ssh_opts="-n"
2012-07-17 16:45:55 +10:00
fi
2008-07-09 14:18:15 +10:00
fi
######################################################################
2017-07-13 12:58:33 +10:00
nodes=$(get_nodes "$nodespec") || exit $?
2008-07-09 14:18:15 +10:00
if $quiet ; then
verbose=false
else
# If $nodes contains a space or a newline then assume multiple nodes.
nl="
"
2022-05-27 23:23:48 +10:00
[ "$nodes" != "${nodes%[ "${nl}"]*}" ] && verbose=true
2008-07-09 14:18:15 +10:00
fi
pids=""
2016-07-06 17:31:51 +10:00
# Intentional multi-word expansion
# shellcheck disable=SC2086
2008-07-09 14:18:15 +10:00
trap 'kill -TERM $pids 2>/dev/null' INT TERM
# There's a small race here where the kill can fail if no processes
# have been added to $pids and the script is interrupted. However,
# the part of the window where it matter is very small.
2008-07-14 09:19:22 +10:00
retcode=0
2008-07-09 14:18:15 +10:00
for n in $nodes ; do
2018-02-27 13:59:50 +11:00
set -o pipefail 2>/dev/null
2020-06-04 09:45:26 +10:00
ssh_cmd="$ONNODE_SSH $ssh_opts"
2018-02-27 13:59:50 +11:00
if $parallel ; then
2019-06-28 15:46:57 +10:00
if $verbose ; then
$ssh_cmd "$n" "$command" 2>&1 | sed -e "s@^@[$n] @"
else
$ssh_cmd "$n" "$command"
fi &
2018-02-27 13:59:50 +11:00
pids="${pids} $!"
else
if $verbose ; then
echo >&2 ; echo ">> NODE: $n <<" >&2
fi
{
2019-06-28 15:44:59 +10:00
$ssh_cmd "$n" "$command"
2018-02-27 13:59:50 +11:00
} || retcode=$?
2008-07-10 14:19:52 +10:00
fi
2008-07-09 14:18:15 +10:00
done
2017-08-11 14:06:30 +10:00
if $parallel ; then
2018-02-27 13:59:50 +11:00
for p in $pids; do
wait "$p" || retcode=$?
done
2017-08-11 14:06:30 +10:00
fi
2008-07-14 09:19:22 +10:00
exit $retcode