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