module Ganeti.HTools.CLI
(
parseOpts
+ , parseEnv
, showVersion
, shTemplate
) where
import System.Console.GetOpt
+import System.Posix.Env
import System.IO
import System.Info
import System
where header = printf "%s %s\nUsage: %s [OPTION...]"
progname Version.version progname
+-- | Parse the environment and return the node/instance names.
+-- This also hardcodes here the default node/instance file names.
+parseEnv :: () -> IO (String, String)
+parseEnv () = do
+ a <- getEnvDefault "HTOOLS_NODES" "nodes"
+ b <- getEnvDefault "HTOOLS_INSTANCES" "instances"
+ return (a, b)
-- | Return a version string for the program
showVersion :: String -- ^ The program name
, optShowCmds :: Maybe FilePath -- ^ Whether to show the command list
, optOneline :: Bool -- ^ Switch output to a single line
, optNodef :: FilePath -- ^ Path to the nodes file
+ , optNodeSet :: Bool -- ^ The nodes have been set by options
, optInstf :: FilePath -- ^ Path to the instances file
+ , optInstSet :: Bool -- ^ The insts have been set by options
, optMaxLength :: Int -- ^ Stop after this many steps
, optMaster :: String -- ^ Collect data from RAPI
, optVerbose :: Int -- ^ Verbosity level
, optShowCmds = Nothing
, optOneline = False
, optNodef = "nodes"
+ , optNodeSet = False
, optInstf = "instances"
+ , optInstSet = False
, optMaxLength = -1
, optMaster = ""
, optVerbose = 0
(NoArg (\ opts -> opts { optOneline = True }))
"print the ganeti command list for reaching the solution"
, Option ['n'] ["nodes"]
- (ReqArg (\ f opts -> opts { optNodef = f }) "FILE")
+ (ReqArg (\ f opts -> opts { optNodef = f, optNodeSet = True }) "FILE")
"the node list FILE"
, Option ['i'] ["instances"]
- (ReqArg (\ f opts -> opts { optInstf = f }) "FILE")
+ (ReqArg (\ f opts -> opts { optInstf = f, optInstSet = True }) "FILE")
"the instance list FILE"
, Option ['m'] ["master"]
(ReqArg (\ m opts -> opts { optMaster = m }) "ADDRESS")
putStr $ CLI.showVersion "hbal"
exitWith ExitSuccess
- let oneline = optOneline opts
+ (env_node, env_inst) <- CLI.parseEnv ()
+ let nodef = if optNodeSet opts then optNodef opts
+ else env_node
+ instf = if optInstSet opts then optInstf opts
+ else env_inst
+ oneline = optOneline opts
verbose = optVerbose opts
(node_data, inst_data) =
case optMaster opts of
- "" -> (readFile $ optNodef opts,
- readFile $ optInstf opts)
+ "" -> (readFile nodef,
+ readFile instf)
host -> (readData getNodes host,
readData getInstances host)
{ optShowNodes :: Bool
, optShowCmds :: Bool
, optNodef :: FilePath
- , optInstf :: FilePath
+ , optNodeSet :: Bool -- ^ The nodes have been set by options
+ , optInstf :: FilePath -- ^ Path to the instances file
+ , optInstSet :: Bool -- ^ The insts have been set by options
, optMinDepth :: Int
, optMaxRemovals :: Int
, optMinDelta :: Int
{ optShowNodes = False
, optShowCmds = False
, optNodef = "nodes"
+ , optNodeSet = False
, optInstf = "instances"
+ , optInstSet = False
, optMinDepth = 1
, optMaxRemovals = -1
, optMinDelta = 0
, optMaxDelta = -1
- , optMaster = ""
- , optShowVer = False
- , optShowHelp = False
+ , optMaster = ""
+ , optShowVer = False
+ , optShowHelp = False
}
{- | Start computing the solution at the given depth and recurse until
(NoArg (\ opts -> opts { optShowCmds = True }))
"print the ganeti command list for reaching the solution"
, Option ['n'] ["nodes"]
- (ReqArg (\ f opts -> opts { optNodef = f }) "FILE")
+ (ReqArg (\ f opts -> opts { optNodef = f, optNodeSet = True }) "FILE")
"the node list FILE"
, Option ['i'] ["instances"]
- (ReqArg (\ f opts -> opts { optInstf = f }) "FILE")
+ (ReqArg (\ f opts -> opts { optInstf = f, optInstSet = True }) "FILE")
"the instance list FILE"
, Option ['d'] ["depth"]
(ReqArg (\ i opts -> opts { optMinDepth = (read i)::Int }) "D")
printf $ CLI.showVersion "hn1"
exitWith ExitSuccess
- let min_depth = optMinDepth opts
- let (node_data, inst_data) =
+ (env_node, env_inst) <- CLI.parseEnv ()
+ let nodef = if optNodeSet opts then optNodef opts
+ else env_node
+ instf = if optInstSet opts then optInstf opts
+ else env_inst
+ min_depth = optMinDepth opts
+ (node_data, inst_data) =
case optMaster opts of
- "" -> (readFile $ optNodef opts,
- readFile $ optInstf opts)
+ "" -> (readFile nodef,
+ readFile instf)
host -> (readData getNodes host,
readData getInstances host)