-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tonodes.t and fixed a regression in atnodes
- Loading branch information
Showing
7 changed files
with
556 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use lib 'lib'; | ||
use Net::OpenSSH; | ||
use Term::ReadKey; | ||
use SSH::Batch::ForNodes; | ||
use File::Temp qw/ :POSIX /; | ||
|
||
sub help ($); | ||
|
||
if (!@ARGV) { | ||
warn "No argument specified.\n\n"; | ||
help(1); | ||
} | ||
|
||
my $list_hosts_only = 0; | ||
my ($user, $port, $timeout, $verbose, $ask_for_pass, $recursive, $use_glob); | ||
my (@files, @exprs); | ||
my $fetch_value; | ||
my $found_sep; | ||
for (@ARGV) { | ||
if (defined $fetch_value) { | ||
$fetch_value->($_); | ||
undef $fetch_value; | ||
next; | ||
} | ||
if ($_ eq '--') { | ||
@files = @exprs; | ||
@exprs = (); | ||
$found_sep = 1; | ||
next; | ||
} | ||
if (/^-([A-Za-z])(.*)/) { | ||
if ($2 ne '') { | ||
die "Unknown option: $_\n"; | ||
} | ||
my $group = $1; | ||
if ($group eq 'l') { | ||
$list_hosts_only = 1; | ||
} elsif ($group eq 'u') { | ||
$fetch_value = sub { $user = shift }; | ||
} elsif ($group eq 't') { | ||
$fetch_value = sub { $timeout = shift }; | ||
} elsif ($group eq 'h') { | ||
help(0); | ||
} elsif ($group eq 'p') { | ||
$fetch_value = sub { $port = shift }; | ||
} elsif ($group eq 'v') { | ||
$verbose = 1; | ||
} elsif ($group eq 'w') { | ||
$ask_for_pass = 1; | ||
} elsif ($group eq 'r') { | ||
$recursive = 1; | ||
} elsif ($group eq 'g') { | ||
$use_glob = 1; | ||
} else { | ||
die "Unknown option: $_\n"; | ||
} | ||
next; | ||
} | ||
push @exprs, $_; | ||
} | ||
|
||
if (!$found_sep && !@files) { | ||
push @files, shift @exprs; | ||
} | ||
if (!@files) { | ||
die "No local files/directories specified.\n"; | ||
} | ||
|
||
for my $file (@files) { | ||
if (!-e $file) { | ||
die "Local file $file not found.\n"; | ||
} | ||
} | ||
|
||
if ($verbose) { | ||
warn "Local files: ", (map { "[$_]" } @files), "\n"; | ||
} | ||
|
||
my $expr = join ' ', @exprs; | ||
my $target_path; | ||
if ($expr =~ s/\s*:(\D\S*)\s*$//) { | ||
$target_path = $1; | ||
} else { | ||
die "No remote target path specified.\n", | ||
" (You forgot to specify \":/path/to/target\" at the end of the command line?)\n"; | ||
} | ||
|
||
if ($expr =~ /^\s*$/) { | ||
die "No cluster expression specified.\n"; | ||
} | ||
|
||
if ($verbose) { | ||
warn "Cluster expression: $expr\n"; | ||
warn "Target path: ", defined $target_path ? $target_path : '', "\n"; | ||
} | ||
|
||
my ($rc, $rcfile) = SSH::Batch::ForNodes::init(); | ||
SSH::Batch::ForNodes::load_rc($rc, $rcfile); | ||
my $set = SSH::Batch::ForNodes::parse_expr($expr); | ||
|
||
if ($set->is_empty) { | ||
die "No machine to be operated.\n"; | ||
} | ||
my @hosts = sort $set->elements; | ||
|
||
if ($verbose) { | ||
warn "Cluster set: @hosts\n"; | ||
} elsif ($list_hosts_only) { | ||
print "Cluster set: @hosts\n"; | ||
} | ||
|
||
if ($list_hosts_only) { | ||
exit(0); | ||
} | ||
|
||
my $password; | ||
if ($ask_for_pass) { | ||
print STDERR "Password:"; | ||
ReadMode(2); | ||
while (not defined ($password = ReadLine(0))) { | ||
} | ||
ReadMode(0); | ||
chomp $password; | ||
if (!$password) { | ||
die "No password specified.\n"; | ||
} | ||
} | ||
|
||
my %conns; | ||
for my $host (@hosts) { | ||
$conns{$host} = Net::OpenSSH->new( | ||
$host, | ||
async => 1, | ||
defined $timeout ? (timeout => $timeout) : (), | ||
defined $user ? (user => $user) : (), | ||
defined $port ? (port => $port) : (), | ||
defined $password ? (password => $password) : (), | ||
); | ||
} | ||
my (@pids, @outs); | ||
for my $host (@hosts) { | ||
my ($out, $outfile) = tmpnam(); | ||
push @outs, $outfile; | ||
push @pids, $conns{$host}->scp_put({ | ||
stdout_fh => $out, | ||
stderr_to_stdout => 1, | ||
async => 1, | ||
defined $recursive ? (recursive => $recursive) : (), | ||
defined $use_glob ? (glob => $use_glob) : (), | ||
# XXX recursive | ||
}, @files, $target_path); | ||
} | ||
|
||
my $i = 0; | ||
for my $pid (@pids) { | ||
my $host = $hosts[$i++]; | ||
print "===" x 10, " $host ", "===" x 10, "\n"; | ||
if (!defined $pid) { | ||
warn "Failed to connect to host $host.\n"; | ||
next; | ||
} | ||
if (waitpid($pid, 0) > 0) { | ||
my $exit = ($? >> 8); | ||
if ($exit) { | ||
warn "$host: Transfer of files failed (status code: $exit)\n"; | ||
next; | ||
} | ||
} else { | ||
#redo if ($! == EINTR); | ||
warn "$host: waitpid($pid) failed: $!\n"; | ||
next; | ||
} | ||
my $outfile = shift @outs; | ||
my $in; | ||
if (!open $in, $outfile) { | ||
warn "Can't open $outfile for reading: $!\n"; | ||
next; | ||
} | ||
while (<$in>) { | ||
print; | ||
} | ||
print "\n"; | ||
close $in; | ||
} | ||
|
||
sub help ($) { | ||
my $exit_code = shift; | ||
my $msg = <<'_EOC_'; | ||
USAGE: | ||
tonodes [OPTIONS] FILE... -- HOST_PATTERN... [OPTIONS] | ||
tonodes [OPTIONS] FILE HOST_PATTERN... [OPTIONS] | ||
OPTIONS: | ||
-g Use glob to process the input files/directories. | ||
-h Print this help. | ||
-l List the hosts and do nothing else. | ||
-p <port> Port for the remote SSH service. | ||
-r Recurse into directories too. | ||
-t <timeout> Specify timeout for net traffic. | ||
-u <user> User account for SSH login. | ||
-v Be verbose. | ||
-w Prompt for password (used mostly for login and sudo). | ||
_EOC_ | ||
if ($exit_code == 0) { | ||
print $msg; | ||
exit(0); | ||
} else { | ||
warn $msg; | ||
exit($exit_code); | ||
} | ||
} | ||
__END__ | ||
=head1 NAME | ||
tonodes - Upload local files/directories to remote clusters | ||
=head1 SYNOPSIS | ||
$ tonodes /tmp/*.inst -- '{as}:/tmp/' | ||
$ tonodes foo.txt 'ws1105*' :/tmp/ | ||
=head1 USAGE | ||
tonodes [OPTIONS] FILE... -- HOST_PATTERN... [OPTIONS] | ||
tonodes [OPTIONS] FILE HOST_PATTERN... [OPTIONS] | ||
=head1 OPTIONS | ||
-g Use glob to process the input files/directories. | ||
-h Print this help. | ||
-l List the hosts and do nothing else. | ||
-p <port> Port for the remote SSH service. | ||
-r Recurse into directories too. | ||
-t <timeout> Specify timeout for net traffic. | ||
-u <user> User account for SSH login. | ||
-v Be verbose. | ||
-w Prompt for password (used mostly for login and sudo). | ||
=head1 SEE ALSO | ||
L<fornodes>, L<atnodes>, L<SSH::Batch>, L<Net::OpenSSH>. | ||
=head1 COPYRIGHT AND LICENSE | ||
This module as well as its programs are licensed under the BSD License. | ||
Copyright (c) 2009, Yahoo! China EEEE Works, Alibaba Inc. All rights reserved. | ||
Copyright (C) 2009, Agent Zhang (agentzh). All rights reserved. | ||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
=over | ||
=item * | ||
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
=item * | ||
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
=item * | ||
Neither the name of the Yahoo! China EEEE Works, Alibaba Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. | ||
=back | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.