Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Text.hs @ d575c755

History | View | Annotate | Download (12.9 kB)

1 525bfb36 Iustin Pop
{-| Parsing data from text-files.
2 040afc35 Iustin Pop
3 040afc35 Iustin Pop
This module holds the code for loading the cluster state from text
4 525bfb36 Iustin Pop
files, as produced by @gnt-node@ and @gnt-instance@ @list@ command.
5 040afc35 Iustin Pop
6 040afc35 Iustin Pop
-}
7 040afc35 Iustin Pop
8 e2fa2baf Iustin Pop
{-
9 e2fa2baf Iustin Pop
10 b37f4a76 Iustin Pop
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
11 e2fa2baf Iustin Pop
12 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
13 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
14 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
15 e2fa2baf Iustin Pop
(at your option) any later version.
16 e2fa2baf Iustin Pop
17 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
18 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
19 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 e2fa2baf Iustin Pop
General Public License for more details.
21 e2fa2baf Iustin Pop
22 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
23 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
24 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 e2fa2baf Iustin Pop
02110-1301, USA.
26 e2fa2baf Iustin Pop
27 e2fa2baf Iustin Pop
-}
28 e2fa2baf Iustin Pop
29 040afc35 Iustin Pop
module Ganeti.HTools.Text
30 ebf38064 Iustin Pop
  ( loadData
31 ebf38064 Iustin Pop
  , parseData
32 ebf38064 Iustin Pop
  , loadInst
33 ebf38064 Iustin Pop
  , loadNode
34 bcd17bf0 Iustin Pop
  , loadISpec
35 bcd17bf0 Iustin Pop
  , loadIPolicy
36 ebf38064 Iustin Pop
  , serializeInstances
37 ebf38064 Iustin Pop
  , serializeNode
38 ebf38064 Iustin Pop
  , serializeNodes
39 bcd17bf0 Iustin Pop
  , serializeGroup
40 bcd17bf0 Iustin Pop
  , serializeISpec
41 bcd17bf0 Iustin Pop
  , serializeIPolicy
42 ebf38064 Iustin Pop
  , serializeCluster
43 ebf38064 Iustin Pop
  ) where
44 040afc35 Iustin Pop
45 040afc35 Iustin Pop
import Control.Monad
46 3bf75b7d Iustin Pop
import Data.List
47 3bf75b7d Iustin Pop
48 3bf75b7d Iustin Pop
import Text.Printf (printf)
49 040afc35 Iustin Pop
50 040afc35 Iustin Pop
import Ganeti.HTools.Utils
51 040afc35 Iustin Pop
import Ganeti.HTools.Loader
52 e4c5beaf Iustin Pop
import Ganeti.HTools.Types
53 3bf75b7d Iustin Pop
import qualified Ganeti.HTools.Container as Container
54 a679e9dc Iustin Pop
import qualified Ganeti.HTools.Group as Group
55 040afc35 Iustin Pop
import qualified Ganeti.HTools.Node as Node
56 040afc35 Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
57 040afc35 Iustin Pop
58 b37f4a76 Iustin Pop
-- * Helper functions
59 b37f4a76 Iustin Pop
60 b37f4a76 Iustin Pop
-- | Simple wrapper over sepSplit
61 b37f4a76 Iustin Pop
commaSplit :: String -> [String]
62 b37f4a76 Iustin Pop
commaSplit = sepSplit ','
63 b37f4a76 Iustin Pop
64 525bfb36 Iustin Pop
-- * Serialisation functions
65 525bfb36 Iustin Pop
66 525bfb36 Iustin Pop
-- | Serialize a single group.
67 e4d8071d Iustin Pop
serializeGroup :: Group.Group -> String
68 e4d8071d Iustin Pop
serializeGroup grp =
69 ebf38064 Iustin Pop
  printf "%s|%s|%s" (Group.name grp) (Group.uuid grp)
