Commit 5296ee23 authored by Iustin Pop's avatar Iustin Pop

A bit of cleanup in hspace

The node offline/mcpu is identical to hbal's setNodesStatus, so let's
move that to CLI.hs and reuse it in hspace (also, rename it and drop
one 's').

Also, the check for the number of nodes is obsolete, as we compute
that from the disk template.

The patch does a bit of other small cleanups.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 7eda951b
...@@ -36,6 +36,7 @@ module Ganeti.HTools.CLI ...@@ -36,6 +36,7 @@ module Ganeti.HTools.CLI
, maybePrintNodes , maybePrintNodes
, maybePrintInsts , maybePrintInsts
, maybeShowWarnings , maybeShowWarnings
, setNodeStatus
-- * The options -- * The options
, oDataFile , oDataFile
, oDiskMoves , oDiskMoves
...@@ -83,12 +84,15 @@ import System.Console.GetOpt ...@@ -83,12 +84,15 @@ import System.Console.GetOpt
import System.IO import System.IO
import System.Info import System.Info
import System import System
import Text.Printf (printf) import Text.Printf (printf, hPrintf)
import qualified Ganeti.HTools.Version as Version(version) import qualified Ganeti.HTools.Version as Version(version)
import qualified Ganeti.HTools.Container as Container
import qualified Ganeti.HTools.Node as Node
import qualified Ganeti.Constants as C import qualified Ganeti.Constants as C
import Ganeti.HTools.Types import Ganeti.HTools.Types
import Ganeti.HTools.Utils import Ganeti.HTools.Utils
import Ganeti.HTools.Loader
-- * Constants -- * Constants
...@@ -515,3 +519,29 @@ maybeShowWarnings fix_msgs = ...@@ -515,3 +519,29 @@ maybeShowWarnings fix_msgs =
unless (null fix_msgs) $ do unless (null fix_msgs) $ do
hPutStrLn stderr "Warning: cluster has inconsistent data:" hPutStrLn stderr "Warning: cluster has inconsistent data:"
hPutStrLn stderr . unlines . map (printf " - %s") $ fix_msgs hPutStrLn stderr . unlines . map (printf " - %s") $ fix_msgs
-- | Set node properties based on command line options.
setNodeStatus :: Options -> Node.List -> IO Node.List
setNodeStatus opts fixed_nl = do
let offline_passed = optOffline opts
all_nodes = Container.elems fixed_nl
offline_lkp = map (lookupName (map Node.name all_nodes)) offline_passed
offline_wrong = filter (not . goodLookupResult) offline_lkp
offline_names = map lrContent offline_lkp
offline_indices = map Node.idx $
filter (\n -> Node.name n `elem` offline_names)
all_nodes
m_cpu = optMcpu opts
m_dsk = optMdsk opts
when (not (null offline_wrong)) $ do
hPrintf stderr "Error: Wrong node name(s) set as offline: %s\n"
(commaJoin (map lrContent offline_wrong)) :: IO ()
exitWith $ ExitFailure 1
let nm = Container.map (\n -> if Node.idx n `elem` offline_indices
then Node.setOffline n True
else n) fixed_nl
nlf = Container.map (flip Node.setMdsk m_dsk . flip Node.setMcpu m_cpu)
nm
return nlf
...@@ -255,32 +255,6 @@ runJobSet master fin_nl il cmd_jobs = do ...@@ -255,32 +255,6 @@ runJobSet master fin_nl il cmd_jobs = do
[(hangleSigTerm, softwareTermination), (hangleSigInt, keyboardSignal)] [(hangleSigTerm, softwareTermination), (hangleSigInt, keyboardSignal)]
execWrapper master fin_nl il cref cmd_jobs execWrapper master fin_nl il cref cmd_jobs
-- | Set node properties based on command line options.
setNodesStatus :: Options -> Node.List -> IO Node.List
setNodesStatus opts fixed_nl = do
let offline_passed = optOffline opts
all_nodes = Container.elems fixed_nl
offline_lkp = map (lookupName (map Node.name all_nodes)) offline_passed
offline_wrong = filter (not . goodLookupResult) offline_lkp
offline_names = map lrContent offline_lkp
offline_indices = map Node.idx $
filter (\n -> Node.name n `elem` offline_names)
all_nodes
m_cpu = optMcpu opts
m_dsk = optMdsk opts
when (not (null offline_wrong)) $ do
hPrintf stderr "Error: Wrong node name(s) set as offline: %s\n"
(commaJoin (map lrContent offline_wrong)) :: IO ()
exitWith $ ExitFailure 1
let nm = Container.map (\n -> if Node.idx n `elem` offline_indices
then Node.setOffline n True
else n) fixed_nl
nlf = Container.map (flip Node.setMdsk m_dsk . flip Node.setMcpu m_cpu)
nm
return nlf
-- | Select the target node group. -- | Select the target node group.
selectGroup :: Options -> Group.List -> Node.List -> Instance.List selectGroup :: Options -> Group.List -> Node.List -> Instance.List
-> IO (String, (Node.List, Instance.List)) -> IO (String, (Node.List, Instance.List))
...@@ -385,7 +359,7 @@ main = do ...@@ -385,7 +359,7 @@ main = do
when (verbose > 1) $ when (verbose > 1) $
putStrLn $ "Loaded cluster tags: " ++ intercalate "," ctags putStrLn $ "Loaded cluster tags: " ++ intercalate "," ctags
nlf <- setNodesStatus opts fixed_nl nlf <- setNodeStatus opts fixed_nl
checkCluster verbose nlf ilf checkCluster verbose nlf ilf
maybeSaveData (optSaveCluster opts) "original" "before balancing" ini_cdata maybeSaveData (optSaveCluster opts) "original" "before balancing" ini_cdata
......
...@@ -29,7 +29,6 @@ import Control.Monad ...@@ -29,7 +29,6 @@ import Control.Monad
import Data.Char (toUpper, isAlphaNum) import Data.Char (toUpper, isAlphaNum)
import Data.Function (on) import Data.Function (on)
import Data.List import Data.List
import Data.Maybe (isJust, fromJust)
import Data.Ord (comparing) import Data.Ord (comparing)
import System (exitWith, ExitCode(..)) import System (exitWith, ExitCode(..))
import System.IO import System.IO
...@@ -346,6 +345,12 @@ failureReason = show . fst . head ...@@ -346,6 +345,12 @@ failureReason = show . fst . head
sortReasons :: [(FailMode, Int)] -> [(FailMode, Int)] sortReasons :: [(FailMode, Int)] -> [(FailMode, Int)]
sortReasons = reverse . sortBy (comparing snd) sortReasons = reverse . sortBy (comparing snd)
-- | Aborts the program if we get a bad value.
exitIfBad :: Result a -> IO a
exitIfBad (Bad s) =
hPrintf stderr "Failure: %s\n" s >> exitWith (ExitFailure 1)
exitIfBad (Ok v) = return v
-- | Main function. -- | Main function.
main :: IO () main :: IO ()
main = do main = do
...@@ -365,43 +370,17 @@ main = do ...@@ -365,43 +370,17 @@ main = do
(ClusterData gl fixed_nl il ctags) <- loadExternalData opts (ClusterData gl fixed_nl il ctags) <- loadExternalData opts
let num_instances = length $ Container.elems il let num_instances = Container.size il
let offline_passed = optOffline opts
all_nodes = Container.elems fixed_nl all_nodes = Container.elems fixed_nl
offline_lkp = map (lookupName (map Node.name all_nodes)) offline_passed
offline_wrong = filter (not . goodLookupResult) offline_lkp
offline_names = map lrContent offline_lkp
offline_indices = map Node.idx $
filter (\n -> Node.name n `elem` offline_names)
all_nodes
m_cpu = optMcpu opts m_cpu = optMcpu opts
m_dsk = optMdsk opts
when (not (null offline_wrong)) $ do
hPrintf stderr "Error: Wrong node name(s) set as offline: %s\n"
(commaJoin (map lrContent offline_wrong)) :: IO ()
exitWith $ ExitFailure 1
when (req_nodes /= 1 && req_nodes /= 2) $ do
hPrintf stderr "Error: Invalid required nodes (%d)\n"
req_nodes :: IO ()
exitWith $ ExitFailure 1
let nm = Container.map (\n -> if Node.idx n `elem` offline_indices
then Node.setOffline n True
else n) fixed_nl
nl = Container.map (flip Node.setMdsk m_dsk . flip Node.setMcpu m_cpu)
nm
csf = commonSuffix fixed_nl il csf = commonSuffix fixed_nl il
when (length csf > 0 && verbose > 1) $ nl <- setNodeStatus opts fixed_nl
when (not (null csf) && verbose > 1) $
hPrintf stderr "Note: Stripping common suffix of '%s' from names\n" csf hPrintf stderr "Note: Stripping common suffix of '%s' from names\n" csf
when (isJust shownodes) $ maybePrintNodes shownodes "Initial cluster" (Cluster.printNodes nl)
do
hPutStrLn stderr "Initial cluster status:"
hPutStrLn stderr $ Cluster.printNodes nl (fromJust shownodes)
let ini_cv = Cluster.compCV nl let ini_cv = Cluster.compCV nl
ini_stats = Cluster.totalResources nl ini_stats = Cluster.totalResources nl
...@@ -415,25 +394,19 @@ main = do ...@@ -415,25 +394,19 @@ main = do
printISpec machine_r ispec SpecNormal disk_template printISpec machine_r ispec SpecNormal disk_template
let bad_nodes = fst $ Cluster.computeBadItems nl il let bad_nodes = fst $ Cluster.computeBadItems nl il
stop_allocation = length bad_nodes > 0 stop_allocation = not $ null bad_nodes
result_noalloc = ([(FailN1, 1)]::FailStats, nl, il, [], []) result_noalloc = ([(FailN1, 1)]::FailStats, nl, il, [], [])
-- utility functions -- utility functions
let iofspec spx = Instance.create "new" (rspecMem spx) (rspecDsk spx) let iofspec spx = Instance.create "new" (rspecMem spx) (rspecDsk spx)
(rspecCpu spx) "running" [] True (-1) (-1) disk_template (rspecCpu spx) "running" [] True (-1) (-1) disk_template
exitifbad val = (case val of
Bad s -> do
hPrintf stderr "Failure: %s\n" s :: IO ()
exitWith $ ExitFailure 1
Ok x -> return x)
let reqinst = iofspec ispec let reqinst = iofspec ispec
alloclimit = if optMaxLength opts == -1 alloclimit = if optMaxLength opts == -1
then Nothing then Nothing
else Just (optMaxLength opts) else Just (optMaxLength opts)
allocnodes <- exitifbad $ Cluster.genAllocNodes gl nl req_nodes True allocnodes <- exitIfBad $ Cluster.genAllocNodes gl nl req_nodes True
-- Run the tiered allocation, if enabled -- Run the tiered allocation, if enabled
...@@ -443,7 +416,7 @@ main = do ...@@ -443,7 +416,7 @@ main = do
(treason, trl_nl, trl_il, trl_ixes, _) <- (treason, trl_nl, trl_il, trl_ixes, _) <-
if stop_allocation if stop_allocation
then return result_noalloc then return result_noalloc
else exitifbad (Cluster.tieredAlloc nl il alloclimit (iofspec tspec) else exitIfBad (Cluster.tieredAlloc nl il alloclimit (iofspec tspec)
allocnodes [] []) allocnodes [] [])
let spec_map' = tieredSpecMap trl_ixes let spec_map' = tieredSpecMap trl_ixes
treason' = sortReasons treason treason' = sortReasons treason
...@@ -466,7 +439,7 @@ main = do ...@@ -466,7 +439,7 @@ main = do
(ereason, fin_nl, fin_il, ixes, _) <- (ereason, fin_nl, fin_il, ixes, _) <-
if stop_allocation if stop_allocation
then return result_noalloc then return result_noalloc
else exitifbad (Cluster.iterateAlloc nl il alloclimit else exitIfBad (Cluster.iterateAlloc nl il alloclimit
reqinst allocnodes [] []) reqinst allocnodes [] [])
let allocs = length ixes let allocs = length ixes
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment