1 {-| Implementation of command-line functions.
3 This module holds the common cli-related functions for the binaries,
4 separated into this module since Utils.hs is used in many other places
5 and this is more IO oriented.
9 module Ganeti.HTools.CLI
16 import System.Console.GetOpt
17 import System.Posix.Env
22 import Text.Printf (printf)
23 import qualified Data.Version
25 import qualified Ganeti.HTools.Version as Version(version)
27 -- | Class for types which support show help and show version
28 class CLIOptions a where
30 showVersion :: a -> Bool
32 -- | Command line parser, using the 'options' structure.
33 parseOpts :: (CLIOptions b) =>
34 [String] -- ^ The command line arguments
35 -> String -- ^ The program name
36 -> [OptDescr (b -> b)] -- ^ The supported command line options
37 -> b -- ^ The default options record
38 -> IO (b, [String]) -- ^ The resulting options a leftover
40 parseOpts argv progname options defaultOptions =
41 case getOpt Permute options argv of
44 let resu@(po, _) = (foldl (flip id) defaultOptions o, n)
45 when (showHelp po) $ do
46 putStr $ usageInfo header options
48 when (showVersion po) $ do
49 printf "%s %s\ncompiled with %s %s\nrunning on %s %s\n"
50 progname Version.version
51 compilerName (Data.Version.showVersion compilerVersion)
56 ioError (userError (concat errs ++ usageInfo header options))
57 where header = printf "%s %s\nUsage: %s [OPTION...]"
58 progname Version.version progname
60 -- | Parse the environment and return the node/instance names.
61 -- This also hardcodes here the default node/instance file names.
62 parseEnv :: () -> IO (String, String)
64 a <- getEnvDefault "HTOOLS_NODES" "nodes"
65 b <- getEnvDefault "HTOOLS_INSTANCES" "instances"
68 -- | A shell script template for autogenerated scripts
71 printf "#!/bin/sh\n\n\
72 \# Auto-generated script for executing cluster rebalancing\n\n\
73 \# To stop, touch the file /tmp/stop-htools\n\n\
76 \ if [ -f /tmp/stop-htools ]; then\n\
77 \ echo 'Stop requested, exiting'\n\