Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / Text.hs @ c96d44df

History | View | Annotate | Download (3.7 kB)

1 040afc35 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 a8946537 Iustin Pop
files, as produced by gnt-node and gnt-instance list.
5 040afc35 Iustin Pop
6 040afc35 Iustin Pop
-}
7 040afc35 Iustin Pop
8 e2fa2baf Iustin Pop
{-
9 e2fa2baf Iustin Pop
10 e2fa2baf Iustin Pop
Copyright (C) 2009 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 b2278348 Iustin Pop
    (
31 b2278348 Iustin Pop
      loadData
32 1ae7a904 Iustin Pop
    , loadInst
33 39d11971 Iustin Pop
    , loadNode
34 b2278348 Iustin Pop
    ) where
35 040afc35 Iustin Pop
36 040afc35 Iustin Pop
import Control.Monad
37 040afc35 Iustin Pop
38 040afc35 Iustin Pop
import Ganeti.HTools.Utils
39 040afc35 Iustin Pop
import Ganeti.HTools.Loader
40 e4c5beaf Iustin Pop
import Ganeti.HTools.Types
41 040afc35 Iustin Pop
import qualified Ganeti.HTools.Node as Node
42 040afc35 Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
43 040afc35 Iustin Pop
44 9188aeef Iustin Pop
-- | Load a node from a field list.
45 040afc35 Iustin Pop
loadNode :: (Monad m) => [String] -> m (String, Node.Node)
46 27671a61 Iustin Pop
loadNode [name, tm, nm, fm, td, fd, tc, fo] = do
47 040afc35 Iustin Pop
  new_node <-
48 1a82215d Iustin Pop
      if any (== "?") [tm,nm,fm,td,fd,tc] || fo == "Y" then
49 1a82215d Iustin Pop
          return $ Node.create name 0 0 0 0 0 0 True
50 040afc35 Iustin Pop
      else do
51 040afc35 Iustin Pop
        vtm <- tryRead name tm
52 040afc35 Iustin Pop
        vnm <- tryRead name nm
53 040afc35 Iustin Pop
        vfm <- tryRead name fm
54 040afc35 Iustin Pop
        vtd <- tryRead name td
55 040afc35 Iustin Pop
        vfd <- tryRead name fd
56 1a82215d Iustin Pop
        vtc <- tryRead name tc
57 1a82215d Iustin Pop
        return $ Node.create name vtm vnm vfm vtd vfd vtc False
58 040afc35 Iustin Pop
  return (name, new_node)
59 9f6dcdea Iustin Pop
loadNode s = fail $ "Invalid/incomplete node data: '" ++ show s ++ "'"
60 040afc35 Iustin Pop
61 9188aeef Iustin Pop
-- | Load an instance from a field list.
62 040afc35 Iustin Pop
loadInst :: (Monad m) =>
63 608efcce Iustin Pop
            [(String, Ndx)] -> [String] -> m (String, Instance.Instance)
64 17e7af2b Iustin Pop
loadInst ktn [name, mem, dsk, vcpus, status, pnode, snode, tags] = do
65 040afc35 Iustin Pop
  pidx <- lookupNode ktn name pnode
66 040afc35 Iustin Pop
  sidx <- (if null snode then return Node.noSecondary
67 040afc35 Iustin Pop
           else lookupNode ktn name snode)
68 040afc35 Iustin Pop
  vmem <- tryRead name mem
69 040afc35 Iustin Pop
  vdsk <- tryRead name dsk
70 d752eb39 Iustin Pop
  vvcpus <- tryRead name vcpus
71 040afc35 Iustin Pop
  when (sidx == pidx) $ fail $ "Instance " ++ name ++
72 040afc35 Iustin Pop
           " has same primary and secondary node - " ++ pnode
73 17e7af2b Iustin Pop
  let vtags = sepSplit ',' tags
74 17e7af2b Iustin Pop
      newinst = Instance.create name vmem vdsk vvcpus status vtags pidx sidx
75 040afc35 Iustin Pop
  return (name, newinst)
76 9f6dcdea Iustin Pop
loadInst _ s = fail $ "Invalid/incomplete instance data: '" ++ show s ++ "'"
77 040afc35 Iustin Pop
78 9188aeef Iustin Pop
-- | Convert newline and delimiter-separated text.
79 9188aeef Iustin Pop
--
80 9188aeef Iustin Pop
-- This function converts a text in tabular format as generated by
81 9188aeef Iustin Pop
-- @gnt-instance list@ and @gnt-node list@ to a list of objects using
82 9188aeef Iustin Pop
-- a supplied conversion function.
83 497e30a1 Iustin Pop
loadTabular :: (Monad m, Element a) =>
84 f5197d89 Iustin Pop
               [String] -> ([String] -> m (String, a))
85 497e30a1 Iustin Pop
            -> m ([(String, Int)], [(Int, a)])
86 f5197d89 Iustin Pop
loadTabular lines_data convert_fn = do
87 f5197d89 Iustin Pop
  let rows = map (sepSplit '|') lines_data
88 040afc35 Iustin Pop
  kerows <- mapM convert_fn rows
89 497e30a1 Iustin Pop
  return $ assignIndices kerows
90 040afc35 Iustin Pop
91 16c2369c Iustin Pop
-- | Builds the cluster data from text input.
92 16c2369c Iustin Pop
loadData :: String -- ^ Path to the text file
93 94e05c32 Iustin Pop
         -> IO (Result (Node.AssocList, Instance.AssocList, [String]))
94 16c2369c Iustin Pop
loadData afile = do -- IO monad
95 16c2369c Iustin Pop
  fdata <- readFile afile
96 16c2369c Iustin Pop
  let flines = lines fdata
97 16c2369c Iustin Pop
      (nlines, ilines) = break null flines
98 040afc35 Iustin Pop
  return $ do
99 16c2369c Iustin Pop
    ifixed <- case ilines of
100 16c2369c Iustin Pop
                [] -> Bad "Invalid format of the input file (no instance data)"
101 16c2369c Iustin Pop
                _:xs -> Ok xs
102 040afc35 Iustin Pop
    {- node file: name t_mem n_mem f_mem t_disk f_disk -}
103 16c2369c Iustin Pop
    (ktn, nl) <- loadTabular nlines loadNode
104 040afc35 Iustin Pop
    {- instance file: name mem disk status pnode snode -}
105 16c2369c Iustin Pop
    (_, il) <- loadTabular ifixed (loadInst ktn)
106 94e05c32 Iustin Pop
    return (nl, il, [])