mirror of
				https://github.com/xcat2/xcat-core.git
				synced 2025-11-03 21:02:34 +00:00 
			
		
		
		
	Support ssh based console in rcons for openbmc
This commit is contained in:
		@@ -1,11 +1,21 @@
 | 
			
		||||
#!/usr/bin/perl
 | 
			
		||||
## IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
 | 
			
		||||
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
 | 
			
		||||
#-------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
=head1
 | 
			
		||||
  xCAT plugin package to handle openbmc 
 | 
			
		||||
 | 
			
		||||
   Supported command:
 | 
			
		||||
       getopenbmccons
 | 
			
		||||
 | 
			
		||||
=cut
 | 
			
		||||
 | 
			
		||||
#-------------------------------------------------------
 | 
			
		||||
package xCAT_plugin::openbmc;
 | 
			
		||||
 | 
			
		||||
BEGIN
 | 
			
		||||
{
 | 
			
		||||
    $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
 | 
			
		||||
    $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : -d '/opt/xcat' ? '/opt/xcat' : '/usr';
 | 
			
		||||
}
 | 
			
		||||
use lib "$::XCATROOT/lib/perl";
 | 
			
		||||
use strict;
 | 
			
		||||
@@ -24,6 +34,10 @@ use JSON;
 | 
			
		||||
 | 
			
		||||
$::OPENBMC_DEVEL = $ENV{'OPENBMC_DEVEL'};
 | 
			
		||||
 | 
			
		||||
my $VERBOSE = 0;
 | 
			
		||||
my %allerrornodes = ();
 | 
			
		||||
my $callback;
 | 
			
		||||
 | 
			
		||||
#-------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
=head3  handled_commands
 | 
			
		||||
