Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / HTools / Program / Main.hs @ b6d9bec8

History | View | Annotate | Download (4.3 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.Harep as Harep
42
import qualified Ganeti.HTools.Program.Hbal as Hbal
43
import qualified Ganeti.HTools.Program.Hcheck as Hcheck
44
import qualified Ganeti.HTools.Program.Hscan as Hscan
45
import qualified Ganeti.HTools.Program.Hspace as Hspace
46
import qualified Ganeti.HTools.Program.Hinfo as Hinfo
47
import qualified Ganeti.HTools.Program.Hroller as Hroller
48
import Ganeti.Utils
49

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

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

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