root / src / Ganeti / Rpc.hs @ 5ce9cc30
History | View | Annotate | Download (20.2 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 |
, executeRpcCalls |
37 |
, logRpcErrors |
38 |
|
39 |
, rpcCallName |
40 |
, rpcCallTimeout |
41 |
, rpcCallData |
42 |
, rpcCallAcceptOffline |
43 |
|
44 |
, rpcResultFill |
45 |
|
46 |
, RpcCallInstanceInfo(..) |
47 |
, InstanceState(..) |
48 |
, InstanceInfo(..) |
49 |
, RpcResultInstanceInfo(..) |
50 |
|
51 |
, RpcCallAllInstancesInfo(..) |
52 |
, RpcResultAllInstancesInfo(..) |
53 |
|
54 |
, InstanceConsoleInfoParams(..) |
55 |
, InstanceConsoleInfo(..) |
56 |
, RpcCallInstanceConsoleInfo(..) |
57 |
, RpcResultInstanceConsoleInfo(..) |
58 |
|
59 |
, RpcCallInstanceList(..) |
60 |
, RpcResultInstanceList(..) |
61 |
|
62 |
, HvInfo(..) |
63 |
, StorageInfo(..) |
64 |
, RpcCallNodeInfo(..) |
65 |
, RpcResultNodeInfo(..) |
66 |
|
67 |
, RpcCallVersion(..) |
68 |
, RpcResultVersion(..) |
69 |
|
70 |
, RpcCallStorageList(..) |
71 |
, RpcResultStorageList(..) |
72 |
|
73 |
, RpcCallTestDelay(..) |
74 |
, RpcResultTestDelay(..) |
75 |
|
76 |
, RpcCallExportList(..) |
77 |
, RpcResultExportList(..) |
78 |
|
79 |
, RpcCallJobqueueUpdate(..) |
80 |
, RpcCallSetWatcherPause(..) |
81 |
) where |
82 |
|
83 |
import Control.Arrow (second) |
84 |
import qualified Codec.Compression.Zlib as Zlib |
85 |
import qualified Data.ByteString.Lazy.Char8 as BL |
86 |
import qualified Data.Map as Map |
87 |
import Data.Maybe (fromMaybe) |
88 |
import qualified Text.JSON as J |
89 |
import Text.JSON.Pretty (pp_value) |
90 |
import qualified Data.ByteString.Base64.Lazy as Base64 |
91 |
|
92 |
import Network.Curl hiding (content) |
93 |
import qualified Ganeti.Path as P |
94 |
|
95 |
import Ganeti.BasicTypes |
96 |
import qualified Ganeti.Constants as C |
97 |
import Ganeti.JSON |
98 |
import Ganeti.Logging |
99 |
import Ganeti.Objects |
100 |
import Ganeti.THH |
101 |
import Ganeti.Types |
102 |
import Ganeti.Curl.Multi |
103 |
import Ganeti.Utils |
104 |
|
105 |
-- * Base RPC functionality and types |
106 |
|
107 |
-- | The curl options used for RPC. |
108 |
curlOpts :: [CurlOption] |
109 |
curlOpts = [ CurlFollowLocation False |
110 |
, CurlSSLVerifyHost 0 |
111 |
, CurlSSLVerifyPeer True |
112 |
, CurlSSLCertType "PEM" |
113 |
, CurlSSLKeyType "PEM" |
114 |
, CurlConnectTimeout (fromIntegral C.rpcConnectTimeout) |
115 |
] |
116 |
|
117 |
-- | Data type for RPC error reporting. |
118 |
data RpcError |
119 |
= CurlLayerError String |
120 |
| JsonDecodeError String |
121 |
| RpcResultError String |
122 |
| OfflineNodeError |
123 |
deriving (Show, Eq) |
124 |
|
125 |
-- | Provide explanation to RPC errors. |
126 |
explainRpcError :: RpcError -> String |
127 |
explainRpcError (CurlLayerError code) = |
128 |
"Curl error:" ++ code |
129 |
explainRpcError (JsonDecodeError msg) = |
130 |
"Error while decoding JSON from HTTP response: " ++ msg |
131 |
explainRpcError (RpcResultError msg) = |
132 |
"Error reponse received from RPC server: " ++ msg |
133 |
explainRpcError OfflineNodeError = |
134 |
"Node is marked offline" |
135 |
|
136 |
type ERpcError = Either RpcError |
137 |
|
138 |
-- | A generic class for RPC calls. |
139 |
class (J.JSON a) => RpcCall a where |
140 |
-- | Give the (Python) name of the procedure. |
141 |
rpcCallName :: a -> String |
142 |
-- | Calculate the timeout value for the call execution. |
143 |
rpcCallTimeout :: a -> Int |
144 |
-- | Prepare arguments of the call to be send as POST. |
145 |
rpcCallData :: Node -> a -> String |
146 |
-- | Whether we accept offline nodes when making a call. |
147 |
rpcCallAcceptOffline :: a -> Bool |
148 |
|
149 |
-- | Generic class that ensures matching RPC call with its respective |
150 |
-- result. |
151 |
class (RpcCall a, J.JSON b) => Rpc a b | a -> b, b -> a where |
152 |
-- | Create a result based on the received HTTP response. |
153 |
rpcResultFill :: a -> J.JSValue -> ERpcError b |
154 |
|
155 |
-- | Http Request definition. |
156 |
data HttpClientRequest = HttpClientRequest |
157 |
{ requestUrl :: String -- ^ The actual URL for the node endpoint |
158 |
, requestData :: String -- ^ The arguments for the call |
159 |
, requestOpts :: [CurlOption] -- ^ The various curl options |
160 |
} |
161 |
|
162 |
-- | Prepare url for the HTTP request. |
163 |
prepareUrl :: (RpcCall a) => Node -> a -> String |
164 |
prepareUrl node call = |
165 |
let node_ip = nodePrimaryIp node |
166 |
port = C.defaultNodedPort |
167 |
path_prefix = "https://" ++ node_ip ++ ":" ++ show port |
168 |
in path_prefix ++ "/" ++ rpcCallName call |
169 |
|
170 |
-- | Create HTTP request for a given node provided it is online, |
171 |
-- otherwise create empty response. |
172 |
prepareHttpRequest :: (RpcCall a) => [CurlOption] -> Node -> a |
173 |
-> ERpcError HttpClientRequest |
174 |
prepareHttpRequest opts node call |
175 |
| rpcCallAcceptOffline call || not (nodeOffline node) = |
176 |
Right HttpClientRequest { requestUrl = prepareUrl node call |
177 |
, requestData = rpcCallData node call |
178 |
, requestOpts = opts ++ curlOpts |
179 |
} |
180 |
| otherwise = Left OfflineNodeError |
181 |
|
182 |
-- | Parse an HTTP reply. |
183 |
parseHttpReply :: (Rpc a b) => |
184 |
a -> ERpcError (CurlCode, String) -> ERpcError b |
185 |
parseHttpReply _ (Left e) = Left e |
186 |
parseHttpReply call (Right (CurlOK, body)) = parseHttpResponse call body |
187 |
parseHttpReply _ (Right (code, err)) = |
188 |
Left . CurlLayerError $ "code: " ++ show code ++ ", explanation: " ++ err |
189 |
|
190 |
-- | Parse a result based on the received HTTP response. |
191 |
parseHttpResponse :: (Rpc a b) => a -> String -> ERpcError b |
192 |
parseHttpResponse call res = |
193 |
case J.decode res of |
194 |
J.Error val -> Left $ JsonDecodeError val |
195 |
J.Ok (True, res'') -> rpcResultFill call res'' |
196 |
J.Ok (False, jerr) -> case jerr of |
197 |
J.JSString msg -> Left $ RpcResultError (J.fromJSString msg) |
198 |
_ -> Left . JsonDecodeError $ show (pp_value jerr) |
199 |
|
200 |
-- | Scan the list of results produced by executeRpcCall and log all the RPC |
201 |
-- errors. |
202 |
logRpcErrors :: [(a, ERpcError b)] -> IO () |
203 |
logRpcErrors allElems = |
204 |
let logOneRpcErr (_, Right _) = return () |
205 |
logOneRpcErr (_, Left err) = |
206 |
logError $ "Error in the RPC HTTP reply: " ++ show err |
207 |
in mapM_ logOneRpcErr allElems |
208 |
|
209 |
-- | Get options for RPC call |
210 |
getOptionsForCall :: (Rpc a b) => FilePath -> a -> [CurlOption] |
211 |
getOptionsForCall certPath call = |
212 |
[ CurlTimeout (fromIntegral $ rpcCallTimeout call) |
213 |
, CurlSSLCert certPath |
214 |
, CurlSSLKey certPath |
215 |
, CurlCAInfo certPath |
216 |
] |
217 |
|
218 |
-- | Execute multiple RPC calls in parallel |
219 |
executeRpcCalls :: (Rpc a b) => [(Node, a)] -> IO [(Node, ERpcError b)] |
220 |
executeRpcCalls nodeCalls = do |
221 |
cert_file <- P.nodedCertFile |
222 |
let (nodes, calls) = unzip nodeCalls |
223 |
opts = map (getOptionsForCall cert_file) calls |
224 |
opts_urls = zipWith3 (\n c o -> |
225 |
case prepareHttpRequest o n c of |
226 |
Left v -> Left v |
227 |
Right request -> |
228 |
Right (CurlPostFields [requestData request]: |
229 |
requestOpts request, |
230 |
requestUrl request) |
231 |
) nodes calls opts |
232 |
-- split the opts_urls list; we don't want to pass the |
233 |
-- failed-already nodes to Curl |
234 |
let (lefts, rights, trail) = splitEithers opts_urls |
235 |
results <- execMultiCall rights |
236 |
results' <- case recombineEithers lefts results trail of |
237 |
Bad msg -> error msg |
238 |
Ok r -> return r |
239 |
-- now parse the replies |
240 |
let results'' = zipWith parseHttpReply calls results' |
241 |
pairedList = zip nodes results'' |
242 |
logRpcErrors pairedList |
243 |
return pairedList |
244 |
|
245 |
-- | Execute an RPC call for many nodes in parallel. |
246 |
executeRpcCall :: (Rpc a b) => [Node] -> a -> IO [(Node, ERpcError b)] |
247 |
executeRpcCall nodes call = executeRpcCalls . zip nodes $ repeat call |
248 |
|
249 |
-- | Helper function that is used to read dictionaries of values. |
250 |
sanitizeDictResults :: [(String, J.Result a)] -> ERpcError [(String, a)] |
251 |
sanitizeDictResults = |
252 |
foldr sanitize1 (Right []) |
253 |
where |
254 |
sanitize1 _ (Left e) = Left e |
255 |
sanitize1 (_, J.Error e) _ = Left $ JsonDecodeError e |
256 |
sanitize1 (name, J.Ok v) (Right res) = Right $ (name, v) : res |
257 |
|
258 |
-- | Helper function to tranform JSON Result to Either RpcError b. |
259 |
-- Note: For now we really only use it for b s.t. Rpc c b for some c |
260 |
fromJResultToRes :: J.Result a -> (a -> b) -> ERpcError b |
261 |
fromJResultToRes (J.Error v) _ = Left $ JsonDecodeError v |
262 |
fromJResultToRes (J.Ok v) f = Right $ f v |
263 |
|
264 |
-- | Helper function transforming JSValue to Rpc result type. |
265 |
fromJSValueToRes :: (J.JSON a) => J.JSValue -> (a -> b) -> ERpcError b |
266 |
fromJSValueToRes val = fromJResultToRes (J.readJSON val) |
267 |
|
268 |
-- * RPC calls and results |
269 |
|
270 |
-- ** Instance info |
271 |
|
272 |
-- | Returns information about a single instance |
273 |
$(buildObject "RpcCallInstanceInfo" "rpcCallInstInfo" |
274 |
[ simpleField "instance" [t| String |] |
275 |
, simpleField "hname" [t| Hypervisor |] |
276 |
]) |
277 |
|
278 |
$(declareILADT "InstanceState" |
279 |
[ ("InstanceStateRunning", 0) |
280 |
, ("InstanceStateShutdown", 1) |
281 |
]) |
282 |
|
283 |
$(makeJSONInstance ''InstanceState) |
284 |
|
285 |
instance PyValue InstanceState where |
286 |
showValue = show . instanceStateToRaw |
287 |
|
288 |
$(buildObject "InstanceInfo" "instInfo" |
289 |
[ simpleField "memory" [t| Int|] |
290 |
, simpleField "state" [t| InstanceState |] |
291 |
, simpleField "vcpus" [t| Int |] |
292 |
, simpleField "time" [t| Int |] |
293 |
]) |
294 |
|
295 |
-- This is optional here because the result may be empty if instance is |
296 |
-- not on a node - and this is not considered an error. |
297 |
$(buildObject "RpcResultInstanceInfo" "rpcResInstInfo" |
298 |
[ optionalField $ simpleField "inst_info" [t| InstanceInfo |]]) |
299 |
|
300 |
instance RpcCall RpcCallInstanceInfo where |
301 |
rpcCallName _ = "instance_info" |
302 |
rpcCallTimeout _ = rpcTimeoutToRaw Urgent |
303 |
rpcCallAcceptOffline _ = False |
304 |
rpcCallData _ call = J.encode |
305 |
( rpcCallInstInfoInstance call |
306 |
, rpcCallInstInfoHname call |
307 |
) |
308 |
|
309 |
instance Rpc RpcCallInstanceInfo RpcResultInstanceInfo where |
310 |
rpcResultFill _ res = |
311 |
case res of |
312 |
J.JSObject res' -> |
313 |
case J.fromJSObject res' of |
314 |
[] -> Right $ RpcResultInstanceInfo Nothing |
315 |
_ -> fromJSValueToRes res (RpcResultInstanceInfo . Just) |
316 |
_ -> Left $ JsonDecodeError |
317 |
("Expected JSObject, got " ++ show (pp_value res)) |
318 |
|
319 |
-- ** AllInstancesInfo |
320 |
|
321 |
-- | Returns information about all running instances on the given nodes |
322 |
$(buildObject "RpcCallAllInstancesInfo" "rpcCallAllInstInfo" |
323 |
[ simpleField "hypervisors" [t| [(Hypervisor, HvParams)] |] ]) |
324 |
|
325 |
$(buildObject "RpcResultAllInstancesInfo" "rpcResAllInstInfo" |
326 |
[ simpleField "instances" [t| [(String, InstanceInfo)] |] ]) |
327 |
|
328 |
instance RpcCall RpcCallAllInstancesInfo where |
329 |
rpcCallName _ = "all_instances_info" |
330 |
rpcCallTimeout _ = rpcTimeoutToRaw Urgent |
331 |
rpcCallAcceptOffline _ = False |
332 |
rpcCallData _ call = J.encode ( |
333 |
map fst $ rpcCallAllInstInfoHypervisors call, |
334 |
GenericContainer . Map.fromList $ rpcCallAllInstInfoHypervisors call) |
335 |
|
336 |
instance Rpc RpcCallAllInstancesInfo RpcResultAllInstancesInfo where |
337 |
-- FIXME: Is there a simpler way to do it? |
338 |
rpcResultFill _ res = |
339 |
case res of |
340 |
J.JSObject res' -> |
341 |
let res'' = map (second J.readJSON) (J.fromJSObject res') |
342 |
:: [(String, J.Result InstanceInfo)] in |
343 |
case sanitizeDictResults res'' of |
344 |
Left err -> Left err |
345 |
Right insts -> Right $ RpcResultAllInstancesInfo insts |
346 |
_ -> Left $ JsonDecodeError |
347 |
("Expected JSObject, got " ++ show (pp_value res)) |
348 |
|
349 |
-- ** InstanceConsoleInfo |
350 |
|
351 |
-- | Returns information about how to access instances on the given node |
352 |
$(buildObject "InstanceConsoleInfoParams" "instConsInfoParams" |
353 |
[ simpleField "instance" [t| Instance |] |
354 |
, simpleField "node" [t| Node |] |
355 |
, simpleField "group" [t| NodeGroup |] |
356 |
, simpleField "hvParams" [t| HvParams |] |
357 |
, simpleField "beParams" [t| FilledBeParams |] |
358 |
]) |
359 |
|
360 |
$(buildObject "RpcCallInstanceConsoleInfo" "rpcCallInstConsInfo" |
361 |
[ simpleField "instanceInfo" [t| [(String, InstanceConsoleInfoParams)] |] ]) |
362 |
|
363 |
$(buildObject "InstanceConsoleInfo" "instConsInfo" |
364 |
[ simpleField "instance" [t| String |] |
365 |
, simpleField "kind" [t| String |] |
366 |
, optionalField $ |
367 |
simpleField "message" [t| String |] |
368 |
, optionalField $ |
369 |
simpleField "host" [t| String |] |
370 |
, optionalField $ |
371 |
simpleField "port" [t| Int |] |
372 |
, optionalField $ |
373 |
simpleField "user" [t| String |] |
374 |
, optionalField $ |
375 |
simpleField "command" [t| [String] |] |
376 |
, optionalField $ |
377 |
simpleField "display" [t| String |] |
378 |
]) |
379 |
|
380 |
$(buildObject "RpcResultInstanceConsoleInfo" "rpcResInstConsInfo" |
381 |
[ simpleField "instancesInfo" [t| [(String, InstanceConsoleInfo)] |] ]) |
382 |
|
383 |
instance RpcCall RpcCallInstanceConsoleInfo where |
384 |
rpcCallName _ = "instance_console_info" |
385 |
rpcCallTimeout _ = rpcTimeoutToRaw Urgent |
386 |
rpcCallAcceptOffline _ = False |
387 |
rpcCallData _ call = J.encode . |
388 |
GenericContainer $ Map.fromList (rpcCallInstConsInfoInstanceInfo call) |
389 |
|
390 |
instance Rpc RpcCallInstanceConsoleInfo RpcResultInstanceConsoleInfo where |
391 |
rpcResultFill _ res = |
392 |
case res of |
393 |
J.JSObject res' -> |
394 |
let res'' = map (second J.readJSON) (J.fromJSObject res') |
395 |
:: [(String, J.Result InstanceConsoleInfo)] in |
396 |
case sanitizeDictResults res'' of |
397 |
Left err -> Left err |
398 |
Right instInfos -> Right $ RpcResultInstanceConsoleInfo instInfos |
399 |
_ -> Left $ JsonDecodeError |
400 |
("Expected JSObject, got " ++ show (pp_value res)) |
401 |
|
402 |
-- ** InstanceList |
403 |
|
404 |
-- | Returns the list of running instances on the given nodes |
405 |
$(buildObject "RpcCallInstanceList" "rpcCallInstList" |
406 |
[ simpleField "hypervisors" [t| [Hypervisor] |] ]) |
407 |
|
408 |
$(buildObject "RpcResultInstanceList" "rpcResInstList" |
409 |
[ simpleField "instances" [t| [String] |] ]) |
410 |
|
411 |
instance RpcCall RpcCallInstanceList where |
412 |
rpcCallName _ = "instance_list" |
413 |
rpcCallTimeout _ = rpcTimeoutToRaw Urgent |
414 |
rpcCallAcceptOffline _ = False |
415 |
rpcCallData _ call = J.encode [rpcCallInstListHypervisors call] |
416 |
|
417 |
instance Rpc RpcCallInstanceList RpcResultInstanceList where |
418 |
rpcResultFill _ res = fromJSValueToRes res RpcResultInstanceList |
419 |
|
420 |
-- ** NodeInfo |
421 |
|
422 |
-- | Returns node information |
423 |
$(buildObject "RpcCallNodeInfo" "rpcCallNodeInfo" |
424 |
[ simpleField "storage_units" [t| Map.Map String [StorageUnit] |] |
425 |
, simpleField "hypervisors" [t| [ (Hypervisor, HvParams) ] |] |
426 |
]) |
427 |
|
428 |
$(buildObject "StorageInfo" "storageInfo" |
429 |
[ simpleField "name" [t| String |] |
430 |
, simpleField "type" [t| String |] |
431 |
, optionalField $ simpleField "storage_free" [t| Int |] |
432 |
, optionalField $ simpleField "storage_size" [t| Int |] |
433 |
]) |
434 |
|
435 |
-- | We only provide common fields as described in hv_base.py. |
436 |
$(buildObject "HvInfo" "hvInfo" |
437 |
[ simpleField "memory_total" [t| Int |] |
438 |
, simpleField "memory_free" [t| Int |] |
439 |
, simpleField "memory_dom0" [t| Int |] |
440 |
, simpleField "cpu_total" [t| Int |] |
441 |
, simpleField "cpu_nodes" [t| Int |] |
442 |
, simpleField "cpu_sockets" [t| Int |] |
443 |
, simpleField "cpu_dom0" [t| Int |] |
444 |
]) |
445 |
|
446 |
$(buildObject "RpcResultNodeInfo" "rpcResNodeInfo" |
447 |
[ simpleField "boot_id" [t| String |] |
448 |
, simpleField "storage_info" [t| [StorageInfo] |] |
449 |
, simpleField "hv_info" [t| [HvInfo] |] |
450 |
]) |
451 |
|
452 |
instance RpcCall RpcCallNodeInfo where |
453 |
rpcCallName _ = "node_info" |
454 |
rpcCallTimeout _ = rpcTimeoutToRaw Urgent |
455 |
rpcCallAcceptOffline _ = False |
456 |
rpcCallData n call = J.encode |
457 |
( fromMaybe (error $ "Programmer error: missing parameter for node named " |
458 |
++ nodeName n) |
459 |
$ Map.lookup (nodeUuid n) (rpcCallNodeInfoStorageUnits call) |
460 |
, rpcCallNodeInfoHypervisors call |
461 |
) |
462 |
|
463 |
instance Rpc RpcCallNodeInfo RpcResultNodeInfo where |
464 |
rpcResultFill _ res = |
465 |
fromJSValueToRes res (\(b, vg, hv) -> RpcResultNodeInfo b vg hv) |
466 |
|
467 |
-- ** Version |
468 |
|
469 |
-- | Query node version. |
470 |
$(buildObject "RpcCallVersion" "rpcCallVersion" []) |
471 |
|
472 |
-- | Query node reply. |
473 |
$(buildObject "RpcResultVersion" "rpcResultVersion" |
474 |
[ simpleField "version" [t| Int |] |
475 |
]) |
476 |
|
477 |
instance RpcCall RpcCallVersion where |
478 |
rpcCallName _ = "version" |
479 |
rpcCallTimeout _ = rpcTimeoutToRaw Urgent |
480 |
rpcCallAcceptOffline _ = True |
481 |
rpcCallData _ = J.encode |
482 |
|
483 |
instance Rpc RpcCallVersion RpcResultVersion where |
484 |
rpcResultFill _ res = fromJSValueToRes res RpcResultVersion |
485 |
|
486 |
-- ** StorageList |
487 |
|
488 |
$(buildObject "RpcCallStorageList" "rpcCallStorageList" |
489 |
[ simpleField "su_name" [t| StorageType |] |
490 |
, simpleField "su_args" [t| [String] |] |
491 |
, simpleField "name" [t| String |] |
492 |
, simpleField "fields" [t| [StorageField] |] |
493 |
]) |
494 |
|
495 |
-- FIXME: The resulting JSValues should have types appropriate for their |
496 |
-- StorageField value: Used -> Bool, Name -> String etc |
497 |
$(buildObject "RpcResultStorageList" "rpcResStorageList" |
498 |
[ simpleField "storage" [t| [[(StorageField, J.JSValue)]] |] ]) |
499 |
|
500 |
instance RpcCall RpcCallStorageList where |
501 |
rpcCallName _ = "storage_list" |
502 |
rpcCallTimeout _ = rpcTimeoutToRaw Normal |
503 |
rpcCallAcceptOffline _ = False |
504 |
rpcCallData _ call = J.encode |
505 |
( rpcCallStorageListSuName call |
506 |
, rpcCallStorageListSuArgs call |
507 |
, rpcCallStorageListName call |
508 |
, rpcCallStorageListFields call |
509 |
) |
510 |
|
511 |
instance Rpc RpcCallStorageList RpcResultStorageList where |
512 |
rpcResultFill call res = |
513 |
let sfields = rpcCallStorageListFields call in |
514 |
fromJSValueToRes res (RpcResultStorageList . map (zip sfields)) |
515 |
|
516 |
-- ** TestDelay |
517 |
|
518 |
-- | Call definition for test delay. |
519 |
$(buildObject "RpcCallTestDelay" "rpcCallTestDelay" |
520 |
[ simpleField "duration" [t| Double |] |
521 |
]) |
522 |
|
523 |
-- | Result definition for test delay. |
524 |
data RpcResultTestDelay = RpcResultTestDelay |
525 |
deriving Show |
526 |
|
527 |
-- | Custom JSON instance for null result. |
528 |
instance J.JSON RpcResultTestDelay where |
529 |
showJSON _ = J.JSNull |
530 |
readJSON J.JSNull = return RpcResultTestDelay |
531 |
readJSON _ = fail "Unable to read RpcResultTestDelay" |
532 |
|
533 |
instance RpcCall RpcCallTestDelay where |
534 |
rpcCallName _ = "test_delay" |
535 |
rpcCallTimeout = ceiling . (+ 5) . rpcCallTestDelayDuration |
536 |
rpcCallAcceptOffline _ = False |
537 |
rpcCallData _ call = J.encode [rpcCallTestDelayDuration call] |
538 |
|
539 |
instance Rpc RpcCallTestDelay RpcResultTestDelay where |
540 |
rpcResultFill _ res = fromJSValueToRes res id |
541 |
|
542 |
-- ** ExportList |
543 |
|
544 |
-- | Call definition for export list. |
545 |
|
546 |
$(buildObject "RpcCallExportList" "rpcCallExportList" []) |
547 |
|
548 |
-- | Result definition for export list. |
549 |
$(buildObject "RpcResultExportList" "rpcResExportList" |
550 |
[ simpleField "exports" [t| [String] |] |
551 |
]) |
552 |
|
553 |
instance RpcCall RpcCallExportList where |
554 |
rpcCallName _ = "export_list" |
555 |
rpcCallTimeout _ = rpcTimeoutToRaw Fast |
556 |
rpcCallAcceptOffline _ = False |
557 |
rpcCallData _ = J.encode |
558 |
|
559 |
instance Rpc RpcCallExportList RpcResultExportList where |
560 |
rpcResultFill _ res = fromJSValueToRes res RpcResultExportList |
561 |
|
562 |
-- ** Job Queue Replication |
563 |
|
564 |
-- | Update a job queue file |
565 |
|
566 |
$(buildObject "RpcCallJobqueueUpdate" "rpcCallJobqueueUpdate" |
567 |
[ simpleField "file_name" [t| String |] |
568 |
, simpleField "content" [t| String |] |
569 |
]) |
570 |
|
571 |
$(buildObject "RpcResultJobQueueUpdate" "rpcResultJobQueueUpdate" []) |
572 |
|
573 |
instance RpcCall RpcCallJobqueueUpdate where |
574 |
rpcCallName _ = "jobqueue_update" |
575 |
rpcCallTimeout _ = rpcTimeoutToRaw Fast |
576 |
rpcCallAcceptOffline _ = False |
577 |
rpcCallData _ call = J.encode |
578 |
( rpcCallJobqueueUpdateFileName call |
579 |
, ( C.rpcEncodingZlibBase64 |
580 |
, BL.unpack . Base64.encode . Zlib.compress . BL.pack |
581 |
$ rpcCallJobqueueUpdateContent call |
582 |
) |
583 |
) |
584 |
|
585 |
instance Rpc RpcCallJobqueueUpdate RpcResultJobQueueUpdate where |
586 |
rpcResultFill _ res = |
587 |
case res of |
588 |
J.JSNull -> Right RpcResultJobQueueUpdate |
589 |
_ -> Left $ JsonDecodeError |
590 |
("Expected JSNull, got " ++ show (pp_value res)) |
591 |
|
592 |
-- ** Watcher Status Update |
593 |
|
594 |
-- | Set the watcher status |
595 |
|
596 |
$(buildObject "RpcCallSetWatcherPause" "rpcCallSetWatcherPause" |
597 |
[ simpleField "time" [t| Maybe Double |] |
598 |
]) |
599 |
|
600 |
instance RpcCall RpcCallSetWatcherPause where |
601 |
rpcCallName _ = "set_watcher_pause" |
602 |
rpcCallTimeout _ = rpcTimeoutToRaw Fast |
603 |
rpcCallAcceptOffline _ = False |
604 |
rpcCallData _ call = J.encode |
605 |
[ maybe J.JSNull J.showJSON $ rpcCallSetWatcherPauseTime call ] |
606 |
|
607 |
$(buildObject "RpcResultSetWatcherPause" "rpcResultSetWatcherPause" []) |
608 |
|
609 |
instance Rpc RpcCallSetWatcherPause RpcResultSetWatcherPause where |
610 |
rpcResultFill _ res = |
611 |
case res of |
612 |
J.JSNull -> Right RpcResultSetWatcherPause |
613 |
_ -> Left $ JsonDecodeError |
614 |
("Expected JSNull, got " ++ show (pp_value res)) |
615 |
|