Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Simu.hs @ 23fe06c2

History | View | Annotate | Download (3.8 kB)

1 525bfb36 Iustin Pop
{-| Parsing data from a simulated description of the cluster.
2 b2278348 Iustin Pop
3 b2278348 Iustin Pop
This module holds the code for parsing a cluster description.
4 b2278348 Iustin Pop
5 b2278348 Iustin Pop
-}
6 b2278348 Iustin Pop
7 b2278348 Iustin Pop
{-
8 b2278348 Iustin Pop
9 7af6975a Iustin Pop
Copyright (C) 2009, 2010, 2011 Google Inc.
10 b2278348 Iustin Pop
11 b2278348 Iustin Pop
This program is free software; you can redistribute it and/or modify
12 b2278348 Iustin Pop
it under the terms of the GNU General Public License as published by
13 b2278348 Iustin Pop
the Free Software Foundation; either version 2 of the License, or
14 b2278348 Iustin Pop
(at your option) any later version.
15 b2278348 Iustin Pop
16 b2278348 Iustin Pop
This program is distributed in the hope that it will be useful, but
17 b2278348 Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
18 b2278348 Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 b2278348 Iustin Pop
General Public License for more details.
20 b2278348 Iustin Pop
21 b2278348 Iustin Pop
You should have received a copy of the GNU General Public License
22 b2278348 Iustin Pop
along with this program; if not, write to the Free Software
23 b2278348 Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 b2278348 Iustin Pop
02110-1301, USA.
25 b2278348 Iustin Pop
26 b2278348 Iustin Pop
-}
27 b2278348 Iustin Pop
28 b2278348 Iustin Pop
module Ganeti.HTools.Simu
29 b2278348 Iustin Pop
    (
30 b2278348 Iustin Pop
      loadData
31 5e718042 Iustin Pop
    , parseData
32 b2278348 Iustin Pop
    ) where
33 b2278348 Iustin Pop
34 2ef8013f Iustin Pop
import Control.Monad (mplus)
35 b2278348 Iustin Pop
import Text.Printf (printf)
36 b2278348 Iustin Pop
37 b2278348 Iustin Pop
import Ganeti.HTools.Utils
38 b2278348 Iustin Pop
import Ganeti.HTools.Types
39 f4f6eb0b Iustin Pop
import Ganeti.HTools.Loader
40 99b63608 Iustin Pop
import qualified Ganeti.HTools.Container as Container
41 a679e9dc Iustin Pop
import qualified Ganeti.HTools.Group as Group
42 b2278348 Iustin Pop
import qualified Ganeti.HTools.Node as Node
43 b2278348 Iustin Pop
44 2ef8013f Iustin Pop
-- | Parse a shortened policy string (for command line usage).
45 2ef8013f Iustin Pop
apolAbbrev :: String -> Result AllocPolicy
46 2ef8013f Iustin Pop
apolAbbrev c | c == "p"  = return AllocPreferred
47 2ef8013f Iustin Pop
             | c == "a"  = return AllocLastResort
48 2ef8013f Iustin Pop
             | c == "u"  = return AllocUnallocable
49 2ef8013f Iustin Pop
             | otherwise = fail $ "Cannot parse AllocPolicy abbreviation '"
50 2ef8013f Iustin Pop
                           ++ c ++ "'"
51 2ef8013f Iustin Pop
52 9983063b Iustin Pop
-- | Parse the string description into nodes.
53 6c7448bb Iustin Pop
parseDesc :: String -> Result (AllocPolicy, Int, Int, Int, Int)
54 b2278348 Iustin Pop
parseDesc desc =
55 b2278348 Iustin Pop
    case sepSplit ',' desc of
56 6c7448bb Iustin Pop
      [a, n, d, m, c] -> do
57 2c9336a4 Iustin Pop
        apol <- allocPolicyFromString a `mplus` apolAbbrev a
58 b2278348 Iustin Pop
        ncount <- tryRead "node count" n
59 247f77b7 Iustin Pop
        disk <- annotateResult "disk size" (parseUnit d)
60 247f77b7 Iustin Pop
        mem <- annotateResult "memory size" (parseUnit m)
61 b2278348 Iustin Pop
        cpu <- tryRead "cpu count" c
62 6c7448bb Iustin Pop
        return (apol, ncount, disk, mem, cpu)
63 6c7448bb Iustin Pop
      es -> fail $ printf
64 6c7448bb Iustin Pop
            "Invalid cluster specification, expected 5 comma-separated\
65 6c7448bb Iustin Pop
            \ sections (allocation policy, node count, disk size,\
66 6c7448bb Iustin Pop
            \ memory size, number of CPUs) but got %d: '%s'" (length es) desc
67 b2278348 Iustin Pop
68 9983063b Iustin Pop
-- | Creates a node group with the given specifications.
69 9983063b Iustin Pop
createGroup :: Int    -- ^ The group index
70 9983063b Iustin Pop
            -> String -- ^ The group specification
71 9983063b Iustin Pop
            -> Result (Group.Group, [Node.Node])
72 9983063b Iustin Pop
createGroup grpIndex spec = do
73 6c7448bb Iustin Pop
  (apol, ncount, disk, mem, cpu) <- parseDesc spec
74 9983063b Iustin Pop
  let nodes = map (\idx ->
75 9983063b Iustin Pop
                       Node.create (printf "node-%02d-%03d" grpIndex idx)
76 9983063b Iustin Pop
                               (fromIntegral mem) 0 mem
77 9983063b Iustin Pop
                               (fromIntegral disk) disk
78 9983063b Iustin Pop
                               (fromIntegral cpu) False grpIndex
79 9983063b Iustin Pop
                  ) [1..ncount]
80 9983063b Iustin Pop
      grp = Group.create (printf "group-%02d" grpIndex)
81 6c7448bb Iustin Pop
            (printf "fake-uuid-%02d" grpIndex) apol
82 7af6975a Iustin Pop
  return (Group.setIdx grp grpIndex, nodes)
83 9983063b Iustin Pop
84 b2278348 Iustin Pop
-- | Builds the cluster data from node\/instance files.
85 9983063b Iustin Pop
parseData :: [String] -- ^ Cluster description in text format
86 f4f6eb0b Iustin Pop
          -> Result ClusterData
87 5e718042 Iustin Pop
parseData ndata = do
88 9983063b Iustin Pop
  grpNodeData <- mapM (uncurry createGroup) $ zip [1..] ndata
89 9983063b Iustin Pop
  let (groups, nodes) = unzip grpNodeData
90 9983063b Iustin Pop
      nodes' = concat nodes
91 9983063b Iustin Pop
  let ktn = map (\(idx, n) -> (idx, Node.setIdx n idx))
92 9983063b Iustin Pop
            $ zip [1..] nodes'
93 9983063b Iustin Pop
      ktg = map (\g -> (Group.idx g, g)) groups
94 cb0c77ff Iustin Pop
  return (ClusterData (Container.fromList ktg)
95 cb0c77ff Iustin Pop
                      (Container.fromList ktn) Container.empty [])
96 5e718042 Iustin Pop
97 5e718042 Iustin Pop
-- | Builds the cluster data from node\/instance files.
98 9983063b Iustin Pop
loadData :: [String] -- ^ Cluster description in text format
99 f4f6eb0b Iustin Pop
         -> IO (Result ClusterData)
100 5e718042 Iustin Pop
loadData = -- IO monad, just for consistency with the other loaders
101 5e718042 Iustin Pop
  return . parseData