#!/usr/bin/env perl #--------------------------------------------------------- # 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; Getopt::Long::Configure("bundling"); $Getopt::Long::ignorecase = 0; #global variables my @nodes; my @filternodes; #--------------------------------------------------------- #Main # parse the options if ( !GetOptions( 'h|help' => \$::HELP, 'switches=s' => \$::SWITCH, 'config' => \$::CONFIG, 'ip' => \$::IP, 'name' => \$::NAME, 'all' => \$::ALL, ) ) { &usage; exit(1); } # display the usage if -h or --help is specified if ($::HELP) { &usage; exit(0); } 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"); } # 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"); exit(1); } } else { xCAT::MsgUtils->message("E","Invalid flag, please provide switches with --switches"); &usage; exit(1); } my $switches = join(",",@nodes); my $user; my $cmd; my $rc; my $master; if (($::IP) || ($::ALL)) { config_ip(); } if (($::NAME) || ($::ALL)) { config_hostname(); } 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,['ip','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}; 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"; } $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); if ($::RUNCMD_RC != 0) { xCAT::MsgUtils->message("E","Couldn't communicate with $dswitch, $dip"); 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); push (@discover_switches, $dswitch); push (@config_switches, $switch); } if (@config_switches) { #update switch status 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 $dsw"; $rc= xCAT::Utils->runcmd($cmd, 0); $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) { 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; } push (@config_switches, $switch); } if (@config_switches) { #update switch status 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`; 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" `; push (@config_switches, $switch); } if (@config_switches) { #update switch status my $csw = join(",",@config_switches); $cmd = "chdef $csw status=switch_configed" ; $rc= xCAT::Utils->runcmd($cmd, 0); } } #--------------------------------------------------------- =head3 usage Displays message for -h option =cut #--------------------------------------------------------- sub usage { print "Usage: configMellonax [-?│-h│--help] configMellonax [--switches switchnames] [--all] configMellonax [--switches switchnames] [--ip] configMellonax [--switches switchnames] [--name] configMellonax [--switches switchnames] [--config] \n"; }