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

Modify code from code review

This commit is contained in:
Casandra Qiu
2016-08-02 22:10:45 -04:00
parent 843525e73b
commit 5d4d0d169e
3 changed files with 122 additions and 134 deletions

View File

@ -1133,14 +1133,14 @@ sub get_hostname {
#--------------------------------------------------------------------------------
sub get_switchtype {
my $vendor = shift;
my $key;
my $key = "Not support";
my $search_string = join '|', keys(%global_switch_type);
if ($vendor =~ /($search_string)/) {
$key = $1;
return $global_switch_type{$key};
} else {
return $vendor;
return $key;
}
}
@ -1370,9 +1370,7 @@ sub switchsetup {
my $dshcmd;
my $static_ip;
my $discover_switch;
my @rmnodes;
my @BNTswitches;
my @MLNXswitches;
my $nodes_to_config;
#print Dumper($outhash);
my $macmap = xCAT::MacMap->new();
@ -1402,39 +1400,25 @@ sub switchsetup {
if (exists($globalopt{verbose})) {
send_msg($request, 0, "Found Discovery switch $dswitch, $ip, $mac with predefine switch $node, $static_ip $stype switch\n" );
}
xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$node,"otherinterfaces=$ip",'status=Matched',"mac=$mac","switchtype=$stype","usercomment=$vendor"] }, $sub_req, 0, 1);
#run xdsh command to set discover ip address to static ip address
#each switch type has different CLI for setup ip address
push (@{$nodes_to_config->{$stype}}, $node);
}
# BNT switches
if ( $stype =~ /BNT/ ) {
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/ ) {
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 {
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);
foreach my $mytype (keys %$nodes_to_config) {
my $config_script = "$::XCATROOT/shart/xcat/tools/config".$mytype;
if (-r -x $config_script) {
my $switches = join(",",@{${nodes_to_config}->{$mytype}});
send_msg($request, 0, "call to config $mytype switches $switches\n");
my $out = `$config_script --switches $switches --all`;
send_msg($request, 0, "output = $out\n");
} else {
send_msg($request, 0, "the switch type $mytype is not support yet\n");
}
}
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;
}

View File

