Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / HTools / Program / Hail.hs @ 3add7574

History | View | Annotate | Download (2.8 kB)

1
{-| IAllocator plugin for Ganeti.
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 2009, 2010, 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.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
  when (null args) $ exitErr "This program needs an input file."
63

    
64
  r1 <- readRequest (head args)
65
  if isJust (optDataFile opts) ||  (not . null . optNodeSim) opts
66
    then do
67
      cdata <- loadExternalData opts
68
      let Request rqt _ = r1
69
      return $ Request rqt cdata
70
    else return r1
71

    
72

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

    
80
  request <- wrapReadRequest opts args
81

    
82
  let Request rq cdata = request
83

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

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

    
90
  maybePrintNodes shownodes "Initial cluster"
91
       (Cluster.printNodes (cdNodes cdata))
92

    
93
  maybeSaveData savecluster "pre-ialloc" "before iallocator run" cdata
94

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

    
99
  maybePrintNodes shownodes "Final cluster" (Cluster.printNodes fin_nl)
100

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