Fix hspace's KM metrics
[ganeti-local] / Ganeti / HTools / Simu.hs
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 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     ) where
32
33 import Text.Printf (printf)
34
35 import Ganeti.HTools.Utils
36 import Ganeti.HTools.Types
37 import qualified Ganeti.HTools.Node as Node
38 import qualified Ganeti.HTools.Instance as Instance
39
40 -- | Parse the string description into nodes
41 parseDesc :: String -> Result (Int, Int, Int, Int)
42 parseDesc desc =
43     case sepSplit ',' desc of
44       [n, d, m, c] -> do
45         ncount <- tryRead "node count" n
46         disk <- tryRead "disk size" d
47         mem <- tryRead "memory size" m
48         cpu <- tryRead "cpu count" c
49         return (ncount, disk, mem, cpu)
50       _ -> fail "Invalid cluster specification"
51
52 -- | Builds the cluster data from node\/instance files.
53 loadData :: String -- ^ Cluster description in text format
54          -> IO (Result (Node.AssocList, Instance.AssocList, [String]))
55 loadData ndata = -- IO monad, just for consistency with the other loaders
56   return $ do
57     (cnt, disk, mem, cpu) <- parseDesc ndata
58     let nodes = map (\idx ->
59                          let n = Node.create (printf "node%03d" idx)
60                                  (fromIntegral mem) 0 mem
61                                  (fromIntegral disk) disk
62                                  (fromIntegral cpu) False
63                          in (idx, Node.setIdx n idx)
64                     ) [1..cnt]
65     return (nodes, [], [])