2008-03-18 14:37:55 +00:00
|
|
|
#!/usr/bin/perl
|
2007-10-26 22:44:33 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
2007-12-11 19:15:28 +00:00
|
|
|
BEGIN
|
|
|
|
{
|
2008-02-08 15:45:13 +00:00
|
|
|
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : -d '/opt/xcat' ? '/opt/xcat' : '/usr';
|
2007-12-11 19:15:28 +00:00
|
|
|
}
|
2012-03-29 13:30:10 +00:00
|
|
|
close(STDIN);
|
2012-04-15 13:11:55 +00:00
|
|
|
open(STDIN,"<","/dev/null");
|
2007-12-11 19:15:28 +00:00
|
|
|
use lib "$::XCATROOT/lib/perl";
|
2007-10-26 22:44:33 +00:00
|
|
|
use IO::Socket::SSL;
|
|
|
|
use XML::Simple;
|
2009-07-29 16:05:42 +00:00
|
|
|
$XML::Simple::PREFERRED_PARSER='XML::Parser';
|
2011-04-11 16:17:08 +00:00
|
|
|
#use Data::Dumper;
|
2007-10-26 22:44:33 +00:00
|
|
|
use IO::Handle;
|
|
|
|
use IO::Select;
|
|
|
|
use xCAT::Utils;
|
|
|
|
use Getopt::Long;
|
|
|
|
use POSIX qw(:signal_h :errno_h :sys_wait_h);
|
2008-03-20 14:19:12 +00:00
|
|
|
use Thread qw(yield);
|
2007-10-26 22:44:33 +00:00
|
|
|
my $interface;
|
2008-03-18 14:37:55 +00:00
|
|
|
my $username;
|
|
|
|
my $help;
|
2013-06-17 17:50:35 +00:00
|
|
|
my $timeout=0;
|
2007-10-26 22:44:33 +00:00
|
|
|
Getopt::Long::Configure("require_order");
|
2008-03-18 14:37:55 +00:00
|
|
|
Getopt::Long::Configure("no_pass_through");
|
|
|
|
if (!GetOptions(
|
|
|
|
"i|interface=s" => \$interface,
|
|
|
|
'l|loginname=s' => \$username,
|
2013-06-17 17:50:35 +00:00
|
|
|
't|timeout=s' => \$timeout,
|
2012-06-07 19:02:49 +00:00
|
|
|
'f|fanout=s' => \$fanout,
|
2008-02-08 14:59:29 +00:00
|
|
|
"nonodecheck" => \$::NONODECHECK, #does not check the noderange, in this case, noderange need to be a list of nodes.
|
2008-03-18 14:37:55 +00:00
|
|
|
'h|help' => \$help,
|
|
|
|
) || $help || scalar(@ARGV)<2 ) {
|
2012-06-07 19:02:49 +00:00
|
|
|
print "Usage: psh [-i <interface>] [-l <user>] [-f <fanout>] <noderange> <command>\n";
|
2008-03-18 14:37:55 +00:00
|
|
|
exit;
|
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
my %nodehdl;
|
|
|
|
my $xcathost='localhost:3001';
|
2012-06-08 11:47:04 +00:00
|
|
|
my $pshmaxp = 64;
|
2007-10-26 22:44:33 +00:00
|
|
|
if ($ENV{XCATHOST}) {
|
2008-02-08 14:59:29 +00:00
|
|
|
$xcathost=$ENV{XCATHOST};
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2012-06-08 11:47:04 +00:00
|
|
|
if ($ENV{XCATPSHFANOUT}) {
|
|
|
|
$pshmaxp=$ENV{XCATPSHFANOUT};
|
|
|
|
}
|
2012-06-07 19:02:49 +00:00
|
|
|
if ($fanout) { # see if they overroad the fanout from the command line
|
|
|
|
$pshmaxp=$fanout;
|
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
my $noderange = $ARGV[0];
|
2008-02-08 14:59:29 +00:00
|
|
|
my @nodes=();
|
|
|
|
|
|
|
|
if ($::NONODECHECK) {
|
|
|
|
@nodes=split(/,/, $noderange);
|
|
|
|
}
|
|
|
|
else {
|
2012-03-22 13:52:39 +00:00
|
|
|
my @user = getpwuid($>);
|
|
|
|
my $homedir=$user[7];
|
2008-02-08 14:59:29 +00:00
|
|
|
my $client = IO::Socket::SSL->new(
|
2007-10-26 22:44:33 +00:00
|
|
|
PeerAddr=>$xcathost,
|
2012-03-22 13:52:39 +00:00
|
|
|
SSL_key_file=>$homedir."/.xcat/client-cred.pem",
|
|
|
|
SSL_cert_file=>$homedir."/.xcat/client-cred.pem",
|
|
|
|
SSL_ca_file => $homedir."/.xcat/ca.pem",
|
2007-10-26 22:44:33 +00:00
|
|
|
SSL_use_cert => 1,
|
|
|
|
#SSL_verify_mode => 1,
|
|
|
|
);
|
2008-02-08 14:59:29 +00:00
|
|
|
die "Connection failure: $!\n" unless ($client);
|
|
|
|
my %cmdref = (command => 'noderange', noderange => $noderange);
|
|
|
|
$SIG{ALRM} = sub { die "No response getting noderange" };
|
|
|
|
alarm(15);
|
|
|
|
print $client XMLout(\%cmdref,RootName=>'xcatrequest', NoAttr=>1, KeyAttr => []);
|
|
|
|
alarm(15);
|
|
|
|
my $response="";
|
|
|
|
while (<$client>) {
|
|
|
|
alarm(0);
|
|
|
|
$response .= $_;
|
|
|
|
if ($response =~ m/<\/xcatresponse>/) {
|
|
|
|
$rsp=XMLin($response, ForceArray => ['node']);
|
|
|
|
$response='';
|
|
|
|
if ($rsp->{warning}) {
|
|
|
|
printf "Warning: ".$rsp->{warning}."\n";
|
|
|
|
}
|
|
|
|
if ($rsp->{error}) {
|
|
|
|
die ("ERROR: ".$rsp->{error}."\n");
|
|
|
|
} elsif ($rsp->{node}) {
|
|
|
|
@nodes=@{$rsp->{node}};
|
|
|
|
}
|
|
|
|
if ($rsp->{serverdone}) {
|
|
|
|
last;
|
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-08 14:59:29 +00:00
|
|
|
close($client);
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2008-02-08 14:59:29 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
my $children = 0;
|
|
|
|
my $inputs = new IO::Select;
|
2008-08-05 19:40:06 +00:00
|
|
|
my %pids; # pid => node
|
2008-10-10 18:48:11 +00:00
|
|
|
my %exitcodes; # Keep a list of children with known exit codes
|
|
|
|
my %foundcodes;
|
2008-08-05 19:40:06 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
if ($interface) {
|
|
|
|
foreach (@nodes) {
|
|
|
|
s/$/-$interface/;
|
|
|
|
}
|
|
|
|
}
|
2013-06-17 17:50:35 +00:00
|
|
|
local $SIG{ALRM}= sub {
|
|
|
|
my @proclist = `ps -ef`;
|
|
|
|
my %ownedpids;
|
|
|
|
foreach (@proclist) {
|
|
|
|
m/\S+\s+(\S+)\s+(\S+)/;
|
|
|
|
$ownedpids{$2}=$1; #only recall one child per parent
|
|
|
|
}
|
|
|
|
foreach my $pid (keys %pids) {
|
|
|
|
my $node = $pids{$pid};
|
|
|
|
unless (defined $exitcodes{$node}) {
|
|
|
|
print stderr "$node: timeout exceeded\n";
|
|
|
|
if ($ownedpids{$pid}) {
|
|
|
|
kill 15,$ownedpids{$pid};
|
|
|
|
} else {
|
|
|
|
kill 15,$pid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if ($timeout) { alarm($timeout); }
|
2007-10-26 22:44:33 +00:00
|
|
|
foreach (@nodes) {
|
|
|
|
my $node=$_;
|
2013-04-10 21:01:09 +00:00
|
|
|
while ($children >= $pshmaxp) { processoutput($inputs); }
|
2007-10-26 22:44:33 +00:00
|
|
|
$children++;
|
2013-06-24 19:17:16 +00:00
|
|
|
sshnode($inputs,\%nodehdl,$node,$username,@ARGV[1 .. $#ARGV]);
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2009-05-05 19:13:15 +00:00
|
|
|
while ($inputs->count) {
|
2007-10-26 22:44:33 +00:00
|
|
|
processoutput($inputs);
|
|
|
|
}
|
|
|
|
while (processoutput($inputs)) {};
|
2009-05-05 19:13:15 +00:00
|
|
|
while (wait != -1) {
|
|
|
|
yield;
|
|
|
|
}
|
2008-10-10 18:48:11 +00:00
|
|
|
my $exitcode=0;
|
|
|
|
foreach (values %pids) {
|
|
|
|
my $possible_codes = join ",",keys %foundcodes;
|
|
|
|
unless (defined $exitcodes{$_}) {
|
|
|
|
print stderr "$_: *** psh missed exit code, probably one of the following: $possible_codes\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach (keys %exitcodes) {
|
|
|
|
if ($exitcodes{$_}) {
|
|
|
|
print stderr "$_: *** ssh exited with error code ".$exitcodes{$_}.".\n";
|
|
|
|
$exitcode++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($exitcode) { #Exit code reflects number of failed nodes
|
|
|
|
$exitcode=$exitcode%256; #keep from overflowing valid values
|
|
|
|
unless ($exitcode) { #if number of failed nodes happened to be evenly divisible by 256, make it non-zero again
|
|
|
|
$exitcode++;
|
|
|
|
}
|
2008-08-05 19:40:06 +00:00
|
|
|
}
|
|
|
|
exit($exitcode);
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
sub processoutput { #This way, one arbiter handles output, no interrupting
|
|
|
|
my $inputs = shift;
|
|
|
|
my @readyins = $inputs->can_read(1);
|
|
|
|
my $rc = @readyins;
|
|
|
|
my $readyh;
|
|
|
|
foreach $readyh (@readyins) {
|
2008-08-05 19:40:06 +00:00
|
|
|
my $cursel = new IO::Select;
|
|
|
|
$cursel->add($readyh);
|
|
|
|
while ($cursel->can_read(0)) {
|
|
|
|
my $line = <$readyh>;
|
|
|
|
unless ($line) {
|
|
|
|
$inputs->remove($readyh);
|
|
|
|
close($readyh);
|
2010-05-13 18:41:08 +00:00
|
|
|
$exitcodes{$nodehdl{$readyh}} = $? >> 8;
|
|
|
|
$children--;
|
2008-08-05 19:40:06 +00:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
chomp($line);
|
|
|
|
print $nodehdl{$readyh}.": ".$line."\n";
|
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2008-03-20 14:19:12 +00:00
|
|
|
IO::Handle::flush(stdout);
|
|
|
|
yield; #Explicitly give all children a chance to refill any buffers
|
2007-10-26 22:44:33 +00:00
|
|
|
return $rc;
|
|
|
|
}
|
|
|
|
sub sshnode {
|
2013-06-24 19:17:16 +00:00
|
|
|
my $inputs = shift;
|
|
|
|
my $nodehdl = shift;
|
2007-10-26 22:44:33 +00:00
|
|
|
my $node = shift;
|
2008-03-18 14:37:55 +00:00
|
|
|
my $username = shift;
|
2013-06-24 19:17:16 +00:00
|
|
|
my $out;
|
2008-03-18 14:37:55 +00:00
|
|
|
if (length($username)) { $username = "-l $username"; }
|
2007-10-26 22:44:33 +00:00
|
|
|
my $in;
|
|
|
|
my $args = join(" ",@_);
|
2008-03-18 14:37:55 +00:00
|
|
|
#print "ssh -o BatchMode=yes $username $node " . xCAT::Utils->quote($args) . " 2>&1 |\n";
|
2013-06-24 19:17:16 +00:00
|
|
|
my $pid = open($out,"ssh -o BatchMode=yes $username $node " . xCAT::Utils->quote($args) . " 2>&1 |");
|
|
|
|
$inputs->add($out);
|
|
|
|
$nodehdl->{$out} = $node;
|
2008-08-05 19:40:06 +00:00
|
|
|
$pids{$pid} = $node;
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2008-02-08 14:59:29 +00:00
|
|
|
|
2008-08-05 19:40:06 +00:00
|
|
|
# vim: set et ts=2 sts=2 sw=2 :
|