2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2025-06-18 12:20:40 +00:00

Restructure code flow for switch-based switch discovery

This commit is contained in:
Casandra Qiu
2016-07-29 17:25:45 -04:00
parent efec9f073c
commit 843525e73b
3 changed files with 483 additions and 374 deletions

View File

@ -163,7 +163,7 @@ sub parse_args {
#############################################
# Check for node range
#############################################
if ( scalar(@ARGV) eq 1 ) {
if ( scalar(@ARGV) == 1 ) {
my @nodes = xCAT::NodeRange::noderange( @ARGV );
if (nodesmissed) {
return (usage( "The following nodes are not defined in xCAT DB:\n " . join(',', nodesmissed)));
@ -456,7 +456,7 @@ sub process_request {
if (exists($result->{$key}->{vendor})) {
$vendor = $result->{$key}->{vendor};
}
if ($key != /nomac/) {
if ($key !~ /nomac/) {
$mac = $key;
}
my $msg = sprintf $format, $ip, $name, $vendor, $key;
@ -472,11 +472,6 @@ sub process_request {
}
if (exists($globalopt{setup})) {
#discovered switches need to write to xcatdb before we can set them up
# It will be removed once we found predefine switch.
if (!exists($globalopt{w})) {
xCATdB($result, \%request, $sub_req);
}
switchsetup($result, \%request, $sub_req);
}
@ -1376,6 +1371,8 @@ sub switchsetup {
my $static_ip;
my $discover_switch;
my @rmnodes;
my @BNTswitches;
my @MLNXswitches;
#print Dumper($outhash);
my $macmap = xCAT::MacMap->new();
@ -1392,14 +1389,9 @@ sub switchsetup {
# issue makehosts so we can use xdsh
my $dswitch = get_hostname($outhash->{$mac}->{name}, $ip);
#add discovered switch to /etc/hosts
$output = xCAT::Utils->runxcmd({command => ["makehosts"],
node =>[$dswitch]}, $sub_req, 0, 1);
my $node = $macmap->find_mac($mac,0);
if (!$node) {
send_msg($request, 0, "NO predefined switch matched this switch $dswitch with mac address $mac");
$ret = xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$dswitch,"ip=$ip",'status=no predefined switch'] }, $sub_req, 0, 1);
send_msg($request, 0, "NO predefined switch matched this switch $dswitch with ip address $ip and mac address $mac");
next;
}
@ -1416,209 +1408,35 @@ sub switchsetup {
# BNT switches
if ( $stype =~ /BNT/ ) {
$ret = config_BNT($dswitch, $node, $static_ip, $request, $sub_req);
if ($ret == 1) {
xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$node,"ip=$static_ip","otherinterfaces=dhcp:$ip",'status=config failed'] }, $sub_req, 0, 1);
}
push (@BNTswitches, $node);
xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$node,"ip=$static_ip","otherinterfaces=$ip",'status=Matched',"mac=$mac",'switchtype=BNT','username=root','password=admin'] }, $sub_req, 0, 1);
}
# Mellanox switches
elsif ( $stype =~ /Mellanox/ ) {
#set static ip address the mgmt0, we don't have EthSwitch yet, will use IBSwitch for now
#NOTE: this expect routine will timeout after set up address
$ret = config_Mellanox($dswitch, $node, $static_ip, $request, $sub_req);
if ($ret == 1) {
xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$node,"ip=$static_ip","otherinterfaces=dhcp:$ip",'status=config failed'] }, $sub_req, 0, 1);
}
push (@MLNXswitches, $node);
xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$node,"ip=$static_ip","otherinterfaces=$ip",'status=Matched',"mac=$mac",'switchtype=Mellanox','username=admin'] }, $sub_req, 0, 1);
}
else {
send_msg($request, 0, "the switch $dswitch type $stype is not support\n");
$ret = xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$node,"ip=$static_ip","switchtype=$stype","otherinterfaces=dhcp:$ip",'status=switch type is not supported yet'] }, $sub_req, 0, 1);
xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$node,"ip=$static_ip","switchtype=$stype","otherinterfaces=$ip",'status=not supported',"mac=$mac"] }, $sub_req, 0, 1);
}
# save for update mac address and remove node definition
$discovered_switch->{$node}->{mac}=$mac;
push (@rmnodes, $dswitch);
}
#after switch ip address is setup to static ip
#remove discover switch definition
$output = xCAT::Utils->runxcmd({command => ["makehosts"],
node => \@rmnodes,
arg => ['-d'] }, $sub_req, 0, 1);
my $remove_nodes = join(",", @rmnodes);
my $ccmd = "rmdef $remove_nodes";
$output = xCAT::Utils->runcmd($ccmd, 0);
#send_msg($request, 0, Dumper($output));
#update mac address
my $mactab = xCAT::Table->new('mac');
if ($mactab) {
$mactab->setNodesAttribs($discovered_switch);
if (@BNTswitches) {
my $bntsw = join(",",@BNTswitches);
send_msg($request, 0, "call to config BNT switches $bntsw\n");
my $out = `/opt/xcat/share/xcat/tools/configBNT --range $bntsw --all`;
send_msg($request, 0, "output = $out\n");
}
if (@MLNXswitches) {
my $mlnxsw = join(",",@MLNXswitches);
send_msg($request, 0, "call to config Mellanox switches $mlnxsw\n");
my $out = `/opt/xcat/share/xcat/tools/configMellanox --range $mlnxsw --all`;
send_msg($request, 0, "output = $out\n");
}
return;
}
#--------------------------------------------------------------------------------
=head3 config_BNT
config BNT switches
---setup static ip address
---setup hostname
---set default user/password
Arguments:
dswitch: discovered switch
node: predefine switch
ip: static ip address
Returns:
result:
=cut
#--------------------------------------------------------------------------------
sub config_BNT {
my $dswitch=shift;
my $node = shift;
my $static_ip = shift;
my $request = shift;
my $sub_req = shift;
my $dshcmd;
my $ret;
#xdsh switch-192-168-5-152 --devicetype EthSwitch::BNT
# "enable;configure terminal;show interface ip;interface ip 1;ip address 192.168.5.22"
$dshcmd="enable;configure terminal;show interface ip;interface ip 1;ip address $static_ip;exit;exit";
$output = xCAT::Utils->runxcmd({command => ["xdsh"],
node =>[$dswitch],
arg => ["--devicetype", "EthSwitch::BNT", "$dshcmd"] }, $sub_req, 0, 1);
#send_msg($request, 0, Dumper($output));
#add default attribute for BNT switch
my $ret = xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$node,"ip=$static_ip",'username=root','password=admin','protocol=telnet','switchtype=BNT','otherinterfaces=','status='] }, $sub_req, 0, 1);
#send_msg($request, 0, Dumper($ret));
#set up hostname
$dshcmd="enable;configure terminal;hostname $node;write memory";
$output = xCAT::Utils->runxcmd({command => ["xdsh"],
node =>[$node],
arg => ["--devicetype", "EthSwitch::BNT", "$dshcmd"] }, $sub_req, 0, 1);
if ($::RUNCMD_RC != 0)
{
send_msg($request, 0, "Failed to change hostname on $node");
return 1;
}
return 0;
}
#--------------------------------------------------------------------------------
=head3 config_Mellanox
config Mellanox switches
---setup static ip address
---setup hostname
---set default user/password
Arguments:
dswitch: discovered switch
node: predefine switch
ip: static ip address
Returns:
result:
=cut
#--------------------------------------------------------------------------------
sub config_Mellanox {
my $dswitch=shift;
my $node = shift;
my $static_ip = shift;
my $request = shift;
my $sub_req = shift;
my $dshcmd;
my $mask;
# get netmask from network table
my $nettab = xCAT::Table->new("networks");
if ($nettab) {
my @nets = $nettab->getAllAttribs('net','mask');
foreach my $net (@nets) {
if (xCAT::NetworkUtils::isInSameSubnet( $net->{'net'}, $ip, $net->{'mask'}, 0)) {
$mask=$net->{'mask'};
}
}
}
#set static ip address the mgmt0, we don't have EthSwitch yet, will use IBSwitch for now
#NOTE: this expect routine will timeout after set up address
my $myexp = new Expect;
my $timeout = 10;
my $login_cmd = "ssh -ladmin $dswitch\r";
my $first_prompt = "^.*>";
my $config_prompt="^.*#";
my $cfg_ip="no interface mgmt0 dhcp\r";
my $cfg_ip1="interface mgmt0 ip address $static_ip $mask\r";
unless ($myexp->spawn($login_cmd))
{
$myexp->soft_close();
send_msg($request, 0, "Unable to run $login_cmd\n");
next;
}
my @result = $myexp->expect(
$timeout,
[
"-re", $first_prompt,
sub {
$myexp->clear_accum();
$myexp->send("enable\r");
$myexp->clear_accum();
$myexp->send("configure terminal\r");
$myexp->clear_accum();
$myexp->exp_continue();
}
],
[
"-re", $config_prompt,
sub {
$myexp->clear_accum();
$myexp->send($cfg_ip);
$myexp->send($cfg_ip1);
$myexp->clear_accum();
$myexp->send("configuration write\r");
$myexp->send("exit\r");
$myexp->send("exit\r");
}
],
);
if (defined($result[1]))
{
my $errmsg = $result[1];
$myexp->soft_close();
send_msg($request,0,"Failed expect command $errmsg\n");
}
$myexp->soft_close();
#add default attribute for Mellanox switch
$output = xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$node,"ip=$static_ip",'username=admin','switchtype=Mellanox','otherinterfaces=','status='] }, $sub_req, 0, 1);
#send_msg($request, 0, Dumper($output));
#set up hostname
$dshcmd="enable;configure terminal;hostname $node;configuration write";
$output = xCAT::Utils->runxcmd({command => ["xdsh"],
node =>[$node],
arg => ["-l", "admin", "--devicetype", "IBSwitch::Mellanox", "$dshcmd"] }, $sub_req, 0, 1);
if ($::RUNCMD_RC != 0)
{
send_msg($request, 0, "Failed to change hostname on $node");
return 1;
}
return 0;
}
1;

