Simu.hs 3.13 KB
Newer Older
1
2
3
4
5
6
7
8
{-| Parsing data from a simulated description of the cluster

This module holds the code for parsing a cluster description.

-}

{-

9
Copyright (C) 2009, 2010 Google Inc.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

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.

-}

module Ganeti.HTools.Simu
    (
      loadData
31
    , parseData
32
33
34
35
36
37
    ) where

import Text.Printf (printf)

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

43
-- | Parse the string description into nodes.
44
45
46
parseDesc :: String -> Result (Int, Int, Int, Int)
parseDesc desc =
    case sepSplit ',' desc of
47
      [n, d, m, c] -> do
48
49
50
51
52
53
54
        ncount <- tryRead "node count" n
        disk <- tryRead "disk size" d
        mem <- tryRead "memory size" m
        cpu <- tryRead "cpu count" c
        return (ncount, disk, mem, cpu)
      _ -> fail "Invalid cluster specification"

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
-- | Creates a node group with the given specifications.
createGroup :: Int    -- ^ The group index
            -> String -- ^ The group specification
            -> Result (Group.Group, [Node.Node])
createGroup grpIndex spec = do
  (ncount, disk, mem, cpu) <- parseDesc spec
  let nodes = map (\idx ->
                       Node.create (printf "node-%02d-%03d" grpIndex idx)
                               (fromIntegral mem) 0 mem
                               (fromIntegral disk) disk
                               (fromIntegral cpu) False grpIndex
                  ) [1..ncount]
      grp = Group.create (printf "group-%02d" grpIndex)
            (printf "fake-uuid-%02d" grpIndex) AllocPreferred
  return (grp, nodes)

71
-- | Builds the cluster data from node\/instance files.
72
73
parseData :: [String] -- ^ Cluster description in text format
          -> Result (Group.List, Node.List, Instance.List, [String])
74
parseData ndata = do
75
76
77
78
79
80
81
82
  grpNodeData <- mapM (uncurry createGroup) $ zip [1..] ndata
  let (groups, nodes) = unzip grpNodeData
      nodes' = concat nodes
  let ktn = map (\(idx, n) -> (idx, Node.setIdx n idx))
            $ zip [1..] nodes'
      ktg = map (\g -> (Group.idx g, g)) groups
  return (Container.fromAssocList ktg,
          Container.fromAssocList ktn, Container.empty, [])
83

84
-- | Builds the cluster data from node\/instance files.
85
loadData :: [String] -- ^ Cluster description in text format
86
         -> IO (Result (Group.List, Node.List, Instance.List, [String]))
87
88
loadData = -- IO monad, just for consistency with the other loaders
  return . parseData