@ -23,10 +23,6 @@ use xCAT::Utils;
use xCAT::Table;
use xCAT::MsgUtils;
my $args = join ' ', @ARGV;
$::command = "$0 $args";
Getopt::Long::Configure("bundling");
$Getopt::Long::ignorecase = 0;
@ -41,17 +37,17 @@ my @filternodes;
# parse the options
if (
!GetOptions(
'h|help' => \$::HELP,
'range=s' => \$::SWITCH,
'h|help' => \$::HELP,
'switches=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,
'snmp' => \$::SNMP,
'ip' => \$::IP,
'name' => \$::NAME,
'all' => \$::ALL,
)
)
{
@ -72,15 +68,22 @@ if ($::SWITCH) {
my $nodenotdefined = join(',', nodesmissed);
xCAT::MsgUtils->message("I","The following nodes are not defined in xCAT DB: $nodenotdefined");
}
foreach (@filternodes) {
push @nodes, $_;
# check switch type
my $switchestab = xCAT::Table->new('switches');
my $switches_hash = $switchestab->getNodesAttribs(\@filternodes,['switchtype']);
foreach my $fsw (@filternodes) {
if (($switches_hash->{$fsw}->[0]->{switchtype}) =~ /BNT/) {
push @nodes, $fsw;
} else {
xCAT::MsgUtils->message("E","The $fsw is not BNT switch, will not config");
}
}
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");
xCAT::MsgUtils->message("E","Invalid flag, please provide switches with --switches");
&usage;
exit(1);
}
@ -90,25 +93,18 @@ my $cmd;
my $vlan;
my $port;
my $sub_req;
my $switch;
my $rc;
if ($::ALL) {
config_ip();
config_hostname();
config_snmp();
}
if ($::IP)
if (($::IP) || ($::ALL))
{
config_ip();
}
if ($::NAME)
if (($::NAME) || ($::ALL))
{
config_hostname();
}
if ($::SNMP)
if (($::SNMP) || ($::ALL))
{
config_snmp();
}
@ -118,12 +114,14 @@ if ($::VLAN)
}
sub config_ip {
my @config_switches;
my @discover_switches;
my $nodetab = xCAT::Table->new('hosts');
my $nodehash = $nodetab->getNodesAttribs(\@nodes,['otherinterfaces']);
my $nodehash = $nodetab->getNodesAttribs(\@nodes,['ip','otherinterfaces']);
foreach my $switch (@nodes) {
print "change $switch to static ip address\n";
my $dip= $nodehash->{$switch}->[0]->{otherinterfaces};
my $static_ip= $nodehash->{$switch}->[0]->{ip};
#get hostname
my $dswitch = xCAT::NetworkUtils->gethostname($dip);
@ -133,20 +131,12 @@ sub config_ip {
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);
}
$cmd = "chdef -t node -o $dswitch groups=switch ip=$dip switchtype=BNT username=root password=admin protocol=telnet nodetype=switch";
$rc= xCAT::Utils->runcmd($cmd, 0);
$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);
@ -154,29 +144,30 @@ sub config_ip {
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";
push (@discover_switches, $dswitch);
push (@config_switches, $switch);
}
if (@config_switches) {
#update switch status
$cmd = "chdef $switch status=ip_configed otherinterface=";
my $csw = join(",",@config_switches);
$cmd = "chdef $csw status=ip_configed otherinterfaces=";
$rc= xCAT::Utils->runcmd($cmd, 0);
}
if (@discover_switches) {
my $dsw = join(",",@discover_switches);
#remove discover switch from xCATdb and /etc/hosts
$cmd = "makehosts -d $dswitch";
$cmd = "makehosts -d $dsw";
$rc= xCAT::Utils->runcmd($cmd, 0);
$cmd = "rmdef $dswitch";
$cmd = "rmdef $dsw";
$rc= xCAT::Utils->runcmd($cmd, 0);
}
}
sub config_hostname {
my @config_switches;
my $switchtab = xCAT::Table->new('switches');
my $switchhash = $switchtab->getNodesAttribs(\@nodes,['sshusername','sshpassword']);
foreach my $switch (@nodes) {
@ -193,8 +184,12 @@ sub config_hostname {
xCAT::MsgUtils->message("E","Failed to setup hostname for $switch");
next;
}
push (@config_switches, $switch);
}
if (@config_switches) {
#update switch status
$cmd = "chdef $switch status=hostname_configed" ;
my $csw = join(",",@config_switches);
$cmd = "chdef $csw status=hostname_configed" ;
$rc= xCAT::Utils->runcmd($cmd, 0);
}
}
@ -205,6 +200,7 @@ sub config_snmp {
my $snmp_user;
my $snmp_passwd;
my $snmp_group;
my @config_switches;
if ($::USER) {
$snmp_user = $::USER;
@ -326,9 +322,14 @@ sub config_snmp {
exit(1);
}
$mysw->soft_close();
push (@config_switches, $switch);
}
if (@config_switches) {
#update switch status
$cmd = "chdef $switch status=switch_configed" ;
my $csw = join(",",@config_switches);
$cmd = "chdef $csw status=switch_configed" ;
$rc= xCAT::Utils->runcmd($cmd, 0);
$cmd = "chtab switch=$csw switches.snmpversion=3 switches.auth=sha switches.username=$snmp_user switches.password=$snmp_passwd" ;
$rc= xCAT::Utils->runcmd($cmd, 0);
}
}
@ -361,11 +362,11 @@ sub usage
{
print "Usage:
configBNT [-?│-h│--help]
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]
configBNT [--switches switchnames] [--all]
configBNT [--switches switchnames] [--ip]
configBNT [--switches switchnames] [--name ]
configBNT [--switches switchnames] [--snmp] [--user snmp_user] [--password snmp_password] [--group snmp_group]
configBNT [--switches switchnames] [--port port] [--vlan vlan]
\n";
}

View File