70 ebf38064 Iustin Pop
           (allocPolicyToRaw (Group.allocPolicy grp))
71 e4d8071d Iustin Pop
72 525bfb36 Iustin Pop
-- | Generate group file data from a group list.
73 e4d8071d Iustin Pop
serializeGroups :: Group.List -> String
74 e4d8071d Iustin Pop
serializeGroups = unlines . map serializeGroup . Container.elems
75 e4d8071d Iustin Pop
76 525bfb36 Iustin Pop
-- | Serialize a single node.
77 525bfb36 Iustin Pop
serializeNode :: Group.List -- ^ The list of groups (needed for group uuid)
78 525bfb36 Iustin Pop
              -> Node.Node  -- ^ The node to be serialised
79 525bfb36 Iustin Pop
              -> String
80 10ef6b4e Iustin Pop
serializeNode gl node =
81 f951bd09 Iustin Pop
  printf "%s|%.0f|%d|%d|%.0f|%d|%.0f|%c|%s|%d" (Node.name node)
82 ebf38064 Iustin Pop
           (Node.tMem node) (Node.nMem node) (Node.fMem node)
83 ebf38064 Iustin Pop
           (Node.tDsk node) (Node.fDsk node) (Node.tCpu node)
84 ebf38064 Iustin Pop
           (if Node.offline node then 'Y' else 'N')
85 ebf38064 Iustin Pop
           (Group.uuid grp)
86 f951bd09 Iustin Pop
           (Node.spindleCount node)
87 10ef6b4e Iustin Pop
    where grp = Container.find (Node.group node) gl
88 3bf75b7d Iustin Pop
89 525bfb36 Iustin Pop
-- | Generate node file data from node objects.
90 10ef6b4e Iustin Pop
serializeNodes :: Group.List -> Node.List -> String
91 10ef6b4e Iustin Pop
serializeNodes gl = unlines . map (serializeNode gl) . Container.elems
92 3bf75b7d Iustin Pop
93 525bfb36 Iustin Pop
-- | Serialize a single instance.
94 525bfb36 Iustin Pop
serializeInstance :: Node.List         -- ^ The node list (needed for
95 525bfb36 Iustin Pop
                                       -- node names)
96 525bfb36 Iustin Pop
                  -> Instance.Instance -- ^ The instance to be serialised
97 525bfb36 Iustin Pop
                  -> String
98 3bf75b7d Iustin Pop
serializeInstance nl inst =
99 ebf38064 Iustin Pop
  let iname = Instance.name inst
100 ebf38064 Iustin Pop
      pnode = Container.nameOf nl (Instance.pNode inst)
101 ebf38064 Iustin Pop
      sidx = Instance.sNode inst
102 ebf38064 Iustin Pop
      snode = (if sidx == Node.noSecondary
103 ebf38064 Iustin Pop
                 then ""
104 ebf38064 Iustin Pop
                 else Container.nameOf nl sidx)
105 52cc1370 René Nussbaumer
  in printf "%s|%d|%d|%d|%s|%s|%s|%s|%s|%s|%d"
106 ebf38064 Iustin Pop
       iname (Instance.mem inst) (Instance.dsk inst)
107 ebf38064 Iustin Pop
       (Instance.vcpus inst) (instanceStatusToRaw (Instance.runSt inst))
108 ebf38064 Iustin Pop
       (if Instance.autoBalance inst then "Y" else "N")
109 ebf38064 Iustin Pop
       pnode snode (diskTemplateToRaw (Instance.diskTemplate inst))
110 ec629280 René Nussbaumer
       (intercalate "," (Instance.tags inst)) (Instance.spindleUse inst)
111 3bf75b7d Iustin Pop
112 525bfb36 Iustin Pop
-- | Generate instance file data from instance objects.
113 3bf75b7d Iustin Pop
serializeInstances :: Node.List -> Instance.List -> String
114 3bf75b7d Iustin Pop
serializeInstances nl =
115 ebf38064 Iustin Pop
  unlines . map (serializeInstance nl) . Container.elems
