root / src / Ganeti / Rpc.hs @ 030ab01a
History | View | Annotate | Download (15.8 kB)
1 |
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, |
---|---|
2 |
BangPatterns, TemplateHaskell #-} |
3 |
|
4 |
{-| Implementation of the RPC client. |
5 |
|
6 |
-} |
7 |
|
8 |
{- |
9 |
|
10 |
Copyright (C) 2012, 2013 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.Rpc |
30 |
( RpcCall |
31 |
, Rpc |
32 |
, RpcError(..) |
33 |
, ERpcError |
34 |
, explainRpcError |
35 |
, executeRpcCall |
36 |
|
37 |
, rpcCallName |
38 |
, rpcCallTimeout |
39 |
, rpcCallData |
40 |
, rpcCallAcceptOffline |
41 |
|
42 |
, rpcResultFill |
43 |
|
44 |
, InstanceInfo(..) |
45 |
, RpcCallInstanceInfo(..) |
46 |
, RpcResultInstanceInfo(..) |
47 |
|
48 |
, RpcCallAllInstancesInfo(..) |
49 |
, RpcResultAllInstancesInfo(..) |
50 |
|
51 |
, RpcCallInstanceList(..) |
52 |
, RpcResultInstanceList(..) |
53 |
|
54 |
, HvInfo(..) |
55 |
, VgInfo(..) |
56 |
, RpcCallNodeInfo(..) |
57 |
, RpcResultNodeInfo(..) |
58 |
|
59 |
, RpcCallVersion(..) |
60 |
, RpcResultVersion(..) |
61 |
|
62 |
, StorageField(..) |
63 |
, RpcCallStorageList(..) |
64 |
, RpcResultStorageList(..) |
65 |
|
66 |
, RpcCallTestDelay(..) |
67 |
, RpcResultTestDelay(..) |
68 |
|
69 |
, RpcCallExportList(..) |
70 |
, RpcResultExportList(..) |
71 |
|
72 |
, rpcTimeoutFromRaw -- FIXME: Not used anywhere |
73 |
) where |
74 |
|
75 |
import Control.Arrow (second) |
76 |
import qualified Data.Map as Map |
77 |
import Data.Maybe (fromMaybe) |
78 |
import qualified Text.JSON as J |
79 |
import Text.JSON.Pretty (pp_value) |
80 |
|
81 |
import Network.Curl |
82 |
import qualified Ganeti.Path as P |
83 |
|
84 |
import Ganeti.BasicTypes |
85 |
import qualified Ganeti.Constants as C |
86 |
import Ganeti.Objects |
87 |
import Ganeti.THH |
88 |
import Ganeti.Types |
89 |
import Ganeti.Curl.Multi |
90 |
import Ganeti.Utils |
91 |
|
92 |
-- * Base RPC functionality and types |
93 |
|
94 |
-- | The curl options used for RPC. |
95 |
curlOpts :: [CurlOption] |
96 |
curlOpts = [ CurlFollowLocation False |
97 |
, CurlSSLVerifyHost 0 |
98 |
, CurlSSLVerifyPeer True |
99 |
, CurlSSLCertType "PEM" |
100 |
, CurlSSLKeyType "PEM" |
101 |
, CurlConnectTimeout (fromIntegral C.rpcConnectTimeout) |
102 |
] |
103 |
|
104 |
-- | Data type for RPC error reporting. |
105 |
data RpcError |
106 |
= CurlLayerError String |
107 |
| JsonDecodeError String |
108 |
| RpcResultError String |
109 |
| OfflineNodeError |
110 |
deriving (Show, Eq) |
111 |
|
112 |
-- | Provide explanation to RPC errors. |
113 |
explainRpcError :: RpcError -> String |
114 |
explainRpcError (CurlLayerError code) = |
115 |
"Curl error:" ++ code |
116 |
explainRpcError (JsonDecodeError msg) = |
117 |
"Error while decoding JSON from HTTP response: " ++ msg |
118 |
explainRpcError (RpcResultError msg) = |
119 |
"Error reponse received from RPC server: " ++ msg |
120 |
explainRpcError OfflineNodeError = |
121 |
"Node is marked offline" |
122 |
|
123 |
type ERpcError = Either RpcError |
124 |
|
125 |
-- | Basic timeouts for RPC calls. |
126 |
$(declareIADT "RpcTimeout" |
127 |
[ ( "Urgent", 'C.rpcTmoUrgent ) |
128 |
, ( "Fast", 'C.rpcTmoFast ) |
129 |
, ( "Normal", 'C.rpcTmoNormal ) |
130 |
, ( "Slow", 'C.rpcTmoSlow ) |
131 |
, ( "FourHours", 'C.rpcTmo4hrs ) |
132 |
, ( "OneDay", 'C.rpcTmo1day ) |
133 |
]) |
134 |
|
135 |
-- | A generic class for RPC calls. |
136 |
class (J.JSON a) => RpcCall a where |
137 |
-- | Give the (Python) name of the procedure. |
138 |
rpcCallName :: a -> String |
139 |
-- | Calculate the timeout value for the call execution. |
140 |
rpcCallTimeout :: a -> Int |
141 |
-- | Prepare arguments of the call to be send as POST. |
142 |
rpcCallData :: Node -> a -> String |
143 |
-- | Whether we accept offline nodes when making a call. |
144 |
rpcCallAcceptOffline :: a -> Bool |
145 |
|
146 |
-- | Generic class that ensures matching RPC call with its respective |
147 |
-- result. |
148 |
class (RpcCall a, J.JSON b) => Rpc a b | a -> b, b -> a where |
149 |
-- | Create a result based on the received HTTP response. |
150 |
rpcResultFill :: a -> J.JSValue -> ERpcError b |
151 |
|
152 |
-- | Http Request definition. |
153 |
data HttpClientRequest = HttpClientRequest |
154 |
{ requestUrl :: String -- ^ The actual URL for the node endpoint |
155 |
, requestData :: String -- ^ The arguments for the call |
156 |
, requestOpts :: [CurlOption] -- ^ The various curl options |
157 |
} |
158 |
|
159 |
-- | Prepare url for the HTTP request. |
160 |
prepareUrl :: (RpcCall a) => Node -> a -> String |
161 |
prepareUrl node call = |
162 |
let node_ip = nodePrimaryIp node |
163 |
port = snd C.daemonsPortsGanetiNoded |
164 |
path_prefix = "https://" ++ node_ip ++ ":" ++ show port |
165 |
in path_prefix ++ "/" ++ rpcCallName call |
166 |
|
167 |
-- | Create HTTP request for a given node provided it is online, |
168 |
-- otherwise create empty response. |
169 |
prepareHttpRequest :: (RpcCall a) => [CurlOption] -> Node -> a |
170 |
-> ERpcError HttpClientRequest |
171 |
prepareHttpRequest opts node call |
172 |
| rpcCallAcceptOffline call || not (nodeOffline node) = |
173 |
Right HttpClientRequest { requestUrl = prepareUrl node call |
174 |
, requestData = rpcCallData node call |
175 |
, requestOpts = opts ++ curlOpts |
176 |
} |
177 |
| otherwise = Left OfflineNodeError |
178 |
|
179 |
-- | Parse an HTTP reply. |
180 |
parseHttpReply :: (Rpc a b) => |
181 |
a -> ERpcError (CurlCode, String) -> ERpcError b |
182 |
parseHttpReply _ (Left e) = Left e |
183 |
parseHttpReply call (Right (CurlOK, body)) = parseHttpResponse call body |
184 |
parseHttpReply _ (Right (code, err)) = |
185 |
Left . CurlLayerError $ "code: " ++ show code ++ ", explanation: " ++ err |
186 |
|
187 |
-- | Parse a result based on the received HTTP response. |
188 |
parseHttpResponse :: (Rpc a b) => a -> String -> ERpcError b |
189 |
parseHttpResponse call res = |
190 |
case J.decode res of |
191 |
J.Error val -> Left $ JsonDecodeError val |
192 |
J.Ok (True, res'') -> rpcResultFill call res'' |
193 |
J.Ok (False, jerr) -> case jerr of |
194 |
J.JSString msg -> Left $ RpcResultError (J.fromJSString msg) |
195 |
_ -> Left . JsonDecodeError $ show (pp_value jerr) |
196 |
|
197 |
-- | Execute RPC call for many nodes in parallel. |
198 |
executeRpcCall :: (Rpc a b) => [Node] -> a -> IO [(Node, ERpcError b)] |
199 |
executeRpcCall nodes call = do |
200 |
cert_file <- P.nodedCertFile |
201 |
let opts = [ CurlTimeout (fromIntegral $ rpcCallTimeout call) |
202 |
, CurlSSLCert cert_file |
203 |
, CurlSSLKey cert_file |
204 |
, CurlCAInfo cert_file |
205 |
] |
206 |
opts_urls = map (\n -> |
207 |
case prepareHttpRequest opts n call of |
208 |
Left v -> Left v |
209 |
Right request -> |
210 |
Right (CurlPostFields [requestData request]: |
211 |
requestOpts request, |
212 |
requestUrl request) |
213 |
) nodes |
214 |
-- split the opts_urls list; we don't want to pass the |
215 |
-- failed-already nodes to Curl |
216 |
let (lefts, rights, trail) = splitEithers opts_urls |
217 |
results <- execMultiCall rights |
218 |
results' <- case recombineEithers lefts results trail of |
219 |
Bad msg -> error msg |
220 |
Ok r -> return r |
221 |
-- now parse the replies |
222 |
let results'' = map (parseHttpReply call) results' |
223 |
return $ zip nodes results'' |
224 |
|
225 |
-- | Helper function that is used to read dictionaries of values. |
226 |
sanitizeDictResults :: [(String, J.Result a)] -> ERpcError [(String, a)] |
227 |
sanitizeDictResults = |
228 |
foldr sanitize1 (Right []) |
229 |
where |
230 |
sanitize1 _ (Left e) = Left e |
231 |
sanitize1 (_, J.Error e) _ = Left $ JsonDecodeError e |
232 |
sanitize1 (name, J.Ok v) (Right res) = Right $ (name, v) : res |
233 |
|
234 |
-- | Helper function to tranform JSON Result to Either RpcError b. |
235 |
-- Note: For now we really only use it for b s.t. Rpc c b for some c |
236 |
fromJResultToRes :: J.Result a -> (a -> b) -> ERpcError b |
237 |
fromJResultToRes (J.Error v) _ = Left $ JsonDecodeError v |
238 |
fromJResultToRes (J.Ok v) f = Right $ f v |
239 |
|
240 |
-- | Helper function transforming JSValue to Rpc result type. |
241 |
fromJSValueToRes :: (J.JSON a) => J.JSValue -> (a -> b) -> ERpcError b |
242 |
fromJSValueToRes val = fromJResultToRes (J.readJSON val) |
243 |
|
244 |
-- * RPC calls and results |
245 |
|
246 |
-- ** Instance info |
247 |
|
248 |
-- | InstanceInfo |
249 |
-- Returns information about a single instance. |
250 |
|
251 |
$(buildObject "RpcCallInstanceInfo" "rpcCallInstInfo" |
252 |
[ simpleField "instance" [t| String |] |
253 |
, simpleField "hname" [t| Hypervisor |] |
254 |
]) |
255 |
|
256 |
$(buildObject "InstanceInfo" "instInfo" |
257 |
[ simpleField "memory" [t| Int|] |
258 |
, simpleField "state" [t| String |] -- It depends on hypervisor :( |
259 |
, simpleField "vcpus" [t| Int |] |
260 |
, simpleField "time" [t| Int |] |
261 |
]) |
262 |
|
263 |
-- This is optional here because the result may be empty if instance is |
264 |
-- not on a node - and this is not considered an error. |
265 |
$(buildObject "RpcResultInstanceInfo" "rpcResInstInfo" |
266 |
[ optionalField $ simpleField "inst_info" [t| InstanceInfo |]]) |
267 |
|
268 |
instance RpcCall RpcCallInstanceInfo where |
269 |
rpcCallName _ = "instance_info" |
270 |
rpcCallTimeout _ = rpcTimeoutToRaw Urgent |
271 |
rpcCallAcceptOffline _ = False |
272 |
rpcCallData _ call = J.encode |
273 |
( rpcCallInstInfoInstance call |
274 |
, rpcCallInstInfoHname call |
275 |
) |
276 |
|
277 |
instance Rpc RpcCallInstanceInfo RpcResultInstanceInfo where |
278 |
rpcResultFill _ res = |
279 |
case res of |
280 |
J.JSObject res' -> |
281 |
case J.fromJSObject res' of |
282 |
[] -> Right $ RpcResultInstanceInfo Nothing |
283 |
_ -> fromJSValueToRes res (RpcResultInstanceInfo . Just) |
284 |
_ -> Left $ JsonDecodeError |
285 |
("Expected JSObject, got " ++ show (pp_value res)) |
286 |
|
287 |
-- ** AllInstancesInfo |
288 |
|
289 |
-- | AllInstancesInfo |
290 |
-- Returns information about all running instances on the given nodes |
291 |
$(buildObject "RpcCallAllInstancesInfo" "rpcCallAllInstInfo" |
292 |
[ simpleField "hypervisors" [t| [Hypervisor] |] ]) |
293 |
|
294 |
$(buildObject "RpcResultAllInstancesInfo" "rpcResAllInstInfo" |
295 |
[ simpleField "instances" [t| [(String, InstanceInfo)] |] ]) |
296 |
|
297 |
instance RpcCall RpcCallAllInstancesInfo where |
298 |
rpcCallName _ = "all_instances_info" |
299 |
rpcCallTimeout _ = rpcTimeoutToRaw Urgent |
300 |
rpcCallAcceptOffline _ = False |
301 |
rpcCallData _ call = J.encode [rpcCallAllInstInfoHypervisors call] |
302 |
|
303 |
instance Rpc RpcCallAllInstancesInfo RpcResultAllInstancesInfo where |
304 |
-- FIXME: Is there a simpler way to do it? |
305 |
rpcResultFill _ res = |
306 |
case res of |
307 |
J.JSObject res' -> |
308 |
let res'' = map (second J.readJSON) (J.fromJSObject res') |
309 |
:: [(String, J.Result InstanceInfo)] in |
310 |
case sanitizeDictResults res'' of |
311 |
Left err -> Left err |
312 |
Right insts -> Right $ RpcResultAllInstancesInfo insts |
313 |
_ -> Left $ JsonDecodeError |
314 |
("Expected JSObject, got " ++ show (pp_value res)) |
315 |
|
316 |
-- ** InstanceList |
317 |
|
318 |
-- | InstanceList |
319 |
-- Returns the list of running instances on the given nodes. |
320 |
$(buildObject "RpcCallInstanceList" "rpcCallInstList" |
321 |
[ simpleField "hypervisors" [t| [Hypervisor] |] ]) |
322 |
|
323 |
$(buildObject "RpcResultInstanceList" "rpcResInstList" |
324 |
[ simpleField "instances" [t| [String] |] ]) |
325 |
|
326 |
instance RpcCall RpcCallInstanceList where |
327 |
rpcCallName _ = "instance_list" |
328 |
rpcCallTimeout _ = rpcTimeoutToRaw Urgent |
329 |
rpcCallAcceptOffline _ = False |
330 |
rpcCallData _ call = J.encode [rpcCallInstListHypervisors call] |
331 |
|
332 |
instance Rpc RpcCallInstanceList RpcResultInstanceList where |
333 |
rpcResultFill _ res = fromJSValueToRes res RpcResultInstanceList |
334 |
|
335 |
-- ** NodeInfo |
336 |
|
337 |
-- | NodeInfo |
338 |
-- Return node information. |
339 |
$(buildObject "RpcCallNodeInfo" "rpcCallNodeInfo" |
340 |
[ simpleField "storage_units" [t| [ (StorageType, String) ] |] |
341 |
, simpleField "hypervisors" [t| [ (Hypervisor, HvParams) ] |] |
342 |
, simpleField "exclusive_storage" [t| Map.Map String Bool |] |
343 |
]) |
344 |
|
345 |
$(buildObject "VgInfo" "vgInfo" |
346 |
[ simpleField "name" [t| String |] |
347 |
, optionalField $ simpleField "vg_free" [t| Int |] |
348 |
, optionalField $ simpleField "vg_size" [t| Int |] |
349 |
]) |
350 |
|
351 |
-- | We only provide common fields as described in hv_base.py. |
352 |
$(buildObject "HvInfo" "hvInfo" |
353 |
[ simpleField "memory_total" [t| Int |] |
354 |
, simpleField "memory_free" [t| Int |] |
355 |
, simpleField "memory_dom0" [t| Int |] |
356 |
, simpleField "cpu_total" [t| Int |] |
357 |
, simpleField "cpu_nodes" [t| Int |] |
358 |
, simpleField "cpu_sockets" [t| Int |] |
359 |
]) |
360 |
|
361 |
$(buildObject "RpcResultNodeInfo" "rpcResNodeInfo" |
362 |
[ simpleField "boot_id" [t| String |] |
363 |
, simpleField "vg_info" [t| [VgInfo] |] |
364 |
, simpleField "hv_info" [t| [HvInfo] |] |
365 |
]) |
366 |
|
367 |
instance RpcCall RpcCallNodeInfo where |
368 |
rpcCallName _ = "node_info" |
369 |
rpcCallTimeout _ = rpcTimeoutToRaw Urgent |
370 |
rpcCallAcceptOffline _ = False |
371 |
rpcCallData n call = J.encode |
372 |
( rpcCallNodeInfoStorageUnits call |
373 |
, rpcCallNodeInfoHypervisors call |
374 |
, fromMaybe (error $ "Programmer error: missing parameter for node named " |
375 |
++ nodeName n) |
376 |
$ Map.lookup (nodeName n) (rpcCallNodeInfoExclusiveStorage call) |
377 |
) |
378 |
|
379 |
instance Rpc RpcCallNodeInfo RpcResultNodeInfo where |
380 |
rpcResultFill _ res = |
381 |
fromJSValueToRes res (\(b, vg, hv) -> RpcResultNodeInfo b vg hv) |
382 |
|
383 |
-- ** Version |
384 |
|
385 |
-- | Query node version. |
386 |
$(buildObject "RpcCallVersion" "rpcCallVersion" []) |
387 |
|
388 |
-- | Query node reply. |
389 |
$(buildObject "RpcResultVersion" "rpcResultVersion" |
390 |
[ simpleField "version" [t| Int |] |
391 |
]) |
392 |
|
393 |
instance RpcCall RpcCallVersion where |
394 |
rpcCallName _ = "version" |
395 |
rpcCallTimeout _ = rpcTimeoutToRaw Urgent |
396 |
rpcCallAcceptOffline _ = True |
397 |
rpcCallData _ = J.encode |
398 |
|
399 |
instance Rpc RpcCallVersion RpcResultVersion where |
400 |
rpcResultFill _ res = fromJSValueToRes res RpcResultVersion |
401 |
|
402 |
-- ** StorageList |
403 |
|
404 |
-- | StorageList |
405 |
|
406 |
-- FIXME: This may be moved to Objects |
407 |
$(declareSADT "StorageField" |
408 |
[ ( "SFUsed", 'C.sfUsed) |
409 |
, ( "SFName", 'C.sfName) |
410 |
, ( "SFAllocatable", 'C.sfAllocatable) |
411 |
, ( "SFFree", 'C.sfFree) |
412 |
, ( "SFSize", 'C.sfSize) |
413 |
]) |
414 |
$(makeJSONInstance ''StorageField) |
415 |
|
416 |
$(buildObject "RpcCallStorageList" "rpcCallStorageList" |
417 |
[ simpleField "su_name" [t| StorageType |] |
418 |
, simpleField "su_args" [t| [String] |] |
419 |
, simpleField "name" [t| String |] |
420 |
, simpleField "fields" [t| [StorageField] |] |
421 |
]) |
422 |
|
423 |
-- FIXME: The resulting JSValues should have types appropriate for their |
424 |
-- StorageField value: Used -> Bool, Name -> String etc |
425 |
$(buildObject "RpcResultStorageList" "rpcResStorageList" |
426 |
[ simpleField "storage" [t| [[(StorageField, J.JSValue)]] |] ]) |
427 |
|
428 |
instance RpcCall RpcCallStorageList where |
429 |
rpcCallName _ = "storage_list" |
430 |
rpcCallTimeout _ = rpcTimeoutToRaw Normal |
431 |
rpcCallAcceptOffline _ = False |
432 |
rpcCallData _ call = J.encode |
433 |
( rpcCallStorageListSuName call |
434 |
, rpcCallStorageListSuArgs call |
435 |
, rpcCallStorageListName call |
436 |
, rpcCallStorageListFields call |
437 |
) |
438 |
|
439 |
instance Rpc RpcCallStorageList RpcResultStorageList where |
440 |
rpcResultFill call res = |
441 |
let sfields = rpcCallStorageListFields call in |
442 |
fromJSValueToRes res (RpcResultStorageList . map (zip sfields)) |
443 |
|
444 |
-- ** TestDelay |
445 |
|
446 |
-- | Call definition for test delay. |
447 |
$(buildObject "RpcCallTestDelay" "rpcCallTestDelay" |
448 |
[ simpleField "duration" [t| Double |] |
449 |
]) |
450 |
|
451 |
-- | Result definition for test delay. |
452 |
data RpcResultTestDelay = RpcResultTestDelay |
453 |
deriving Show |
454 |
|
455 |
-- | Custom JSON instance for null result. |
456 |
instance J.JSON RpcResultTestDelay where |
457 |
showJSON _ = J.JSNull |
458 |
readJSON J.JSNull = return RpcResultTestDelay |
459 |
readJSON _ = fail "Unable to read RpcResultTestDelay" |
460 |
|
461 |
instance RpcCall RpcCallTestDelay where |
462 |
rpcCallName _ = "test_delay" |
463 |
rpcCallTimeout = ceiling . (+ 5) . rpcCallTestDelayDuration |
464 |
rpcCallAcceptOffline _ = False |
465 |
rpcCallData _ call = J.encode [rpcCallTestDelayDuration call] |
466 |
|
467 |
instance Rpc RpcCallTestDelay RpcResultTestDelay where |
468 |
rpcResultFill _ res = fromJSValueToRes res id |
469 |
|
470 |
-- ** ExportList |
471 |
|
472 |
-- | Call definition for export list. |
473 |
|
474 |
$(buildObject "RpcCallExportList" "rpcCallExportList" []) |
475 |
|
476 |
-- | Result definition for export list. |
477 |
$(buildObject "RpcResultExportList" "rpcResExportList" |
478 |
[ simpleField "exports" [t| [String] |] |
479 |
]) |
480 |
|
481 |
instance RpcCall RpcCallExportList where |
482 |
rpcCallName _ = "export_list" |
483 |
rpcCallTimeout _ = rpcTimeoutToRaw Fast |
484 |
rpcCallAcceptOffline _ = False |
485 |
rpcCallData _ = J.encode |
486 |
|
487 |
instance Rpc RpcCallExportList RpcResultExportList where |
488 |
rpcResultFill _ res = fromJSValueToRes res RpcResultExportList |