@ -22,10 +22,6 @@ use xCAT::Utils;
use xCAT::Table;
use xCAT::MsgUtils;
my $args = join ' ', @ARGV;
$::command = "$0 $args";
Getopt::Long::Configure("bundling");
$Getopt::Long::ignorecase = 0;
@ -41,7 +37,7 @@ my @filternodes;
if (
!GetOptions(
'h|help' => \$::HELP,
'range=s' => \$::SWITCH,
'switches=s' => \$::SWITCH,
'config' => \$::CONFIG,
'ip' => \$::IP,
'name' => \$::NAME,
@ -67,8 +63,15 @@ if ($::SWITCH)
my $nodenotdefined = join(',', nodesmissed);
xCAT::MsgUtils->message("I","The following nodes are not defined in xCAT DB: $nodenotdefined");
}
foreach (@filternodes) {
push @nodes, $_;
# check switch type
my $switchestab = xCAT::Table->new('switches');
my $switches_hash = $switchestab->getNodesAttribs(\@filternodes,['switchtype']);
foreach my $fsw (@filternodes) {
if (($switches_hash->{$fsw}->[0]->{switchtype}) =~ /Mellanox/) {
push @nodes, $fsw;
} else {
xCAT::MsgUtils->message("E","The $fsw is not BNT switch, will not config");
}
}
unless (@nodes) {
xCAT::MsgUtils->message("E","No Valid Switch to process");
@ -83,34 +86,29 @@ if ($::SWITCH)
my $switches = join(",",@nodes);
my $user;
my $cmd;
my $switch;
my $rc;
my $master;
if ($::ALL) {
config_ip();
config_hostname();
run_rspconfig();
}
if ($::IP) {
if (($::IP) || ($::ALL)) {
config_ip();
}
if ($::NAME) {
if (($::NAME) || ($::ALL)) {
config_hostname();
}
if ($::CONFIG)
{
if (($::CONFIG) || ($::ALL)) {
run_rspconfig();
}
sub config_ip {
my @config_switches;
my @discover_switches;
# get host table for otherinterfaces
my $nodetab = xCAT::Table->new('hosts');
my $nodehash = $nodetab->getNodesAttribs(\@nodes,['otherinterfaces']);
my $nodehash = $nodetab->getNodesAttribs(\@nodes,['ip','otherinterfaces']);
# get netmask from network table
my $nettab = xCAT::Table->new("networks");
my @nets;
@ -120,29 +118,23 @@ sub config_ip {
foreach my $switch (@nodes) {
print "change $switch to static ip address\n";
my $dip= $nodehash->{$switch}->[0]->{otherinterfaces};
my $static_ip= $nodehash->{$switch}->[0]->{ip};
#get hostname
my $dswitch = xCAT::NetworkUtils->gethostname($dip);
print "dip=$dip, static=$static_ip, dsw=$dswitch\n";
#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);
}
$cmd = "chdef -t node -o $dswitch groups=switch ip=$dip switchtype=Mellanox username=admin nodetype=switch";
$rc= xCAT::Utils->runcmd($cmd, 0);
$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);
@ -150,12 +142,6 @@ sub config_ip {
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) {
@ -166,20 +152,29 @@ sub config_ip {
$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);
push (@discover_switches, $dswitch);
push (@config_switches, $switch);
}
if (@config_switches) {
#update switch status
$cmd = "chdef $switch status=ip_configed otherinterface=";
my $csw = join(",",@config_switches);
$cmd = "chdef $csw status=ip_configed otherinterfaces=";
$rc= xCAT::Utils->runcmd($cmd, 0);
}
if (@discover_switches) {
my $dsw = join(",",@discover_switches);
#remove discover switch from xCATdb and /etc/hosts
$cmd = "makehosts -d $dswitch";
$cmd = "makehosts -d $dsw";
$rc= xCAT::Utils->runcmd($cmd, 0);
$cmd = "rmdef $dswitch";
$cmd = "rmdef $dsw";
$rc= xCAT::Utils->runcmd($cmd, 0);
}
}
sub config_hostname {
my @config_switches;
my $switchtab = xCAT::Table->new('switches');
my $switchhash = $switchtab->getNodesAttribs(\@nodes,['sshusername']);
foreach my $switch (@nodes) {
@ -196,13 +191,18 @@ sub config_hostname {
xCAT::MsgUtils->message("E","Failed to setup hostname for $switch");
next;
}
push (@config_switches, $switch);
}
if (@config_switches) {
#update switch status
$cmd = "chdef $switch status=hostname_configed" ;
my $csw = join(",",@config_switches);
$cmd = "chdef $csw status=hostname_configed" ;
$rc= xCAT::Utils->runcmd($cmd, 0);
}
}
sub run_rspconfig {
my @config_switches;
my $switchtab = xCAT::Table->new('switches');
my $switchhash = $switchtab->getNodesAttribs(\@nodes,['sshusername']);
$master = `hostname -i`;
@ -224,9 +224,12 @@ sub run_rspconfig {
#config ntp
$cmd = `xdsh $switch -l $user --devicetype IBSwitch::Mellanox "enable;configure terminal;ntp enable;ntpdate $master; ntp server $master;configuration write;show ntp" `;
push (@config_switches, $switch);
}
if (@config_switches) {
#update switch status
$cmd = "chdef $switch status=switch_configed" ;
my $csw = join(",",@config_switches);
$cmd = "chdef $csw status=switch_configed" ;
$rc= xCAT::Utils->runcmd($cmd, 0);
}