Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Simu.hs @ 2c9336a4

History | View | Annotate | Download (3.8 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 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
    (
30
      loadData
31
    , parseData
32
    ) where
33

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

    
37
import Ganeti.HTools.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 -> Result (AllocPolicy, Int, Int, Int, Int)
54
parseDesc desc =
55
    case sepSplit ',' desc of
56
      [a, n, d, m, c] -> do
57
        apol <- allocPolicyFromString a `mplus` apolAbbrev a
58
        ncount <- tryRead "node count" n
59
        disk <- annotateResult "disk size" (parseUnit d)
60
        mem <- annotateResult "memory size" (parseUnit m)
61
        cpu <- tryRead "cpu count" c
62
        return (apol, ncount, disk, mem, cpu)
63
      es -> fail $ printf
64
            "Invalid cluster specification, expected 5 comma-separated\
65
            \ sections (allocation policy, node count, disk size,\
66
            \ memory size, number of CPUs) but got %d: '%s'" (length es) desc
67

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

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

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