Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.8 kB)

1
{-| Parsing data from text-files
2

    
3
This module holds the code for loading the cluster state from text
4
files, as produced by gnt-node and gnt-instance list.
5

    
6
-}
7

    
8
{-
9

    
10
Copyright (C) 2009, 2010, 2011 Google Inc.
11

    
12
This program is free software; you can redistribute it and/or modify
13
it under the terms of the GNU General Public License as published by
14
the Free Software Foundation; either version 2 of the License, or
15
(at your option) any later version.
16

    
17
This program is distributed in the hope that it will be useful, but
18
WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20
General Public License for more details.
21

    
22
You should have received a copy of the GNU General Public License
23
along with this program; if not, write to the Free Software
24
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
02110-1301, USA.
26

    
27
-}
28

    
29
module Ganeti.HTools.Text
30
    (
31
      loadData
32
    , parseData
33
    , loadInst
34
    , loadNode
35
    , serializeInstances
36
    , serializeNode
37
    , serializeNodes
38
    , serializeCluster
39
    ) where
40

    
41
import Control.Monad
42
import Data.List
43

    
44
import Text.Printf (printf)
45

    
46
import Ganeti.HTools.Utils
47
import Ganeti.HTools.Loader
48
import Ganeti.HTools.Types
49
import qualified Ganeti.HTools.Container as Container
50
import qualified Ganeti.HTools.Group as Group
51
import qualified Ganeti.HTools.Node as Node
52
import qualified Ganeti.HTools.Instance as Instance
53

    
54
-- | Serialize a single group
55
serializeGroup :: Group.Group -> String
56
serializeGroup grp =
57
    printf "%s|%s|%s" (Group.name grp) (Group.uuid grp)
58
               (apolToString (Group.allocPolicy grp))
59

    
60
-- | Generate group file data from a group list
61
serializeGroups :: Group.List -> String
62
serializeGroups = unlines . map serializeGroup . Container.elems
63

    
64
-- | Serialize a single node
65
serializeNode :: Group.List -> Node.Node -> String
66
serializeNode gl node =
67
    printf "%s|%.0f|%d|%d|%.0f|%d|%.0f|%c|%s" (Node.name node)
68
               (Node.tMem node) (Node.nMem node) (Node.fMem node)
69
               (Node.tDsk node) (Node.fDsk node) (Node.tCpu node)
70
               (if Node.offline node then 'Y' else 'N')
71
               (Group.uuid grp)
72
    where grp = Container.find (Node.group node) gl
73

    
74
-- | Generate node file data from node objects
75
serializeNodes :: Group.List -> Node.List -> String
76
serializeNodes gl = unlines . map (serializeNode gl) . Container.elems
77

    
78
-- | Serialize a single instance
79
serializeInstance :: Node.List -> Instance.Instance -> String
80
serializeInstance nl inst =
81
    let
82
        iname = Instance.name inst
83
        pnode = Container.nameOf nl (Instance.pNode inst)
84
        sidx = Instance.sNode inst
85
        snode = (if sidx == Node.noSecondary
86
                    then ""
87
                    else Container.nameOf nl sidx)
88
    in
89
      printf "%s|%d|%d|%d|%s|%s|%s|%s"
90
             iname (Instance.mem inst) (Instance.dsk inst)
91
             (Instance.vcpus inst) (Instance.runSt inst)
92
             pnode snode (intercalate "," (Instance.tags inst))
93

    
94
-- | Generate instance file data from instance objects
95
serializeInstances :: Node.List -> Instance.List -> String
96
serializeInstances nl =
97
    unlines . map (serializeInstance nl) . Container.elems
98

    
99
-- | Generate complete cluster data from node and instance lists
100
serializeCluster :: ClusterData -> String
101
serializeCluster (ClusterData gl nl il ctags) =
102
  let gdata = serializeGroups gl
103
      ndata = serializeNodes gl nl
104
      idata = serializeInstances nl il
105
  -- note: not using 'unlines' as that adds too many newlines
106
  in intercalate "\n" [gdata, ndata, idata, unlines ctags]
