diff --git a/xCAT-server/lib/xcat/plugins/xcat2nim.pm b/xCAT-server/lib/xcat/plugins/xcat2nim.pm index 363fbab99..d5176e534 100644 --- a/xCAT-server/lib/xcat/plugins/xcat2nim.pm +++ b/xCAT-server/lib/xcat/plugins/xcat2nim.pm @@ -7,7 +7,11 @@ ##################################################### package xCAT_plugin::xcat2nim; - +BEGIN +{ + $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat'; +} +use lib "$::XCATROOT/lib/perl"; use Sys::Hostname; use xCAT::NodeRange; use xCAT::Schema; @@ -16,19 +20,12 @@ use xCAT::DBobjUtils; use Data::Dumper; use Getopt::Long; use xCAT::MsgUtils; -use strict; use Socket; # options can be bundled up like -vV Getopt::Long::Configure("bundling"); $Getopt::Long::ignorecase = 0; -# -# Globals -# - -@::noderange; # list of nodes derived from command line - #------------------------------------------------------------------------------ =head1 xcat2nim @@ -77,8 +74,10 @@ sub preprocess_request my $req = shift; my $cb = shift; my %sn; + + #exit if preprocessed if ($req->{_xcatpreprocessed}->[0] == 1) { return [$req]; } - #exit if preprocessed + my $nodes = $req->{node}; # this may not be the list of nodes we need! my $command = $req->{command}->[0]; $::args = $req->{arg}; @@ -89,7 +88,7 @@ sub preprocess_request # handle -h etc. # list of nodes could be derived multiple ways!! - my ($ret, $mynodes) = &prexcat2nim($cb); + my ($ret, $mynodes, $servnodes) = &prexcat2nim($cb); if ( $ret ) { # either error or -h was processed etc. my $rsp; if ($ret eq "1") { @@ -98,27 +97,37 @@ sub preprocess_request xCAT::MsgUtils->message("E", $rsp, $cb, $ret); } return undef; - } else { - if ($mynodes) { - # set up the requests to go to the service nodes - # all get the same request - # get the hash of service nodes - for the nodes that were provided - my $sn; - $sn = xCAT::Utils->get_ServiceNode($mynodes, $service, "MN"); - foreach my $snkey (keys %$sn) { - my $reqcopy = {%$req}; - $reqcopy->{node} = $sn->{$snkey}; - $reqcopy->{'_xcatdest'} = $snkey; - $reqcopy->{_xcatpreprocessed}->[0] = 1; + } elsif (scalar(@{$mynodes})) { - push @requests, $reqcopy; - } - return \@requests; + # set up the requests to go to the service nodes + # - for the nodes that were provided + # - to handle node and group objects + my $sn; + $sn = xCAT::Utils->get_ServiceNode($mynodes, $service, "MN"); + foreach my $snkey (keys %$sn) { + my $reqcopy = {%$req}; + $reqcopy->{node} = $sn->{$snkey}; + $reqcopy->{'_xcatdest'} = $snkey; + $reqcopy->{_xcatpreprocessed}->[0] = 1; + push @requests, $reqcopy; } + return \@requests; + + } elsif (scalar(@{$servnodes} )) { + # set up the requests to go to the service nodes + # for network objects + foreach my $sn (@{$servnodes}) { + my $reqcopy = {%$req}; + $reqcopy->{'_xcatdest'} = $sn; + $reqcopy->{_xcatpreprocessed}->[0] = 1; + push @requests, $reqcopy; + } + + return \@requests; } } - - return undef; + + return undef; } #---------------------------------------------------------------------------- @@ -127,18 +136,10 @@ sub preprocess_request Check for xCAT command and call the appropriate subroutine. - Arguments: - Returns: 0 - OK 1 - error - Globals: - - Error: - Example: - - Comments: =cut #----------------------------------------------------------------------------- @@ -154,9 +155,7 @@ sub process_request my $rsp; # globals used by all subroutines. - $::command = $::request->{command}->[0]; $::args = $::request->{arg}; - $::filedata = $::request->{stdin}->[0]; ($ret, $msg) = &x2n($callback); if ($ret > 0) { @@ -187,16 +186,13 @@ sub prexcat2nim { my $callback = shift; - $::callback = $callback; - - my @nodelist; # pass back list of nodes - - $::msgstr = ""; + my @nodelist=(); # pass back list of nodes - if applicable + my @servicenodes=(); # pass back list of service nodes - if applicable if (defined(@{$::args})) { @ARGV = @{$::args}; } else { - &xcat2nim_usage; + &xcat2nim_usage($callback); return 1; } @@ -209,25 +205,27 @@ sub prexcat2nim 'list|l' => \$::opt_l, 'update|u' => \$::opt_u, 'remove|r' => \$::opt_r, + 'managementnode|M' => \$::MN, 'o=s' => \$::opt_o, 't=s' => \$::opt_t, + 's=s' => \$::SERVERS, 'verbose|V' => \$::opt_V, 'version|v' => \$::opt_v, ) ) { - &xcat2nim_usage; + &xcat2nim_usage($callback); return 1; } # Option -h for Help if ($::opt_h ) { - &xcat2nim_usage; + &xcat2nim_usage($callback); return 2; } - # Option -v for version - do we need this??? + # Option -v for version if ($::opt_v) { my $rsp; @@ -237,23 +235,17 @@ sub prexcat2nim return 2; # - just exit } -######## TODO ################## -# this needs to be cleaned up!!!!! -# too much duplicate code being called ... -################################# - # process the command line - my $rc = &processArgs; + my $rc = &processArgs($callback); + # figure out what nodes are involved - if any + # - so we can send the request to the correct service nodes my %objhash = xCAT::DBobjUtils->getobjdefs(\%::objtype); - - # need to figure out what nodes are involved so - # we can create the node/group defs on the - # correct service nodes + my $donet = 0; foreach my $o (@::clobjnames) { if ($::objtype{$o} eq 'node') { push (@nodelist, $o); - } + } if ($::objtype{$o} eq 'group') { my $memberlist = xCAT::DBobjUtils->getGroupMembers($o, \%objhash); my @members = split(',', $memberlist); @@ -263,22 +255,53 @@ sub prexcat2nim } } } + if ($::objtype{$o} eq 'network') { + if ($::FORCE || $::opt_u || $::opt_r) { + my $rsp; + $rsp->{data}->[0] = "The -f, -r and -u options are not supported for network objects.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + &xcat2nim_usage($callback); + return 2; + } + $donet = 1; + } + } + + # NIM network defs need to be created on the NIM primary + my $nimprime = xCAT::InstUtils->getnimprime(); + chomp $nimprime; + @servicenodes=($nimprime); + +if(0) { # only do networks on the management node (NIM primary) for now + if ($donet) { + if ($::MN) { + # only do management node + @servicenodes=("$nimprime"); + } elsif ($::SERVERS) { + @servicenodes=split(',', $::SERVERS); + } else { + # do MN and all servers + @servicenodes=xCAT::Utils->getAllSN(); + push(@servicenodes, $nimprime); + } } - # make sure the nodes are resolvable - # - if not then exit - foreach my $n (@nodelist) { - my $packed_ip = gethostbyname($n); - if (!$packed_ip) { - my $rsp; - $rsp->{data}->[0] = "Could not resolve node $n.\n"; - xCAT::MsgUtils->message("E", $rsp, $callback); - return 1; + if (scalar(@nodelist) ) { + # make sure the nodes are resolvable + # - if not then exit + foreach my $n (@nodelist) { + my $packed_ip = gethostbyname($n); + if (!$packed_ip) { + my $rsp; + $rsp->{data}->[0] = "Could not resolve node $n.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + } + } +} - } - } - - return (0, \@nodelist); + return (0, \@nodelist, \@servicenodes); } #---------------------------------------------------------------------------- @@ -308,6 +331,8 @@ sub prexcat2nim sub processArgs { + my $callback = shift; + my $gotattrs = 0; if (defined(@{$::args})) { @@ -317,6 +342,8 @@ sub processArgs } my %ObjTypeHash; + @::clobjnames = (); + @::clobjtypes = (); # parse the options Getopt::Long::Configure("no_pass_through"); @@ -328,7 +355,9 @@ sub processArgs 'list|l' => \$::opt_l, 'update|u' => \$::opt_u, 'remove|r' => \$::opt_r, + 'managementnode|M' => \$::MN, 'o=s' => \$::opt_o, + 's=s' => \$::SERVERS, 't=s' => \$::opt_t, 'verbose|V' => \$::opt_V, 'version|v' => \$::opt_v, @@ -385,7 +414,7 @@ sub processArgs { my $rsp; $rsp->{data}->[0] = "Incorrect \'attr=val\' pair - $a\n"; - xCAT::MsgUtils->message("E", $rsp, $::callback); + xCAT::MsgUtils->message("E", $rsp, $callback); return 3; } @@ -421,8 +450,8 @@ sub processArgs { my $rsp; $rsp->{data}->[0] = - "$::msgstr Cannot combine multiple types with \'att=val\' pairs on the command line.\n"; - xCAT::MsgUtils->message("E", $rsp, $::callback); + "Cannot combine multiple types with \'att=val\' pairs on the command line.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); return 3; } else @@ -450,7 +479,7 @@ sub processArgs $rsp->{data}->[0] = "$::msgstr Type \'$t\' is not a valid xCAT object type.\n"; $rsp->{data}->[1] = "Skipping to the next type.\n"; - xCAT::MsgUtils->message("I", $rsp, $::callback); + xCAT::MsgUtils->message("I", $rsp, $callback); } else { @@ -466,9 +495,9 @@ sub processArgs # make the default type = 'node' if not specified push(@::clobjtypes, 'node'); - my $rsp; - $rsp->{data}->[0] = "$::msgstr Assuming an object type of \'node\'.\n"; - xCAT::MsgUtils->message("I", $rsp, $::callback); + #my $rsp; + #$rsp->{data}->[0] = "Assuming an object type of \'node\'.\n"; + #xCAT::MsgUtils->message("I", $rsp, $callback); } # @@ -476,7 +505,7 @@ sub processArgs # # - get object names from the -o option or the noderange - # - this assumes the ronderange was not provided as an arg!!! + # - this assumes the noderange was not provided as an arg!!! if ($::opt_o) { @@ -492,6 +521,11 @@ sub processArgs push(@::clobjnames, $::opt_o); } + if (!$::opt_t || ($::opt_t eq 'node')) { + # if the type is "node" + @::noderange = &noderange($::opt_o, 0); + @::clobjnames = @::noderange; + } } # if there is no opt_o & no noderange then try to find all the objects @@ -517,9 +551,9 @@ sub processArgs { my $rsp; $rsp->{data}->[0] = - "$::msgstr Could not get objects of type \'$t\'.\n"; + "Could not get objects of type \'$t\'.\n"; $rsp->{data}->[1] = "Skipping to the next type.\n"; - xCAT::MsgUtils->message("I", $rsp, $::callback); + xCAT::MsgUtils->message("I", $rsp, $callback); next; } @@ -540,8 +574,8 @@ sub processArgs my $rsp; $rsp->{data}->[0] = - "$::msgstr Cannot use \'-a\' with \'-o\' or a noderange.\n"; - xCAT::MsgUtils->message("E", $rsp, $::callback); + "Cannot use \'-a\' with \'-o\' or a noderange.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); return 3; } @@ -556,9 +590,7 @@ sub processArgs # for every type of data object get the list of defined objects foreach my $t (keys %{xCAT::Schema::defspec}) { - - # just for node and group for now!!!!!!! - if ( ($t eq 'node') || ($t eq 'group') ) { + if ( ($t eq 'node') || ($t eq 'group') || ($t eq 'network')) { my @tmplist; @tmplist = xCAT::DBobjUtils->getObjectsOfType($t); @@ -596,7 +628,7 @@ sub processArgs } elsif ($::objectsfrom_args || $::objectsfrom_opto) { # from the opt_o or as an argument # - there can only be one type - $::objtype{$o}=@::clobjtypes[0]; + $::objtype{$o}=$::clobjtypes[0]; } } @@ -631,25 +663,21 @@ sub x2n my $rc = 0; my $error = 0; - # get the local short host name - ($::local_host = hostname()) =~ s/\..*$//; - chomp $::local_host; + # get this systems name as known by xCAT management node + $::Sname = xCAT::InstUtils->myxCATname(); + chomp $::Sname; - if (xCAT::Utils->isMN()){ - $::msgstr = ""; - } else { - $::msgstr = "$::local_host: "; - } + $::msgstr = "$::Sname: "; # process the command line - $rc = &processArgs; + $rc = &processArgs($callback); if ($rc != 0) { # rc: 0 - ok, 1 - return, 2 - help, 3 - error if ($rc != 1) { - &xcat2nim_usage; + &xcat2nim_usage($callback); } return ($rc - 1); } @@ -659,101 +687,119 @@ sub x2n if (!defined(%::objhash)) { my $rsp; - $rsp->{data}->[0] = "$::msgstr Could not get xCAT object definitions.\n"; - xCAT::MsgUtils->message("E", $rsp, $::callback); + $rsp->{data}->[0] = "Could not get xCAT object definitions.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); return 1; } - # create NIM defs for each xCAT def - foreach my $objname (keys %::objhash) - { + my @nodes; + my @networks; + my @groups; + foreach my $obj (keys %::objhash) { + if ($::objtype{$obj} eq 'node') { + push(@nodes, $obj); + } elsif ($::objtype{$obj} eq 'network') { + push(@networks, $obj); + } elsif ($::objtype{$obj} eq 'group') { + push(@groups, $obj); + } + } - # list the NIM object definition - if there is one - if ($::opt_l || $::opt_r) { - if (&rm_or_list_nim_object($objname, $::objtype{$objname})) { - # the routine failed - $error++; - next; - } - } else { + # NIM machine definitions + if (scalar(@nodes)) { + foreach my $objname (@nodes) { - # create a NIM machine definition - if ($::objtype{$objname} eq 'node') { - # need to set group type to either static or dynamic + if ($::opt_l || $::opt_r) { + + if (&rm_or_list_nim_object($objname, $::objtype{$objname}, $callback)) { + # the routine failed + $error++; + } + } else { if (mkclientdef($objname, $callback)) { - # could not create client definition + # could not create client definition $error++; - } - next; + } } + next; + } + } - # create a NIM group definition - if ($::objtype{$objname} eq 'group') { - $::objhash{$objname}{'grouptype'}='static'; - my $grptab = xCAT::Table->new('nodegroup'); - my @grplist = @{$grptab->getAllEntries()}; #dynamic groups and static groups in nodegroup table - foreach my $grpdef_ref (@grplist) { - my %grpdef = %$grpdef_ref; - if (($grpdef{'groupname'} eq $objname) && ($grpdef{'grouptype'} eq 'dynamic')) { - $::objhash{$objname}{'grouptype'}='dynamic'; - last; - } - } - $grptab->close; + # NIM network definitions + if (scalar(@networks) ){ + if ($::opt_l) { + # list network def + if (&listNIMnetwork($callback, \@networks, \%::objhash) ) { + # could not create client definition + $error++; + } + } elsif ($::opt_r) { + # remove network def + + #if (&rmNIMnetwork($callback, \@networks) ) { + # # could not create client definition + # $error++; + #} - if (mkgrpdef($objname, $callback)) { + my $rsp; + push @{$rsp->{data}}, "$::msgstr The remove option is not supported for NIM network definitions.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + $error++; + + } else { + if (&mkNIMnetwork($callback, \@networks, \%::objhash)) { + # could not create client definition + $error++; + } + } + } + + # NIM group definitions + if (scalar(@groups) ) { + foreach my $objname (@groups) { + + # make sure grouptype is set ??? + $::objhash{$objname}{'grouptype'}='static'; + my $grptab = xCAT::Table->new('nodegroup'); + #dynamic groups and static groups in nodegroup table + my @grplist = @{$grptab->getAllEntries()}; + foreach my $grpdef_ref (@grplist) { + my %grpdef = %$grpdef_ref; + if (($grpdef{'groupname'} eq $objname) && ($grpdef{'grouptype'} eq 'dynamic')) { + $::objhash{$objname}{'grouptype'}='dynamic'; + last; + } + } + $grptab->close; + + if ($::opt_l || $::opt_r) { + if (&rm_or_list_nim_object($objname, $::objtype{$objname}, $callback)) { + # the routine failed + $error++; + } + } else { + if (&mkgrpdef($objname, $callback)) { # could not create group definition $error++; } - next; } + next; } - } + } if ($error) { my $rsp; $rsp->{data}->[0] = "$::msgstr One or more errors occured.\n"; - xCAT::MsgUtils->message("E", $rsp, $::callback); + xCAT::MsgUtils->message("E", $rsp, $callback); return 1; } else { - if ($::verbose) - { - - # give results - my $rsp; - if ($::opt_r) { - $rsp->{data}->[0] = "$::msgstr The following definitions were removed:"; - } elsif ($::opt_u) { - $rsp->{data}->[0] = "$::msgstr The following definitions were updated:"; - } elsif (!$::opt_l) { - $rsp->{data}->[0] = "$::msgstr The following definitions were created:"; - } - - xCAT::MsgUtils->message("I", $rsp, $::callback); - - my $n = 1; - foreach my $o (sort(keys %::objhash)) - { - $rsp->{data}->[$n] = "$o\n"; - $n++; - } - xCAT::MsgUtils->message("I", $rsp, $::callback); - } - else - { - if (!$::opt_l) { - my $rsp; - $rsp->{data}->[0] = - "$::local_host: NIM operations have completed successfully.\n"; - xCAT::MsgUtils->message("I", $rsp, $::callback); - } - } return 0; } + return 0; } #---------------------------------------------------------------------------- @@ -795,7 +841,7 @@ sub mkclientdef # check to see if the client is already defined # - sets $::client_exists - if (&check_nim_client($node, $::local_host)) { + if (&check_nim_client($node, $::Sname, $callback)) { # the routine failed return 1; } @@ -823,7 +869,7 @@ sub mkclientdef } else { # no force my $rsp; $rsp->{data}->[0] = "$::msgstr The NIM client machine \'$shorthost\' already exists. Use the \'-f\' option to remove and recreate or the \'-u\' option to update an existing definition.\n"; - xCAT::MsgUtils->message("I", $rsp, $::callback); + xCAT::MsgUtils->message("I", $rsp, $callback); return 1; } @@ -869,7 +915,7 @@ sub mkclientdef { my $rsp; $rsp->{data}->[0] = "$::msgstr Missing the MAC for node \'$node\'.\n"; - xCAT::MsgUtils->message("E", $rsp, $::callback); + xCAT::MsgUtils->message("E", $rsp, $callback); return 1; } else { $::objhash{$node}{'mac'} =~ s/://g; @@ -898,7 +944,6 @@ sub mkclientdef $cmd = "nim -o define $nim_type $nim_args $shorthost"; } - # may need to use dsh if it is a remote server my $nimcmd; $nimcmd = qq~$cmd 2>&1~; @@ -912,7 +957,7 @@ sub mkclientdef { $rsp->{data}->[1] = "$output"; } - xCAT::MsgUtils->message("E", $rsp, $::callback); + xCAT::MsgUtils->message("E", $rsp, $callback); return 1; } @@ -945,100 +990,106 @@ sub mkgrpdef my $group = shift; my $callback = shift; - my $cmd = undef; - my $servnode = undef; - my $GrpSN = undef; + # get members of xCAT group + my $memberlist = xCAT::DBobjUtils->getGroupMembers($group, \%::objhash); + my @members = split(',', $memberlist); - # get members and determine all the different masters - # For example, the xCAT group "all" will have nodes that are managed - # by multiple NIM masters - so we will create a local group "all" - # on each of those masters - my %ServerList = &getMasterGroupLists($group); - - foreach my $servname (keys %ServerList) + # get list of nim groups defined locally + $cmd = qq~lsnim -c groups | cut -f1 -d' ' 2>/dev/null~; + my @GroupList = xCAT::Utils->runcmd("$cmd", -1); + if ($::RUNCMD_RC != 0) { - - # get the members for the group def on this system - my @members = @{$ServerList{$servname}}; - - # check to see if the group is already defined - sets $::grp_exists - if (&check_nim_group($group, $servname)) { - # the routine failed - return 1; - } - - # don't update an existing def unless we're told - if ($::grp_exists && !$::opt_u) { - if ($::FORCE) { - # get rid of the old definition - - # ???? - does remove alone do the deallocate?? - my $rmcmd = "/usr/sbin/nim -Fo remove $group"; - my $output = xCAT::Utils->runcmd("$rmcmd", -1); - if ($::RUNCMD_RC != 0) { - my $rsp; - push @{$rsp->{data}}, "$::msgstr Could not remove the existing NIM group named \'$group\'.\n"; - if ($::VERBOSE) { - push @{$rsp->{data}}, "$output"; - } - xCAT::MsgUtils->message("E", $rsp, $callback); - return 1; - } - - } else { # no force - my $rsp; - $rsp->{data}->[0] = "$::msgstr The NIM group \'$group\' already exists. Use the \'-f\' option to remove and recreate or the \'-u\' option to update an existing definition.\n"; - xCAT::MsgUtils->message("I", $rsp, $::callback); - return 1; - } - } - - # either create or update the group def on this master - - # any list with more than 1024 members is an error - # - NIM can't handle that - if ($#members > 1024) { - my $rsp; - $rsp->{data}->[0] = "$::msgstr Cannot create a NIM group definition with more than 1024 members - on \'$servname\'."; - xCAT::MsgUtils->message("E", $rsp, $::callback); - next; - } - - # - # The list may become quite long and not fit on one cmd line - # so we do it one at a time for now - need to revisit this - # (like do blocks at a time) - TODO - # - my $justadd=0; # after the first define we just need to add - foreach my $memb (@members) { - - my $shorthost; - ($shorthost = $memb) =~ s/\..*$//; - - # do we change or create - my $cmd; - if (($::grp_exists && $::opt_u) || $justadd) { - $cmd = "nim -o change -a add_member=$shorthost $group 2>&1"; - } else { - $cmd = "nim -o define -t mac_group -a add_member=$shorthost $group 2>&1"; - $justadd++; - } - - my $output = xCAT::Utils->runcmd("$cmd", -1); - if ($::RUNCMD_RC != 0) - { - my $rsp; - $rsp->{data}->[0] = "$::msgstr Could not create a NIM definition for \'$group\'.\n"; - if ($::verbose) - { - $rsp->{data}->[1] = "$output"; - } - xCAT::MsgUtils->message("E", $rsp, $::callback); - return 1; - } - } + my $rsp; + $rsp->{data}->[0] = "$::msgstr Could not get a list of NIM group definitions.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; } + # get list of nim machine defined locally + $mcmd = qq~lsnim -c machines | cut -f1 -d' ' 2>/dev/null~; + my @nimnodes = xCAT::Utils->runcmd("$mcmd", -1); + if ($::RUNCMD_RC != 0) + { + my $rsp; + $rsp->{data}->[0] = "$::msgstr Could not get a list of NIM group definit +ions.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + + # don't update an existing def unless we're told + if ( (grep(/$group/, @GroupList)) && !$::opt_u) { + if ($::FORCE) { + # get rid of the old definition + my $rmcmd = "/usr/sbin/nim -Fo remove $group"; + my $output = xCAT::Utils->runcmd("$rmcmd", -1); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "$::msgstr Could not remove the existing NIM group named \'$group\'.\n"; + if ($::VERBOSE) { + push @{$rsp->{data}}, "$output"; + } + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + + } else { # no force + my $rsp; + $rsp->{data}->[0] = "$::msgstr The NIM group \'$group\' already exists. Use the \'-f\' option to remove and recreate or the \'-u\' option to update an existing definition.\n"; + xCAT::MsgUtils->message("I", $rsp, $callback); + return 1; + } + } + + # either create or update the group def on this master + + # any list with more than 1024 members is an error + # - NIM can't handle that + if ($#members > 1024) { + my $rsp; + $rsp->{data}->[0] = "$::msgstr Cannot create a NIM group definition with more than 1024 members - on \'$servname\'."; + xCAT::MsgUtils->message("E", $rsp, $callback); + next; + } + + # + # The list may become quite long and not fit on one cmd line + # so we do it one at a time for now - need to revisit this + # (like do blocks at a time) - TODO + # + my $justadd=0; # after the first define we just need to add + foreach my $memb (@members) { + + # if this node is not defined locally then don't add it to the list + if (!grep(/$memb/, @nimnodes)) { + next; + } + + my $shorthost; + ($shorthost = $memb) =~ s/\..*$//; + + # do we change or create + my $cmd; + if (((grep(/$group/, @GroupList)) && $::opt_u) || $justadd) { + $cmd = "nim -o change -a add_member=$shorthost $group 2>&1"; + } else { + $cmd = "nim -o define -t mac_group -a add_member=$shorthost $group 2>&1"; + $justadd++; + } + + my $output = xCAT::Utils->runcmd("$cmd", -1); + if ($::RUNCMD_RC != 0) + { + my $rsp; + $rsp->{data}->[0] = "$::msgstr Could not create a NIM definition for \'$group\'.\n"; + if ($::verbose) + { + $rsp->{data}->[1] = "$output"; + } + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + } return 0; } @@ -1048,7 +1099,7 @@ sub mkgrpdef List a NIM object definition. - Arguments: + Argument: Returns: 0 - OK 1 - error @@ -1065,7 +1116,7 @@ sub mkgrpdef sub rm_or_list_nim_object { - my ($object, $type) = @_; + my ($object, $type, $callback) = @_; if ($type eq 'node') { @@ -1073,7 +1124,7 @@ sub rm_or_list_nim_object my $cmd; $cmd = qq~lsnim -l $object 2>/dev/null~; - + my $outref = xCAT::Utils->runcmd("$cmd", -1); if ($::RUNCMD_RC != 0) { @@ -1083,16 +1134,14 @@ sub rm_or_list_nim_object { $rsp->{data}->[1] = "$outref"; } - xCAT::MsgUtils->message("E", $rsp, $::callback); + xCAT::MsgUtils->message("E", $rsp, $callback); return 1; } else { # display to NIM output my $rsp; - # $rsp->{data}->[0] = "NIM master: $nim_master"; - # $rsp->{data}->[1] = "Client name: $object"; $rsp->{data}->[0] = "$outref"; - xCAT::MsgUtils->message("I", $rsp, $::callback); + xCAT::MsgUtils->message("I", $rsp, $callback); return 0; } @@ -1115,7 +1164,7 @@ sub rm_or_list_nim_object { $rsp->{data}->[1] = "$outref"; } - xCAT::MsgUtils->message("E", $rsp, $::callback); + xCAT::MsgUtils->message("E", $rsp, $callback); return 1; } } @@ -1123,61 +1172,45 @@ sub rm_or_list_nim_object if ($type eq 'group') { - # get members and determine all the different masters - my %servgroups = &getMasterGroupLists($object); - - # get the group definition from each master and - # display it - foreach my $servname (keys %servgroups) - { - - # only handle the defs for the local SN - my $shortsn; - ($shortsn = $servname) =~ s/\..*$//; - if ($shortsn ne $::local_host) { - next; - } - - my $cmd; - if ($::opt_l) { - $cmd = qq~lsnim -l $object 2>/dev/null~; + my $cmd; + if ($::opt_l) { + $cmd = qq~lsnim -l $object 2>/dev/null~; - my $outref = xCAT::Utils->runcmd("$cmd", -1); - if ($::RUNCMD_RC != 0) - { - my $rsp; - $rsp->{data}->[0] = "$::msgstr Could not list the NIM definition for \'$object\'.\n"; - if ($::verbose) - { - $rsp->{data}->[1] = "$outref"; - } - xCAT::MsgUtils->message("E", $rsp, $::callback); - return 1; - } else { - - # display NIM output - my $rsp; - $rsp->{data}->[0] = "$outref"; - xCAT::MsgUtils->message("I", $rsp, $::callback); - return 0; - } - } elsif ($::opt_r) { - $cmd = qq~nim -Fo remove $object 2>/dev/null~; - - my $outref = xCAT::Utils->runcmd("$cmd", -1); - if ($::RUNCMD_RC != 0) - { - my $rsp; - $rsp->{data}->[0] = "$::msgstr Could not remove the NIM definition for \'$object\'.\n"; - if ($::verbose) - { - $rsp->{data}->[1] = "$outref"; - } - xCAT::MsgUtils->message("E", $rsp, $::callback); - return 1; + my $outref = xCAT::Utils->runcmd("$cmd", -1); + if ($::RUNCMD_RC != 0) + { + my $rsp; + $rsp->{data}->[0] = "$::msgstr Could not list the NIM definition for \'$object\'.\n"; + if ($::verbose) + { + $rsp->{data}->[1] = "$outref"; } - } - } + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } else { + + # display NIM output + my $rsp; + $rsp->{data}->[0] = "$outref"; + xCAT::MsgUtils->message("I", $rsp, $callback); + return 0; + } + } elsif ($::opt_r) { + $cmd = qq~nim -Fo remove $object 2>/dev/null~; + + my $outref = xCAT::Utils->runcmd("$cmd", -1); + if ($::RUNCMD_RC != 0) + { + my $rsp; + $rsp->{data}->[0] = "$::msgstr Could not remove the NIM definition for \'$object\'.\n"; + if ($::verbose) + { + $rsp->{data}->[1] = "$outref"; + } + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + } } return 0; } @@ -1233,152 +1266,6 @@ sub getNIMmaster #---------------------------------------------------------------------------- -=head3 getMasterGroupLists - - Get a hash of all the masters that have a certain group defined - and a list of members for each masters definition. - - Arguments: - Returns: - server group list hash - undef - error - Globals: - - Error: - - Example: - - Comments: -=cut - -#----------------------------------------------------------------------------- - -sub getMasterGroupLists -{ - my ($group) = @_; - - my $NimMaster; - my $thismaster; - my %membhash; - my %memberhash; - my %ServerList; - - # get the members list - my $memberlist = xCAT::DBobjUtils->getGroupMembers($group, \%::objhash); - my @members = split(',', $memberlist); - - # get the node(member) definitions - if (@members) { - foreach my $n (@members) { - $membhash{$n} = 'node'; - } - %memberhash = xCAT::DBobjUtils->getobjdefs(\%membhash); - } else { - my $rsp; - $rsp->{data}->[0] = "$::msgstr Could not get members of the xCAT group \'$group\'.\n"; - xCAT::MsgUtils->message("E", $rsp, $::callback); - return undef; - } - - if ( defined(%memberhash)) { - # sort the list by server node - one list per server - foreach my $m (@members) { - - $NimMaster = hostname(); - - if ($memberhash{$m}{nimmaster}) { - $NimMaster = $memberhash{$m}{nimmaster}; - } - - if ($memberhash{$m}{servicenode}) { - # the servicenode attr is set for this node - $NimMaster = $memberhash{$m}{servicenode}; - } - - if ($memberhash{$m}{xcatmaster}) { - $NimMaster = $memberhash{$m}{xcatmaster}; - } - - if ($NimMaster =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/) { - # convert IP to hostname - my $packedaddr = inet_aton($NimMaster); - my $hostname = gethostbyaddr($packedaddr, AF_INET); - $NimMaster = $hostname; - } - - # assume short hostnames for now??? - ($thismaster = $NimMaster) =~ s/\..*$//; - - push(@{$ServerList{$thismaster}}, $m); - } - - } else { - # could not get node def - my $rsp; - $rsp->{data}->[0] = "$::msgstr Could not get xCAT node definition for all members of the xCAT group \'$group\'.\n"; - xCAT::MsgUtils->message("E", $rsp, $::callback); - return undef; - } - - return %ServerList; -} - -#---------------------------------------------------------------------------- - -=head3 check_nim_group - - See if an xCAT group has already been defined as a NIM machine group. - - Arguments: - Returns: - 0 - OK - 1 - error - Globals: - - Error: - - Example: - - Comments: -=cut - -#----------------------------------------------------------------------------- - -sub check_nim_group -{ - my ($group, $servnode) = @_; - my ($cmd, @output); - - my @GroupList; - - chomp $::local_host; - chomp $servnode; - - if ( $::NIMGroupList{$servnode}) { - @GroupList = @{$::NIMGroupList{$servnode}}; - } else { - $cmd = qq~lsnim -c groups | cut -f1 -d' ' 2>/dev/null~; - - @GroupList = xCAT::Utils->runcmd("$cmd", -1); - if ($::RUNCMD_RC != 0) - { - my $rsp; - $rsp->{data}->[0] = "$::msgstr Could not get a list of NIM group definitions.\n"; - xCAT::MsgUtils->message("E", $rsp, $::callback); - return 1; - } - # save member list for each server - @{$::NIMGroupList{$servnode}} = @GroupList; - } - - - $::grp_exists = grep(/^$group$/,@GroupList) ? 1 : 0; - - return 0; -} - -#---------------------------------------------------------------------------- - =head3 check_nim_client See if an xCAT node has already been defined as a NIM client. @@ -1400,24 +1287,17 @@ sub check_nim_group sub check_nim_client { - my ($node, $servnode) = @_; + my ($node, $servnode, $callback) = @_; my ($cmd, @ClientList); - if ( $::NIMclientList{$servnode}) { - @ClientList = @{$::NIMclientList{$servnode}}; - } else { - $cmd = qq~lsnim -c machines | cut -f1 -d' ' 2>/dev/null~; - - @ClientList = xCAT::Utils->runcmd("$cmd", -1); - if ($::RUNCMD_RC != 0) - { - my $rsp; - $rsp->{data}->[0] = "$::msgstr Could not get a list of NIM client definitions from \'$servnode\'.\n"; - xCAT::MsgUtils->message("E", $rsp, $::callback); - return 1; - } - # save member list for each server - @{$::NIMclientList{$servnode}} = @ClientList; + $cmd = qq~lsnim -c machines | cut -f1 -d' ' 2>/dev/null~; + @ClientList = xCAT::Utils->runcmd("$cmd", -1); + if ($::RUNCMD_RC != 0) + { + my $rsp; + $rsp->{data}->[0] = "$::msgstr Could not get a list of NIM client definitions from \'$servnode\'.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; } $::client_exists = grep(/^$node$/,@ClientList) ? 1 : 0; @@ -1425,6 +1305,8 @@ sub check_nim_client return 0; } + + #---------------------------------------------------------------------------- =head3 xcat2nim_usage @@ -1444,19 +1326,423 @@ sub check_nim_client sub xcat2nim_usage { + my $callback = shift; my $rsp; - $rsp->{data}->[0] = - "\nUsage: xcat2nim - Use this command to create and manage AIX NIM definitions based on xCAT object definitions.\n"; - $rsp->{data}->[1] = " xcat2nim [-h|--help ]\n"; - $rsp->{data}->[2] = - " xcat2nim [-V|--verbose] [-a|--all] [-l|--list] [-u|--update] "; - - $rsp->{data}->[3] =" [-f|--force] [-r|--remove] [-t object-types] [-o object-names]"; - $rsp->{data}->[4] = - " [noderange] [attr=val [attr=val...]]\n"; - xCAT::MsgUtils->message("I", $rsp, $::callback); + push @{$rsp->{data}}, "\nUsage: xcat2nim - Use this command to create and manage AIX NIM definitions based on xCAT object definitions.\n"; + push @{$rsp->{data}}, " xcat2nim [-h|--help ]\n"; + push @{$rsp->{data}}, " xcat2nim [-V|--verbose] [-l|--list] [-r|--remove] [-u|--update]\n [-f|--force] [-t object-types] [-o object-names] [-a|--allobjects]\n [noderange] [attr=val [attr=val...]]\n"; + xCAT::MsgUtils->message("I", $rsp, $callback); return 0; } +#------------------------------------------------------------------------------- + +=head3 mkNIMnetwork + Create NIM network definitions corresponding to xCAT network + definitions. This routine runs on the NIMprime and AIX SNs + + Arguments: + + Returns: + + + Comments: + + ex. &mkNIMnetwork($callback, \@networks, \%nethash); + +=cut + +#------------------------------------------------------------------------------- +sub mkNIMnetwork +{ + my $callback = shift; + my $xnets = shift; + my $xnhash = shift; + + my @xnetworks = @{$xnets}; + my %xnethash; # hash of xCAT network definitions + if ($xnhash) { + %xnethash = %{$xnhash}; + } + + # + # get all the nim network names and attrs defined on this server + # + my $cmd = qq~/usr/sbin/lsnim -c networks | /usr/bin/cut -f1 -d' ' 2>/dev/null~; + my @networks = xCAT::Utils->runcmd("$cmd", -1); + + # for each NIM network - get the attrs + my %NIMnets; + foreach my $netwk (@networks) { + my $cmd = qq~/usr/sbin/lsnim -Z -a net_addr -a snm $netwk 2>/dev/null~; + my @result = xCAT::Utils->runcmd("$cmd", -1); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "$::msgstr Could not run lsnim command: \'$cmd\'.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + + foreach my $l (@result){ + # skip comment lines + next if ($l =~ /^\s*#/); + + my ($nimname, $net_addr, $snm) = split(':', $l); + $NIMnets{$netwk}{'net_addr'} = $net_addr; + $NIMnets{$netwk}{'snm'} = $snm; + } + } + + # + # for each xCAT network - see if the net we need is defined + # + foreach my $net (@xnetworks) { + + # see if it's already defined - or equivalent is defined + # split mask + my ($nm1, $nm2, $nm3, $nm4) = split('\.', $xnethash{$net}{mask}); + + # split net addr + my ($nn1, $nn2, $nn3, $nn4) = split('\.', $xnethash{$net}{net}); + + # foreach nim network name + foreach my $netwk (@networks) { + + # split definition mask + my ($dm1, $dm2, $dm3, $dm4) = split('\.', $NIMnets{$netwk}{'snm'}); + + # split definition net addr + my ($dn1, $dn2, $dn3, $dn4) = split('\.', $NIMnets{$netwk}{'net_addr'}); + # check for the same netmask and network address + if ( ($nn1 == $dn1) && ($nn2 ==$dn2) && ($nn3 == $dn3) && ($nn4 == $dn4) ) { + if ( ($nm1 == $dm1) && ($nm2 ==$dm2) && ($nm3 == $dm3) && ($nm4 == $dm4) ) { + $foundmatch=1; + } + } + } + + # if not defined then define it! + if (!$foundmatch) { + + # create new nim network def + # use the same network name as xCAT uses + my $cmd; + $cmd = qq~/usr/sbin/nim -o define -t ent -a net_addr=$xnethash{$net}{net} -a snm=$xnethash{$net}{mask} -a routing1='default $xnethash{$net}{gateway}' $net 2>/dev/null~; + + my $output1 = xCAT::Utils->runcmd("$cmd", -1); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "$::msgstr Could not run \'$cmd\'.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + + # + # create an interface def (if*) for the master + # + # first get the if* and cable_type* attrs + # - the -A option gets the next avail index for this attr + my $ifcmd = qq~/usr/sbin/lsnim -A if master 2>/dev/null~; + my $ifindex = xCAT::Utils->runcmd("$ifcmd", -1); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "Could not run \'$ifcmd\'.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + + my $ctcmd = qq~/usr/sbin/lsnim -A cable_type master 2>/dev/null~; + my $ctindex = xCAT::Utils->runcmd("$ctcmd", -1); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "$::msgstr Could not run \'$ctcmd\'.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + + # get the local adapter hostname for this network + # get all the possible IPs for the node I'm running on + my $ifgcmd = "ifconfig -a | grep 'inet '"; + my @result = xCAT::Utils->runcmd($ifgcmd, 0); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "$::msgstr Could not run \'$ifgcmd\'.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + + my $adapterhostname; + foreach my $int (@result) { + my ($inet, $myIP, $str) = split(" ", $int); + chomp $myIP; + + # split interface IP + my ($h1, $h2, $h3, $h4) = split('\.', $myIP); + + # split mask + my ($m1, $m2, $m3, $m4) = split('\.', $xnethash{$net}{mask}); + + # split net address + my ($n1, $n2, $n3, $n4) = split('\.', $xnethash{$net}{net}); + + # AND this interface IP with the netmask of the network + my $a1 = ((int $h1) & (int $m1)); + my $a2 = ((int $h2) & (int $m2)); + my $a3 = ((int $h3) & (int $m3)); + my $a4 = ((int $h4) & (int $m4)); + + # if all the octals match the network addr then we have + # the right interface + if ( ($n1 == $a1) && ($n2 ==$a2) && ($n3 == $a3) && ($n4 == $a4) ) { + my $packedaddr = inet_aton($myIP); + $adapterhostname = gethostbyaddr($packedaddr, AF_INET); + last; + } + } + + # define the new interface + my $chcmd = qq~/usr/sbin/nim -o change -a if$ifindex='$net $adapterhostname 0' -a cable_type$ctindex=N/A master 2>/dev/null~; + + my $output2 = xCAT::Utils->runcmd("$chcmd", -1); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "$::msgstr Could not run \'$chcmd\'.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + + # get the next index for the routing attr + my $ncmd = qq~/usr/sbin/lsnim -A routing master_net 2>/dev/null~; + my $rtindex = xCAT::Utils->runcmd("$ncmd", -1); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "$::msgstr Could not run \'$ncmd\'.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + + # get hostname of primary int - always if1 + my $hncmd = qq~/usr/sbin/lsnim -a if1 -Z master 2>/dev/null~; + my @ifone = xCAT::Utils->runcmd("$hncmd", -1); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "$::msgstr Could not run \'$hncmd\'.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + my ($junk1, $junk2, $adapterhost); + foreach my $l (@ifone){ + # skip comment lines + next if ($l =~ /^\s*#/); + ($junk1, $junk2, $adapterhost) = split(':', $l); + + } + + # get the ip of the nim primary interface + my $gwIP = inet_ntoa(inet_aton($adapterhost)); + chomp $gwIP; + + # create static routes between the networks + my $rtgcmd = qq~/usr/sbin/nim -o change -a routing$rtindex='master_net $gwIP $xnethash{$net}{gateway}' $net 2>/dev/null~; + my $output3 = xCAT::Utils->runcmd("$rtgcmd", -1); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "$::msgstr Could not run \'$rtgcmd\'.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + + } # end - define new nim network + + } # end - for each network + return 0; +} + +#------------------------------------------------------------------------------- + +=head3 listNIMnetwork + List NIM network definitions corresponding to xCAT network + definitions. This routine runs on the NIMprime and AIX SNs + + Arguments: + + Returns: + + Comments: + + ex. &listNIMnetwork($callback, \@networks, \%nethash); + +=cut + +#------------------------------------------------------------------------------- +sub listNIMnetwork +{ + my $callback = shift; + my $xnets = shift; + my $xnhash = shift; + + my %xnethash; # hash of xCAT network definitions + if ($xnhash) { + %xnethash = %{$xnhash}; + } + + my @xcatnetworks = @{$xnets}; + + # + # get all the nim network names + # + my $cmd = qq~/usr/sbin/lsnim -c networks | /usr/bin/cut -f1 -d' ' 2>/dev/null~; + my @nimnetworks = xCAT::Utils->runcmd("$cmd", -1); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "$::msgstr Could not run lsnim command: \'$cmd\'.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + + # for each NIM network - get the attrs + my %NIMnets; + foreach my $netwk (@nimnetworks) { + my $cmd = qq~/usr/sbin/lsnim -Z -a net_addr -a snm $netwk 2>/dev/null~; + my @result = xCAT::Utils->runcmd("$cmd", -1); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "$::msgstr Could not run lsnim command: \'$cmd\'.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + + foreach my $l (@result){ + # skip comment lines + next if ($l =~ /^\s*#/); + + my ($nimname, $net_addr, $snm) = split(':', $l); + $NIMnets{$netwk}{'net_addr'} = $net_addr; + $NIMnets{$netwk}{'snm'} = $snm; + } + } + + # for each xCAT network check for an equivalent NIM network + # - if a match then display the NIM def + # - display output of "lsnim -l " + + foreach my $xcatnet (@xcatnetworks) { + + # split mask + my ($nm1, $nm2, $nm3, $nm4) = split('\.', $xnethash{$xcatnet}{mask}); + + # split net addr + my ($nn1, $nn2, $nn3, $nn4) = split('\.', $xnethash{$xcatnet}{net}); + + foreach my $nimnet (@nimnetworks) { + # split definition mask + my ($dm1, $dm2, $dm3, $dm4) = split('\.', $NIMnets{$nimnet}{'snm'}); + + # split definition net addr + my ($dn1, $dn2, $dn3, $dn4) = split('\.', $NIMnets{$nimnet}{'net_addr'}); + # check for the same netmask and network address + if ( ($nn1 == $dn1) && ($nn2 ==$dn2) && ($nn3 == $dn3) && ($nn4 == $dn4) ) { + if ( ($nm1 == $dm1) && ($nm2 ==$dm2) && ($nm3 == $dm3) && ($nm4== $dm4) ) { + # found match so display NIM net def + my $cmd = qq~/usr/sbin/lsnim -l $nimnet 2>/dev/null~; + my $output = xCAT::Utils->runcmd("$cmd", -1); + if ($::RUNCMD_RC != 0) { + # my $rsp; + # push @{$rsp->{data}}, "Could not run lsnim command: \'$cmd\'.\n"; + # xCAT::MsgUtils->message("E", $rsp, $callback); + next; + } else { + my $rsp; + push @{$rsp->{data}}, "Note: The following is a match for \nxCAT network named \'$xcatnet\'\n"; + push @{$rsp->{data}}, "$output\n"; + xCAT::MsgUtils->message("I", $rsp, $callback); + } + } + } + } # end for each nim net + } # end foreach xcat net + + return 0; +} + +#------------------------------------------------------------------------------- + +=head3 rmNIMnetwork + Remove NIM network definitions corresponding to xCAT network + definitions. This routine runs on the NIMprime and AIX SNs + + Arguments: + + Returns: + + + Comments: + + ex. &rmNIMnetwork($callback, \@networks); + +=cut + +#------------------------------------------------------------------------------- +sub rmNIMnetwork +{ + my $callback = shift; + my $xnets = shift; + + + + +############################################ +# +# This will not work - you must first remove the master if* definition +# and the route defs AND any objects that may reference the network +# - it is not worth the effort - maybe a future +# enhancement - I'll leave the code here for now. +######################################################### + + my @xcatnetworks = @{$xnets}; + + # + # get all the nim network names + # + my $cmd = qq~/usr/sbin/lsnim -c networks | /usr/bin/cut -f1 -d' ' 2>/dev/null~; + my @nimnetworks = xCAT::Utils->runcmd("$cmd", -1); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "Could not run lsnim command: \'$cmd\'.\n"; + xCAT::MsgUtils->message("E", $rsp, $callback); + return 1; + } + + foreach my $xcatnet (@xcatnetworks) { + chomp $xcatnet; + + foreach my $nimnet (@nimnetworks) { + chomp $nimnet; + + if ($xcatnet eq $nimnet) { + # get rid of the old definition + my $rmcmd = "/usr/sbin/nim -Fo remove $nimnet"; + my $output = xCAT::Utils->runcmd("$rmcmd", -1); + if ($::RUNCMD_RC != 0) { + my $rsp; + push @{$rsp->{data}}, "Could not remove the existing NIM network named \'$nimnet\'.\n"; + if ($::VERBOSE) { + push @{$rsp->{data}}, "$output"; + } + xCAT::MsgUtils->message("E", $rsp, $callback); + } else { + if ($::VERBOSE) { + my $rsp; + push @{$rsp->{data}}, "Removed the NIM network definition called \'$nimnet\'\n"; + xCAT::MsgUtils->message("I", $rsp, $callback); + } + } + } + } + } + + return 0; +} + 1;