116 3bf75b7d Iustin Pop
117 b37f4a76 Iustin Pop
-- | Generate a spec data from a given ISpec object.
118 b37f4a76 Iustin Pop
serializeISpec :: ISpec -> String
119 b37f4a76 Iustin Pop
serializeISpec ispec =
120 b37f4a76 Iustin Pop
  -- this needs to be kept in sync with the object definition
121 d953a965 René Nussbaumer
  let ISpec mem_s cpu_c disk_s disk_c nic_c su = ispec
122 d953a965 René Nussbaumer
      strings = [show mem_s, show cpu_c, show disk_s, show disk_c, show nic_c,
123 d953a965 René Nussbaumer
                 show su]
124 b37f4a76 Iustin Pop
  in intercalate "," strings
125 b37f4a76 Iustin Pop
126 b37f4a76 Iustin Pop
-- | Generate disk template data.
127 b37f4a76 Iustin Pop
serializeDiskTemplates :: [DiskTemplate] -> String
128 b37f4a76 Iustin Pop
serializeDiskTemplates = intercalate "," . map diskTemplateToRaw
129 b37f4a76 Iustin Pop
130 b37f4a76 Iustin Pop
-- | Generate policy data from a given policy object.
131 b37f4a76 Iustin Pop
serializeIPolicy :: String -> IPolicy -> String
132 b37f4a76 Iustin Pop
serializeIPolicy owner ipol =
133 c22d4dd4 Iustin Pop
  let IPolicy stdspec minspec maxspec dts vcpu_ratio spindle_ratio = ipol
134 b37f4a76 Iustin Pop
      strings = [ owner
135 b37f4a76 Iustin Pop
                , serializeISpec stdspec
136 b37f4a76 Iustin Pop
                , serializeISpec minspec
137 b37f4a76 Iustin Pop
                , serializeISpec maxspec
138 b37f4a76 Iustin Pop
                , serializeDiskTemplates dts
139 e8fa4ff6 Iustin Pop
                , show vcpu_ratio
140 c22d4dd4 Iustin Pop
                , show spindle_ratio
141 b37f4a76 Iustin Pop
                ]
142 b37f4a76 Iustin Pop
  in intercalate "|" strings
143 b37f4a76 Iustin Pop
144 b37f4a76 Iustin Pop
-- | Generates the entire ipolicy section from the cluster and group
145 b37f4a76 Iustin Pop
-- objects.
146 b37f4a76 Iustin Pop
serializeAllIPolicies :: IPolicy -> Group.List -> String
147 b37f4a76 Iustin Pop
serializeAllIPolicies cpol gl =
148 b37f4a76 Iustin Pop
  let groups = Container.elems gl
149 b37f4a76 Iustin Pop
      allpolicies = [("", cpol)] ++
150 b37f4a76 Iustin Pop
                    map (\g -> (Group.name g, Group.iPolicy g)) groups
151 b37f4a76 Iustin Pop
      strings = map (uncurry serializeIPolicy) allpolicies
152 b37f4a76 Iustin Pop
  in unlines strings
153 b37f4a76 Iustin Pop
154 525bfb36 Iustin Pop
-- | Generate complete cluster data from node and instance lists.
155 c0e31451 Iustin Pop
serializeCluster :: ClusterData -> String
156 b37f4a76 Iustin Pop
serializeCluster (ClusterData gl nl il ctags cpol) =
157 e4d8071d Iustin Pop
  let gdata = serializeGroups gl
158 e4d8071d Iustin Pop
      ndata = serializeNodes gl nl
159 4a273e97 Iustin Pop
      idata = serializeInstances nl il
160 b37f4a76 Iustin Pop
      pdata = serializeAllIPolicies cpol gl
161 716c6be5 Iustin Pop
  -- note: not using 'unlines' as that adds too many newlines
