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