/usr/bin/onnode is in ctdb 1.12+git20120201-5.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | #!/bin/bash
# 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.
# Copyright (C) Andrew Tridgell 2007
# 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.
# 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.
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
prog=$(basename $0)
usage ()
{
cat >&2 <<EOF
Usage: onnode [OPTION] ... <NODES> <COMMAND> ...
options:
-c Run in current working directory on specified nodes.
-o <prefix> Save standard output from each node to file <prefix>.<ip>
-p Run command in parallel on specified nodes.
-q Do not print node addresses (overrides -v).
-n Allow nodes to be specified by name.
-f Specify nodes file, overrides CTDB_NODES_FILE.
-v Print node address even for a single node.
<NODES> "all", "any", "ok" (or "healthy"), "con" (or "connected"),
"rm" (or "recmaster"), "lvs" (or "lvsmaster"),
"natgw" (or "natgwlist"); or
a node number (0 base); or
a hostname (if -n is specified); or
list (comma separated) of <NODES>; or
range (hyphen separated) of node numbers.
EOF
exit 1
}
invalid_nodespec ()
{
echo "Invalid <nodespec>" >&2 ; echo >&2
usage
}
# Defaults.
current=false
parallel=false
verbose=false
quiet=false
prefix=""
names_ok=false
ctdb_base="${CTDB_BASE:-/etc/ctdb}"
parse_options ()
{
# $POSIXLY_CORRECT means that the command passed to onnode can
# take options and getopt won't reorder things to make them
# options ot onnode.
local temp
# Not on the previous line - local returns 0!
temp=$(POSIXLY_CORRECT=1 getopt -n "$prog" -o "cf:hno:pqv" -l help -- "$@")
[ $? != 0 ] && usage
eval set -- "$temp"
while true ; do
case "$1" in
-c) current=true ; shift ;;
-f) CTDB_NODES_FILE="$2" ; shift 2 ;;
-n) names_ok=true ; shift ;;
-o) prefix="$2" ; shift 2 ;;
-p) parallel=true ; shift ;;
-q) quiet=true ; shift ;;
-v) verbose=true ; shift ;;
--) shift ; break ;;
-h|--help|*) usage ;; # Shouldn't happen, so this is reasonable.
esac
done
[ $# -lt 2 ] && usage
nodespec="$1" ; shift
command="$@"
}
echo_nth ()
{
local n="$1" ; shift
shift $n
local node="$1"
if [ -n "$node" -a "$node" != "#DEAD" ] ; then
echo $node
else
echo "${prog}: \"node ${n}\" does not exist" >&2
exit 1
fi
}
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 ;;
# Separate lines for readability.
all|any|ok|healthy|con|connected) echo "$i" ;;
rm|recmaster|lvs|lvsmaster|natgw|natgwlist) echo "$i" ;;
*)
[ $i -gt -1 ] 2>/dev/null || $names_ok || invalid_nodespec
echo $i
esac
done
)
}
ctdb_status_output="" # cache
get_nodes_with_status ()
{
local all_nodes="$1"
local status="$2"
if [ -z "$ctdb_status_output" ] ; then
ctdb_status_output=$(ctdb -Y status 2>&1)
if [ $? -ne 0 ] ; then
echo "${prog}: unable to get status of CTDB nodes" >&2
echo "$ctdb_status_output" >&2
exit 1
fi
local nl="
"
ctdb_status_output="${ctdb_status_output#*${nl}}"
fi
(
local i
IFS="${IFS}:"
while IFS="" read i ; do
set -- $i # split line on colons
shift # line starts with : so 1st field is empty
local pnn="$1" ; shift
local ip="$1" ; shift
case "$status" in
healthy)
# If any bit is not 0, don't match this address.
local s
for s ; do
[ "$s" = "0" ] || continue 2
done
;;
connected)
# If disconnected bit is not 0, don't match this address.
[ "$1" = "0" ] || continue
;;
*)
invalid_nodespec
esac
echo_nth "$pnn" $all_nodes
done <<<"$ctdb_status_output"
)
}
ctdb_props="" # cache
get_node_with_property ()
{
local all_nodes="$1"
local prop="$2"
local prop_node=""
if [ "${ctdb_props##:${prop}:}" = "$ctdb_props" ] ; then
# Not in cache.
prop_node=$(ctdb "$prop" -Y 2>/dev/null)
if [ $? -eq 0 ] ; then
if [ "$prop" = "natgwlist" ] ; then
prop_node="${prop_node%% *}" # 1st word
if [ "$prop_node" = "-1" ] ; then
# This works around natgwlist returning 0 even
# when there's no natgw.
prop_node=""
fi
else
# We only want the first line.
local nl="
"
prop_node="${prop_node%%${nl}*}"
fi
else
prop_node=""
fi
if [ -n "$prop_node" ] ; then
# Add to cache.
ctdb_props="${ctdb_props}${ctdb_props:+ }:${prop}:${prop_node}"
fi
else
# Get from cache.
prop_node="${ctdb_props##:${prop}:}"
prop_node="${prop_node%% *}"
fi
if [ -n "$prop_node" ] ; then
echo_nth "$prop_node" $all_nodes
else
echo "${prog}: No ${prop} available" >&2
exit 1
fi
}
get_any_available_node ()
{
local all_nodes="$1"
# We do a recursive onnode to find which nodes are up and running.
local out=$($0 -pq all ctdb pnn 2>&1)
local line
while read line ; do
local pnn="${line#PNN:}"
if [ "$pnn" != "$line" ] ; then
echo_nth "$pnn" $all_nodes
return 0
fi
# Else must be an error message from a down node.
done <<<"$out"
return 1
}
get_nodes ()
{
local all_nodes
if [ -n "$CTDB_NODES_SOCKETS" ] ; then
all_nodes="$CTDB_NODES_SOCKETS"
else
local f="${ctdb_base}/nodes"
if [ -n "$CTDB_NODES_FILE" ] ; then
f="$CTDB_NODES_FILE"
if [ ! -e "$f" -a "${f#/}" = "$f" ] ; then
# $f is relative, try in $ctdb_base
f="${ctdb_base}/${f}"
fi
fi
if [ ! -r "$f" ] ; then
echo "${prog}: unable to open nodes file \"${f}\"" >&2
exit 1
fi
all_nodes=$(sed -e 's@#.*@@g' -e 's@ *@@g' -e 's@^$@#DEAD@' "$f")
fi
local nodes=""
local n
for n in $(parse_nodespec "$1") ; do
[ $? != 0 ] && exit 1 # Required to catch exit in above subshell.
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
;;
rm|recmaster)
get_node_with_property "$all_nodes" "recmaster" || exit 1
;;
lvs|lvsmaster)
get_node_with_property "$all_nodes" "lvsmaster" || exit 1
;;
natgw|natgwlist)
get_node_with_property "$all_nodes" "natgwlist" || exit 1
;;
[0-9]|[0-9][0-9]|[0-9][0-9][0-9])
echo_nth $n $all_nodes
;;
*)
$names_ok || invalid_nodespec
echo $n
esac
done
}
fakessh ()
{
CTDB_SOCKET="$1" sh -c "$2" 3>/dev/null
}
stdout_filter ()
{
if [ -n "$prefix" ] ; then
cat >"${prefix}.${n//\//_}"
elif $verbose && $parallel ; then
sed -e "s@^@[$n] @"
else
cat
fi
}
stderr_filter ()
{
if $verbose && $parallel ; then
sed -e "s@^@[$n] @"
else
cat
fi
}
######################################################################
parse_options "$@"
$current && command="cd $PWD && $command"
ssh_opts=
if [ -n "$CTDB_NODES_SOCKETS" ] ; then
SSH=fakessh
EXTRA_SSH_OPTS=""
else
# Could "2>/dev/null || true" but want to see errors from typos in file.
[ -r "${ctdb_base}/onnode.conf" ] && . "${ctdb_base}/onnode.conf"
[ -n "$SSH" ] || SSH=ssh
if [ "$SSH" = "ssh" ] ; then
ssh_opts="-n"
else
: # rsh? All bets are off!
fi
fi
######################################################################
nodes=$(get_nodes "$nodespec")
[ $? != 0 ] && exit 1 # Required to catch exit in above subshell.
if $quiet ; then
verbose=false
else
# If $nodes contains a space or a newline then assume multiple nodes.
nl="
"
[ "$nodes" != "${nodes%[ ${nl}]*}" ] && verbose=true
fi
pids=""
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.
retcode=0
for n in $nodes ; do
set -o pipefail 2>/dev/null
if $parallel ; then
{ exec 3>&1 ; { $SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command" | stdout_filter >&3 ; } 2>&1 | stderr_filter ; } &
pids="${pids} $!"
else
if $verbose ; then
echo >&2 ; echo ">> NODE: $n <<" >&2
fi
{ exec 3>&1 ; { $SSH $ssh_opts $EXTRA_SSH_OPTS $n "$command" | stdout_filter >&3 ; } 2>&1 | stderr_filter ; }
[ $? = 0 ] || retcode=$?
fi
done
$parallel && {
for p in $pids; do
wait $p
[ $? = 0 ] || retcode=$?
done
}
exit $retcode
|