Add reading the file names from env vars
authorIustin Pop <iustin@google.com>
Mon, 20 Apr 2009 11:07:09 +0000 (13:07 +0200)
committerIustin Pop <iustin@google.com>
Mon, 20 Apr 2009 11:07:09 +0000 (13:07 +0200)
This patch adds support for selecting the instance/node file names via
two environment variables (HTOOLS_NODES, HTOOLS_INSTANCES).

Unfortunately we still have lots of duplicated code, since the options
are not unified.

Ganeti/HTools/CLI.hs
hbal.hs
hn1.hs

index 1a30f58..3d14b4d 100644 (file)
@@ -9,11 +9,13 @@ and this is more IO oriented.
 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
@@ -46,6 +48,13 @@ parseOpts argv progname options defaultOptions fn =
       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
diff --git a/hbal.hs b/hbal.hs
index 7424b3d..bcd517a 100644 (file)
--- a/hbal.hs
+++ b/hbal.hs
@@ -28,7 +28,9 @@ data Options = Options
     , 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
@@ -44,7 +46,9 @@ defaultOptions  = Options
  , optShowCmds  = Nothing
  , optOneline   = False
  , optNodef     = "nodes"
+ , optNodeSet   = False
  , optInstf     = "instances"
+ , optInstSet   = False
  , optMaxLength = -1
  , optMaster    = ""
  , optVerbose   = 0
@@ -69,10 +73,10 @@ options =
       (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")
@@ -152,12 +156,17 @@ main = do
          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)
 
diff --git a/hn1.hs b/hn1.hs
index 65ab4bb..e307bad 100644 (file)
--- a/hn1.hs
+++ b/hn1.hs
@@ -26,7 +26,9 @@ data Options = Options
     { 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
@@ -42,14 +44,16 @@ defaultOptions    = Options
  { 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
@@ -91,10 +95,10 @@ options =
       (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")
@@ -134,11 +138,16 @@ main = do
          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)