162 b37f4a76 Iustin Pop
  in intercalate "\n" [gdata, ndata, idata, unlines ctags, pdata]
163 4a273e97 Iustin Pop
164 525bfb36 Iustin Pop
-- * Parsing functions
165 525bfb36 Iustin Pop
166 a679e9dc Iustin Pop
-- | Load a group from a field list.
167 525bfb36 Iustin Pop
loadGroup :: (Monad m) => [String]
168 525bfb36 Iustin Pop
          -> m (String, Group.Group) -- ^ The result, a tuple of group
169 525bfb36 Iustin Pop
                                     -- UUID and group object
170 f4c7d37a Iustin Pop
loadGroup [name, gid, apol] = do
171 5f828ce4 Agata Murawska
  xapol <- allocPolicyFromRaw apol
172 6cff91f5 Iustin Pop
  return (gid, Group.create name gid xapol defIPolicy)
173 a679e9dc Iustin Pop
174 a679e9dc Iustin Pop
loadGroup s = fail $ "Invalid/incomplete group data: '" ++ show s ++ "'"
175 a679e9dc Iustin Pop
176 9188aeef Iustin Pop
-- | Load a node from a field list.
177 525bfb36 Iustin Pop
loadNode :: (Monad m) =>
178 525bfb36 Iustin Pop
            NameAssoc             -- ^ Association list with current groups
179 525bfb36 Iustin Pop
         -> [String]              -- ^ Input data as a list of fields
180 525bfb36 Iustin Pop
         -> m (String, Node.Node) -- ^ The result, a tuple o node name
181 525bfb36 Iustin Pop
                                  -- and node object
182 f951bd09 Iustin Pop
loadNode ktg [name, tm, nm, fm, td, fd, tc, fo, gu, spindles] = do
183 10ef6b4e Iustin Pop
  gdx <- lookupGroup ktg name gu
184 040afc35 Iustin Pop
  new_node <-
185 1a82215d Iustin Pop
      if any (== "?") [tm,nm,fm,td,fd,tc] || fo == "Y" then
186 8bc34c7b Iustin Pop
          return $ Node.create name 0 0 0 0 0 0 True 0 gdx
187 040afc35 Iustin Pop
      else do
188 040afc35 Iustin Pop
        vtm <- tryRead name tm
189 040afc35 Iustin Pop
        vnm <- tryRead name nm
190 040afc35 Iustin Pop
        vfm <- tryRead name fm
191 040afc35 Iustin Pop
        vtd <- tryRead name td
192 040afc35 Iustin Pop
        vfd <- tryRead name fd
193 1a82215d Iustin Pop
        vtc <- tryRead name tc
194 f951bd09 Iustin Pop
        vspindles <- tryRead name spindles
195 f951bd09 Iustin Pop
        return $ Node.create name vtm vnm vfm vtd vfd vtc False vspindles gdx
196 040afc35 Iustin Pop
  return (name, new_node)
197 f951bd09 Iustin Pop
198 f951bd09 Iustin Pop
loadNode ktg [name, tm, nm, fm, td, fd, tc, fo, gu] =
199 f951bd09 Iustin Pop
  loadNode ktg [name, tm, nm, fm, td, fd, tc, fo, gu, "1"]
200 f951bd09 Iustin Pop
201 10ef6b4e Iustin Pop
loadNode _ s = fail $ "Invalid/incomplete node data: '" ++ show s ++ "'"
202 040afc35 Iustin Pop
203 9188aeef Iustin Pop
-- | Load an instance from a field list.
204 6429e8d8 Iustin Pop
loadInst :: NameAssoc -- ^ Association list with the current nodes
205 6429e8d8 Iustin Pop
         -> [String]  -- ^ Input data as a list of fields
206 6429e8d8 Iustin Pop
         -> Result (String, Instance.Instance) -- ^ A tuple of
207 6429e8d8 Iustin Pop
                                               -- instance name and
