From 79eef90baacbd6692aa761605b83ede7bfa9fa9f Mon Sep 17 00:00:00 2001
From: Agata Murawska <agatamurawska@google.com>
Date: Thu, 21 Jun 2012 14:30:10 +0200
Subject: [PATCH] Generalize functions used by both hspace and hcheck
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

As hspace and hcheck both use machine readable options, they require
similar functions - which are now generalized and moved to CLI and
Utils.

Signed-off-by: Agata Murawska <agatamurawska@google.com>
Reviewed-by: RenΓ© Nussbaumer <rn@google.com>
---
 htools/Ganeti/HTools/CLI.hs            | 16 ++++++++
 htools/Ganeti/HTools/Program/Hspace.hs | 55 +++++++++++---------------
 htools/Ganeti/HTools/Utils.hs          |  9 ++++-
 3 files changed, 48 insertions(+), 32 deletions(-)

diff --git a/htools/Ganeti/HTools/CLI.hs b/htools/Ganeti/HTools/CLI.hs
index aa4dc2c98..308fe6db3 100644
--- a/htools/Ganeti/HTools/CLI.hs
+++ b/htools/Ganeti/HTools/CLI.hs
@@ -39,6 +39,8 @@ module Ganeti.HTools.CLI
   , maybePrintNodes
   , maybePrintInsts
   , maybeShowWarnings
+  , printKeys
+  , printFinal
   , setNodeStatus
   -- * The options
   , oDataFile
@@ -83,6 +85,7 @@ module Ganeti.HTools.CLI
   ) where
 
 import Control.Monad
+import Data.Char (toUpper)
 import Data.Maybe (fromMaybe)
 import qualified Data.Version
 import System.Console.GetOpt
@@ -563,6 +566,19 @@ maybeShowWarnings fix_msgs =
     hPutStrLn stderr "Warning: cluster has inconsistent data:"
     hPutStrLn stderr . unlines . map (printf "  - %s") $ fix_msgs
 
+-- | Format a list of key, value as a shell fragment.
+printKeys :: String -> [(String, String)] -> IO ()
+printKeys prefix = mapM_ (\(k, v) ->
+                       printf "%s_%s=%s\n" prefix (map toUpper k) (ensureQuoted v))
+
+-- | Prints the final @OK@ marker in machine readable output.
+printFinal :: String -> Bool -> IO ()
+printFinal prefix True =
+  -- this should be the final entry
+  printKeys prefix [("OK", "1")]
+
+printFinal _ False = return ()
+
 -- | Potentially set the node as offline based on passed offline list.
 setNodeOffline :: [Ndx] -> Node.Node -> Node.Node
 setNodeOffline offline_indices n =
diff --git a/htools/Ganeti/HTools/Program/Hspace.hs b/htools/Ganeti/HTools/Program/Hspace.hs
index ed5c39ba6..e7c592869 100644
--- a/htools/Ganeti/HTools/Program/Hspace.hs
+++ b/htools/Ganeti/HTools/Program/Hspace.hs
@@ -26,7 +26,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 module Ganeti.HTools.Program.Hspace (main, options) where
 
 import Control.Monad
-import Data.Char (toUpper, isAlphaNum, toLower)
+import Data.Char (toUpper, toLower)
 import Data.Function (on)
 import Data.List
 import Data.Maybe (fromMaybe)
@@ -81,6 +81,10 @@ data Phase = PInitial
 data SpecType = SpecNormal
               | SpecTiered
 
+-- | Prefix for machine readable names
+htsPrefix :: String
+htsPrefix = "HTS"
+
 -- | What we prefix a spec with.
 specPrefix :: SpecType -> String
 specPrefix SpecNormal = "SPEC"
@@ -177,14 +181,14 @@ printResults True _ fin_nl num_instances allocs sreason = do
                   \ != counted (%d)\n" (num_instances + allocs)
            (Cluster.csNinst fin_stats)
 
