s3: smbd: Correctly set smb2req->smb1req->posix_pathnames from the calling fsp on...
[Samba.git] / ctdb / tools / onnode
blob425db8047ccbb24cb3dd04facf5fc4786ee8d045
1 #!/usr/bin/env bash
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/>.
26 prog=$(basename "$0")
28 usage ()
30 cat >&2 <<EOF
31 Usage: onnode [OPTION] ... <NODES> <COMMAND> ...
32 options:
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.
46 EOF
47 exit 1
51 invalid_nodespec ()
53 echo "Invalid <nodespec>" >&2 ; echo >&2
54 usage
57 # Defaults.
58 current=false
59 ctdb_nodes_file=""
60 parallel=false
61 verbose=false
62 quiet=false
63 names_ok=false
64 push=false
65 stdin=false
67 if [ -z "$CTDB_BASE" ] ; then
68 CTDB_BASE="/usr/local/etc/ctdb"
71 parse_options ()
73 local opt
75 while getopts "cf:hnpqvPi?" opt ; do
76 case "$opt" in
77 c) current=true ;;
78 f) ctdb_nodes_file="$OPTARG" ;;
79 n) names_ok=true ;;
80 p) parallel=true ;;
81 q) quiet=true ;;
82 v) verbose=true ;;
83 P) push=true ;;
84 i) stdin=true ;;
85 \?|h) usage ;;
86 esac
87 done
88 shift $((OPTIND - 1))
90 if [ $# -lt 2 ] ; then
91 usage
94 nodespec="$1" ; shift
95 command="$*"
98 echo_nth ()
100 local n="$1" ; shift
102 # Note that this is 0-based
103 local node=""
104 if [ "$n" -le $# ] ; then
105 shift "$n"
106 node="$1"
109 if [ -n "$node" ] && [ "$node" != "#DEAD" ] ; then
110 echo "$node"
111 else
112 echo "${prog}: \"node ${n}\" does not exist" >&2
113 exit 1
117 parse_nodespec ()
119 # Subshell avoids hacks to restore $IFS.
121 IFS=","
122 for i in $1 ; do
123 case "$i" in
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
128 echo "$i"
129 esac
130 done
134 ctdb_status_output="" # cache
135 get_nodes_with_status ()
137 local all_nodes="$1"
138 local status="$2"
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
147 exit 1
149 local nl="
151 ctdb_status_output="${ctdb_status_output#*"${nl}"}"
155 local i
156 IFS="${IFS}|"
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
166 case "$status" in
167 healthy)
168 # If any bit is 1, don't match this address.
169 local s
170 for s ; do
171 [ "$s" != "1" ] || continue 2
172 done
174 connected)
175 # If disconnected bit is not 0, don't match this address.
176 [ "$1" = "0" ] || continue
179 invalid_nodespec
180 esac
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 ()
191 local all_nodes="$1"
193 # We do a recursive onnode to find which nodes are up and running.
194 local out line
195 out=$("$0" -pq all ctdb pnn 2>&1)
196 while read -r line ; do
197 if [[ "$line" =~ ^[0-9]+$ ]] ; then
198 local pnn="$line"
199 # Intentional multi-word expansion
200 # shellcheck disable=SC2086
201 echo_nth "$pnn" $all_nodes
202 return 0
204 # Else must be an error message from a down node.
205 done <<<"$out"
206 return 1
209 get_nodes ()
211 local all_nodes
213 local f="${CTDB_BASE}/nodes"
214 if [ -n "$ctdb_nodes_file" ] ; then
215 f="$ctdb_nodes_file"
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
224 exit 1
227 all_nodes=$(sed -e 's@#.*@@g' -e 's@ *@@g' -e 's@^$@#DEAD@' "$f")
229 local n nodes
230 nodes=$(parse_nodespec "$1") || exit $?
231 for n in $nodes ; do
232 case "$n" in
233 all)
234 echo "${all_nodes//#DEAD/}"
236 any)
237 get_any_available_node "$all_nodes" || exit 1
239 ok|healthy)
240 get_nodes_with_status "$all_nodes" "healthy" || exit 1
242 con|connected)
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
252 echo "$n"
253 esac
254 done
257 push ()
259 local host="$1"
260 local files="$2"
262 local f
263 for f in $files ; do
264 $verbose && echo "Pushing $f"
265 case "$f" in
266 /*) rsync "$f" "[${host}]:${f}" ;;
267 *) rsync "${PWD}/${f}" "[${host}]:${PWD}/${f}" ;;
268 esac
269 done
272 ######################################################################
274 parse_options "$@"
276 ssh_opts=
277 if $push ; then
278 if [ -n "$ONNODE_SSH" ] ; then
279 export RSYNC_RSH="$ONNODE_SSH"
281 ONNODE_SSH=push
282 else
283 $current && command="cd $PWD && $command"
285 # Could "2>/dev/null || true" but want to see errors from typos in file.
286 [ -r "${CTDB_BASE}/onnode.conf" ] && . "${CTDB_BASE}/onnode.conf"
287 [ -n "$ONNODE_SSH" ] || ONNODE_SSH=ssh
288 # $ONNODE_SSH must accept the -n option - it can be ignored!
289 if $parallel || ! $stdin ; then
290 ssh_opts="-n"
294 ######################################################################
296 nodes=$(get_nodes "$nodespec") || exit $?
298 if $quiet ; then
299 verbose=false
300 else
301 # If $nodes contains a space or a newline then assume multiple nodes.
302 nl="
304 [ "$nodes" != "${nodes%[ "${nl}"]*}" ] && verbose=true
307 pids=""
308 # Intentional multi-word expansion
309 # shellcheck disable=SC2086
310 trap 'kill -TERM $pids 2>/dev/null' INT TERM
311 # There's a small race here where the kill can fail if no processes
312 # have been added to $pids and the script is interrupted. However,
313 # the part of the window where it matter is very small.
314 retcode=0
315 for n in $nodes ; do
316 set -o pipefail 2>/dev/null
318 ssh_cmd="$ONNODE_SSH $ssh_opts"
319 if $parallel ; then
320 if $verbose ; then
321 $ssh_cmd "$n" "$command" 2>&1 | sed -e "s@^@[$n] @"
322 else
323 $ssh_cmd "$n" "$command"
324 fi &
325 pids="${pids} $!"
326 else
327 if $verbose ; then
328 echo >&2 ; echo ">> NODE: $n <<" >&2
331 $ssh_cmd "$n" "$command"
332 } || retcode=$?
334 done
336 if $parallel ; then
337 for p in $pids; do
338 wait "$p" || retcode=$?
339 done
342 exit $retcode