@@ -37,6 +51,7 @@ sub handled_commands {
 | 
			
		||||
    return {
 | 
			
		||||
        rpower => 'nodehm:mgt',
 | 
			
		||||
        rinv   => 'nodehm:mgt',
 | 
			
		||||
        getopenbmccons => 'nodehm:cons',
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -233,6 +248,33 @@ sub process_request {
 | 
			
		||||
        }
 | 
			
		||||
    } 
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    #Assume the openbmc user/password is defined in the passwd
 | 
			
		||||
    my $passtab = xCAT::Table->new('passwd');
 | 
			
		||||
    if ($passtab) {
 | 
			
		||||
       ($dba) = $passtab->getAttribs({key=>'openbmc'},qw(username password));
 | 
			
		||||
        if ($dba) {
 | 
			
		||||
            if ($dba->{username}) { $username = $dba->{username}; }
 | 
			
		||||
            if ($dba->{password}) { $password = $dba->{password}; }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    foreach (@$noderange) {
 | 
			
		||||
        my $node     = $_;
 | 
			
		||||
        my $nodeip = $node;
 | 
			
		||||
        push @donargs, [ $node,$nodeip,$username, $password];
 | 
			
		||||
        my $output = "openbmc, get $username and $password from passwd table for $nodeip";
 | 
			
		||||
        xCAT::SvrUtils::sendmsg($output, $callback, $node, %allerrornodes);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    if ($request->{command}->[0] eq "getopenbmccons") {
 | 
			
		||||
        foreach (@donargs) {
 | 
			
		||||
            getopenbmccons($_, $callback);
 | 
			
		||||
        }
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    return;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -625,4 +667,23 @@ sub rinv_response {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
sub getopenbmccons {
 | 
			
		||||
    my $argr = shift;
 | 
			
		||||
 | 
			
		||||
    #$argr is [$node,$nodeuser,$nodepass];
 | 
			
		||||
    my $callback = shift;
 | 
			
		||||
 | 
			
		||||
    my $rsp;
 | 
			
		||||
    my $node=$argr->[0];
 | 
			
		||||
    my $output = "openbmc, getopenbmccoms";
 | 
			
		||||
    xCAT::SvrUtils::sendmsg($output, $callback, $argr->[0], %allerrornodes);
 | 
			
		||||
 | 
			
		||||
    $rsp = { node => [ { name => [ $argr->[0] ] } ] };
 | 
			
		||||
    $rsp->{node}->[0]->{nodeip}->[0]    = $argr->[1];
 | 
			
		||||
    $rsp->{node}->[0]->{username}->[0]    = $argr->[2];
 | 
			
		||||
    $rsp->{node}->[0]->{passwd}->[0]  = $argr->[3];
 | 
			
		||||
    $callback->($rsp);
 | 
			
		||||
    return $rsp;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
1;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										100
									
								
								xCAT-server/share/xcat/cons/openbmc
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										100
									
								
								xCAT-server/share/xcat/cons/openbmc
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,100 @@
 | 
			
		||||
#!/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';
 | 
			
		||||
}
 | 
			
		||||
my $sleepint = int(rand(10)); #Stagger start to avoid overwhelming conserver/xCATd
 | 
			
		||||
my ($lockfd, $nodeip);
 | 
			
		||||
my $username = 'UESRID';
 | 
			
		||||
my $password = 'PASSWD';
 | 
			
		||||
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)) {
 | 
			
		||||
        print "Unable to open file ".CONSOLE_LOCK_FILE."\n";
 | 
			
		||||
        sleep(15);
 | 
			
		||||
        exit 1;
 | 
			
		||||
    }
 | 
			
		||||
    unless (flock($lockfd, LOCK_EX)) {
 | 
			
		||||
        print "Unable to lock file ".CONSOLE_LOCK_FILE."\n";
 | 
			
		||||
        close($lockfd);
 | 
			
		||||
        sleep(15);
 | 
			
		||||
        exit 1;
 | 
			
		||||
    }
 | 
			
		||||
    print "done\n";
 | 
			
		||||
    unless (syswrite($lockfd, $$, length($$))) {
 | 
			
		||||
        print "Unable to write file ".CONSOLE_LOCK_FILE."\n";
 | 
			
		||||
        close($lockfd);
 | 
			
		||||
        sleep(15);
 | 
			
		||||
        exit 1;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sub release_lock {
 | 
			
		||||
    flock($lockfd, LOCK_UN);
 | 
			
		||||
    close($lockfd);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
use lib "$::XCATROOT/lib/perl";
 | 
			
		||||
require xCAT::Client;
 | 
			
		||||
 | 
			
		||||
sub getans {
 | 
			
		||||
    my $rsp = shift;
 | 
			
		||||
    if ($rsp->{node}) {
 | 
			
		||||
        $nodeip = $rsp->{node}->[0]->{nodeip}->[0];
 | 
			
		||||
        $username = $rsp->{node}->[0]->{username}->[0];
 | 
			
		||||
        $password = $rsp->{node}->[0]->{passwd}->[0];
 | 
			
		||||
        if (exists $rsp->{node}->[0]->{error}) {
 | 
			
		||||
            my $error = $rsp->{node}->[0]->{error}->[0];
 | 
			
		||||
            print "$error\n";
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
my $cmdref = {
 | 
			
		||||
    command   => ["getopenbmccons"],
 | 
			
		||||
    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 $nodeip ) {
 | 
			
		||||
    #Let other clients have a go
 | 
			
		||||
    $sleepint = 10 + int(rand(20));
 | 
			
		||||
    print "Console not ready, retrying in $sleepint seconds (Ctrl-e,c,o to skip delay) \n";
 | 
			
		||||
    sleep ($sleepint);
 | 
			
		||||
    acquire_lock();
 | 
			
		||||
    sleep(0.1);
 | 
			
		||||
    release_lock();
 | 
			
		||||
    xCAT::Client::submit_request($cmdref, \&getans);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
my $isintel = 0;
 | 
			
		||||
my $sleepint;
 | 
			
		||||
my $rc;
 | 
			
		||||
 
 | 
			
		||||
my $sshport = 2200;
 | 
			
		||||
if ($ENV{SSHCONSOLEPORT}) {
 | 
			
		||||
    $sshport= $ENV{SSHCONSOLEPORT};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#sshpass command needs to install before can run that
 | 
			
		||||
if (-x '/usr/bin/sshpass') {
 | 
			
		||||
    exec "/usr/bin/sshpass -p $password ssh -p $sshport -l $username $nodeip";
 | 
			
		||||
} else {
 | 
			
		||||
    print "Please install sshpass first. \n";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user