Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Text.hs @ 417cc253

History | View | Annotate | Download (12.5 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 ebf38064 Iustin Pop
  in printf "%s|%d|%d|%d|%s|%s|%s|%s|%s|%s"
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 ebf38064 Iustin Pop
       (intercalate "," (Instance.tags 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 b37f4a76 Iustin Pop
  let ISpec mem_s cpu_c disk_s disk_c nic_c = ispec
122 b37f4a76 Iustin Pop
      strings = [show mem_s, show cpu_c, show disk_s, show disk_c, show nic_c]
123 b37f4a76 Iustin Pop
  in intercalate "," strings
124 b37f4a76 Iustin Pop
125 b37f4a76 Iustin Pop
-- | Generate disk template data.
126 b37f4a76 Iustin Pop
serializeDiskTemplates :: [DiskTemplate] -> String
127 b37f4a76 Iustin Pop
serializeDiskTemplates = intercalate "," . map diskTemplateToRaw
128 b37f4a76 Iustin Pop
129 b37f4a76 Iustin Pop
-- | Generate policy data from a given policy object.
130 b37f4a76 Iustin Pop
serializeIPolicy :: String -> IPolicy -> String
131 b37f4a76 Iustin Pop
serializeIPolicy owner ipol =
132 c22d4dd4 Iustin Pop
  let IPolicy stdspec minspec maxspec dts vcpu_ratio spindle_ratio = ipol
133 b37f4a76 Iustin Pop
      strings = [ owner
134 b37f4a76 Iustin Pop
                , serializeISpec stdspec
135 b37f4a76 Iustin Pop
                , serializeISpec minspec
136 b37f4a76 Iustin Pop
                , serializeISpec maxspec
137 b37f4a76 Iustin Pop
                , serializeDiskTemplates dts
138 e8fa4ff6 Iustin Pop
                , show vcpu_ratio
139 c22d4dd4 Iustin Pop
                , show spindle_ratio
140 b37f4a76 Iustin Pop
                ]
141 b37f4a76 Iustin Pop
  in intercalate "|" strings
142 b37f4a76 Iustin Pop
143 b37f4a76 Iustin Pop
-- | Generates the entire ipolicy section from the cluster and group
144 b37f4a76 Iustin Pop
-- objects.
145 b37f4a76 Iustin Pop
serializeAllIPolicies :: IPolicy -> Group.List -> String
146 b37f4a76 Iustin Pop
serializeAllIPolicies cpol gl =
147 b37f4a76 Iustin Pop
  let groups = Container.elems gl
148 b37f4a76 Iustin Pop
      allpolicies = [("", cpol)] ++
149 b37f4a76 Iustin Pop
                    map (\g -> (Group.name g, Group.iPolicy g)) groups
150 b37f4a76 Iustin Pop
      strings = map (uncurry serializeIPolicy) allpolicies
151 b37f4a76 Iustin Pop
  in unlines strings
152 b37f4a76 Iustin Pop
153 525bfb36 Iustin Pop
-- | Generate complete cluster data from node and instance lists.
154 c0e31451 Iustin Pop
serializeCluster :: ClusterData -> String
155 b37f4a76 Iustin Pop
serializeCluster (ClusterData gl nl il ctags cpol) =
156 e4d8071d Iustin Pop
  let gdata = serializeGroups gl
157 e4d8071d Iustin Pop
      ndata = serializeNodes gl nl
158 4a273e97 Iustin Pop
      idata = serializeInstances nl il
159 b37f4a76 Iustin Pop
      pdata = serializeAllIPolicies cpol gl
160 716c6be5 Iustin Pop
  -- note: not using 'unlines' as that adds too many newlines
161 b37f4a76 Iustin Pop
  in intercalate "\n" [gdata, ndata, idata, unlines ctags, pdata]
162 4a273e97 Iustin Pop
163 525bfb36 Iustin Pop
-- * Parsing functions
164 525bfb36 Iustin Pop
165 a679e9dc Iustin Pop
-- | Load a group from a field list.
166 525bfb36 Iustin Pop
loadGroup :: (Monad m) => [String]
167 525bfb36 Iustin Pop
          -> m (String, Group.Group) -- ^ The result, a tuple of group
168 525bfb36 Iustin Pop
                                     -- UUID and group object
169 f4c7d37a Iustin Pop
loadGroup [name, gid, apol] = do
170 5f828ce4 Agata Murawska
  xapol <- allocPolicyFromRaw apol
171 6cff91f5 Iustin Pop
  return (gid, Group.create name gid xapol defIPolicy)
172 a679e9dc Iustin Pop
173 a679e9dc Iustin Pop
loadGroup s = fail $ "Invalid/incomplete group data: '" ++ show s ++ "'"
174 a679e9dc Iustin Pop
175 9188aeef Iustin Pop
-- | Load a node from a field list.
176 525bfb36 Iustin Pop
loadNode :: (Monad m) =>
177 525bfb36 Iustin Pop
            NameAssoc             -- ^ Association list with current groups
178 525bfb36 Iustin Pop
         -> [String]              -- ^ Input data as a list of fields
179 525bfb36 Iustin Pop
         -> m (String, Node.Node) -- ^ The result, a tuple o node name
180 525bfb36 Iustin Pop
                                  -- and node object
181 f951bd09 Iustin Pop
loadNode ktg [name, tm, nm, fm, td, fd, tc, fo, gu, spindles] = do
182 10ef6b4e Iustin Pop
  gdx <- lookupGroup ktg name gu
183 040afc35 Iustin Pop
  new_node <-
184 1a82215d Iustin Pop
      if any (== "?") [tm,nm,fm,td,fd,tc] || fo == "Y" then
185 8bc34c7b Iustin Pop
          return $ Node.create name 0 0 0 0 0 0 True 0 gdx
186 040afc35 Iustin Pop
      else do
187 040afc35 Iustin Pop
        vtm <- tryRead name tm
188 040afc35 Iustin Pop
        vnm <- tryRead name nm
189 040afc35 Iustin Pop
        vfm <- tryRead name fm
190 040afc35 Iustin Pop
        vtd <- tryRead name td
191 040afc35 Iustin Pop
        vfd <- tryRead name fd
192 1a82215d Iustin Pop
        vtc <- tryRead name tc
193 f951bd09 Iustin Pop
        vspindles <- tryRead name spindles
194 f951bd09 Iustin Pop
        return $ Node.create name vtm vnm vfm vtd vfd vtc False vspindles gdx
195 040afc35 Iustin Pop
  return (name, new_node)
196 f951bd09 Iustin Pop
197 f951bd09 Iustin Pop
loadNode ktg [name, tm, nm, fm, td, fd, tc, fo, gu] =
198 f951bd09 Iustin Pop
  loadNode ktg [name, tm, nm, fm, td, fd, tc, fo, gu, "1"]
199 f951bd09 Iustin Pop
200 10ef6b4e Iustin Pop
loadNode _ s = fail $ "Invalid/incomplete node data: '" ++ show s ++ "'"
201 040afc35 Iustin Pop
202 9188aeef Iustin Pop
-- | Load an instance from a field list.
203 6429e8d8 Iustin Pop
loadInst :: NameAssoc -- ^ Association list with the current nodes
204 6429e8d8 Iustin Pop
         -> [String]  -- ^ Input data as a list of fields
205 6429e8d8 Iustin Pop
         -> Result (String, Instance.Instance) -- ^ A tuple of
206 6429e8d8 Iustin Pop
                                               -- instance name and
207 6429e8d8 Iustin Pop
                                               -- the instance object
208 6429e8d8 Iustin Pop
loadInst ktn [ name, mem, dsk, vcpus, status, auto_bal, pnode, snode
209 6429e8d8 Iustin Pop
             , dt, tags ] = do
210 040afc35 Iustin Pop
  pidx <- lookupNode ktn name pnode
211 3603605a Iustin Pop
  sidx <- if null snode
212 3603605a Iustin Pop
            then return Node.noSecondary
213 3603605a Iustin Pop
            else lookupNode ktn name snode
214 040afc35 Iustin Pop
  vmem <- tryRead name mem
215 040afc35 Iustin Pop
  vdsk <- tryRead name dsk
216 d752eb39 Iustin Pop
  vvcpus <- tryRead name vcpus
217 7dd14211 Agata Murawska
  vstatus <- instanceStatusFromRaw status
218 bc782180 Iustin Pop
  auto_balance <- case auto_bal of
219 bc782180 Iustin Pop
                    "Y" -> return True
220 bc782180 Iustin Pop
                    "N" -> return False
221 bc782180 Iustin Pop
                    _ -> fail $ "Invalid auto_balance value '" ++ auto_bal ++
222 bc782180 Iustin Pop
                         "' for instance " ++ name
223 2c9336a4 Iustin Pop
  disk_template <- annotateResult ("Instance " ++ name)
224 5f828ce4 Agata Murawska
                   (diskTemplateFromRaw dt)
225 040afc35 Iustin Pop
  when (sidx == pidx) $ fail $ "Instance " ++ name ++
226 040afc35 Iustin Pop
           " has same primary and secondary node - " ++ pnode
227 b37f4a76 Iustin Pop
  let vtags = commaSplit tags
228 7dd14211 Agata Murawska
      newinst = Instance.create name vmem vdsk vvcpus vstatus vtags
229 6429e8d8 Iustin Pop
                auto_balance pidx sidx disk_template
230 040afc35 Iustin Pop
  return (name, newinst)
231 9f6dcdea Iustin Pop
loadInst _ s = fail $ "Invalid/incomplete instance data: '" ++ show s ++ "'"
232 040afc35 Iustin Pop
233 b37f4a76 Iustin Pop
-- | Loads a spec from a field list.
234 b37f4a76 Iustin Pop
loadISpec :: String -> [String] -> Result ISpec
235 b37f4a76 Iustin Pop
loadISpec owner [mem_s, cpu_c, dsk_s, dsk_c, nic_c] = do
236 b37f4a76 Iustin Pop
  xmem_s <- tryRead (owner ++ "/memsize") mem_s
237 b37f4a76 Iustin Pop
  xcpu_c <- tryRead (owner ++ "/cpucount") cpu_c
238 b37f4a76 Iustin Pop
  xdsk_s <- tryRead (owner ++ "/disksize") dsk_s
239 b37f4a76 Iustin Pop
  xdsk_c <- tryRead (owner ++ "/diskcount") dsk_c
240 b37f4a76 Iustin Pop
  xnic_c <- tryRead (owner ++ "/niccount") nic_c
241 b37f4a76 Iustin Pop
  return $ ISpec xmem_s xcpu_c xdsk_s xdsk_c xnic_c
242 b37f4a76 Iustin Pop
loadISpec owner s = fail $ "Invalid ispec data for " ++ owner ++ ": " ++ show s
243 b37f4a76 Iustin Pop
244 b37f4a76 Iustin Pop
-- | Loads an ipolicy from a field list.
245 b37f4a76 Iustin Pop
loadIPolicy :: [String] -> Result (String, IPolicy)
246 c22d4dd4 Iustin Pop
loadIPolicy [owner, stdspec, minspec, maxspec, dtemplates,
247 c22d4dd4 Iustin Pop
             vcpu_ratio, spindle_ratio] = do
248 b37f4a76 Iustin Pop
  xstdspec <- loadISpec (owner ++ "/stdspec") (commaSplit stdspec)
249 b37f4a76 Iustin Pop
  xminspec <- loadISpec (owner ++ "/minspec") (commaSplit minspec)
250 b37f4a76 Iustin Pop
  xmaxspec <- loadISpec (owner ++ "/maxspec") (commaSplit maxspec)
251 b37f4a76 Iustin Pop
  xdts <- mapM diskTemplateFromRaw $ commaSplit dtemplates
252 e8fa4ff6 Iustin Pop
  xvcpu_ratio <- tryRead (owner ++ "/vcpu_ratio") vcpu_ratio
253 c22d4dd4 Iustin Pop
  xspindle_ratio <- tryRead (owner ++ "/spindle_ratio") spindle_ratio
254 c22d4dd4 Iustin Pop
  return $ (owner, IPolicy xstdspec xminspec xmaxspec xdts
255 c22d4dd4 Iustin Pop
            xvcpu_ratio xspindle_ratio)
256 b37f4a76 Iustin Pop
loadIPolicy s = fail $ "Invalid ipolicy data: '" ++ show s ++ "'"
257 b37f4a76 Iustin Pop
258 b37f4a76 Iustin Pop
loadOnePolicy :: (IPolicy, Group.List) -> String
259 b37f4a76 Iustin Pop
              -> Result (IPolicy, Group.List)
260 b37f4a76 Iustin Pop
loadOnePolicy (cpol, gl) line = do
261 b37f4a76 Iustin Pop
  (owner, ipol) <- loadIPolicy (sepSplit '|' line)
262 b37f4a76 Iustin Pop
  case owner of
263 b37f4a76 Iustin Pop
    "" -> return (ipol, gl) -- this is a cluster policy (no owner)
264 b37f4a76 Iustin Pop
    _ -> do
265 b37f4a76 Iustin Pop
      grp <- Container.findByName gl owner
266 b37f4a76 Iustin Pop
      let grp' = grp { Group.iPolicy = ipol }
267 b37f4a76 Iustin Pop
          gl' = Container.add (Group.idx grp') grp' gl
268 b37f4a76 Iustin Pop
      return (cpol, gl')
269 b37f4a76 Iustin Pop
270 b37f4a76 Iustin Pop
-- | Loads all policies from the policy section
271 b37f4a76 Iustin Pop
loadAllIPolicies :: Group.List -> [String] -> Result (IPolicy, Group.List)
272 b37f4a76 Iustin Pop
loadAllIPolicies gl =
273 b37f4a76 Iustin Pop
  foldM loadOnePolicy (defIPolicy, gl)
274 b37f4a76 Iustin Pop
275 9188aeef Iustin Pop
-- | Convert newline and delimiter-separated text.
276 9188aeef Iustin Pop
--
277 9188aeef Iustin Pop
-- This function converts a text in tabular format as generated by
278 9188aeef Iustin Pop
-- @gnt-instance list@ and @gnt-node list@ to a list of objects using
279 9188aeef Iustin Pop
-- a supplied conversion function.
280 497e30a1 Iustin Pop
loadTabular :: (Monad m, Element a) =>
281 525bfb36 Iustin Pop
               [String] -- ^ Input data, as a list of lines
282 525bfb36 Iustin Pop
            -> ([String] -> m (String, a)) -- ^ Conversion function
283 525bfb36 Iustin Pop
            -> m ( NameAssoc
284 525bfb36 Iustin Pop
                 , Container.Container a ) -- ^ A tuple of an
285 525bfb36 Iustin Pop
                                           -- association list (name
286 525bfb36 Iustin Pop
                                           -- to object) and a set as
287 525bfb36 Iustin Pop
                                           -- used in
288 525bfb36 Iustin Pop
                                           -- "Ganeti.HTools.Container"
289 525bfb36 Iustin Pop
290 f5197d89 Iustin Pop
loadTabular lines_data convert_fn = do
291 f5197d89 Iustin Pop
  let rows = map (sepSplit '|') lines_data
292 040afc35 Iustin Pop
  kerows <- mapM convert_fn rows
293 497e30a1 Iustin Pop
  return $ assignIndices kerows
294 040afc35 Iustin Pop
295 dadfc261 Iustin Pop
-- | Load the cluser data from disk.
296 525bfb36 Iustin Pop
--
297 525bfb36 Iustin Pop
-- This is an alias to 'readFile' just for consistency with the other
298 525bfb36 Iustin Pop
-- modules.
299 525bfb36 Iustin Pop
readData :: String    -- ^ Path to the text file
300 525bfb36 Iustin Pop
         -> IO String -- ^ Contents of the file
301 dadfc261 Iustin Pop
readData = readFile
302 dadfc261 Iustin Pop
303 16c2369c Iustin Pop
-- | Builds the cluster data from text input.
304 dadfc261 Iustin Pop
parseData :: String -- ^ Text data
305 f4f6eb0b Iustin Pop
          -> Result ClusterData
306 dadfc261 Iustin Pop
parseData fdata = do
307 16c2369c Iustin Pop
  let flines = lines fdata
308 b37f4a76 Iustin Pop
  (glines, nlines, ilines, ctags, pollines) <-
309 a604456d Iustin Pop
      case sepSplit "" flines of
310 b37f4a76 Iustin Pop
        [a, b, c, d, e] -> Ok (a, b, c, d, e)
311 b37f4a76 Iustin Pop
        [a, b, c, d] -> Ok (a, b, c, d, [])
312 a604456d Iustin Pop
        xs -> Bad $ printf "Invalid format of the input file: %d sections\
313 b37f4a76 Iustin Pop
                           \ instead of 4 or 5" (length xs)
314 a679e9dc Iustin Pop
  {- group file: name uuid -}
315 10ef6b4e Iustin Pop
  (ktg, gl) <- loadTabular glines loadGroup
316 dadfc261 Iustin Pop
  {- node file: name t_mem n_mem f_mem t_disk f_disk -}
317 a604456d Iustin Pop
  (ktn, nl) <- loadTabular nlines (loadNode ktg)
318 dadfc261 Iustin Pop
  {- instance file: name mem disk status pnode snode -}
319 a604456d Iustin Pop
  (_, il) <- loadTabular ilines (loadInst ktn)
320 afcd5a0b Iustin Pop
  {- the tags are simply line-based, no processing needed -}
321 b37f4a76 Iustin Pop
  {- process policies -}
322 b37f4a76 Iustin Pop
  (cpol, gl') <- loadAllIPolicies gl pollines
323 b37f4a76 Iustin Pop
  return (ClusterData gl' nl il ctags cpol)
324 dadfc261 Iustin Pop
325 525bfb36 Iustin Pop
-- | Top level function for data loading.
326 dadfc261 Iustin Pop
loadData :: String -- ^ Path to the text file
327 f4f6eb0b Iustin Pop
         -> IO (Result ClusterData)
328 2a8e2dc9 Iustin Pop
loadData = fmap parseData . readData