Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / HTools / Backend / Simu.hs @ c324da14

History | View | Annotate | Download (4.3 kB)

1
{-| Parsing data from a simulated description of the cluster.
2

    
3
This module holds the code for parsing a cluster description.
4

    
5
-}
6

    
7
{-
8

    
9
Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc.
10

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

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

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

    
26
-}
27

    
28
module Ganeti.HTools.Backend.Simu
29
  ( loadData
30
  , parseData
31
  ) where
32

    
33
import Control.Monad (mplus, zipWithM)
34
import Text.Printf (printf)
35

    
36
import Ganeti.BasicTypes
37
import Ganeti.Utils
38
import Ganeti.HTools.Types
39
import Ganeti.HTools.Loader
40
import qualified Ganeti.HTools.Container as Container
41
import qualified Ganeti.HTools.Group as Group
42
import qualified Ganeti.HTools.Node as Node
43

    
44
-- | Parse a shortened policy string (for command line usage).
45
apolAbbrev :: String -> Result AllocPolicy
46
apolAbbrev c | c == "p"  = return AllocPreferred
47
             | c == "a"  = return AllocLastResort
48
             | c == "u"  = return AllocUnallocable
49
             | otherwise = fail $ "Cannot parse AllocPolicy abbreviation '"
50
                           ++ c ++ "'"
51

    
52
-- | Parse the string description into nodes.
53
parseDesc :: String -> [String]
54
          -> Result (AllocPolicy, Int, Int, Int, Int, Int, Bool)
55
parseDesc _ [a, n, d, m, c, s, exstor] = do
56
  apol <- allocPolicyFromRaw a `mplus` apolAbbrev a
57
  ncount <- tryRead "node count" n
58
  disk <- annotateResult "disk size" (parseUnit d)
59
  mem <- annotateResult "memory size" (parseUnit m)
60
  cpu <- tryRead "cpu count" c
61
  spindles <- tryRead "spindles" s
62
  excl_stor <- tryRead "exclusive storage" exstor
63
  return (apol, ncount, disk, mem, cpu, spindles, excl_stor)
64

    
65
parseDesc desc [a, n, d, m, c] = parseDesc desc [a, n, d, m, c, "1"]
66

    
67
parseDesc desc [a, n, d, m, c, s] = parseDesc desc [a, n, d, m, c, s, "False"]
68

    
69
parseDesc desc es =
70
  fail $ printf
71
         "Invalid cluster specification, expected 6 comma-separated\
72
         \ sections (allocation policy, node count, disk size,\
73
         \ memory size, number of CPUs, spindles) but got %d: '%s'"
74
         (length es) desc
75

    
76
-- | Creates a node group with the given specifications.
77
createGroup :: Int    -- ^ The group index
78
            -> String -- ^ The group specification
79
            -> Result (Group.Group, [Node.Node])
80
createGroup grpIndex spec = do
81
  (apol, ncount, disk, mem, cpu, spindles, excl_stor) <- parseDesc spec $
82
                                                         sepSplit ',' spec
83
  let nodes = map (\idx ->
84
                    flip Node.setMaster (grpIndex == 1 && idx == 1) $
85
                    Node.create (printf "node-%02d-%03d" grpIndex idx)
86
                      (fromIntegral mem) 0 mem
87
                      (fromIntegral disk) disk
88
                      (fromIntegral cpu) False spindles grpIndex excl_stor
89
                  ) [1..ncount]
90
      -- TODO: parse networks to which this group is connected
91
      grp = Group.create (printf "group-%02d" grpIndex)
92
            (printf "fake-uuid-%02d" grpIndex) apol [] defIPolicy []
93
  return (Group.setIdx grp grpIndex, nodes)
94

    
95
-- | Builds the cluster data from node\/instance files.
96
parseData :: [String] -- ^ Cluster description in text format
97
          -> Result ClusterData
98
parseData ndata = do
99
  grpNodeData <- zipWithM createGroup [1..] ndata
100
  let (groups, nodes) = unzip grpNodeData
101
      nodes' = concat nodes
102
  let ktn = map (\(idx, n) -> (idx, Node.setIdx n idx))
103
            $ zip [1..] nodes'
104
      ktg = map (\g -> (Group.idx g, g)) groups
105
  return (ClusterData (Container.fromList ktg)
106
                      (Container.fromList ktn) Container.empty [] defIPolicy)
107

    
108
-- | Builds the cluster data from node\/instance files.
109
loadData :: [String] -- ^ Cluster description in text format
110
         -> IO (Result ClusterData)
111
loadData = -- IO monad, just for consistency with the other loaders
112
  return . parseData