Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Program / Hail.hs @ 707cd3d7

History | View | Annotate | Download (2.7 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.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 :: [OptType]
47
options =
48
  [ oPrintNodes
49
  , oSaveCluster
50
  , oDataFile
51
  , oNodeSim
52
  , oVerbose
53
  ]
54

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

    
59
wrapReadRequest :: Options -> [String] -> IO Request
60
wrapReadRequest opts args = do
61
  when (null args) $ exitErr "This program needs an input file."
62

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

    
71

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

    
79
  request <- wrapReadRequest opts args
80

    
81
  let Request rq cdata = request
82

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

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

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

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

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

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

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