208 6429e8d8 Iustin Pop
                                               -- the instance object
209 6429e8d8 Iustin Pop
loadInst ktn [ name, mem, dsk, vcpus, status, auto_bal, pnode, snode
210 52cc1370 René Nussbaumer
             , dt, tags, su ] = do
211 040afc35 Iustin Pop
  pidx <- lookupNode ktn name pnode
212 3603605a Iustin Pop
  sidx <- if null snode
213 3603605a Iustin Pop
            then return Node.noSecondary
214 3603605a Iustin Pop
            else lookupNode ktn name snode
215 040afc35 Iustin Pop
  vmem <- tryRead name mem
216 040afc35 Iustin Pop
  vdsk <- tryRead name dsk
217 d752eb39 Iustin Pop
  vvcpus <- tryRead name vcpus
218 7dd14211 Agata Murawska
  vstatus <- instanceStatusFromRaw status
219 bc782180 Iustin Pop
  auto_balance <- case auto_bal of
220 bc782180 Iustin Pop
                    "Y" -> return True
221 bc782180 Iustin Pop
                    "N" -> return False
222 bc782180 Iustin Pop
                    _ -> fail $ "Invalid auto_balance value '" ++ auto_bal ++
223 bc782180 Iustin Pop
                         "' for instance " ++ name
224 2c9336a4 Iustin Pop
  disk_template <- annotateResult ("Instance " ++ name)
225 5f828ce4 Agata Murawska
                   (diskTemplateFromRaw dt)
226 ec629280 René Nussbaumer
  spindle_use <- tryRead name su
227 040afc35 Iustin Pop
  when (sidx == pidx) $ fail $ "Instance " ++ name ++
228 040afc35 Iustin Pop
           " has same primary and secondary node - " ++ pnode
229 b37f4a76 Iustin Pop
  let vtags = commaSplit tags
230 7dd14211 Agata Murawska
      newinst = Instance.create name vmem vdsk vvcpus vstatus vtags
231 ec629280 René Nussbaumer
                auto_balance pidx sidx disk_template spindle_use
232 040afc35 Iustin Pop
  return (name, newinst)
233 52cc1370 René Nussbaumer
234 52cc1370 René Nussbaumer
loadInst ktn [ name, mem, dsk, vcpus, status, auto_bal, pnode, snode
235 52cc1370 René Nussbaumer
             , dt, tags ] = loadInst ktn [ name, mem, dsk, vcpus, status,
236 52cc1370 René Nussbaumer
                                           auto_bal, pnode, snode, dt, tags,
237 52cc1370 René Nussbaumer
                                           "1" ]
238 9f6dcdea Iustin Pop
loadInst _ s = fail $ "Invalid/incomplete instance data: '" ++ show s ++ "'"
239 040afc35 Iustin Pop
240 b37f4a76 Iustin Pop
-- | Loads a spec from a field list.
241 b37f4a76 Iustin Pop
loadISpec :: String -> [String] -> Result ISpec
242 d953a965 René Nussbaumer
loadISpec owner [mem_s, cpu_c, dsk_s, dsk_c, nic_c, su] = do
243 b37f4a76 Iustin Pop
  xmem_s <- tryRead (owner ++ "/memsize") mem_s
244 b37f4a76 Iustin Pop
  xcpu_c <- tryRead (owner ++ "/cpucount") cpu_c
245 b37f4a76 Iustin Pop
  xdsk_s <- tryRead (owner ++ "/disksize") dsk_s
246 b37f4a76 Iustin Pop
  xdsk_c <- tryRead (owner ++ "/diskcount") dsk_c
247 b37f4a76 Iustin Pop
  xnic_c <- tryRead (owner ++ "/niccount") nic_c
248 d953a965 René Nussbaumer
  xsu    <- tryRead (owner ++ "/spindleuse") su
