Node operations: take into account auto_balance
[ganeti-local] / htools / 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, 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 Text.Printf (printf)
35
36 import Ganeti.HTools.Utils
37 import Ganeti.HTools.Types
38 import Ganeti.HTools.Loader
39 import qualified Ganeti.HTools.Container as Container
40 import qualified Ganeti.HTools.Group as Group
41 import qualified Ganeti.HTools.Node as Node
42
43 -- | Parse the string description into nodes.
44 parseDesc :: String -> Result (AllocPolicy, Int, Int, Int, Int)
45 parseDesc desc =
46     case sepSplit ',' desc of
47       [a, n, d, m, c] -> do
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)
54       es -> fail $ printf
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
58
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
70                   ) [1..ncount]
71       grp = Group.create (printf "group-%02d" grpIndex)
72             (printf "fake-uuid-%02d" grpIndex) apol
73   return (Group.setIdx grp grpIndex, nodes)
74
75 -- | Builds the cluster data from node\/instance files.
76 parseData :: [String] -- ^ Cluster description in text format
77           -> Result ClusterData
78 parseData ndata = do
79   grpNodeData <- mapM (uncurry createGroup) $ zip [1..] ndata
80   let (groups, nodes) = unzip grpNodeData
81       nodes' = concat nodes
82   let ktn = map (\(idx, n) -> (idx, Node.setIdx n idx))
83             $ zip [1..] nodes'
84       ktg = map (\g -> (Group.idx g, g)) groups
85   return (ClusterData (Container.fromList ktg)
86                       (Container.fromList ktn) Container.empty [])
87
88 -- | Builds the cluster data from node\/instance files.
89 loadData :: [String] -- ^ Cluster description in text format
90          -> IO (Result ClusterData)
91 loadData = -- IO monad, just for consistency with the other loaders
92   return . parseData