Hail.hs 2.76 KB
Newer Older
1
{-| IAllocator plugin for Ganeti.
Iustin Pop's avatar
Iustin Pop committed
2
3
4

-}

Iustin Pop's avatar
Iustin Pop committed
5
6
{-

7
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
Iustin Pop's avatar
Iustin Pop committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

-}

26
27
28
29
30
module Ganeti.HTools.Program.Hail
  ( main
  , options
  , arguments
  ) where
Iustin Pop's avatar
Iustin Pop committed
31

Iustin Pop's avatar
Iustin Pop committed
32
import Control.Monad
33
import Data.Maybe (fromMaybe, isJust)
Iustin Pop's avatar
Iustin Pop committed
34
35
36
import System.IO

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
Iustin Pop's avatar
Iustin Pop committed
44

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

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

60
61
wrapReadRequest :: Options -> [String] -> IO Request
wrapReadRequest opts args = do
62
  when (null args) $ exitErr "This program needs an input file."
63
64
65
66
67
68
69
70
71
72

  r1 <- readRequest (head args)
  if isJust (optDataFile opts) ||  (not . null . optNodeSim) opts
    then do
      cdata <- loadExternalData opts
      let Request rqt _ = r1
      return $ Request rqt cdata
    else return r1


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

80
  request <- wrapReadRequest opts args
Iustin Pop's avatar
Iustin Pop committed
81

82
  let Request rq cdata = request
83

Iustin Pop's avatar
Iustin Pop committed
84
  when (verbose > 1) .
85
86
       hPutStrLn stderr $ "Received request: " ++ show rq

Iustin Pop's avatar
Iustin Pop committed
87
  when (verbose > 2) .
88
89
       hPutStrLn stderr $ "Received cluster data: " ++ show cdata

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

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

95
  let (maybe_ni, resp) = runIAllocator request
Iustin Pop's avatar
Iustin Pop committed
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
102

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