Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / HTools / Program / Main.hs @ 2fd5a116

History | View | Annotate | Download (4.1 kB)

1
{-| Small module holding program definitions.
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 2011, 2012 Google Inc.
8

    
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
13

    
14
This program is distributed in the hope that it will be useful, but
15
WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
General Public License for more details.
18

    
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
02110-1301, USA.
23

    
24
-}
25

    
26
module Ganeti.HTools.Program.Main
27
  ( personalities
28
  , main
29
  ) where
30

    
31
import Control.Exception
32
import Control.Monad (guard)
33
import Data.Char (toLower)
34
import System.Environment
35
import System.IO
36
import System.IO.Error (isDoesNotExistError)
37

    
38
import Ganeti.Common (formatCommands, PersonalityList)
39
import Ganeti.HTools.CLI (Options, parseOpts, genericOpts)
40
import qualified Ganeti.HTools.Program.Hail as Hail
41
import qualified Ganeti.HTools.Program.Hbal as Hbal
42
import qualified Ganeti.HTools.Program.Hcheck as Hcheck
43
import qualified Ganeti.HTools.Program.Hscan as Hscan
44
import qualified Ganeti.HTools.Program.Hspace as Hspace
45
import qualified Ganeti.HTools.Program.Hinfo as Hinfo
46
import qualified Ganeti.HTools.Program.Hroller as Hroller
47
import Ganeti.Utils
48

    
49
-- | Supported binaries.
50
personalities :: PersonalityList Options
51
personalities =
52
  [ ("hail",    (Hail.main,    Hail.options,    Hail.arguments,
53
                 "Ganeti IAllocator plugin that implements the instance\
54
                 \ placement and movement using the same algorithm as\
55
                 \ hbal(1)"))
56
  , ("hbal",    (Hbal.main,    Hbal.options,    Hbal.arguments,
57
                 "cluster balancer that looks at the current state of\
58
                 \ the cluster and computes a series of steps designed\
59
                 \ to bring the cluster into a better state"))
60
  , ("hcheck",  (Hcheck.main,  Hcheck.options,  Hcheck.arguments,
61
                "cluster checker; prints information about cluster's\
62
                \ health and checks whether a rebalance done using\
63
                \ hbal would help"))
64
  , ("hscan",   (Hscan.main,   Hscan.options,   Hscan.arguments,
65
                "tool for scanning clusters via RAPI and saving their\
66
                \ data in the input format used by hbal(1) and hspace(1)"))
67
  , ("hspace",  (Hspace.main,  Hspace.options,  Hspace.arguments,
68
                "computes how many additional instances can be fit on a\
69
                \ cluster, while maintaining N+1 status."))
70
  , ("hinfo",   (Hinfo.main,   Hinfo.options,   Hinfo.arguments,
71
                "cluster information printer; it prints information\
72
                \ about the current cluster state and its residing\
73
                \ nodes/instances"))
74
  , ("hroller", (Hroller.main, Hroller.options, Hroller.arguments,
75
                "cluster rolling maintenance helper; it helps scheduling\
76
                \ node reboots in a manner that doesn't conflict with the\
77
                \ instances' topology"))
78
  ]
79

    
80
-- | Display usage and exit.
81
usage :: String -> IO ()
82
usage name = do
83
  hPutStrLn stderr $ "Unrecognised personality '" ++ name ++ "'."
84
  hPutStrLn stderr "This program must be installed under one of the following\
85
                   \ names:"
86
  hPutStrLn stderr . unlines $ formatCommands personalities
87
  exitErr "Please either rename/symlink the program or set\n\
88
          \the environment variable HTOOLS to the desired role."
89

    
90
main :: IO ()
91
main = do
92
  binary <- catchJust (guard . isDoesNotExistError)
93
            (getEnv "HTOOLS") (const getProgName)
94
  let name = map toLower binary
95
  case name `lookup` personalities of
96
    Nothing -> usage name
97
    Just (fn, options, arguments, _) -> do
98
         cmd_args <- getArgs
99
         real_options <- options
100
         (opts, args) <- parseOpts cmd_args name (real_options ++ genericOpts)
101
                           arguments
102
         fn opts args