1 {-| Parsing data from a simulated description of the cluster
3 This module holds the code for parsing a cluster description.
9 Copyright (C) 2009, 2010 Google Inc.
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.
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.
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
28 module Ganeti.HTools.Simu
34 import Text.Printf (printf)
36 import Ganeti.HTools.Utils
37 import Ganeti.HTools.Types
38 import qualified Ganeti.HTools.Container as Container
39 import qualified Ganeti.HTools.Group as Group
40 import qualified Ganeti.HTools.Node as Node
41 import qualified Ganeti.HTools.Instance as Instance
43 -- | Parse the string description into nodes.
44 parseDesc :: String -> Result (AllocPolicy, Int, Int, Int, Int)
46 case sepSplit ',' desc of
48 apol <- apolFromString a
49 ncount <- tryRead "node count" n
50 disk <- tryRead "disk size" d
51 mem <- tryRead "memory size" m
52 cpu <- tryRead "cpu count" c
53 return (apol, ncount, disk, mem, cpu)
55 "Invalid cluster specification, expected 5 comma-separated\
56 \ sections (allocation policy, node count, disk size,\
57 \ memory size, number of CPUs) but got %d: '%s'" (length es) desc
59 -- | Creates a node group with the given specifications.
60 createGroup :: Int -- ^ The group index
61 -> String -- ^ The group specification
62 -> Result (Group.Group, [Node.Node])
63 createGroup grpIndex spec = do
64 (apol, ncount, disk, mem, cpu) <- parseDesc spec
65 let nodes = map (\idx ->
66 Node.create (printf "node-%02d-%03d" grpIndex idx)
67 (fromIntegral mem) 0 mem
68 (fromIntegral disk) disk
69 (fromIntegral cpu) False grpIndex
71 grp = Group.create (printf "group-%02d" grpIndex)
72 (printf "fake-uuid-%02d" grpIndex) apol
75 -- | Builds the cluster data from node\/instance files.
76 parseData :: [String] -- ^ Cluster description in text format
77 -> Result (Group.List, Node.List, Instance.List, [String])
79 grpNodeData <- mapM (uncurry createGroup) $ zip [1..] ndata
80 let (groups, nodes) = unzip grpNodeData
82 let ktn = map (\(idx, n) -> (idx, Node.setIdx n idx))
84 ktg = map (\g -> (Group.idx g, g)) groups
85 return (Container.fromAssocList ktg,
86 Container.fromAssocList ktn, Container.empty, [])
88 -- | Builds the cluster data from node\/instance files.
89 loadData :: [String] -- ^ Cluster description in text format
90 -> IO (Result (Group.List, Node.List, Instance.List, [String]))
91 loadData = -- IO monad, just for consistency with the other loaders