Rpc.hs 14.5 KB
Newer Older
1
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, CPP,
2
  BangPatterns, TemplateHaskell #-}
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

{-| Implementation of the RPC client.

-}

{-

Copyright (C) 2012 Google Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

-}

module Ganeti.Rpc
  ( RpcCall
  , Rpc
  , RpcError(..)
33
  , ERpcError
34
  , explainRpcError
35
  , executeRpcCall
36
37
38
39
40
41
42

  , rpcCallName
  , rpcCallTimeout
  , rpcCallData
  , rpcCallAcceptOffline

  , rpcResultFill
43
44

  , InstanceInfo(..)
Agata Murawska's avatar
Agata Murawska committed
45
46
47
  , RpcCallInstanceInfo(..)
  , RpcResultInstanceInfo(..)

48
49
50
  , RpcCallAllInstancesInfo(..)
  , RpcResultAllInstancesInfo(..)

51
52
53
  , RpcCallInstanceList(..)
  , RpcResultInstanceList(..)

54
55
56
57
58
  , HvInfo(..)
  , VgInfo(..)
  , RpcCallNodeInfo(..)
  , RpcResultNodeInfo(..)

Agata Murawska's avatar
Agata Murawska committed
59
60
61
  , RpcCallVersion(..)
  , RpcResultVersion(..)

Agata Murawska's avatar
Agata Murawska committed
62
63
64
65
66
  , StorageType(..)
  , StorageField(..)
  , RpcCallStorageList(..)
  , RpcResultStorageList(..)

67
  , rpcTimeoutFromRaw -- FIXME: Not used anywhere
68
69
  ) where

70
import Control.Arrow (second)
71
import qualified Text.JSON as J
72
import Text.JSON.Pretty (pp_value)
73
import Text.JSON (makeObj)
74

75
76
#ifndef NO_CURL
import Network.Curl
Agata Murawska's avatar
Agata Murawska committed
77
import qualified Ganeti.Path as P
78
79
80
#endif

import qualified Ganeti.Constants as C
81
import Ganeti.Objects
82
import Ganeti.THH
83
84
import Ganeti.Compat
import Ganeti.JSON
85
86
87
88
89

#ifndef NO_CURL
-- | The curl options used for RPC.
curlOpts :: [CurlOption]
curlOpts = [ CurlFollowLocation False
Agata Murawska's avatar
Agata Murawska committed
90
           , CurlCAInfo P.nodedCertFile
91
92
93
           , CurlSSLVerifyHost 0
           , CurlSSLVerifyPeer True
           , CurlSSLCertType "PEM"
Agata Murawska's avatar
Agata Murawska committed
94
           , CurlSSLCert P.nodedCertFile
95
           , CurlSSLKeyType "PEM"
Agata Murawska's avatar
Agata Murawska committed
96
           , CurlSSLKey P.nodedCertFile
97
98
99
           , CurlConnectTimeout (fromIntegral C.rpcConnectTimeout)
           ]
#endif
100
101
102
103
104
105

-- | Data type for RPC error reporting.
data RpcError
  = CurlDisabledError
  | CurlLayerError Node String
  | JsonDecodeError String
Agata Murawska's avatar
Agata Murawska committed
106
  | RpcResultError String
107
  | OfflineNodeError Node
108
  deriving (Show, Eq)
109

110
111
112
-- | Provide explanation to RPC errors.
explainRpcError :: RpcError -> String
explainRpcError CurlDisabledError =
113
    "RPC/curl backend disabled at compile time"
114
explainRpcError (CurlLayerError node code) =
Agata Murawska's avatar
Agata Murawska committed
115
    "Curl error for " ++ nodeName node ++ ", " ++ code
116
explainRpcError (JsonDecodeError msg) =
Agata Murawska's avatar
Agata Murawska committed
117
    "Error while decoding JSON from HTTP response: " ++ msg
118
explainRpcError (RpcResultError msg) =
Agata Murawska's avatar
Agata Murawska committed
119
    "Error reponse received from RPC server: " ++ msg
120
explainRpcError (OfflineNodeError node) =
121
122
    "Node " ++ nodeName node ++ " is marked as offline"

