-
Notifications
You must be signed in to change notification settings - Fork 5
/
xssh
executable file
·390 lines (333 loc) · 9 KB
/
xssh
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
#!/usr/bin/perl
######### xssh
# launches a program on any number of nodes
# can be the same on all, or variations based on expansion
#########
# grun - lightweight jobs queueing system
# Copyright (C) 2011 Erik Aronesty
#
# 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/>.
use strict;
use Getopt::Long qw(:config pass_through);
$Getopt::Long::order = $REQUIRE_ORDER;
use IO::File;
use Cwd qw(cwd);
use File::Spec;
use Data::Dumper;
sub usage;
our %opt;
my $pwd = cwd();
die unless $pwd;
my $defnodes;
my %filerules;
my @mnt;
readconf();
GetOptions(\%opt, "nodes=s@", "echo", "plain", "seq", "sudo", "debug", "recursive","force","list") || die usage();
push @{$opt{nodes}}, $defnodes if (!defined($opt{nodes}));
$opt{mode} = $0 =~ /diff/ ? 'diff' : $0 =~ /sync/ ? 'sync' : 'ssh';
die usage() unless @ARGV || $opt{list};
my $spid;
# this kills kids when you press control-C
sub on_int {
kill 2, $spid if $spid;
}
$opt{nodes} = join ',', @{$opt{nodes}};
# expand node names
my @nodes=expandnodes($opt{nodes});
# uniquify
my %seen;
foreach my $node (@nodes) {chomp; $seen{$node}=1 if $node; }
@nodes=keys(%seen);
if ($opt{list}) {
for (@nodes) {
print $_, "\n";
}
exit 0;
}
print STDERR "Nodes: " . join(' ', @nodes). "\n\n";
my $windows= $^O =~ /^(ms)?win/i;
my $host = $windows ? $ENV{COMPUTERNAME} : $ENV{HOSTNAME};
# on windows, plink is the ssh equivalent TODO: config this
my $ssh = $windows?'plink':'ssh';
my $sshopts = '-o ForwardX11=no -o ForwardX11Trusted=no -o ConnectTimeout=30 -o SendEnv="PATH LANG" -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no';
# launch kids on the nodes
if ($opt{mode} eq 'diff') {
for (@ARGV) {
my $f = File::Spec->rel2abs($_);
if (! -s $f) {
warn "file not found: $f";
next;
}
if (-d $f) {
die "Won't diff $f recursively";
}
for (@mnt) {
if ($f =~ /^\Q$_\E/) {
die "$_ is mounted now, so no need to $opt{mode}"
}
}
my $fb = $f;
$fb =~ s/.*\///;
for my $node (@nodes) {
next if $ENV{HOSTNAME} =~ /^$node\b/;
my $opt;
if (!$opt && $opt{sudo}) {
$opt="--rsync-path='sudo rsync' ";
}
$opt = "--recursive $opt" if $opt{recursive};
my $tmp = "/tmp/xdiff:$node:$fb";
fexec($ENV{HOSTNAME}, "rsync -e 'ssh $sshopts' -lpgtoD $opt $node:$f $tmp");
}
while (wait != -1) {};
for my $node (@nodes) {
next if $ENV{HOSTNAME} =~ /^$node/;
my $tmp = "/tmp/xdiff:$node:$fb";
my $pre = "sudo " if $opt{sudo};
system("${pre}diff -u $tmp $f");
unlink $tmp;
}
}
}
if ($opt{mode} eq 'sync') {
for (@ARGV) {
my $f = File::Spec->rel2abs($_);
if (! -s $f) {
warn "file not found: $f";
next;
}
for (@mnt) {
if ($f =~ /^\Q$_\E/) {
die "$_ is mounted now, so no need to $opt{mode}\n" unless $opt{force}
}
}
my %nosync;
if ($filerules{$f}) {
for (@{$filerules{$f}}) {
my ($rule, $nodes) = @$_;
if ($rule eq 'nosync') {
if ($nodes eq '*') {
die "Won't sync $f because of rule in config";
} else {
for (expandnodes($nodes)) {
$nosync{$_} = 1;
}
}
}
}
}
if (-d $f && !$opt{recursive}) {
die "Won't sync $f recursively unless you specify -r or --recursive\n";
}
for my $node (@nodes) {
my $opt;
next if $ENV{HOSTNAME} =~ /^$node\./;
if ($nosync{$node}) {
print "Skipping $node:$f because of config.\n";
next;
}
if (!$opt && $opt{sudo}) {
$opt="--rsync-path='sudo rsync' ";
}
$opt = "--recursive $opt" if $opt{recursive};
my $d = $f;
$f = $f . '/' if (-d $f);
fexec($ENV{HOSTNAME}, "rsync -e 'ssh $sshopts' -lpgtoD $opt$f $node:$d");
}
}
}
if ($opt{mode} eq 'ssh') {
my @cmd=@ARGV;
for my $node (@nodes) {
if ($opt{seq}) {
nexec($node, @cmd);
} else {
fexec($node, @cmd);
}
}
}
# wait for all kids
while (wait != -1) {};
sub nexec {
my ($node, @cmd) = @_;
my $cmd;
my $norig = $node;
grep s/(["()'])/\\$1/g, @cmd if ($opt{mode} eq 'ssh');
$cmd = join " ", @cmd;
$cmd= "cd ${pwd} && " . $cmd if ($opt{mode} eq 'ssh');
print "in nexec $host ENVH $ENV{HOSTNAME}\n" if $opt{debug};
if (!($ENV{HOSTNAME} =~ /^$node/)) { # not on my own machine
print "$host is host\n" if $opt{debug};
# if you're on windows... specify the username
my $prefix = "$ENV{USERNAME}\@" if $windows;
# quote the command
$cmd =~ s/(["\$])/\\$1/g;
$cmd= "$ssh $sshopts $prefix$node \"$cmd\"";
print "mod cmd is $cmd\n" if $opt{debug};
}
print $cmd, "\n" if $opt{echo};
if ($opt{plain}) {
system($cmd);
} else {
# capture stderr and stdout
$spid=open(my $io=new IO::File, "( $cmd ) 2>&1 |");
if (!$spid) {
# failed to exec
die "$norig\t$!\n";
}
# show output, with nodes prefixed
while(<$io>) {
next if $_ =~ /^Warning: Permanently added.*to the list of known hosts\./;
print "$norig\t$_" if $_;
}
close $io;
}
wait; # don't exit until child is really done... not just done closing their pipe
}
# show usage
sub usage {
if ($opt{mode} eq 'ssh') {
return <<EOF
usage: xssh [-n \"n1 n2 ...\"] [-a] command\n
-n[odes]: node list
-a[ll] : run on all available machines
-e[cho] : echo on
-seq : run each node sequentially
Node Names:
You can specify nodes as single digits, whole hostnames, or
other short names like 'f0', or 'pipeline'.
Rewriting occurs accoring to the rules in the xssh.conf.
Config example:
rewrite: (\\d) to: my-node-$1
nodes: 0 1 2 3 4 my-head-node
EOF
}
if ($opt{mode} eq 'sync') {
return <<EOF
usage: xsync [-n \"n1 n2 ...\"] [-a] file1 [... fileN]\n
-n[odes]: node list
-a[ll] : run on all available machines
-e[cho] : echo on
-seq : run each node sequentially
-list : list all nodes
Node Names:
You can specify nodes as single digits, whole hostnames, or
other short names like 'f0', or 'pipeline'.
Config file example:
rewrite: (\d) to: my-node-$1
nodes: 0 1 2 3 4 my-head-node
EOF
}
}
sub fexec {
my ($node, @cmd) = @_;
my $pid = fork;
if (not defined $pid) {
die "can't fork"
} elsif ($pid == 0) {
$SIG{INT}=on_int if !$windows;
print "b4 nexec $node @cmd\n" if $opt{debug};
nexec($node, @cmd);
exit 0;
} else {
# parent
}
}
my @rewrite;
my %groups;
sub readconf {
my @dn;
my ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir) = getpwuid($<);
my $ok = open(IN, "$dir/.xssh.conf") if $dir;
$ok = open(IN, "/etc/xssh.conf") unless $ok;
die "Need /etc/xssh.conf or ~/.xssh.conf\n" unless $ok;
# list mounts
my @tmp = `mount`;
for (@tmp) {
if (m/:\S+ on (\S+) type nfs/) {
push @mnt, $1;
}
}
while (<IN>) {
next if /^#/;
next if /^\s*$/;
my ($cmd, $suf, $dat) = m|^(\w+)(?:-([^: ]+))?\s*:\s*(.*)|;
if ($cmd eq 'rewrite') {
my ($from,$to) = $dat =~ /^\s*(.*?)\s+(?:to:)?\s*(.*?)\s*$/;
push @rewrite, [$from, '"' . $to . '"'];
} elsif ($cmd eq 'nodes' || $cmd eq 'node') {
if (!$suf) {
$defnodes .= "$dat ";
} else {
my @n=expandnodes($dat);
push @{$groups{$suf}}, @n;
}
} elsif ($cmd eq 'file') {
my ($path,$rule,$value,$rule2,$value2) = $dat =~ /^\s*(.*?)\s+(?:(\w+):\s*(.*?)\s*)+$/;
push @{$filerules{$path}}, [$rule, $value];
push @{$filerules{$path}}, [$rule2, $value2] if $rule2;
}
}
close IN;
$defnodes =~ s/\s+$//;
}
sub expandnodes {
my $dat = shift;
my @n=split(/[\s,]+/, $dat);
my @d;
my @a;
my @x;
for my $n (@n) {
my $not = 1 if $n =~ s/^\-//;
next unless length($n)>0;
if ($n eq '+all') {
@a=expandnodes($defnodes);
} elsif ($groups{$n}) {
@a=@{$groups{$n}};
} else {
$n = rewrite($n);
@a=($n);
}
if ($not) {
push @x, @a;
} else {
push @d, @a;
}
}
for my $x (@x) {
@d = grep !/^\Q$x\E$/, @d;
}
if (!$opt{seq}) {
fisher_yates_shuffle(\@d) if @d;
}
return @d;
}
sub rewrite {
my $n = shift;
for my $rw (@rewrite) {
my ($from, $to) = @$rw;
$n =~ s/^$from$/$to/ee;
die "bad rewrite: $from to: $to" if $n =~ /\$/;
}
return $n;
}
sub fisher_yates_shuffle
{
my $array = shift;
my $i = @$array;
while ( --$i )
{
my $j = int rand( $i+1 );
@$array[$i,$j] = @$array[$j,$i];
}
}