#!/usr/bin/perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html

# confignics postscript for configuring additional ethernet and ib NIC adapters. 
# This module parses NIC environment variables containing data from the nics table
# for the specific node i.e.:
# NICNODE             - the node name
# NICIPS              - comma separated list of ips per NIC
# NICHOSTNAMESUFFIXES - comma spearated list of hostname suffixes per NIC
# NICTYPES            - ethernet or infiniband
# NICCUSTOMSCRIPTS    - script to configure nic, i.e. configeth or configib
# NICNETWORKS         - network and subnetmask for the adapter.

use strict;
use Socket;

# Only two args are supported for confignics:
# "-s" to allow the install nic to be configured. If not set
#      then the install nic will not be configured.
# "--ibaports=x" to specify the number of ports for an ib adapter.
#                This value will set in an environment variable
#                prior to calling configib.

my $ibaports = 1;       # default to one port per ib adapter.
my $cfg_inst_nic = '';
my $arg ='';
while ($arg = shift(@ARGV)) {
    if ( $arg eq "-s" ) {
        $cfg_inst_nic = 1;
    } elsif ( $arg =~ /--ibaports=(\d)$/) {
        $ibaports = $1;
    }
}

my $ibnics = '';
my $nicips = $ENV{NICIPS};
my $niccustomscripts = $ENV{NICCUSTOMSCRIPTS};
my $nictypes = $ENV{NICTYPES};
my $installnic = $ENV{INSTALLNIC};
my $xcatpostdir = "/xcatpost";
my %cust_script_nics = (); # hash to save nics specified in niccustomscripts
my $type = '';
my $nic = '';
my $MAC = $ENV{MACADDRESS};
my $inst_nic = '';

# determine which ethernet nic is the installnic. 
if ($installnic =~ /(e(n|th)\d+)$/ ) {
     $inst_nic = $1;
} elsif ($installnic eq "mac") {
    # determine nic from mac. Get all NICs and their mac addresses from ifconfig
    # and compare that with MACADDR.
    my @ifcfg_info = split(/\n/,`ifconfig -a | grep HWaddr | awk '{print \$1,\$5;}'`);
    foreach my $nic_mac (@ifcfg_info) {
        my @nicmac = split(/ /,$nic_mac);
        if (uc($nicmac[1]) eq uc($MAC)) {
            $inst_nic = $nicmac[0];
            last;
        }
    }
} else { # INSTALLNIC not set or is not known
    system("logger -t xcat -p local4.info 'confignics: install nic $inst_nic not known '");
}


# niccustomscripts specifies which NICS need to be configured. 
# and which script to use to configure it.
# Strip NIC and customscript and then call the custom script with the NIC
# it is up to the custom script to verify information passed in NIC env vars.
# i.e. if customscript is "eth1:configeth eth1, eth2:configeth2"
# then first get eth1 for the nic and "configeth eth1" for the command to run"
# the do the same for eth2.

if ( defined $niccustomscripts && length $niccustomscripts > 0 ) {
    system("logger -t xcat -p local4.info 'confignics: processing custom scripts: $niccustomscripts '");

    foreach my $customscript (split(/,/,$niccustomscripts)) {

        my @script = ();
        if ( $customscript =~ /!/ ) {
            @script = split(/!/,$customscript);
        } else {
            @script = split(/:/,$customscript);
        }
        $cust_script_nics{$script[0]} = 1;
        my @s = split(/ /,$script[1]);

        # if installnic then verify that "-s" flag was passed in.
        if (($inst_nic ne $script[0]) || (($inst_nic eq $script[0]) && $cfg_inst_nic)) {
            runcmd("$script[1]");
            system("logger -t xcat -p local4.info 'confignics: executed custom script: $script[1] '");
        }
    }
}

# Get nic from nicips. If nic is in cust_script_nics hash then ignore this nic.
# otherwise, get nictype if set or determine type from nic name, eth* or en* 
# implies ethernet, ib* implies infiniband.
# 
# configib prefers to have ib adapters configured in one call for performance
# reasons. So add ib nics to a list and call configib outside the loop.
foreach my $nic_ips (split(/,/,$nicips)) {
    $type = '';
    my $type_found = 0;
    $nic = '';
    my @nic_and_ips = ();
    if ( $nic_ips =~ /!/ ) {
        @nic_and_ips = split(/!/,$nic_ips);
    } else {
        @nic_and_ips = split(/:/,$nic_ips);
    }
    $nic = $nic_and_ips[0];
    # do not configure if nic is in the customscript hash
    if ($cust_script_nics{$nic_and_ips[0]} == 1 ) {
        system("logger -t xcat -p local4.info 'confignics: nic $nic_and_ips[0] already configured through custom script '");
        
    }
    else {
        # Find matching type for this nic
        foreach my $nic_type (split(/,/,$nictypes)) {
            my @nic_and_type = ();
            if ( $nic_type =~ /!/ ) {
                @nic_and_type = split(/!/,$nic_type);
            } else {
                @nic_and_type = split(/:/,$nic_type);
            }
            if ($nic_and_type[0] eq $nic ) {
                $type = $nic_and_type[1];
                # verify type is "ethernet" or "infiniband"
                if ($type =~ /ethernet|infiniband/i ) {
                    $type_found = 1;
                }
                last;
            }    
        }
        # if no matching nic type then derive nic type from nic
        if ( !$type_found ) {
            if ( $nic =~ /(eth|en)\d+/i ) {
                $type = "ethernet";
            } elsif ($nic =~ /ib\d+/i ) {
                $type = "infiniband";
            }
        }

        if ("ethernet" eq lc($type)) {
            # Ensure to only configure the install nic if the "-s" flag was set.
            if (($inst_nic ne $nic) || (($inst_nic eq $nic) && $cfg_inst_nic)) {
                runcmd("configeth $nic");
                system("logger -t xcat -p local4.info 'confignics: executed script: configeth $nic '");
            }
            else {
                system("logger -t xcat -p local4.info 'confignics: Not configuring install nic $nic '");
            }
        } elsif ("infiniband" eq lc($type)) {
            if ($ibnics) {
                $ibnics = $ibnics . "," . $nic;
            } else {
                $ibnics = $nic;
            }
        } else {
            system("logger -t xcat -p local4.info 'confignics: unknown type $type for NIC: $nic '");
        }
    }
}

# Call configib now to configure all ib adapters in one call.
if ($ibnics) {
    runcmd("NIC_IBNICS=$ibnics NIC_IBAPORTS=$ibaports configib");
    system("logger -t xcat -p local4.info 'confignics: executed script: configib for nics $ibnics '");
}

exit 0;

sub runcmd {
	my $cmd = shift @_;
	$cmd .= ' 2>&1';
	my @output = `$cmd`;
	my $rc = $? >> 8;
	if ($rc) {
		system("logger -t xcat -p local4.err 'confignics: command $cmd failed with rc $rc: " . join('',@output) . "'");
		my $errout= "confignics: command $cmd failed with rc $rc.\n";
                print $errout;
		exit $rc;
	}
	print join("\n",@output),"\n";
}