View File

@ -4,9 +4,25 @@
# Configure Ethnet BNT switches
#---------------------------------------------------------
BEGIN
{
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
$::XCATDIR = $ENV{'XCATDIR'} ? $ENV{'XCATDIR'} : '/etc/xcat';
}
use lib "$::XCATROOT/lib/perl";
use strict;
use Socket;
use Getopt::Long;
use Expect;
use xCAT::Usage;
use xCAT::NodeRange;
use xCAT::NetworkUtils;
use xCAT::Utils;
use xCAT::Table;
use xCAT::MsgUtils;
my $args = join ' ', @ARGV;
$::command = "$0 $args";
@ -14,6 +30,10 @@ $::command = "$0 $args";
Getopt::Long::Configure("bundling");
$Getopt::Long::ignorecase = 0;
#global variables
my @nodes;
my @filternodes;
#---------------------------------------------------------
#Main
@ -22,13 +42,16 @@ $Getopt::Long::ignorecase = 0;
if (
!GetOptions(
'h|help' => \$::HELP,
'r|range=s' => \$::SWITCH,
'p|port=s' => \$::PORT,
'v|vlan=s' => \$::VLAN,
'u|user=s' => \$::USER,
'w|password=s' => \$::PASSWORD,
'g|group=s' => \$::GROUP,
'range=s' => \$::SWITCH,
'port=s' => \$::PORT,
'vlan=s' => \$::VLAN,
'user=s' => \$::USER,
'password=s' => \$::PASSWORD,
'group=s' => \$::GROUP,
'snmp' => \$::SNMP,
'ip' => \$::IP,
'name' => \$::NAME,
'all' => \$::ALL,
)
)
{
@ -43,54 +66,146 @@ if ($::HELP)
exit(0);
}
my $switch;
if ($::SWITCH)
{
$switch = $::SWITCH;
if ($::SWITCH) {
my @filternodes = xCAT::NodeRange::noderange( $::SWITCH );
if (nodesmissed) {
my $nodenotdefined = join(',', nodesmissed);
xCAT::MsgUtils->message("I","The following nodes are not defined in xCAT DB: $nodenotdefined");
}
foreach (@filternodes) {
push @nodes, $_;
}
unless (@nodes) {
xCAT::MsgUtils->message("E","No Valid Switch to process");
exit(1);
}
} else {
xCAT::MsgUtils->message("E","Invalid flag, please provide switches with --range");
&usage;
exit(1);
}
my $switches = join(",",@nodes);
my $cmd;
my $vlan;
my $port;
if ($::VLAN)
{
if ($::PORT) {
$port = $::PORT;
} else {
print "Need port number to set up VLAN\n";
&usage;
exit(1);
}
$vlan = $::VLAN;
print "Tagging VLAN=$vlan for $switch port $port\n";
#create vlan
#tagged vlan
$cmd = `xdsh $switch --devicetype EthSwitch::BNT "enable;configure terminal;vlan $vlan;exit;interface port $port;switchport mode trunk;switchport trunk allowed vlan $vlan;write memory;exit;exit"`;
my $sub_req;
my $switch;
my $rc;
if ($::ALL) {
config_ip();
config_hostname();
config_snmp();
}
if ($::IP)
{
config_ip();
}
if ($::NAME)
{
config_hostname();
}
if ($::SNMP)
{
config_snmp();
}
if ($::VLAN)
{
config_vlan();
}
sub config_ip {
my $nodetab = xCAT::Table->new('hosts');
my $nodehash = $nodetab->getNodesAttribs(\@nodes,['otherinterfaces']);
foreach my $switch (@nodes) {
print "change $switch to static ip address\n";
my $dip= $nodehash->{$switch}->[0]->{otherinterfaces};
#get hostname
my $dswitch = xCAT::NetworkUtils->gethostname($dip);
#if not defined, need to create one for xdsh to use
if (!$dswitch) {
my $ip_str = $dip;
$ip_str =~ s/\./\-/g;
$dswitch = "switch-$ip_str";
#is there other way we can check if this node is defined in the xCATdb
$cmd = "lsdef $dswitch";
$rc= xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0) {
$cmd = "mkdef -t node -o $dswitch groups=switch ip=$dip switchtype=BNT username=root password=admin protocol=telnet nodetype=switch";
$rc= xCAT::Utils->runcmd($cmd, 0);
}
}
#check if it is in the /etc/hosts
my $output = `grep $dswitch /etc/hosts`;
if ( !$output ) {
$cmd = "makehosts $dswitch";
$rc= xCAT::Utils->runcmd($cmd, 0);
}
# verify if xdsh works
$cmd = "xdsh $dswitch --devicetype EthSwitch::BNT 'enable;configure terminal;exit' ";
$rc= xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0) {
xCAT::MsgUtils->message("E","Couldn't communicate with $dswitch, $dip");
next;
}
#change to static ip address
my $static_ip = xCAT::NetworkUtils->getipaddr($switch);
if (!$static_ip){
xCAT::MsgUtils->message("E","static ip is not defined for $switch");
next;
}
$cmd="xdsh $dswitch -t 10 --devicetype EthSwitch::BNT 'enable;configure terminal;show interface ip;interface ip 1;ip address $static_ip;exit;exit' ";
$rc= xCAT::Utils->runcmd($cmd, 0);
print "finish setup static ip address for $switch\n";
#update switch status
$cmd = "chdef $switch status=ip_configed otherinterface=";
$rc= xCAT::Utils->runcmd($cmd, 0);
#remove discover switch from xCATdb and /etc/hosts
$cmd = "makehosts -d $dswitch";
$rc= xCAT::Utils->runcmd($cmd, 0);
$cmd = "rmdef $dswitch";
$rc= xCAT::Utils->runcmd($cmd, 0);
}
}
sub config_hostname {
my $switchtab = xCAT::Table->new('switches');
my $switchhash = $switchtab->getNodesAttribs(\@nodes,['sshusername','sshpassword']);
foreach my $switch (@nodes) {
my $user= $switchhash->{$switch}->[0]->{sshusername};
my $pwd= $switchhash->{$switch}->[0]->{sshpassword};
if ((!$user)||(!$pwd)) {
print "switch ssh username or password is not define, add default one\n";
$cmd = "chdef $switch username=root password=admin";
$rc= xCAT::Utils->runcmd($cmd, 0);
}
$cmd="xdsh $switch --devicetype EthSwitch::BNT 'enable;configure terminal;hostname $switch;write memory' ";
$rc= xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0) {
xCAT::MsgUtils->message("E","Failed to setup hostname for $switch");
next;
}
#update switch status
$cmd = "chdef $switch status=hostname_configed" ;
$rc= xCAT::Utils->runcmd($cmd, 0);
}
}
#setup secure SNMP v3
if ($::SNMP){
my $mysw;
my $enable_cmd="enable\r";
my $config_cmd="configure terminal\r";
my $exit_cmd="exit\r";
my $pwd_prompt = "password: ";
my $sw_prompt = "$switch>";
my $enable_prompt="$switch#";
my $config_prompt="^.*\\\(config\\\)\#";
$mysw = new Expect;
my $timeout = 20;
my $login_cmd = "telnet $switch\r";
my $passwd = "admin\r";
sub config_snmp {
my $snmp_user;
my $snmp_passwd;
my $snmp_group;
if ($::USER) {
$snmp_user = $::USER;
} else {
@ -107,99 +222,132 @@ if ($::SNMP){
} else {
$snmp_group = "xcatgroup\r";
}
print "Setup SNMP server for user=$snmp_user, password=$snmp_passwd, group=$snmp_group\n";
#create a SNMP user
my $cfg_user1="snmp-server user 5 name $snmp_user\r";
my $cfg_user2="snmp-server user 5 authentication-protocol sha authentication-password\r";
#create a SNMP group
my $cfg_group1="snmp-server group 5 group-name $snmp_group\r";
my $cfg_group2="snmp-server group 5 user-name $snmp_user\r";
my $cfg_group3="snmp-server group 5 security usm\r";
#Add access permission
my $cfg_access1="snmp-server access 5 name $snmp_group\r";
my $cfg_access2="snmp-server access 5 level authNoPriv\r";
my $cfg_access3="snmp-server access 5 security usm\r";
my $cfg_access4="snmp-server access 5 read-view iso\r";
$mysw->slave->stty(qw(sane -echo));
foreach my $switch (@nodes) {
my $mysw;
my $enable_cmd="enable\r";
my $config_cmd="configure terminal\r";
my $exit_cmd="exit\r";
unless ($mysw->spawn($login_cmd))
{
my $pwd_prompt = "password: ";
my $sw_prompt = "$switch>";
my $enable_prompt="$switch#";
my $config_prompt="^.*\\\(config\\\)\#";
$mysw = new Expect;
my $timeout = 20;
my $login_cmd = "telnet $switch\r";
my $passwd = "admin\r";
print "Setup SNMP server for $switch\n";
#create a SNMP user
my $cfg_user1="snmp-server user 5 name $snmp_user\r";
my $cfg_user2="snmp-server user 5 authentication-protocol sha authentication-password\r";
#create a SNMP group
my $cfg_group1="snmp-server group 5 group-name $snmp_group\r";
my $cfg_group2="snmp-server group 5 user-name $snmp_user\r";
my $cfg_group3="snmp-server group 5 security usm\r";
#Add access permission
my $cfg_access1="snmp-server access 5 name $snmp_group\r";
my $cfg_access2="snmp-server access 5 level authNoPriv\r";
my $cfg_access3="snmp-server access 5 security usm\r";
my $cfg_access4="snmp-server access 5 read-view iso\r";
$mysw->slave->stty(qw(sane -echo));
unless ($mysw->spawn($login_cmd))
{
$mysw->soft_close();
print "Unable to run $login_cmd\n";
next;
}
my @result = $mysw->expect(
$timeout,
[
$pwd_prompt,
sub {
$mysw->clear_accum();
$mysw->send("$passwd\r");
$mysw->clear_accum();
$mysw->exp_continue();
}
],
[
"-re", $sw_prompt,
sub {
$mysw->clear_accum();
$mysw->send($enable_cmd);
$mysw->exp_continue();
}
],
[
"-re", $enable_prompt,
sub {
$mysw->clear_accum();
$mysw->send($config_cmd);
$mysw->exp_continue();
}
],
[
"-re", $config_prompt,
sub {
$mysw->clear_accum();
$mysw->send($cfg_user1);
$mysw->send($cfg_user2);
$mysw->send($passwd);
$mysw->send($snmp_passwd);
$mysw->send($snmp_passwd);
sleep 1;
$mysw->clear_accum();
# create snmp group
$mysw->send($cfg_group1);
$mysw->send($cfg_group2);
$mysw->send($cfg_group3);
$mysw->clear_accum();
$mysw->send($cfg_access1);
$mysw->send($cfg_access2);
$mysw->send($cfg_access3);
$mysw->send($cfg_access4);
$mysw->clear_accum();
$mysw->send("write memory\r");
$mysw->send($exit_cmd);
$mysw->send($exit_cmd);
}
],
);
##########################################
# Expect error - report and quit
##########################################
if (defined($result[1]))
{
my $errmsg = $result[1];
$mysw->soft_close();
print "Failed expect command $errmsg\n";
exit(1);
}
$mysw->soft_close();
print "Unable to run $login_cmd\n";
next;
#update switch status
$cmd = "chdef $switch status=switch_configed" ;
$rc= xCAT::Utils->runcmd($cmd, 0);
}
my @result = $mysw->expect(
$timeout,
[
$pwd_prompt,
sub {
$mysw->clear_accum();
$mysw->send("$passwd\r");
$mysw->clear_accum();
$mysw->exp_continue();
}
],
[
"-re", $sw_prompt,
sub {
#print "$switch: sending enable command: $enable_cmd\n";
$mysw->clear_accum();
$mysw->send($enable_cmd);
$mysw->exp_continue();
}
],
[
"-re", $enable_prompt,
sub {
#print "$switch: sending config command: $config_cmd\n";
$mysw->clear_accum();
$mysw->send($config_cmd);
$mysw->exp_continue();
}
],
[
"-re", $config_prompt,
sub {
$mysw->clear_accum();
$mysw->send($cfg_user1);
$mysw->send($cfg_user2);
$mysw->send($passwd);
$mysw->send($snmp_passwd);
$mysw->send($snmp_passwd);
sleep 1;
$mysw->clear_accum();
# create snmp group
$mysw->send($cfg_group1);
$mysw->send($cfg_group2);
$mysw->send($cfg_group3);
$mysw->clear_accum();
$mysw->send($cfg_access1);
$mysw->send($cfg_access2);
$mysw->send($cfg_access3);
$mysw->send($cfg_access4);
$mysw->clear_accum();
$mysw->send("write memory\r");
$mysw->send($exit_cmd);
$mysw->send($exit_cmd);
}
],
);
##########################################
# Expect error - report and quit
##########################################
if (defined($result[1]))
{
my $errmsg = $result[1];
$mysw->soft_close();
print "Failed expect command $errmsg\n";
}
sub config_vlan {
if ($::PORT) {
$port = $::PORT;
} else {
&usage;
exit(1);
}
$mysw->soft_close();
$vlan = $::VLAN;
print "Tagging VLAN=$vlan for $switches port $port\n";
#create vlan, tagged vlan
$cmd = `xdsh $switches --devicetype EthSwitch::BNT "enable;configure terminal;vlan $vlan;exit;interface port $port;switchport mode trunk;switchport trunk allowed vlan $vlan;write memory;exit;exit"`;
}
#---------------------------------------------------------
=head3 usage
@ -213,8 +361,11 @@ sub usage
{
print "Usage:
configBNT [-?│-h│--help]
configBNT [--range switchnames] [-p|--port port] [-v|--vlan vlan]
configBNT [--range switchnames] [--snmp] [-u|--user snmp_user] [-w|--password snmp_password] [-g|--group snmp_group]
configBNT [--range switchnames] [--all]
configBNT [--range switchnames] [--ip]
configBNT [--range switchnames] [--name ]
configBNT [--range switchnames] [--snmp] [--user snmp_user] [--password snmp_password] [--group snmp_group]
configBNT [--range switchnames] [--port port] [--vlan vlan]
\n";
}

View File

@ -4,9 +4,24 @@
# Configure Ethnet Mellonax switches
#---------------------------------------------------------
BEGIN
{
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
$::XCATDIR = $ENV{'XCATDIR'} ? $ENV{'XCATDIR'} : '/etc/xcat';
}
use lib "$::XCATROOT/lib/perl";
use strict;
use Getopt::Long;
use Expect;
use xCAT::Usage;
use xCAT::NodeRange;
use xCAT::NetworkUtils;
use xCAT::Utils;
use xCAT::Table;
use xCAT::MsgUtils;
my $args = join ' ', @ARGV;
$::command = "$0 $args";
@ -14,6 +29,10 @@ $::command = "$0 $args";
Getopt::Long::Configure("bundling");
$Getopt::Long::ignorecase = 0;
#global variables
my @nodes;
my @filternodes;
#---------------------------------------------------------
#Main
@ -21,10 +40,12 @@ $Getopt::Long::ignorecase = 0;
# parse the options
if (
!GetOptions(
'h|help' => \$::HELP,
'r|range=s' => \$::SWITCH,
'u|user=s' => \$::USER,
'i|ip=s' => \$::IP,
'h|help' => \$::HELP,
'range=s' => \$::SWITCH,
'config' => \$::CONFIG,
'ip' => \$::IP,
'name' => \$::NAME,
'all' => \$::ALL,
)
)
{
@ -39,61 +60,177 @@ if ($::HELP)
exit(0);
}
my $switch;
if ($::SWITCH)
{
$switch = $::SWITCH;
my @filternodes = xCAT::NodeRange::noderange( $::SWITCH );
if (nodesmissed) {
my $nodenotdefined = join(',', nodesmissed);
xCAT::MsgUtils->message("I","The following nodes are not defined in xCAT DB: $nodenotdefined");
}
foreach (@filternodes) {
push @nodes, $_;
}
unless (@nodes) {
xCAT::MsgUtils->message("E","No Valid Switch to process");
exit(1);
}
} else {
xCAT::MsgUtils->message("E","Invalid flag, please provide switches with --range");
&usage;
exit(1);
}
my $switches = join(",",@nodes);
my $user;
if ($::USER)
{
$user = $::USER;
} else {
&usage;
exit(1);
}
my $ip;
if ($::IP)
{
$ip = $::IP;
} else {
&usage;
exit(1);
}
my $cmd;
my $switch;
my $rc;
my $master;
#default root/password and protcol for Mellonax switch
$cmd = `chtab switch=$switch switches.sshusername=$user`;
print $cmd;
if ($::ALL) {
config_ip();
config_hostname();
run_rspconfig();
}
if ($::IP) {
config_ip();
}
#call rspconfig command to setup switch
#enable ssh
$cmd=`rspconfig $switch sshcfg=enable`;
print $cmd;
if ($::NAME) {
config_hostname();
}
#enable snmp function on the switch
$cmd=`rspconfig $switch snmpcfg=enable`;
print $cmd;
#enable the snmp trap
$cmd=`rspconfig $switch alert=enable`;
print $cmd;
if ($::CONFIG)
{
run_rspconfig();
}
#Logging destination:
$cmd=`rspconfig $switch logdest=$ip`;
print $cmd;
#config ntp
$cmd = `xdsh $switch -l $user --devicetype IBSwitch::Mellanox "enable;configure terminal;ntp enable;ntpdate $ip; ntp server $ip;configuration write;show ntp" `;
print $cmd;
sub config_ip {
# get host table for otherinterfaces
my $nodetab = xCAT::Table->new('hosts');
my $nodehash = $nodetab->getNodesAttribs(\@nodes,['otherinterfaces']);
# get netmask from network table
my $nettab = xCAT::Table->new("networks");
my @nets;
if ($nettab) {
@nets = $nettab->getAllAttribs('net','mask');
}
foreach my $switch (@nodes) {
print "change $switch to static ip address\n";
my $dip= $nodehash->{$switch}->[0]->{otherinterfaces};
#get hostname
my $dswitch = xCAT::NetworkUtils->gethostname($dip);
#if not defined, need to create one for xdsh to use
if (!$dswitch) {
my $ip_str = $dip;
$ip_str =~ s/\./\-/g;
$dswitch = "switch-$ip_str";
#is there other way we can check if this node is defined in the xCATdb
$cmd = "lsdef $dswitch";
$rc= xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0) {
$cmd = "mkdef -t node -o $dswitch groups=switch ip=$dip switchtype=Mellanox username=admin nodetype=switch";
$rc= xCAT::Utils->runcmd($cmd, 0);
}
}
#check if it is in the /etc/hosts
my $output = `grep $dswitch /etc/hosts`;
if ( !$output ) {
$cmd = "makehosts $dswitch";
$rc= xCAT::Utils->runcmd($cmd, 0);
}
# verify if xdsh works
$cmd = "xdsh $dswitch -l admin --devicetype IBSwitch::Mellanox 'enable;configure terminal;exit' ";
$rc= xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0) {
xCAT::MsgUtils->message("E","Couldn't communicate with $dswitch, $dip");
next;
}
#change to static ip address
my $static_ip = xCAT::NetworkUtils->getipaddr($switch);
if (!$static_ip){
xCAT::MsgUtils->message("E","static ip is not defined for $switch");
next;
}
#get netmask
my $mask;
foreach my $net (@nets) {
if (xCAT::NetworkUtils::isInSameSubnet( $net->{'net'}, $static_ip, $net->{'mask'}, 0)) {
$mask=$net->{'mask'};
}
}
$cmd="xdsh $dswitch -t 10 -l admin --devicetype IBSwitch::Mellanox 'enable;configure terminal;no interface mgmt0 dhcp;interface mgmt0 ip address $static_ip $mask;configuration write;exit;exit' ";
$rc= xCAT::Utils->runcmd($cmd, 0);
#update switch status
$cmd = "chdef $switch status=ip_configed otherinterface=";
$rc= xCAT::Utils->runcmd($cmd, 0);
#remove discover switch from xCATdb and /etc/hosts
$cmd = "makehosts -d $dswitch";
$rc= xCAT::Utils->runcmd($cmd, 0);
$cmd = "rmdef $dswitch";
$rc= xCAT::Utils->runcmd($cmd, 0);
}
}
sub config_hostname {
my $switchtab = xCAT::Table->new('switches');
my $switchhash = $switchtab->getNodesAttribs(\@nodes,['sshusername']);
foreach my $switch (@nodes) {
my $user= $switchhash->{$switch}->[0]->{sshusername};
if (!$user) {
print "switch ssh username is not defined, add default one\n";
$cmd = "chdef $switch username=admin";
$rc= xCAT::Utils->runcmd($cmd, 0);
$user="admin";
}
$cmd="xdsh $switch -l $user --devicetype IBSwitch::Mellanox 'enable;configure terminal;hostname $switch;configuration write' ";
$rc= xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0) {
xCAT::MsgUtils->message("E","Failed to setup hostname for $switch");
next;
}
#update switch status
$cmd = "chdef $switch status=hostname_configed" ;
$rc= xCAT::Utils->runcmd($cmd, 0);
}
}
sub run_rspconfig {
my $switchtab = xCAT::Table->new('switches');
my $switchhash = $switchtab->getNodesAttribs(\@nodes,['sshusername']);
$master = `hostname -i`;
print "master=$master\n";
foreach my $switch (@nodes) {
my $user= $switchhash->{$switch}->[0]->{sshusername};
#call rspconfig command to setup switch
#enable ssh
$cmd=`rspconfig $switch sshcfg=enable`;
#enable snmp function on the switch
$cmd=`rspconfig $switch snmpcfg=enable`;
#enable the snmp trap
$cmd=`rspconfig $switch alert=enable`;
#Logging destination:
$cmd=`rspconfig $switch logdest=$master`;
#config ntp
$cmd = `xdsh $switch -l $user --devicetype IBSwitch::Mellanox "enable;configure terminal;ntp enable;ntpdate $master; ntp server $master;configuration write;show ntp" `;
#update switch status
$cmd = "chdef $switch status=switch_configed" ;
$rc= xCAT::Utils->runcmd($cmd, 0);
}
}
#---------------------------------------------------------
@ -108,7 +245,10 @@ sub usage
{
print "Usage:
configMellonax [-?│-h│--help]
configMellonax [--range switchnames] [-u|--user sshusername] [-i|--ip xcatMN_ip_address]
configMellonax [--range switchnames] [--all]
configMellonax [--range switchnames] [--ip]
configMellonax [--range switchnames] [--name]
configMellonax [--range switchnames] [--config]
\n";
}