123
124
type ERpcError = Either RpcError

125
126
127
128
129
130
131
132
133
134
-- | Basic timeouts for RPC calls.
$(declareIADT "RpcTimeout"
  [ ( "Urgent",    'C.rpcTmoUrgent )
  , ( "Fast",      'C.rpcTmoFast )
  , ( "Normal",    'C.rpcTmoNormal )
  , ( "Slow",      'C.rpcTmoSlow )
  , ( "FourHours", 'C.rpcTmo4hrs )
  , ( "OneDay",    'C.rpcTmo1day )
  ])

135
136
137
138
139
140
141
142
143
144
145
146
147
-- | A generic class for RPC calls.
class (J.JSON a) => RpcCall a where
  -- | Give the (Python) name of the procedure.
  rpcCallName :: a -> String
  -- | Calculate the timeout value for the call execution.
  rpcCallTimeout :: a -> Int
  -- | Prepare arguments of the call to be send as POST.
  rpcCallData :: Node -> a -> String
  -- | Whether we accept offline nodes when making a call.
  rpcCallAcceptOffline :: a -> Bool

-- | Generic class that ensures matching RPC call with its respective
-- result.
Agata Murawska's avatar
Agata Murawska committed
148
149
150
class (RpcCall a, J.JSON b) => Rpc a b  | a -> b, b -> a where
  -- | Create a result based on the received HTTP response.
  rpcResultFill :: (Monad m) => a -> J.JSValue -> m (ERpcError b)
151
152
153
154
155
156
157
158
159
160

-- | Http Request definition.
data HttpClientRequest = HttpClientRequest
  { requestTimeout :: Int
  , requestUrl :: String
  , requestPostData :: String
  }

-- | Execute the request and return the result as a plain String. When
-- curl reports an error, we propagate it.
161
162
executeHttpRequest :: Node -> ERpcError HttpClientRequest
                   -> IO (ERpcError String)
163
164
165
166
167
168
169
170
171
172
173
174

executeHttpRequest _ (Left rpc_err) = return $ Left rpc_err
#ifdef NO_CURL
executeHttpRequest _ _ = return $ Left CurlDisabledError
#else
executeHttpRequest node (Right request) = do
  let reqOpts = [ CurlTimeout (fromIntegral $ requestTimeout request)
                , CurlPostFields [requestPostData request]
                ]
      url = requestUrl request
  -- FIXME: This is very similar to getUrl in Htools/Rapi.hs
  (code, !body) <- curlGetString url $ curlOpts ++ reqOpts
Iustin Pop's avatar
Iustin Pop committed
175
176
177
  return $ case code of
             CurlOK -> Right body
             _ -> Left $ CurlLayerError node (show code)
178
179
180
181
182
183
184
#endif

-- | Prepare url for the HTTP request.
prepareUrl :: (RpcCall a) => Node -> a -> String
prepareUrl node call =
  let node_ip = nodePrimaryIp node
      port = snd C.daemonsPortsGanetiNoded
Iustin Pop's avatar
Iustin Pop committed
185
186
      path_prefix = "https://" ++ node_ip ++ ":" ++ show port
  in path_prefix ++ "/" ++ rpcCallName call
187
188
189
190

-- | Create HTTP request for a given node provided it is online,
-- otherwise create empty response.
prepareHttpRequest ::  (RpcCall a) => Node -> a
191
                   -> ERpcError HttpClientRequest
192
prepareHttpRequest node call
Iustin Pop's avatar
Iustin Pop committed
193
194
195
196
197
  | rpcCallAcceptOffline call || not (nodeOffline node) =
      Right HttpClientRequest { requestTimeout = rpcCallTimeout call
                              , requestUrl = prepareUrl node call
                              , requestPostData = rpcCallData node call
                              }
198
199
  | otherwise = Left $ OfflineNodeError node

200
-- | Parse a result based on the received HTTP response.
Agata Murawska's avatar
Agata Murawska committed
201
202
rpcResultParse :: (Monad m, Rpc a b) => a -> String -> m (ERpcError b)
rpcResultParse call res = do
203
204
205
  res' <- fromJResult "Reading JSON response" $ J.decode res
  case res' of
    (True, res'') ->
Agata Murawska's avatar
Agata Murawska committed
206
       rpcResultFill call res''
207
208
209
210
    (False, jerr) -> case jerr of
       J.JSString msg -> return . Left $ RpcResultError (J.fromJSString msg)
       _ -> (return . Left) . JsonDecodeError $ show (pp_value jerr)

211
-- | Parse the response or propagate the error.
Agata Murawska's avatar
Agata Murawska committed
212
213
214
parseHttpResponse :: (Rpc a b) => a -> ERpcError String -> IO (ERpcError b)
parseHttpResponse _ (Left err) = return $ Left err
parseHttpResponse call (Right response) = rpcResultParse call response
215
216

-- | Execute RPC call for a sigle node.
217
executeSingleRpcCall :: (Rpc a b) => Node -> a -> IO (Node, ERpcError b)
218
219
220
executeSingleRpcCall node call = do
  let request = prepareHttpRequest node call
  response <- executeHttpRequest node request
Agata Murawska's avatar
Agata Murawska committed
221
  result <- parseHttpResponse call response
222
223
224
  return (node, result)

-- | Execute RPC call for many nodes in parallel.
225
executeRpcCall :: (Rpc a b) => [Node] -> a -> IO [(Node, ERpcError b)]
226
227
228
executeRpcCall nodes call =
  sequence $ parMap rwhnf (uncurry executeSingleRpcCall)
               (zip nodes $ repeat call)
229

230
231
232
233
234
235
236
237
238
-- | Helper function that is used to read dictionaries of values.
sanitizeDictResults :: [(String, J.Result a)] -> ERpcError [(String, a)]
sanitizeDictResults [] = Right []
sanitizeDictResults ((_, J.Error err):_) = Left $ JsonDecodeError err
sanitizeDictResults ((name, J.Ok val):xs) =
  case sanitizeDictResults xs of
    Left err -> Left err
    Right res' -> Right $ (name, val):res'

239
240
-- * RPC calls and results

Agata Murawska's avatar
Agata Murawska committed
241
242
243
244
245
246
247
-- | InstanceInfo
--   Returns information about a single instance.

$(buildObject "RpcCallInstanceInfo" "rpcCallInstInfo"
  [ simpleField "instance" [t| String |]
  , simpleField "hname" [t| Hypervisor |]
  ])
248

Iustin Pop's avatar
Iustin Pop committed
249
$(buildObject "InstanceInfo" "instInfo"
250
251
  [ simpleField "memory" [t| Int|]
  , simpleField "state"  [t| String |] -- It depends on hypervisor :(
252
253
254
255
  , simpleField "vcpus"  [t| Int |]
  , simpleField "time"   [t| Int |]
  ])

Agata Murawska's avatar
Agata Murawska committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
-- This is optional here because the result may be empty if instance is
-- not on a node - and this is not considered an error.
$(buildObject "RpcResultInstanceInfo" "rpcResInstInfo"
  [ optionalField $ simpleField "inst_info" [t| InstanceInfo |]])

instance RpcCall RpcCallInstanceInfo where
  rpcCallName _ = "instance_info"
  rpcCallTimeout _ = rpcTimeoutToRaw Urgent
  rpcCallAcceptOffline _ = False
  rpcCallData _ call = J.encode
    ( rpcCallInstInfoInstance call
    , rpcCallInstInfoHname call
    )

instance Rpc RpcCallInstanceInfo RpcResultInstanceInfo where
  rpcResultFill _ res =
    return $ case res of
      J.JSObject res' ->
        case J.fromJSObject res' of
          [] -> Right $ RpcResultInstanceInfo Nothing
          _ ->
            case J.readJSON res of
              J.Error err -> Left $ JsonDecodeError err
              J.Ok val -> Right . RpcResultInstanceInfo $ Just val
      _ -> Left $ JsonDecodeError
           ("Expected JSObject, got " ++ show res)

-- | AllInstancesInfo
--   Returns information about all running instances on the given nodes
$(buildObject "RpcCallAllInstancesInfo" "rpcCallAllInstInfo"
  [ simpleField "hypervisors" [t| [Hypervisor] |] ])

Iustin Pop's avatar
Iustin Pop committed
288
$(buildObject "RpcResultAllInstancesInfo" "rpcResAllInstInfo"
289
  [ simpleField "instances" [t| [(String, InstanceInfo)] |] ])
290
291
292
293
294

instance RpcCall RpcCallAllInstancesInfo where
  rpcCallName _ = "all_instances_info"
  rpcCallTimeout _ = rpcTimeoutToRaw Urgent
  rpcCallAcceptOffline _ = False
295
296
  rpcCallData _ call = J.encode [rpcCallAllInstInfoHypervisors call]

Agata Murawska's avatar
Agata Murawska committed
297
instance Rpc RpcCallAllInstancesInfo RpcResultAllInstancesInfo where
298
  -- FIXME: Is there a simpler way to do it?
Agata Murawska's avatar
Agata Murawska committed
299
  rpcResultFill _ res =
300
301
302
303
304
305
306
307
308
    return $ case res of
      J.JSObject res' -> do
        let res'' = map (second J.readJSON) (J.fromJSObject res')
                        :: [(String, J.Result InstanceInfo)]
        case sanitizeDictResults res'' of
          Left err -> Left err
          Right insts -> Right $ RpcResultAllInstancesInfo insts
      _ -> Left $ JsonDecodeError
           ("Expected JSObject, got " ++ show res)
309

310
311
-- | InstanceList
-- Returns the list of running instances on the given nodes.
Iustin Pop's avatar
Iustin Pop committed
312
$(buildObject "RpcCallInstanceList" "rpcCallInstList"
313
314
  [ simpleField "hypervisors" [t| [Hypervisor] |] ])

Iustin Pop's avatar
Iustin Pop committed
315
$(buildObject "RpcResultInstanceList" "rpcResInstList"
316
  [ simpleField "instances" [t| [String] |] ])
317
318
319
320
321

instance RpcCall RpcCallInstanceList where
  rpcCallName _ = "instance_list"
  rpcCallTimeout _ = rpcTimeoutToRaw Urgent
  rpcCallAcceptOffline _ = False
322
  rpcCallData _ call = J.encode [rpcCallInstListHypervisors call]
323

Agata Murawska's avatar
Agata Murawska committed
324
325
326

instance Rpc RpcCallInstanceList RpcResultInstanceList where
  rpcResultFill _ res =
327
328
329
    return $ case J.readJSON res of
      J.Error err -> Left $ JsonDecodeError err
      J.Ok insts -> Right $ RpcResultInstanceList insts
330

331
332
-- | NodeInfo
-- Return node information.
Iustin Pop's avatar
Iustin Pop committed
333
$(buildObject "RpcCallNodeInfo" "rpcCallNodeInfo"
334
335
  [ simpleField "volume_groups" [t| [String] |]
  , simpleField "hypervisors" [t| [Hypervisor] |]
336
337
  ])

Iustin Pop's avatar
Iustin Pop committed
338
$(buildObject "VgInfo" "vgInfo"
339
  [ simpleField "name" [t| String |]
340
341
  , optionalField $ simpleField "vg_free" [t| Int |]
  , optionalField $ simpleField "vg_size" [t| Int |]
342
343
344
  ])

-- | We only provide common fields as described in hv_base.py.
Iustin Pop's avatar
Iustin Pop committed
345
$(buildObject "HvInfo" "hvInfo"
346
347
348
349
350
351
352
353
  [ simpleField "memory_total" [t| Int |]
  , simpleField "memory_free" [t| Int |]
  , simpleField "memory_dom0" [t| Int |]
  , simpleField "cpu_total" [t| Int |]
  , simpleField "cpu_nodes" [t| Int |]
  , simpleField "cpu_sockets" [t| Int |]
  ])

Iustin Pop's avatar
Iustin Pop committed
354
$(buildObject "RpcResultNodeInfo" "rpcResNodeInfo"
355
356
357
358
359
360
361
362
363
  [ simpleField "boot_id" [t| String |]
  , simpleField "vg_info" [t| [VgInfo] |]
  , simpleField "hv_info" [t| [HvInfo] |]
  ])

instance RpcCall RpcCallNodeInfo where
  rpcCallName _ = "node_info"
  rpcCallTimeout _ = rpcTimeoutToRaw Urgent
  rpcCallAcceptOffline _ = False
364
365
366
367
  rpcCallData _ call = J.encode ( rpcCallNodeInfoVolumeGroups call
                                , rpcCallNodeInfoHypervisors call
                                )

Agata Murawska's avatar
Agata Murawska committed
368
369
instance Rpc RpcCallNodeInfo RpcResultNodeInfo where
  rpcResultFill _ res =
370
371
372
373
    return $ case J.readJSON res of
      J.Error err -> Left $ JsonDecodeError err
      J.Ok (boot_id, vg_info, hv_info) ->
          Right $ RpcResultNodeInfo boot_id vg_info hv_info
374

Agata Murawska's avatar
Agata Murawska committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
-- | Version
-- Query node version.
-- Note: We can't use THH as it does not know what to do with empty dict
data RpcCallVersion = RpcCallVersion {}
  deriving (Show, Read, Eq)

instance J.JSON RpcCallVersion where
  showJSON _ = J.JSNull
  readJSON J.JSNull = return RpcCallVersion
  readJSON _ = fail "Unable to read RpcCallVersion"

$(buildObject "RpcResultVersion" "rpcResultVersion"
  [ simpleField "version" [t| Int |]
  ])

instance RpcCall RpcCallVersion where
  rpcCallName _ = "version"
  rpcCallTimeout _ = rpcTimeoutToRaw Urgent
  rpcCallAcceptOffline _ = True
  rpcCallData call _ = J.encode [call]

Agata Murawska's avatar
Agata Murawska committed
396
397
instance Rpc RpcCallVersion RpcResultVersion where
  rpcResultFill _ res =
Agata Murawska's avatar
Agata Murawska committed
398
399
400
401
    return $ case J.readJSON res of
      J.Error err -> Left $ JsonDecodeError err
      J.Ok ver -> Right $ RpcResultVersion ver

Agata Murawska's avatar
Agata Murawska committed
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
-- | StorageList
-- Get list of storage units.
-- FIXME: This may be moved to Objects
$(declareSADT "StorageType"
  [ ( "STLvmPv", 'C.stLvmPv )
  , ( "STFile",  'C.stFile )
  , ( "STLvmVg", 'C.stLvmVg )
  ])
$(makeJSONInstance ''StorageType)

-- FIXME: This may be moved to Objects
$(declareSADT "StorageField"
  [ ( "SFUsed",        'C.sfUsed)
  , ( "SFName",        'C.sfName)
  , ( "SFAllocatable", 'C.sfAllocatable)
  , ( "SFFree",        'C.sfFree)
  , ( "SFSize",        'C.sfSize)
  ])
$(makeJSONInstance ''StorageField)

$(buildObject "RpcCallStorageList" "rpcCallStorageList"
  [ simpleField "su_name" [t| StorageType |]
  , simpleField "su_args" [t| [String] |]
  , simpleField "name"    [t| String |]
  , simpleField "fields"  [t| [StorageField] |]
  ])

-- FIXME: The resulting JSValues should have types appropriate for their
-- StorageField value: Used -> Bool, Name -> String etc
$(buildObject "RpcResultStorageList" "rpcResStorageList"
  [ simpleField "storage" [t| [[(StorageField, J.JSValue)]] |] ])

instance RpcCall RpcCallStorageList where
  rpcCallName _ = "storage_list"
  rpcCallTimeout _ = rpcTimeoutToRaw Normal
  rpcCallAcceptOffline _ = False
  rpcCallData _ call = J.encode
    ( rpcCallStorageListSuName call
    , rpcCallStorageListSuArgs call
    , rpcCallStorageListName call
    , rpcCallStorageListFields call
    )

instance Rpc RpcCallStorageList RpcResultStorageList where
  rpcResultFill call res =
    let sfields = rpcCallStorageListFields call in
    return $ case J.readJSON res of
      J.Error err -> Left $ JsonDecodeError err
      J.Ok res_lst -> Right $ RpcResultStorageList (map (zip sfields) res_lst)