Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / HTools / Program / Hail.hs @ 2d6bdcc5

History | View | Annotate | Download (2.9 kB)

1
{-| IAllocator plugin for Ganeti.
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 2009, 2010, 2011, 2012, 2013 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.Hail
27
  ( main
28
  , options
29
  , arguments
30
  ) where
31

    
32
import Control.Monad
33
import Data.Maybe (fromMaybe, isJust)
34
import System.IO
35

    
36
import qualified Ganeti.HTools.Cluster as Cluster
37

    
38
import Ganeti.Common
39
import Ganeti.HTools.CLI
40
import Ganeti.HTools.Backend.IAlloc
41
import Ganeti.HTools.Loader (Request(..), ClusterData(..))
42
import Ganeti.HTools.ExtLoader (maybeSaveData, loadExternalData)
43
import Ganeti.Utils
44

    
45
-- | Options list and functions.
46
options :: IO [OptType]
47
options =
48
  return
49
    [ oPrintNodes
50
    , oSaveCluster
51
    , oDataFile
52
    , oNodeSim
53
    , oVerbose
54
    ]
55

    
56
-- | The list of arguments supported by the program.
57
arguments :: [ArgCompletion]
58
arguments = [ArgCompletion OptComplFile 1 (Just 1)]
59

    
60
wrapReadRequest :: Options -> [String] -> IO Request
61
wrapReadRequest opts args = do
62
  r1 <- case args of
63
          []    -> exitErr "This program needs an input file."
64
          _:_:_ -> exitErr "Only one argument is accepted (the input file)"
65
          x:_   -> readRequest x
66

    
67
  if isJust (optDataFile opts) ||  (not . null . optNodeSim) opts
68
    then do
69
      cdata <- loadExternalData opts
70
      let Request rqt _ = r1
71
      return $ Request rqt cdata
72
    else return r1
73

    
74

    
75
-- | Main function.
76
main :: Options -> [String] -> IO ()
77
main opts args = do
78
  let shownodes = optShowNodes opts
79
      verbose = optVerbose opts
80
      savecluster = optSaveCluster opts
81

    
82
  request <- wrapReadRequest opts args
83

    
84
  let Request rq cdata = request
85

    
86
  when (verbose > 1) .
87
       hPutStrLn stderr $ "Received request: " ++ show rq
88

    
89
  when (verbose > 2) .
90
       hPutStrLn stderr $ "Received cluster data: " ++ show cdata
91

    
92
  maybePrintNodes shownodes "Initial cluster"
93
       (Cluster.printNodes (cdNodes cdata))
94

    
95
  maybeSaveData savecluster "pre-ialloc" "before iallocator run" cdata
96

    
97
  let (maybe_ni, resp) = runIAllocator request
98
      (fin_nl, fin_il) = fromMaybe (cdNodes cdata, cdInstances cdata) maybe_ni
99
  putStrLn resp
100

    
101
  maybePrintNodes shownodes "Final cluster" (Cluster.printNodes fin_nl)
102

    
103
  maybeSaveData savecluster "post-ialloc" "after iallocator run"
104
       (cdata { cdNodes = fin_nl, cdInstances = fin_il})