-  printKeys $ printStats PFinal fin_stats
-  printKeys [ ("ALLOC_USAGE", printf "%.8f"
+  printKeysHTS $ printStats PFinal fin_stats
+  printKeysHTS [ ("ALLOC_USAGE", printf "%.8f"
                                 ((fromIntegral num_instances::Double) /
                                  fromIntegral fin_instances))
             , ("ALLOC_INSTANCES", printf "%d" allocs)
             , ("ALLOC_FAIL_REASON", map toUpper . show . fst $ head sreason)
             ]
-  printKeys $ map (\(x, y) -> (printf "ALLOC_%s_CNT" (show x),
+  printKeysHTS $ map (\(x, y) -> (printf "ALLOC_%s_CNT" (show x),
                                printf "%d" y)) sreason
 
 printResults False ini_nl fin_nl _ allocs sreason = do
@@ -193,12 +197,8 @@ printResults False ini_nl fin_nl _ allocs sreason = do
   printFRScores ini_nl fin_nl sreason
 
 -- | Prints the final @OK@ marker in machine readable output.
-printFinal :: Bool -> IO ()
-printFinal True =
-  -- this should be the final entry
-  printKeys [("OK", "1")]
-
-printFinal False = return ()
+printFinalHTS :: Bool -> IO ()
+printFinalHTS = printFinal htsPrefix
 
 -- | Compute the tiered spec counts from a list of allocated
 -- instances.
@@ -232,20 +232,13 @@ printAllocationStats ini_nl fin_nl = do
   let ini_stats = Cluster.totalResources ini_nl
       fin_stats = Cluster.totalResources fin_nl
       (rini, ralo, runa) = Cluster.computeAllocationDelta ini_stats fin_stats
-  printKeys $ formatRSpec "USED" rini
-  printKeys $ formatRSpec "POOL" ralo
-  printKeys $ formatRSpec "UNAV" runa
-
--- | Ensure a value is quoted if needed.
-ensureQuoted :: String -> String
-ensureQuoted v = if not (all (\c -> isAlphaNum c || c == '.') v)
-                 then '\'':v ++ "'"
-                 else v
+  printKeysHTS $ formatRSpec "USED" rini
+  printKeysHTS $ formatRSpec "POOL" ralo
+  printKeysHTS $ formatRSpec "UNAV" runa
 
 -- | Format a list of key\/values as a shell fragment.
-printKeys :: [(String, String)] -> IO ()
-printKeys = mapM_ (\(k, v) ->
-                   printf "HTS_%s=%s\n" (map toUpper k) (ensureQuoted v))
+printKeysHTS :: [(String, String)] -> IO ()
+printKeysHTS = printKeys htsPrefix
 
 -- | Converts instance data to a list of strings.
 printInstance :: Node.List -> Instance.Instance -> [String]
@@ -280,9 +273,9 @@ formatResources res =
 -- | Print the cluster resources.
 printCluster :: Bool -> Cluster.CStats -> Int -> IO ()
 printCluster True ini_stats node_count = do
-  printKeys $ map (\(a, fn) -> ("CLUSTER_" ++ a, fn ini_stats)) clusterData
-  printKeys [("CLUSTER_NODES", printf "%d" node_count)]
-  printKeys $ printStats PInitial ini_stats
+  printKeysHTS $ map (\(a, fn) -> ("CLUSTER_" ++ a, fn ini_stats)) clusterData
+  printKeysHTS [("CLUSTER_NODES", printf "%d" node_count)]
+  printKeysHTS $ printStats PInitial ini_stats
 
 printCluster False ini_stats node_count = do
   printf "The cluster has %d nodes and the following resources:\n  %s.\n"
@@ -294,9 +287,9 @@ printCluster False ini_stats node_count = do
 -- | Prints the normal instance spec.
 printISpec :: Bool -> RSpec -> SpecType -> DiskTemplate -> IO ()
 printISpec True ispec spec disk_template = do
-  printKeys $ map (\(a, fn) -> (prefix ++ "_" ++ a, fn ispec)) specData
-  printKeys [ (prefix ++ "_RQN", printf "%d" req_nodes) ]
-  printKeys [ (prefix ++ "_DISK_TEMPLATE",
+  printKeysHTS $ map (\(a, fn) -> (prefix ++ "_" ++ a, fn ispec)) specData
+  printKeysHTS [ (prefix ++ "_RQN", printf "%d" req_nodes) ]
+  printKeysHTS [ (prefix ++ "_DISK_TEMPLATE",
                diskTemplateToRaw disk_template) ]
       where req_nodes = Instance.requiredNodes disk_template
             prefix = specPrefix spec
@@ -311,8 +304,8 @@ printISpec False ispec spec disk_template =
 printTiered :: Bool -> [(RSpec, Int)]
             -> Node.List -> Node.List -> [(FailMode, Int)] -> IO ()
 printTiered True spec_map nl trl_nl _ = do
-  printKeys $ printStats PTiered (Cluster.totalResources trl_nl)
-  printKeys [("TSPEC", unwords (formatSpecMap spec_map))]
+  printKeysHTS $ printStats PTiered (Cluster.totalResources trl_nl)
+  printKeysHTS [("TSPEC", unwords (formatSpecMap spec_map))]
   printAllocationStats nl trl_nl
 
 printTiered False spec_map ini_nl fin_nl sreason = do
@@ -457,4 +450,4 @@ main opts args = do
 
   -- Print final result
 
-  printFinal machine_r
+  printFinalHTS machine_r
diff --git a/htools/Ganeti/HTools/Utils.hs b/htools/Ganeti/HTools/Utils.hs
index a890c500c..aaf4fb093 100644
--- a/htools/Ganeti/HTools/Utils.hs
+++ b/htools/Ganeti/HTools/Utils.hs
@@ -31,6 +31,7 @@ module Ganeti.HTools.Utils
   , select
   , applyIf
   , commaJoin
+  , ensureQuoted
   , tryRead
   , formatTable
   , printTable
@@ -42,7 +43,7 @@ module Ganeti.HTools.Utils
   , exitUnless
   ) where
 
-import Data.Char (toUpper)
+import Data.Char (toUpper, isAlphaNum)
 import Data.List
 
 import Debug.Trace
@@ -91,6 +92,12 @@ plural :: Int -> String -> String -> String
 plural 1 s _ = s
 plural _ _ p = p
 
+-- | Ensure a value is quoted if needed.
+ensureQuoted :: String -> String
+ensureQuoted v = if not (all (\c -> isAlphaNum c || c == '.') v)
+                 then '\'':v ++ "'"
+                 else v
+
 -- * Mathematical functions
 
 -- Simple and slow statistical functions, please replace with better
-- 
GitLab