From 3bf30fcc3851ea60e2242a39428e9405b45f8ce3 Mon Sep 17 00:00:00 2001
From: lissav <lissav@us.ibm.com>
Date: Wed, 23 Oct 2013 13:50:05 -0400
Subject: [PATCH] defect 3851- handle multiple MN in DB

---
 perl-xCAT/xCAT/DSHCLI.pm                   | 16 +++++++++----
 perl-xCAT/xCAT/ServiceNodeUtils.pm         | 13 ++++++-----
 xCAT-server/lib/perl/xCAT/Postage.pm       |  4 ++--
 xCAT-server/lib/xcat/plugins/updatenode.pm |  7 +++---
 xCAT-server/sbin/xcatconfig                | 26 +++++++++++++---------
 5 files changed, 40 insertions(+), 26 deletions(-)

diff --git a/perl-xCAT/xCAT/DSHCLI.pm b/perl-xCAT/xCAT/DSHCLI.pm
index ecdb2b4c3..73d1a9232 100644
--- a/perl-xCAT/xCAT/DSHCLI.pm
+++ b/perl-xCAT/xCAT/DSHCLI.pm
@@ -3237,7 +3237,14 @@ sub bld_resolve_nodes_hash
 
     # find out if we have an MN in the list, local cp and sh will be used
     # not remote shell
