From 544029d338bcb851a2c2c9a99346223b39bf2ad7 Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Thu, 21 Feb 2013 13:37:41 -0800
Subject: [PATCH] HRoller: print only online nodes

To make the graphs work even when instances live on offline nodes (eg.
because we're offlining them just to exclude them, or because they have
instance still on them) we just filter them out at the end, when we're
going to print out the result.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 src/Ganeti/HTools/Program/Hroller.hs | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/Ganeti/HTools/Program/Hroller.hs b/src/Ganeti/HTools/Program/Hroller.hs
index 25e096ba5..78b650bae 100644
--- a/src/Ganeti/HTools/Program/Hroller.hs
+++ b/src/Ganeti/HTools/Program/Hroller.hs
@@ -83,6 +83,15 @@ getStats colorings = snd . foldr helper (0,"") $ algBySize colorings
             | otherwise = (elsize, str ++ " LOOSE " ++ algostat el)
               where elsize = (IntMap.size.snd) el
 
+-- | Filter the output list.
+-- Only online nodes are shown, and output groups which are empty after being
+-- filtered are removed as well.
+filterOutput :: [[Node.Node]] -> [[Node.Node]]
+filterOutput l =
+  let onlineOnly = filter (not . Node.offline)
+      nonNullOnly = filter (not . null)
+  in nonNullOnly (map onlineOnly l)
+
 -- | Main function.
 main :: Options -> [String] -> IO ()
 main opts args = do
@@ -98,7 +107,6 @@ main opts args = do
 
   maybeSaveData (optSaveCluster opts) "original" "before hroller run" ini_cdata
 
-  -- TODO: only online nodes!
   -- TODO: filter by node group
   -- TODO: fail if instances are running (with option to warn only)
   -- TODO: identify master node, and put it last
@@ -116,11 +124,13 @@ main opts args = do
       colorings = map (\(v,a) -> (v,(colorVertMap.a) nodeGraph)) colorAlgorithms
       smallestColoring =
         (snd . minimumBy (comparing (IntMap.size . snd))) colorings
-      idToName = Node.name  . (`Container.find` nlf)
-      nodesbycoloring = map (map idToName) $ IntMap.elems smallestColoring
+      idToNode = (`Container.find` nlf)
+      nodesRebootGroups = map (map idToNode) $ IntMap.elems smallestColoring
+      outputRebootGroups = filterOutput nodesRebootGroups
+      outputRebootNames = map (map Node.name) outputRebootGroups
 
   when (verbose > 1) . putStrLn $ getStats colorings
 
   unless (optNoHeaders opts) $
          putStrLn "'Node Reboot Groups'"
-  mapM_ (putStrLn . commaJoin) nodesbycoloring
+  mapM_ (putStrLn . commaJoin) outputRebootNames
-- 
GitLab