-
Notifications
You must be signed in to change notification settings - Fork 13
/
rdo
executable file
·107 lines (95 loc) · 2.28 KB
/
rdo
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
#!/usr/bin/env bash
# rdo -- run a command across all hosts
. lib.bash || exit
usage() {
echo "Usage: $progname [-H hosts] [-C path|-N|-P] [-SWq] <command>"
echo
echo "Run a command simultaneously on all hosts."
echo
echo_opt "-H hosts" "list of hosts to connect to"
echo_opt "-S" "connect as superuser (root)"
echo
echo_opt "-C path" "set working directory"
echo_opt "-N" "use local working directory via NFS"
echo_opt "-P" "don't load profile (non-Unix OS)"
echo
echo_opt "-W" "output in order (disables parallel output)"
echo_opt "-q" "don't show hostnames or exit values"
# XXX: We could still invoke the commands in parallel and only show
# output in order, after all have returned. This might increase the
# perceived delay, though, so it should be -WW or something.
}
wd=
nowd=0
nfs=0
hosts=
user=
wait=0
quiet=0
while getopts ":C:H:NPSWq" OPT; do
case $OPT in
C) wd=$OPTARG;;
H) hosts=${OPTARG//,/ };;
N) nfs=1;;
P) nowd=1;;
S) user="root@";;
W) wait=1;;
q) quiet=1;;
*) lib:die_getopts;;
esac
done; shift $((OPTIND-1))
hosts=$(rlisthosts "$hosts") || exit
if (( ! $# )); then
die "command not specified"
elif (( $# == 1 )); then
cmd=$1
else
cmd=${*@Q}
fi
if (( nfs )); then
if [[ ! -e /proc/fs/nfsd/versions ]]; then
die "NFS service not running on $HOSTNAME"
fi
wd=${wd:-"."}
fi
if (( nowd )); then
if [[ $wd ]]; then
# Not really useful but
die "TODO: implement setting wd without invoking shell"
fi
else
if [[ $wd ]]; then
# Allow "-C ." to be specified in a useful way (i.e. translate the
# local working directory into its remote or NFS equivalent)
if [[ $wd != /* ]]; then
wd=$(realpath -s "$wd")
fi
if (( nfs )); then
wd="$wd/"
wd="/net/$HOSTNAME/${wd#/net/*/}"
wd="${wd%/}"
fi
fi
wd=${wd:-"."}
cmd="(cd ${wd@Q} && \$SHELL -l -c ${cmd@Q}) 2>&1"
fi
lck=$(mktemp /tmp/rdo.XXXXXXXX) || exit
trap 'wait; rm -f "$lck"' INT
for host in $hosts; do
{
out=$(mktemp /tmp/rdo.XXXXXXXX) || exit
if (( quiet )); then
ssh -n $user$host "$cmd" > "$out"
flock "$lck" cat "$out"
else
(ssh -n $user$host "$cmd" || echo "=> $?") > "$out"
flock "$lck" awk -v host=$host \
'BEGIN {printf "%s:", host}
{print "\t" $0}
END {if (!NR) print}' < "$out"
fi
rm -f "$out"
} &
if (( wait )); then wait; fi
done
wait; rm -f "$lck"