-    my $mname = xCAT::Utils->noderangecontainsMn(@target_list);
+    # find out the names for the Management Node
+    my @MNnodeinfo   = xCAT::NetworkUtils->determinehostname;
+    my $mname   = pop @MNnodeinfo;                  # hostname
+    #my $rsp = {};
+    #$rsp->{info}->[0] =
+    #      "Management node name is $mname.";
+    #            xCAT::MsgUtils->message("I", $rsp, $::CALLBACK);
+
     foreach my $target (@target_list)
     {
 
@@ -4079,11 +4086,12 @@ sub parse_and_run_dsh
         # check if any node in the noderange is the Management Node and exit 
         # with error, if the Management Node is in the Database and in the
         # noderange
-        my $mname = xCAT::Utils->noderangecontainsMn(@nodelist); 
-        if ($mname) {  # MN in the nodelist
+        my @mname = xCAT::Utils->noderangecontainsMn(@nodelist); 
+        if (@mname) {  # MN in the nodelist
+            my $nodes=join(',', @mname);
             my $rsp = {};
             $rsp->{error}->[0] =
-              "You must not run -K option against the Management Node:$mname.";
+              "You must not run -K option against the Management Node:$nodes.";
             xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 1);
             return;
         } 
diff --git a/perl-xCAT/xCAT/ServiceNodeUtils.pm b/perl-xCAT/xCAT/ServiceNodeUtils.pm
index 5bcf6508b..28bf81a98 100644
--- a/perl-xCAT/xCAT/ServiceNodeUtils.pm
+++ b/perl-xCAT/xCAT/ServiceNodeUtils.pm
@@ -242,12 +242,13 @@ sub getAllSN
     # if did not input "ALL" and there is a MN, remove it
     my @newservicenodes;
     if ((!defined($options)) || ($options ne "ALL")) {   
-       my $mname = xCAT::Utils->noderangecontainsMn(@servicenodes);
-       if ($mname) { # if there is a MN
-        foreach my $nodes (@servicenodes) {
-           if ($mname ne ($nodes)){
-              push @newservicenodes, $nodes;
-           }
+       my @mname = xCAT::Utils->noderangecontainsMn(@servicenodes);
+       if (@mname) { # if there is a MN
+        foreach my $node (@servicenodes) {
+          # check to see if node in MN list
+          if (!(grep(/^$node$/, @mname))) { # if node not in the MN array
+              push @newservicenodes, $node;
+          }
         }
         $servicenodetab->close;
         return @newservicenodes;  # return without the MN in the array
diff --git a/xCAT-server/lib/perl/xCAT/Postage.pm b/xCAT-server/lib/perl/xCAT/Postage.pm
index 0ff0066df..250c28eb8 100644
--- a/xCAT-server/lib/perl/xCAT/Postage.pm
+++ b/xCAT-server/lib/perl/xCAT/Postage.pm
@@ -211,7 +211,7 @@ sub makescript {
      return;
   }
 
-  $mn = xCAT::Utils->noderangecontainsMn(@$nodes);
+  @::mn = xCAT::Utils->noderangecontainsMn(@$nodes);
 
   my $cfgflag=0;
   my $cloudflag=0;
@@ -652,7 +652,7 @@ sub getNodeType
     my $node   = shift;
     my $result;
    
-    if ( $node =~ /^$mn$/) {
+    if (grep(/^$node$/, @::mn)) {   # is it in the Management Node array 
         $result="MN";
         return $result;
     }
diff --git a/xCAT-server/lib/xcat/plugins/updatenode.pm b/xCAT-server/lib/xcat/plugins/updatenode.pm
index 3162d71fa..6adc76019 100644
--- a/xCAT-server/lib/xcat/plugins/updatenode.pm
+++ b/xCAT-server/lib/xcat/plugins/updatenode.pm
@@ -396,12 +396,13 @@ sub preprocess_updatenode
 
         # check to see if the Management Node is in the noderange and
         # if it is abort
-        my $mname = xCAT::Utils->noderangecontainsMn(@$nodes);
-        if ($mname)
+        my @mname = xCAT::Utils->noderangecontainsMn(@$nodes);
+        if (@mname)
         {    # MN in the nodelist
+            my $nodes=join(',', @mname);
             my $rsp = {};
             $rsp->{error}->[0] =
-              "You must not run -k option against the Management Node:$mname.";
+              "You must not run -k option against a management node: $nodes.";
             xCAT::MsgUtils->message("E", $rsp, $callback, 1);
             return;
         }
diff --git a/xCAT-server/sbin/xcatconfig b/xCAT-server/sbin/xcatconfig
index 5d0aa1191..f9d0b428a 100755
--- a/xCAT-server/sbin/xcatconfig
+++ b/xCAT-server/sbin/xcatconfig
@@ -2124,23 +2124,27 @@ sub setupMNinDB
 
    my $defined  = 0;
    my $cmds="XCATBYPASS=Y $::XCATROOT/sbin/tabdump nodelist | grep  __mgmtnode";
-   my $output = xCAT::Utils->runcmd("$cmds", -1);
+   my @output = xCAT::Utils->runcmd("$cmds", -1);
+   
    if ($::RUNCMD_RC == 0)  # found a management node defined
    {
       my $chtabcmds;
-      my @mn = split(",", $output);  
+      my @mn = split(",", $output[0]);  
       $mn[0] =~ s/"//g;   # remove the quotes
       if  ($mn[0] ne $mnname) {   # does not match current host name,delete it
-        $chtabcmds = "$::XCATROOT/sbin/chtab -d node=$mn[0] nodelist;";
-        my $outref = xCAT::Utils->runcmd("$chtabcmds", 0);
-        if ($::RUNCMD_RC != 0)
-        {
-          xCAT::MsgUtils->message('E', "Could not run $chtabcmds.");
-          return;
-        }
+         my $size=@output;   # if more than one management  node defined, do not remove any
+         if ($size == 1) {
+           $chtabcmds = "$::XCATROOT/sbin/chtab -d node=$mn[0] nodelist;";
+           my $outref = xCAT::Utils->runcmd("$chtabcmds", 0);
+           if ($::RUNCMD_RC != 0)
+           {
+             xCAT::MsgUtils->message('E', "Could not run $chtabcmds.");
+             return;
+           }
+         }
       } else {  # it is defined and good
-          xCAT::MsgUtils->message('I', "$mnname already defined in the nodelist table.");
-          $defined=1;     
+             xCAT::MsgUtils->message('I', "$mnname already defined in the nodelist table.");
+             $defined=1;     
       }
     }
     # now add with the new name , if not already there