249 d953a965 René Nussbaumer
  return $ ISpec xmem_s xcpu_c xdsk_s xdsk_c xnic_c xsu
250 b37f4a76 Iustin Pop
loadISpec owner s = fail $ "Invalid ispec data for " ++ owner ++ ": " ++ show s
251 b37f4a76 Iustin Pop
252 b37f4a76 Iustin Pop
-- | Loads an ipolicy from a field list.
253 b37f4a76 Iustin Pop
loadIPolicy :: [String] -> Result (String, IPolicy)
254 c22d4dd4 Iustin Pop
loadIPolicy [owner, stdspec, minspec, maxspec, dtemplates,
255 c22d4dd4 Iustin Pop
             vcpu_ratio, spindle_ratio] = do
256 b37f4a76 Iustin Pop
  xstdspec <- loadISpec (owner ++ "/stdspec") (commaSplit stdspec)
257 b37f4a76 Iustin Pop
  xminspec <- loadISpec (owner ++ "/minspec") (commaSplit minspec)
258 b37f4a76 Iustin Pop
  xmaxspec <- loadISpec (owner ++ "/maxspec") (commaSplit maxspec)
259 b37f4a76 Iustin Pop
  xdts <- mapM diskTemplateFromRaw $ commaSplit dtemplates
260 e8fa4ff6 Iustin Pop
  xvcpu_ratio <- tryRead (owner ++ "/vcpu_ratio") vcpu_ratio
261 c22d4dd4 Iustin Pop
  xspindle_ratio <- tryRead (owner ++ "/spindle_ratio") spindle_ratio
262 c22d4dd4 Iustin Pop
  return $ (owner, IPolicy xstdspec xminspec xmaxspec xdts
263 c22d4dd4 Iustin Pop
            xvcpu_ratio xspindle_ratio)
264 b37f4a76 Iustin Pop
loadIPolicy s = fail $ "Invalid ipolicy data: '" ++ show s ++ "'"
265 b37f4a76 Iustin Pop
266 b37f4a76 Iustin Pop
loadOnePolicy :: (IPolicy, Group.List) -> String
267 b37f4a76 Iustin Pop
              -> Result (IPolicy, Group.List)
268 b37f4a76 Iustin Pop
loadOnePolicy (cpol, gl) line = do
269 b37f4a76 Iustin Pop
  (owner, ipol) <- loadIPolicy (sepSplit '|' line)
270 b37f4a76 Iustin Pop
  case owner of
271 b37f4a76 Iustin Pop
    "" -> return (ipol, gl) -- this is a cluster policy (no owner)
272 b37f4a76 Iustin Pop
    _ -> do
273 b37f4a76 Iustin Pop
      grp <- Container.findByName gl owner
274 b37f4a76 Iustin Pop
      let grp' = grp { Group.iPolicy = ipol }
275 b37f4a76 Iustin Pop
          gl' = Container.add (Group.idx grp') grp' gl
