2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2025-05-30 01:26:38 +00:00
chenglch 2da3f5140d Move sleep retry from xcat to goconserver
If cons script is waiting, goconserver could not aware of
the status of the console connection. This patch check
the environment when running the script, if it is gocons
and retrying is needed, exit immediately.
2018-02-09 12:11:48 +08:00

138 lines
4.1 KiB
Perl
Executable File

#!/usr/bin/env perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
use Fcntl qw(:DEFAULT :flock);
use Time::HiRes qw(sleep);
use File::Path;
BEGIN {
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : -d '/opt/xcat' ? '/opt/xcat' : '/usr';
}
use lib "$::XCATROOT/lib/perl";
require xCAT::Client;
require xCAT::Utils;
my $sleepint = int(rand(10)); #Stagger start to avoid overwhelming conserver/xCATd
my ($lockfd, $bmc);
my $username = 'USERID';
my $password = 'PASSW0RD';
my $node = $ARGV[0];
use constant CONSOLE_LOCK_FILE => "/tmp/xcat/consolelock";
use constant CONSOLE_LOCK_DIR => "/tmp/xcat";
sub acquire_lock {
umask 0077;
mkpath(CONSOLE_LOCK_DIR);
print "Acquiring startup lock...";
unless (sysopen($lockfd, CONSOLE_LOCK_FILE, O_WRONLY | O_CREAT)) {
xCAT::Utils::console_sleep(15, "Unable to open file ".CONSOLE_LOCK_FILE."\n");
exit 1;
}
unless (flock($lockfd, LOCK_EX)) {
close($lockfd);
xCAT::Utils::console_sleep(15, "Unable to lock file ".CONSOLE_LOCK_FILE."\n");
exit 1;
}
print "done\n";
unless (syswrite($lockfd, $$, length($$))) {
close($lockfd);
xCAT::Utils::console_sleep(15, "Unable to write file ".CONSOLE_LOCK_FILE."\n");
exit 1;
}
}
sub release_lock {
flock($lockfd, LOCK_UN);
close($lockfd);
}
sub getans {
my $rsp = shift;
if ($rsp->{node}) {
$bmc = $rsp->{node}->[0]->{bmcaddr}->[0];
$username = $rsp->{node}->[0]->{bmcuser}->[0];
$password = $rsp->{node}->[0]->{bmcpass}->[0];
if (exists $rsp->{node}->[0]->{error}) {
my $error = $rsp->{node}->[0]->{error}->[0];
print "$error\n";
}
}
}
my $cmdref = {
command => ["getipmicons"],
arg => ["text"],
noderange => [ $ARGV[0] ]
};
acquire_lock();
# avoid of congestion
sleep(0.1);
release_lock();
xCAT::Client::submit_request($cmdref, \&getans);
until (($username or $password) and $bmc) {
#Let other clients have a go
$sleepint = 10 + int(rand(20));
xCAT::Utils::console_sleep($sleepint, "Console not ready, retrying in $sleepint seconds (Ctrl-e,c,o to skip delay) \n");
acquire_lock();
sleep(0.1);
release_lock();
xCAT::Client::submit_request($cmdref, \&getans);
}
my ($user, $pass);
if ($username) {
$user = "-U '$username'";
} else {
$user = '';
}
if ($password) {
$pass = "-P '$password'";
} else {
$pass = '';
}
my $isintel = 0;
my $sleepint;
my $rc;
my $ipmitool = "ipmitool";
if (-x "$XCATROOT/bin/ipmitool-xcat") {
$ipmitool = "$XCATROOT/bin/ipmitool-xcat";
}
my @mcinfo = `$ipmitool -I lanplus $user $pass -H $bmc mc info`;
$rc = $?;
if ($rc) { #some shoddy vendors ignore the IPMI 2.0 requirement to support IPMI 1.5 formats for BMC capability determination, attempt IPMI 2.0 even without the ability to confirm IPMI 2.0 support. Though SOL was not baked in prior IPMI 2.0, this script supports pre-2.0 'ISOL' on older devices, hence why we are checking for 1.5/2.0 before proceeding normally
@mcinfo = `$ipmitool -I lan $user $pass -H $bmc mc info`;
$rc = $?;
}
while ($rc != 0) {
$sleepint = 10 + int(rand(20));
xCAT::Utils::console_sleep($sleepint, "Failure to reach IPMI device, retrying in $sleepint seconds (Hit Ctrl-E,c,o to skip)\n");
@mcinfo = `$ipmitool -I lanplus $user $pass -H $bmc mc info`;
$rc = $?;
if ($rc) { #repeat workaround for shoddy vendors
@mcinfo = `$ipmitool -I lan $user $pass -H $bmc mc info`;
$rc = $?;
}
}
my $solcom = "sol";
my $iface = "lanplus";
if (grep /IPMI Version : 1.5/, @mcinfo) {
$solcom = "isol";
$iface = "lan";
} elsif (grep /Manufacturer ID : 343/, @mcinfo && ! grep /Product ID : 117/,@mcinfo) {
if (! grep /Product ID\s*:\s*64/,@mcinfo) {
$isintel = 1;
}
}
my $inteloption = "";
if ($isintel) {
$inteloption = " -o intelplus";
}
if ($iface eq "lanplus") {
system "$ipmitool -I lanplus $inteloption $user $pass -H $bmc $solcom deactivate"; #Stop any active session
}
exec "$ipmitool -I $iface $inteloption $user $pass -H $bmc $solcom activate";