Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / HTools / Program / Hail.hs @ c32c4e4d

History | View | Annotate | Download (3.1 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
                               , queryAllMonDDCs)
44
import Ganeti.Utils
45

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

    
59
-- | The list of arguments supported by the program.
60
arguments :: [ArgCompletion]
61
arguments = [ArgCompletion OptComplFile 1 (Just 1)]
62

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

    
70
  if isJust (optDataFile opts) ||  (not . null . optNodeSim) opts
71
    then do
72
      cdata <- loadExternalData opts
73
      let Request rqt _ = r1
74
      return $ Request rqt cdata
75
    else do
76
      let Request rqt cdata = r1
77
      cdata' <-
78
        if optMonD opts then queryAllMonDDCs cdata opts else return cdata
79
      return $ Request rqt cdata'
80

    
81
-- | Main function.
82
main :: Options -> [String] -> IO ()
83
main opts args = do
84
  let shownodes = optShowNodes opts
85
      verbose = optVerbose opts
86
      savecluster = optSaveCluster opts
87

    
88
  request <- wrapReadRequest opts args
89

    
90
  let Request rq cdata = request
91

    
92
  when (verbose > 1) .
93
       hPutStrLn stderr $ "Received request: " ++ show rq
94

    
95
  when (verbose > 2) .
96
       hPutStrLn stderr $ "Received cluster data: " ++ show cdata
97

    
98
  maybePrintNodes shownodes "Initial cluster"
99
       (Cluster.printNodes (cdNodes cdata))
100

    
101
  maybeSaveData savecluster "pre-ialloc" "before iallocator run" cdata
102

    
103
  let (maybe_ni, resp) = runIAllocator request
104
      (fin_nl, fin_il) = fromMaybe (cdNodes cdata, cdInstances cdata) maybe_ni
105
  putStrLn resp
106

    
107
  maybePrintNodes shownodes "Final cluster" (Cluster.printNodes fin_nl)
108

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