276 b37f4a76 Iustin Pop
      return (cpol, gl')
277 b37f4a76 Iustin Pop
278 b37f4a76 Iustin Pop
-- | Loads all policies from the policy section
279 b37f4a76 Iustin Pop
loadAllIPolicies :: Group.List -> [String] -> Result (IPolicy, Group.List)
280 b37f4a76 Iustin Pop
loadAllIPolicies gl =
281 b37f4a76 Iustin Pop
  foldM loadOnePolicy (defIPolicy, gl)
282 b37f4a76 Iustin Pop
283 9188aeef Iustin Pop
-- | Convert newline and delimiter-separated text.
284 9188aeef Iustin Pop
--
285 9188aeef Iustin Pop
-- This function converts a text in tabular format as generated by
286 9188aeef Iustin Pop
-- @gnt-instance list@ and @gnt-node list@ to a list of objects using
287 9188aeef Iustin Pop
-- a supplied conversion function.
288 497e30a1 Iustin Pop
loadTabular :: (Monad m, Element a) =>
289 525bfb36 Iustin Pop
               [String] -- ^ Input data, as a list of lines
290 525bfb36 Iustin Pop
            -> ([String] -> m (String, a)) -- ^ Conversion function
291 525bfb36 Iustin Pop
            -> m ( NameAssoc
292 525bfb36 Iustin Pop
                 , Container.Container a ) -- ^ A tuple of an
293 525bfb36 Iustin Pop
                                           -- association list (name
294 525bfb36 Iustin Pop
                                           -- to object) and a set as
295 525bfb36 Iustin Pop
                                           -- used in
296 525bfb36 Iustin Pop
                                           -- "Ganeti.HTools.Container"
297 525bfb36 Iustin Pop
298 f5197d89 Iustin Pop
loadTabular lines_data convert_fn = do
299 f5197d89 Iustin Pop
  let rows = map (sepSplit '|') lines_data
300 040afc35 Iustin Pop
  kerows <- mapM convert_fn rows
301 497e30a1 Iustin Pop
  return $ assignIndices kerows
302 040afc35 Iustin Pop
303 dadfc261 Iustin Pop
-- | Load the cluser data from disk.
304 525bfb36 Iustin Pop
--
305 525bfb36 Iustin Pop
-- This is an alias to 'readFile' just for consistency with the other
306 525bfb36 Iustin Pop
-- modules.
307 525bfb36 Iustin Pop
readData :: String    -- ^ Path to the text file
308 525bfb36 Iustin Pop
         -> IO String -- ^ Contents of the file
309 dadfc261 Iustin Pop
readData = readFile
310 dadfc261 Iustin Pop
311 16c2369c Iustin Pop
-- | Builds the cluster data from text input.
312 dadfc261 Iustin Pop
parseData :: String -- ^ Text data
313 f4f6eb0b Iustin Pop
          -> Result ClusterData
314 dadfc261 Iustin Pop
parseData fdata = do
315 16c2369c Iustin Pop
  let flines = lines fdata
316 b37f4a76 Iustin Pop
  (glines, nlines, ilines, ctags, pollines) <-
317 a604456d Iustin Pop
      case sepSplit "" flines of
318 b37f4a76 Iustin Pop
        [a, b, c, d, e] -> Ok (a, b, c, d, e)
319 b37f4a76 Iustin Pop
        [a, b, c, d] -> Ok (a, b, c, d, [])
320 a604456d Iustin Pop
        xs -> Bad $ printf "Invalid format of the input file: %d sections\
321 b37f4a76 Iustin Pop
                           \ instead of 4 or 5" (length xs)
322 a679e9dc Iustin Pop
  {- group file: name uuid -}
323 10ef6b4e Iustin Pop
  (ktg, gl) <- loadTabular glines loadGroup
324 dadfc261 Iustin Pop
  {- node file: name t_mem n_mem f_mem t_disk f_disk -}
325 a604456d Iustin Pop
  (ktn, nl) <- loadTabular nlines (loadNode ktg)
326 dadfc261 Iustin Pop
  {- instance file: name mem disk status pnode snode -}
327 a604456d Iustin Pop
  (_, il) <- loadTabular ilines (loadInst ktn)
328 afcd5a0b Iustin Pop
  {- the tags are simply line-based, no processing needed -}
329 b37f4a76 Iustin Pop
  {- process policies -}
330 b37f4a76 Iustin Pop
  (cpol, gl') <- loadAllIPolicies gl pollines
331 b37f4a76 Iustin Pop
  return (ClusterData gl' nl il ctags cpol)
332 dadfc261 Iustin Pop
333 525bfb36 Iustin Pop
-- | Top level function for data loading.
334 dadfc261 Iustin Pop
loadData :: String -- ^ Path to the text file
335 f4f6eb0b Iustin Pop
         -> IO (Result ClusterData)
336 2a8e2dc9 Iustin Pop
loadData = fmap parseData . readData