Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / CLI.hs @ 5aa48dbe

History | View | Annotate | Download (2.7 kB)

1 209b3711 Iustin Pop
{-| Implementation of command-line functions.
2 209b3711 Iustin Pop
3 209b3711 Iustin Pop
This module holds the common cli-related functions for the binaries,
4 209b3711 Iustin Pop
separated into this module since Utils.hs is used in many other places
5 6ef35e3c Iustin Pop
and this is more IO oriented.
6 209b3711 Iustin Pop
7 209b3711 Iustin Pop
-}
8 209b3711 Iustin Pop
9 209b3711 Iustin Pop
module Ganeti.HTools.CLI
10 209b3711 Iustin Pop
    (
11 209b3711 Iustin Pop
      parseOpts
12 8032b3b5 Iustin Pop
    , parseEnv
13 209b3711 Iustin Pop
    , showVersion
14 e0eb63f0 Iustin Pop
    , shTemplate
15 209b3711 Iustin Pop
    ) where
16 209b3711 Iustin Pop
17 209b3711 Iustin Pop
import System.Console.GetOpt
18 8032b3b5 Iustin Pop
import System.Posix.Env
19 209b3711 Iustin Pop
import System.IO
20 209b3711 Iustin Pop
import System.Info
21 209b3711 Iustin Pop
import System
22 209b3711 Iustin Pop
import Monad
23 209b3711 Iustin Pop
import Text.Printf (printf)
24 209b3711 Iustin Pop
import qualified Data.Version
25 209b3711 Iustin Pop
26 209b3711 Iustin Pop
import qualified Ganeti.HTools.Version as Version(version)
27 209b3711 Iustin Pop
28 209b3711 Iustin Pop
-- | Command line parser, using the 'options' structure.
29 209b3711 Iustin Pop
parseOpts :: [String]            -- ^ The command line arguments
30 209b3711 Iustin Pop
          -> String              -- ^ The program name
31 209b3711 Iustin Pop
          -> [OptDescr (b -> b)] -- ^ The supported command line options
32 209b3711 Iustin Pop
          -> b                   -- ^ The default options record
33 209b3711 Iustin Pop
          -> (b -> Bool)         -- ^ The function which given the options
34 209b3711 Iustin Pop
                                 -- tells us whether we need to show help
35 209b3711 Iustin Pop
          -> IO (b, [String])    -- ^ The resulting options a leftover
36 209b3711 Iustin Pop
                                 -- arguments
37 209b3711 Iustin Pop
parseOpts argv progname options defaultOptions fn =
38 209b3711 Iustin Pop
    case getOpt Permute options argv of
39 209b3711 Iustin Pop
      (o, n, []) ->
40 209b3711 Iustin Pop
          do
41 209b3711 Iustin Pop
            let resu@(po, _) = (foldl (flip id) defaultOptions o, n)
42 209b3711 Iustin Pop
            when (fn po) $ do
43 209b3711 Iustin Pop
              putStr $ usageInfo header options
44 209b3711 Iustin Pop
              exitWith ExitSuccess
45 209b3711 Iustin Pop
            return resu
46 209b3711 Iustin Pop
      (_, _, errs) ->
47 209b3711 Iustin Pop
          ioError (userError (concat errs ++ usageInfo header options))
48 209b3711 Iustin Pop
      where header = printf "%s %s\nUsage: %s [OPTION...]"
49 209b3711 Iustin Pop
                     progname Version.version progname
50 209b3711 Iustin Pop
51 8032b3b5 Iustin Pop
-- | Parse the environment and return the node/instance names.
52 8032b3b5 Iustin Pop
-- This also hardcodes here the default node/instance file names.
53 8032b3b5 Iustin Pop
parseEnv :: () -> IO (String, String)
54 8032b3b5 Iustin Pop
parseEnv () = do
55 8032b3b5 Iustin Pop
  a <- getEnvDefault "HTOOLS_NODES" "nodes"
56 8032b3b5 Iustin Pop
  b <- getEnvDefault "HTOOLS_INSTANCES" "instances"
57 8032b3b5 Iustin Pop
  return (a, b)
58 209b3711 Iustin Pop
59 209b3711 Iustin Pop
-- | Return a version string for the program
60 209b3711 Iustin Pop
showVersion :: String -- ^ The program name
61 209b3711 Iustin Pop
            -> String -- ^ The formatted version and other information data
62 209b3711 Iustin Pop
showVersion name =
63 209b3711 Iustin Pop
    printf "%s %s\ncompiled with %s %s\nrunning on %s %s\n"
64 209b3711 Iustin Pop
           name Version.version
65 209b3711 Iustin Pop
           compilerName (Data.Version.showVersion compilerVersion)
66 209b3711 Iustin Pop
           os arch
67 e0eb63f0 Iustin Pop
68 e0eb63f0 Iustin Pop
-- | A shell script template for autogenerated scripts
69 e0eb63f0 Iustin Pop
shTemplate :: String
70 e0eb63f0 Iustin Pop
shTemplate =
71 e0eb63f0 Iustin Pop
    printf "#!/bin/sh\n\n\
72 e0eb63f0 Iustin Pop
           \# Auto-generated script for executing cluster rebalancing\n\n\
73 e0eb63f0 Iustin Pop
           \# To stop, touch the file /tmp/stop-htools\n\n\
74 e0eb63f0 Iustin Pop
           \set -e\n\n\
75 e0eb63f0 Iustin Pop
           \check() {\n\
76 e0eb63f0 Iustin Pop
           \  if [ -f /tmp/stop-htools ]; then\n\
77 e0eb63f0 Iustin Pop
           \    echo 'Stop requested, exiting'\n\
78 e0eb63f0 Iustin Pop
           \    exit 0\n\
79 e0eb63f0 Iustin Pop
           \  fi\n\
80 e0eb63f0 Iustin Pop
           \}\n\n"