Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Simu.hs @ 0c7d4422

History | View | Annotate | Download (3.9 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.Simu
29
  ( loadData
30
  , parseData
31
  ) where
32

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

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

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

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

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

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

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

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

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