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 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.Node as Node
39 import qualified Ganeti.HTools.Instance as Instance
41 -- | Parse results from readsPrec
42 parseChoices :: (Monad m, Read a) => String -> String -> [(a, String)] -> m a
43 parseChoices _ _ ((v, ""):[]) = return v
44 parseChoices name s ((_, e):[]) =
45 fail $ name ++ ": leftover characters when parsing '"
46 ++ s ++ "': '" ++ e ++ "'"
47 parseChoices name s _ = fail $ name ++ ": cannot parse string '" ++ s ++ "'"
49 -- | Safe 'read' function returning data encapsulated in a Result.
50 tryRead :: (Monad m, Read a) => String -> String -> m a
51 tryRead name s = parseChoices name s $ reads s
53 -- | Parse the string description into nodes
54 parseDesc :: String -> Result (Int, Int, Int, Int)
56 case sepSplit ',' desc of
58 ncount <- tryRead "node count" n
59 disk <- tryRead "disk size" d
60 mem <- tryRead "memory size" m
61 cpu <- tryRead "cpu count" c
62 return (ncount, disk, mem, cpu)
63 _ -> fail "Invalid cluster specification"
65 -- | Builds the cluster data from node\/instance files.
66 loadData :: String -- ^ Cluster description in text format
67 -> IO (Result (Node.AssocList, Instance.AssocList))
68 loadData ndata = -- IO monad, just for consistency with the other loaders
70 (cnt, disk, mem, cpu) <- parseDesc ndata
71 let nodes = map (\idx ->
72 let n = Node.create (printf "node%03d" idx)
73 (fromIntegral mem) 0 mem
74 (fromIntegral disk) disk
75 (fromIntegral cpu) False
76 in (idx, Node.setIdx n idx)