root / Ganeti / HTools / Luxi.hs @ 5182e970
History | View | Annotate | Download (6 kB)
1 | 6583e677 | Iustin Pop | {-| Implementation of the LUXI loader. |
---|---|---|---|
2 | 53ec9022 | Iustin Pop | |
3 | 53ec9022 | Iustin Pop | -} |
4 | 53ec9022 | Iustin Pop | |
5 | 53ec9022 | Iustin Pop | {- |
6 | 53ec9022 | Iustin Pop | |
7 | 53ec9022 | Iustin Pop | Copyright (C) 2009 Google Inc. |
8 | 53ec9022 | Iustin Pop | |
9 | 53ec9022 | Iustin Pop | This program is free software; you can redistribute it and/or modify |
10 | 53ec9022 | Iustin Pop | it under the terms of the GNU General Public License as published by |
11 | 53ec9022 | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
12 | 53ec9022 | Iustin Pop | (at your option) any later version. |
13 | 53ec9022 | Iustin Pop | |
14 | 53ec9022 | Iustin Pop | This program is distributed in the hope that it will be useful, but |
15 | 53ec9022 | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | 53ec9022 | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | 53ec9022 | Iustin Pop | General Public License for more details. |
18 | 53ec9022 | Iustin Pop | |
19 | 53ec9022 | Iustin Pop | You should have received a copy of the GNU General Public License |
20 | 53ec9022 | Iustin Pop | along with this program; if not, write to the Free Software |
21 | 53ec9022 | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 | 53ec9022 | Iustin Pop | 02110-1301, USA. |
23 | 53ec9022 | Iustin Pop | |
24 | 53ec9022 | Iustin Pop | -} |
25 | 53ec9022 | Iustin Pop | |
26 | 53ec9022 | Iustin Pop | module Ganeti.HTools.Luxi |
27 | 53ec9022 | Iustin Pop | ( |
28 | 53ec9022 | Iustin Pop | loadData |
29 | 53ec9022 | Iustin Pop | ) where |
30 | 53ec9022 | Iustin Pop | |
31 | 53ec9022 | Iustin Pop | import qualified Control.Exception as E |
32 | 53ec9022 | Iustin Pop | import Text.JSON.Types |
33 | 53ec9022 | Iustin Pop | |
34 | 6583e677 | Iustin Pop | import qualified Ganeti.Luxi as L |
35 | 53ec9022 | Iustin Pop | import Ganeti.HTools.Loader |
36 | 53ec9022 | Iustin Pop | import Ganeti.HTools.Types |
37 | 53ec9022 | Iustin Pop | import qualified Ganeti.HTools.Node as Node |
38 | 53ec9022 | Iustin Pop | import qualified Ganeti.HTools.Instance as Instance |
39 | f89235f1 | Iustin Pop | import Ganeti.HTools.Utils (fromJVal, annotateResult, tryFromObj, asJSObject) |
40 | 53ec9022 | Iustin Pop | |
41 | 53ec9022 | Iustin Pop | -- * Utility functions |
42 | 53ec9022 | Iustin Pop | |
43 | 53ec9022 | Iustin Pop | -- | Ensure a given JSValue is actually a JSArray. |
44 | 53ec9022 | Iustin Pop | toArray :: (Monad m) => JSValue -> m [JSValue] |
45 | 53ec9022 | Iustin Pop | toArray v = |
46 | 53ec9022 | Iustin Pop | case v of |
47 | 53ec9022 | Iustin Pop | JSArray arr -> return arr |
48 | 53ec9022 | Iustin Pop | o -> fail ("Invalid input, expected array but got " ++ show o) |
49 | 53ec9022 | Iustin Pop | |
50 | 53ec9022 | Iustin Pop | -- * Data querying functionality |
51 | 53ec9022 | Iustin Pop | |
52 | 53ec9022 | Iustin Pop | -- | The input data for node query. |
53 | 53ec9022 | Iustin Pop | queryNodesMsg :: JSValue |
54 | 53ec9022 | Iustin Pop | queryNodesMsg = |
55 | 53ec9022 | Iustin Pop | let nnames = JSArray [] |
56 | 53ec9022 | Iustin Pop | fnames = ["name", |
57 | 53ec9022 | Iustin Pop | "mtotal", "mnode", "mfree", |
58 | 53ec9022 | Iustin Pop | "dtotal", "dfree", |
59 | 53ec9022 | Iustin Pop | "ctotal", |
60 | 53ec9022 | Iustin Pop | "offline", "drained"] |
61 | 53ec9022 | Iustin Pop | fields = JSArray $ map (JSString . toJSString) fnames |
62 | 53ec9022 | Iustin Pop | use_locking = JSBool False |
63 | 53ec9022 | Iustin Pop | in JSArray [nnames, fields, use_locking] |
64 | 53ec9022 | Iustin Pop | |
65 | 53ec9022 | Iustin Pop | -- | The input data for instance query. |
66 | 53ec9022 | Iustin Pop | queryInstancesMsg :: JSValue |
67 | 53ec9022 | Iustin Pop | queryInstancesMsg = |
68 | 53ec9022 | Iustin Pop | let nnames = JSArray [] |
69 | 53ec9022 | Iustin Pop | fnames = ["name", |
70 | 53ec9022 | Iustin Pop | "disk_usage", "be/memory", "be/vcpus", |
71 | 6402a260 | Iustin Pop | "status", "pnode", "snodes", "tags", "oper_ram"] |
72 | 53ec9022 | Iustin Pop | fields = JSArray $ map (JSString . toJSString) fnames |
73 | 53ec9022 | Iustin Pop | use_locking = JSBool False |
74 | 53ec9022 | Iustin Pop | in JSArray [nnames, fields, use_locking] |
75 | 53ec9022 | Iustin Pop | |
76 | f89235f1 | Iustin Pop | -- | The input data for cluster query |
77 | f89235f1 | Iustin Pop | queryClusterInfoMsg :: JSValue |
78 | f89235f1 | Iustin Pop | queryClusterInfoMsg = JSArray [] |
79 | f89235f1 | Iustin Pop | |
80 | 53ec9022 | Iustin Pop | -- | Wraper over callMethod doing node query. |
81 | 6583e677 | Iustin Pop | queryNodes :: L.Client -> IO (Result JSValue) |
82 | 6583e677 | Iustin Pop | queryNodes = L.callMethod L.QueryNodes queryNodesMsg |
83 | 53ec9022 | Iustin Pop | |
84 | 53ec9022 | Iustin Pop | -- | Wraper over callMethod doing instance query. |
85 | 6583e677 | Iustin Pop | queryInstances :: L.Client -> IO (Result JSValue) |
86 | 6583e677 | Iustin Pop | queryInstances = L.callMethod L.QueryInstances queryInstancesMsg |
87 | 53ec9022 | Iustin Pop | |
88 | f89235f1 | Iustin Pop | queryClusterInfo :: L.Client -> IO (Result JSValue) |
89 | f89235f1 | Iustin Pop | queryClusterInfo = L.callMethod L.QueryClusterInfo queryClusterInfoMsg |
90 | f89235f1 | Iustin Pop | |
91 | 53ec9022 | Iustin Pop | -- | Parse a instance list in JSON format. |
92 | 53ec9022 | Iustin Pop | getInstances :: NameAssoc |
93 | 53ec9022 | Iustin Pop | -> JSValue |
94 | 53ec9022 | Iustin Pop | -> Result [(String, Instance.Instance)] |
95 | 53ec9022 | Iustin Pop | getInstances ktn arr = toArray arr >>= mapM (parseInstance ktn) |
96 | 53ec9022 | Iustin Pop | |
97 | 53ec9022 | Iustin Pop | -- | Construct an instance from a JSON object. |
98 | 53ec9022 | Iustin Pop | parseInstance :: [(String, Ndx)] |
99 | 53ec9022 | Iustin Pop | -> JSValue |
100 | 53ec9022 | Iustin Pop | -> Result (String, Instance.Instance) |
101 | 27671a61 | Iustin Pop | parseInstance ktn (JSArray [ name, disk, mem, vcpus |
102 | 6402a260 | Iustin Pop | , status, pnode, snodes, tags, oram ]) = do |
103 | 117dc2d8 | Iustin Pop | xname <- annotateResult "Parsing new instance" (fromJVal name) |
104 | 117dc2d8 | Iustin Pop | let convert v = annotateResult ("Instance '" ++ xname ++ "'") (fromJVal v) |
105 | 117dc2d8 | Iustin Pop | xdisk <- convert disk |
106 | 6402a260 | Iustin Pop | xmem <- (case oram of |
107 | 6402a260 | Iustin Pop | JSRational _ _ -> convert oram |
108 | 6402a260 | Iustin Pop | _ -> convert mem) |
109 | 117dc2d8 | Iustin Pop | xvcpus <- convert vcpus |
110 | 117dc2d8 | Iustin Pop | xpnode <- convert pnode >>= lookupNode ktn xname |
111 | 117dc2d8 | Iustin Pop | xsnodes <- convert snodes::Result [JSString] |
112 | 53ec9022 | Iustin Pop | snode <- (if null xsnodes then return Node.noSecondary |
113 | 53ec9022 | Iustin Pop | else lookupNode ktn xname (fromJSString $ head xsnodes)) |
114 | 117dc2d8 | Iustin Pop | xrunning <- convert status |
115 | 17e7af2b | Iustin Pop | xtags <- convert tags |
116 | 17e7af2b | Iustin Pop | let inst = Instance.create xname xmem xdisk xvcpus |
117 | 17e7af2b | Iustin Pop | xrunning xtags xpnode snode |
118 | 53ec9022 | Iustin Pop | return (xname, inst) |
119 | 53ec9022 | Iustin Pop | |
120 | 53ec9022 | Iustin Pop | parseInstance _ v = fail ("Invalid instance query result: " ++ show v) |
121 | 53ec9022 | Iustin Pop | |
122 | 53ec9022 | Iustin Pop | -- | Parse a node list in JSON format. |
123 | 53ec9022 | Iustin Pop | getNodes :: JSValue -> Result [(String, Node.Node)] |
124 | 53ec9022 | Iustin Pop | getNodes arr = toArray arr >>= mapM parseNode |
125 | 53ec9022 | Iustin Pop | |
126 | 53ec9022 | Iustin Pop | -- | Construct a node from a JSON object. |
127 | 53ec9022 | Iustin Pop | parseNode :: JSValue -> Result (String, Node.Node) |
128 | 27671a61 | Iustin Pop | parseNode (JSArray [ name, mtotal, mnode, mfree, dtotal, dfree |
129 | 27671a61 | Iustin Pop | , ctotal, offline, drained ]) |
130 | 53ec9022 | Iustin Pop | = do |
131 | 117dc2d8 | Iustin Pop | xname <- annotateResult "Parsing new node" (fromJVal name) |
132 | 117dc2d8 | Iustin Pop | let convert v = annotateResult ("Node '" ++ xname ++ "'") (fromJVal v) |
133 | 117dc2d8 | Iustin Pop | xoffline <- convert offline |
134 | b45222ce | Iustin Pop | xdrained <- convert drained |
135 | b45222ce | Iustin Pop | node <- (if xoffline || xdrained |
136 | 53ec9022 | Iustin Pop | then return $ Node.create xname 0 0 0 0 0 0 True |
137 | 53ec9022 | Iustin Pop | else do |
138 | 117dc2d8 | Iustin Pop | xmtotal <- convert mtotal |
139 | 117dc2d8 | Iustin Pop | xmnode <- convert mnode |
140 | 117dc2d8 | Iustin Pop | xmfree <- convert mfree |
141 | 117dc2d8 | Iustin Pop | xdtotal <- convert dtotal |
142 | 117dc2d8 | Iustin Pop | xdfree <- convert dfree |
143 | 117dc2d8 | Iustin Pop | xctotal <- convert ctotal |
144 | 53ec9022 | Iustin Pop | return $ Node.create xname xmtotal xmnode xmfree |
145 | b45222ce | Iustin Pop | xdtotal xdfree xctotal False) |
146 | 53ec9022 | Iustin Pop | return (xname, node) |
147 | 53ec9022 | Iustin Pop | |
148 | 53ec9022 | Iustin Pop | parseNode v = fail ("Invalid node query result: " ++ show v) |
149 | 53ec9022 | Iustin Pop | |
150 | f89235f1 | Iustin Pop | getClusterTags :: JSValue -> Result [String] |
151 | f89235f1 | Iustin Pop | getClusterTags v = do |
152 | f89235f1 | Iustin Pop | let errmsg = "Parsing cluster info" |
153 | f89235f1 | Iustin Pop | obj <- annotateResult errmsg $ asJSObject v |
154 | 5182e970 | Iustin Pop | tryFromObj errmsg (fromJSObject obj) "tags" |
155 | f89235f1 | Iustin Pop | |
156 | 53ec9022 | Iustin Pop | -- * Main loader functionality |
157 | 53ec9022 | Iustin Pop | |
158 | 53ec9022 | Iustin Pop | -- | Builds the cluster data from an URL. |
159 | 53ec9022 | Iustin Pop | loadData :: String -- ^ Unix socket to use as source |
160 | 94e05c32 | Iustin Pop | -> IO (Result (Node.AssocList, Instance.AssocList, [String])) |
161 | 2485487d | Iustin Pop | loadData master = |
162 | 53ec9022 | Iustin Pop | E.bracket |
163 | 6583e677 | Iustin Pop | (L.getClient master) |
164 | 6583e677 | Iustin Pop | L.closeClient |
165 | 53ec9022 | Iustin Pop | (\s -> do |
166 | 53ec9022 | Iustin Pop | nodes <- queryNodes s |
167 | 53ec9022 | Iustin Pop | instances <- queryInstances s |
168 | f89235f1 | Iustin Pop | cinfo <- queryClusterInfo s |
169 | 53ec9022 | Iustin Pop | return $ do -- Result monad |
170 | 53ec9022 | Iustin Pop | node_data <- nodes >>= getNodes |
171 | 53ec9022 | Iustin Pop | let (node_names, node_idx) = assignIndices node_data |
172 | 53ec9022 | Iustin Pop | inst_data <- instances >>= getInstances node_names |
173 | 53ec9022 | Iustin Pop | let (_, inst_idx) = assignIndices inst_data |
174 | f89235f1 | Iustin Pop | ctags <- cinfo >>= getClusterTags |
175 | f89235f1 | Iustin Pop | return (node_idx, inst_idx, ctags) |
176 | 53ec9022 | Iustin Pop | ) |