Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / HTools / Backend / Simu.hs @ 30dd3377

History | View | Annotate | Download (4 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 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)
55
parseDesc _ [a, n, d, m, c, s] = 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
  return (apol, ncount, disk, mem, cpu, spindles)
63

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

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

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

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

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