Skip to content

Commit

Permalink
added tonodes.t and fixed a regression in atnodes
Browse files Browse the repository at this point in the history
  • Loading branch information
agentzh committed Apr 21, 2009
1 parent 805b40c commit 7534b45
Show file tree
Hide file tree
Showing 7 changed files with 556 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ no_index( directory => qw< t inc doc share demo > );

install_script ('bin/fornodes');
install_script ('bin/atnodes');
install_script ('bin/tonodes');

auto_install();
WriteAll();
Expand Down
37 changes: 35 additions & 2 deletions bin/atnodes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use strict;
use warnings;

use Smart::Comments::JSON '##';
use lib 'lib';
use Net::OpenSSH;
use Term::ReadKey;
Expand Down Expand Up @@ -137,21 +138,34 @@ for my $host (@hosts) {
}, @cmd);
}

## HERE...
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;
}
waitpid($pid, 0);
if (waitpid($pid, 0) > 0) {
my $exit = ($? >> 8);
if ($exit) {
warn "$host: Executing commands 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;
}
print "===" x 10, " $host ", "===" x 10, "\n";
while (<$in>) {
print;
}
Expand Down Expand Up @@ -213,6 +227,25 @@ atnodes - Run command on clusters
# or specify a timeout:
$ atnodes 'ping foo.com' '{ps}' -t 3
=head1 USAGE
atnodes [OPTIONS] COMMAND... -- HOST_PATTERN... [OPTIONS]
atnodes [OPTIONS] COMMAND HOST_PATTERN... [OPTIONS]
=head1 OPTIONS
-h Print this help.
-l List the hosts and do nothing else.
-p <port> Port for the remote SSH service.
-t <timeout> Specify timeout for net traffic.
-u <user> User account for SSH login.
-v Be verbose.
-w Prompt for password (used for login and sudo).
=head1 SEE ALSO
L<fornodes>, L<tonodes>, L<SSH::Batch>, L<Net::OpenSSH>.
=head1 COPYRIGHT AND LICENSE
This module as well as its programs are licensed under the BSD License.
Expand Down
277 changes: 277 additions & 0 deletions bin/tonodes
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.
12 changes: 11 additions & 1 deletion lib/SSH/Batch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,19 @@ Run command on clusters. (atnodes calls fornodes internally.)
=item tonodes (TODO)
Transfer local files or directories to clusters.
Upload local files/directories to remote clusters
$ tonodes /tmp/*.inst -- '{as}:/tmp/'
$ tonodes /tmp/*.inst -- 'ws1105*:/tmp/'
=back
=head1 DESCRIPTION
This is a high-level abstraction over the powerful L<Net::OpenSSH> module. A bunch of handy scripts are provided to simplify big cluster operations: L<fornodes>, L<atnodes>, and L<tonodes>.
Parallel SSH communication is used to ensure minimal latency.
=head1 INSTALLATION
perl Makefile.PL
Expand All @@ -83,6 +89,10 @@ public Git repository:
If you have a branch for me to pull, please let me know ;)
=head1 SEE ALSO
L<fornodes>, L<atnodes>, L<tonodes>, L<SSH::Batch::ForNodes>, L<Net::OpenSSH>.
=head1 COPYRIGHT AND LICENSE
This module as well as its programs are licensed under the BSD License.
Expand Down
Loading

0 comments on commit 7534b45

Please sign in to comment.