107

    
108
-- | Load a group from a field list.
109
loadGroup :: (Monad m) => [String] -> m (String, Group.Group)
110
loadGroup [name, gid, apol] = do
111
  xapol <- apolFromString apol
112
  return (gid, Group.create name gid xapol)
113

    
114
loadGroup s = fail $ "Invalid/incomplete group data: '" ++ show s ++ "'"
115

    
116
-- | Load a node from a field list.
117
loadNode :: (Monad m) => NameAssoc -> [String] -> m (String, Node.Node)
118
loadNode ktg [name, tm, nm, fm, td, fd, tc, fo, gu] = do
119
  gdx <- lookupGroup ktg name gu
120
  new_node <-
121
      if any (== "?") [tm,nm,fm,td,fd,tc] || fo == "Y" then
122
          return $ Node.create name 0 0 0 0 0 0 True gdx
123
      else do
124
        vtm <- tryRead name tm
125
        vnm <- tryRead name nm
126
        vfm <- tryRead name fm
127
        vtd <- tryRead name td
128
        vfd <- tryRead name fd
129
        vtc <- tryRead name tc
130
        return $ Node.create name vtm vnm vfm vtd vfd vtc False gdx
131
  return (name, new_node)
132
loadNode _ s = fail $ "Invalid/incomplete node data: '" ++ show s ++ "'"
133

    
134
-- | Load an instance from a field list.
135
loadInst :: (Monad m) =>
136
            NameAssoc -> [String] -> m (String, Instance.Instance)
137
loadInst ktn [name, mem, dsk, vcpus, status, pnode, snode, tags] = do
138
  pidx <- lookupNode ktn name pnode
139
  sidx <- (if null snode then return Node.noSecondary
140
           else lookupNode ktn name snode)
141
  vmem <- tryRead name mem
142
  vdsk <- tryRead name dsk
143
  vvcpus <- tryRead name vcpus
144
  when (sidx == pidx) $ fail $ "Instance " ++ name ++
145
           " has same primary and secondary node - " ++ pnode
146
  let vtags = sepSplit ',' tags
147
      newinst = Instance.create name vmem vdsk vvcpus status vtags
148
                True pidx sidx
149
  return (name, newinst)
150
loadInst _ s = fail $ "Invalid/incomplete instance data: '" ++ show s ++ "'"
151

    
152
-- | Convert newline and delimiter-separated text.
153
--
154
-- This function converts a text in tabular format as generated by
155
-- @gnt-instance list@ and @gnt-node list@ to a list of objects using
156
-- a supplied conversion function.
157
loadTabular :: (Monad m, Element a) =>
158
               [String] -> ([String] -> m (String, a))
159
            -> m (NameAssoc, Container.Container a)
160
loadTabular lines_data convert_fn = do
161
  let rows = map (sepSplit '|') lines_data
162
  kerows <- mapM convert_fn rows
163
  return $ assignIndices kerows
164

    
165
-- | Load the cluser data from disk.
166
readData :: String -- ^ Path to the text file
167
         -> IO String
168
readData = readFile
169

    
170
-- | Builds the cluster data from text input.
171
parseData :: String -- ^ Text data
172
          -> Result ClusterData
173
parseData fdata = do
174
  let flines = lines fdata
175
  (glines, nlines, ilines, ctags) <-
176
      case sepSplit "" flines of
177
        [a, b, c, d] -> Ok (a, b, c, d)
178
        xs -> Bad $ printf "Invalid format of the input file: %d sections\
179
                           \ instead of 4" (length xs)
180
  {- group file: name uuid -}
181
  (ktg, gl) <- loadTabular glines loadGroup
182
  {- node file: name t_mem n_mem f_mem t_disk f_disk -}
183
  (ktn, nl) <- loadTabular nlines (loadNode ktg)
184
  {- instance file: name mem disk status pnode snode -}
185
  (_, il) <- loadTabular ilines (loadInst ktn)
186
  {- the tags are simply line-based, no processing needed -}
187
  return (ClusterData gl nl il ctags)
188

    
189
-- | Top level function for data loading
190
loadData :: String -- ^ Path to the text file
191
         -> IO (Result ClusterData)
192
loadData = fmap parseData . readData