Commit 7c3a6391 authored by Klaus Aehlig's avatar Klaus Aehlig
Browse files

Move saving of a command list to CLI



Move the function that saves a list of a command in a file
to CLI.hs. In this way, it is reusable by other htools.
Signed-off-by: default avatarKlaus Aehlig <aehlig@google.com>
Reviewed-by: default avatarHrvoje Ribicic <riba@google.com>
parent a4417db4
......@@ -36,6 +36,7 @@ module Ganeti.HTools.CLI
, parseYesNo
, parseISpecString
, shTemplate
, maybeSaveCommands
, maybePrintNodes
, maybePrintInsts
, maybeShowWarnings
......@@ -720,6 +721,22 @@ shTemplate =
\ fi\n\
\}\n\n"
-- | Optionally show or save a list of commands
maybeSaveCommands :: String -- ^ Informal description
-> Options
-> String -- ^ commands
-> IO ()
maybeSaveCommands msg opts cmds =
case optShowCmds opts of
Nothing -> return ()
Just "-" -> do
putStrLn ""
putStrLn msg
putStr . unlines . map (" " ++) . filter (/= " check") . lines $ cmds
Just out_path -> do
writeFile out_path (shTemplate ++ cmds)
printf "The commands have been written to file '%s'\n" out_path
-- | Optionally print the node list.
maybePrintNodes :: Maybe [String] -- ^ The field list
-> String -- ^ Informational message
......
......@@ -33,7 +33,7 @@ module Ganeti.HTools.Program.Hbal
import Control.Exception (bracket)
import Control.Monad
import Data.List
import Data.Maybe (isJust, isNothing, fromJust)
import Data.Maybe (isNothing)
import Data.IORef
import System.Exit
import System.IO
......@@ -168,20 +168,6 @@ printStats ini_nl fin_nl = do
printf "Final: mem=%d disk=%d\n"
(Cluster.csFmem fin_cs) (Cluster.csFdsk fin_cs)
-- | Saves the rebalance commands to a text file.
saveBalanceCommands :: Options -> String -> IO ()
saveBalanceCommands opts cmd_data = do
let out_path = fromJust $ optShowCmds opts
putStrLn ""
if out_path == "-"
then printf "Commands to run to reach the above solution:\n%s"
(unlines . map (" " ++) .
filter (/= " check") .
lines $ cmd_data)
else do
writeFile out_path (shTemplate ++ cmd_data)
printf "The commands have been written to file '%s'\n" out_path
-- | Wrapper over execJobSet checking for early termination via an IORef.
execCancelWrapper :: Annotator -> String -> Node.List
-> Instance.List -> IORef Int -> [JobSet] -> IO (Result ())
......@@ -409,8 +395,8 @@ main opts args = do
let cmd_jobs = Cluster.splitJobs cmd_strs
when (isJust $ optShowCmds opts) .
saveBalanceCommands opts $ Cluster.formatCmds cmd_jobs
maybeSaveCommands "Commands to run to reach the above solution:" opts
$ Cluster.formatCmds cmd_jobs
maybeSaveData (optSaveCluster opts) "balanced" "after balancing"
ini_cdata { cdNodes = fin_nl, cdInstances = fin_il }
......
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