Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Luxi.hs @ 9131274c

History | View | Annotate | Download (11.9 kB)

1 a0090487 Agata Murawska
{-# LANGUAGE TemplateHaskell #-}
2 a0090487 Agata Murawska
3 6583e677 Iustin Pop
{-| Implementation of the Ganeti LUXI interface.
4 6583e677 Iustin Pop
5 6583e677 Iustin Pop
-}
6 6583e677 Iustin Pop
7 6583e677 Iustin Pop
{-
8 6583e677 Iustin Pop
9 71a4c605 Petr Pudlak
Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc.
10 6583e677 Iustin Pop
11 6583e677 Iustin Pop
This program is free software; you can redistribute it and/or modify
12 6583e677 Iustin Pop
it under the terms of the GNU General Public License as published by
13 6583e677 Iustin Pop
the Free Software Foundation; either version 2 of the License, or
14 6583e677 Iustin Pop
(at your option) any later version.
15 6583e677 Iustin Pop
16 6583e677 Iustin Pop
This program is distributed in the hope that it will be useful, but
17 6583e677 Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
18 6583e677 Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 6583e677 Iustin Pop
General Public License for more details.
20 6583e677 Iustin Pop
21 6583e677 Iustin Pop
You should have received a copy of the GNU General Public License
22 6583e677 Iustin Pop
along with this program; if not, write to the Free Software
23 6583e677 Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 6583e677 Iustin Pop
02110-1301, USA.
25 6583e677 Iustin Pop
26 6583e677 Iustin Pop
-}
27 6583e677 Iustin Pop
28 6583e677 Iustin Pop
module Ganeti.Luxi
29 ebf38064 Iustin Pop
  ( LuxiOp(..)
30 95d0d502 Iustin Pop
  , LuxiReq(..)
31 ebf38064 Iustin Pop
  , Client
32 0fbc8447 Petr Pudlak
  , Server
33 ccc817a2 Iustin Pop
  , JobId
34 c48711d5 Iustin Pop
  , fromJobId
35 c48711d5 Iustin Pop
  , makeJobId
36 0aff2293 Iustin Pop
  , RecvResult(..)
37 0aff2293 Iustin Pop
  , strOfOp
38 d605e261 Petr Pudlak
  , getLuxiClient
39 d605e261 Petr Pudlak
  , getLuxiServer
40 13f2321c Iustin Pop
  , acceptClient
41 ebf38064 Iustin Pop
  , closeClient
42 0aff2293 Iustin Pop
  , closeServer
43 ebf38064 Iustin Pop
  , callMethod
44 ebf38064 Iustin Pop
  , submitManyJobs
45 ebf38064 Iustin Pop
  , queryJobsStatus
46 cdd495ae Iustin Pop
  , buildCall
47 0aff2293 Iustin Pop
  , buildResponse
48 d79a6502 Petr Pudlak
  , decodeLuxiCall
49 13f2321c Iustin Pop
  , recvMsg
50 0aff2293 Iustin Pop
  , recvMsgExt
51 13f2321c Iustin Pop
  , sendMsg
52 471b6c46 Iustin Pop
  , allLuxiCalls
53 ebf38064 Iustin Pop
  ) where
54 6583e677 Iustin Pop
55 6583e677 Iustin Pop
import Control.Monad
56 71a4c605 Petr Pudlak
import qualified Data.ByteString.UTF8 as UTF8
57 0903280b Iustin Pop
import Text.JSON (encodeStrict, decodeStrict)
58 6583e677 Iustin Pop
import qualified Text.JSON as J
59 7adb7dff Iustin Pop
import Text.JSON.Pretty (pp_value)
60 6583e677 Iustin Pop
import Text.JSON.Types
61 6583e677 Iustin Pop
62 4cd79ca8 Iustin Pop
import Ganeti.BasicTypes
63 92678b3c Iustin Pop
import Ganeti.Constants
64 7adb7dff Iustin Pop
import Ganeti.Errors
65 7adb7dff Iustin Pop
import Ganeti.JSON
66 71a4c605 Petr Pudlak
import Ganeti.UDSServer
67 fa10983e Iustin Pop
import Ganeti.OpParams (pTagsObject)
68 367c4241 Dato Simó
import Ganeti.OpCodes
69 4cab6703 Iustin Pop
import qualified Ganeti.Query.Language as Qlang
70 0fbc8447 Petr Pudlak
import Ganeti.Runtime (GanetiDaemon(..))
71 a0090487 Agata Murawska
import Ganeti.THH
72 c48711d5 Iustin Pop
import Ganeti.Types
73 6583e677 Iustin Pop
74 0aff2293 Iustin Pop
75 a0090487 Agata Murawska
-- | Currently supported Luxi operations and JSON serialization.
76 a0090487 Agata Murawska
$(genLuxiOp "LuxiOp"
77 72295708 Iustin Pop
  [ (luxiReqQuery,
78 88609f00 Iustin Pop
    [ simpleField "what"    [t| Qlang.ItemType |]
79 88609f00 Iustin Pop
    , simpleField "fields"  [t| [String]  |]
80 88609f00 Iustin Pop
    , simpleField "qfilter" [t| Qlang.Filter Qlang.FilterField |]
81 ebf38064 Iustin Pop
    ])
82 72295708 Iustin Pop
  , (luxiReqQueryFields,
83 88609f00 Iustin Pop
    [ simpleField "what"    [t| Qlang.ItemType |]
84 88609f00 Iustin Pop
    , simpleField "fields"  [t| [String]  |]
85 72295708 Iustin Pop
    ])
86 fae980e5 Iustin Pop
  , (luxiReqQueryNodes,
87 88609f00 Iustin Pop
     [ simpleField "names"  [t| [String] |]
88 88609f00 Iustin Pop
     , simpleField "fields" [t| [String] |]
89 88609f00 Iustin Pop
     , simpleField "lock"   [t| Bool     |]
90 ebf38064 Iustin Pop
     ])
91 fae980e5 Iustin Pop
  , (luxiReqQueryGroups,
92 88609f00 Iustin Pop
     [ simpleField "names"  [t| [String] |]
93 88609f00 Iustin Pop
     , simpleField "fields" [t| [String] |]
94 88609f00 Iustin Pop
     , simpleField "lock"   [t| Bool     |]
95 ebf38064 Iustin Pop
     ])
96 795d035d Klaus Aehlig
  , (luxiReqQueryNetworks,
97 795d035d Klaus Aehlig
     [ simpleField "names"  [t| [String] |]
98 795d035d Klaus Aehlig
     , simpleField "fields" [t| [String] |]
99 795d035d Klaus Aehlig
     , simpleField "lock"   [t| Bool     |]
100 795d035d Klaus Aehlig
     ])
101 fae980e5 Iustin Pop
  , (luxiReqQueryInstances,
102 88609f00 Iustin Pop
     [ simpleField "names"  [t| [String] |]
103 88609f00 Iustin Pop
     , simpleField "fields" [t| [String] |]
104 88609f00 Iustin Pop
     , simpleField "lock"   [t| Bool     |]
105 ebf38064 Iustin Pop
     ])
106 fae980e5 Iustin Pop
  , (luxiReqQueryJobs,
107 c48711d5 Iustin Pop
     [ simpleField "ids"    [t| [JobId]  |]
108 88609f00 Iustin Pop
     , simpleField "fields" [t| [String] |]
109 ebf38064 Iustin Pop
     ])
110 fae980e5 Iustin Pop
  , (luxiReqQueryExports,
111 88609f00 Iustin Pop
     [ simpleField "nodes" [t| [String] |]
112 88609f00 Iustin Pop
     , simpleField "lock"  [t| Bool     |]
113 ebf38064 Iustin Pop
     ])
114 fae980e5 Iustin Pop
  , (luxiReqQueryConfigValues,
115 88609f00 Iustin Pop
     [ simpleField "fields" [t| [String] |] ]
116 ebf38064 Iustin Pop
    )
117 fae980e5 Iustin Pop
  , (luxiReqQueryClusterInfo, [])
118 fae980e5 Iustin Pop
  , (luxiReqQueryTags,
119 9131274c Jose A. Lopes
     [ pTagsObject
120 34af39e8 Jose A. Lopes
     , simpleField "name" [t| String |]
121 34af39e8 Jose A. Lopes
     ])
122 fae980e5 Iustin Pop
  , (luxiReqSubmitJob,
123 7e723913 Iustin Pop
     [ simpleField "job" [t| [MetaOpCode] |] ]
124 ebf38064 Iustin Pop
    )
125 346c3037 Klaus Aehlig
  , (luxiReqSubmitJobToDrainedQueue,
126 346c3037 Klaus Aehlig
     [ simpleField "job" [t| [MetaOpCode] |] ]
127 346c3037 Klaus Aehlig
    )
128 fae980e5 Iustin Pop
  , (luxiReqSubmitManyJobs,
129 7e723913 Iustin Pop
     [ simpleField "ops" [t| [[MetaOpCode]] |] ]
130 ebf38064 Iustin Pop
    )
131 fae980e5 Iustin Pop
  , (luxiReqWaitForJobChange,
132 c48711d5 Iustin Pop
     [ simpleField "job"      [t| JobId   |]
133 88609f00 Iustin Pop
     , simpleField "fields"   [t| [String]|]
134 88609f00 Iustin Pop
     , simpleField "prev_job" [t| JSValue |]
135 88609f00 Iustin Pop
     , simpleField "prev_log" [t| JSValue |]
136 88609f00 Iustin Pop
     , simpleField "tmout"    [t| Int     |]
137 ebf38064 Iustin Pop
     ])
138 d9d1e541 Klaus Aehlig
  , (luxiReqPickupJob,
139 d9d1e541 Klaus Aehlig
     [ simpleField "job" [t| JobId |] ]
140 d9d1e541 Klaus Aehlig
    )
141 fae980e5 Iustin Pop
  , (luxiReqArchiveJob,
142 c48711d5 Iustin Pop
     [ simpleField "job" [t| JobId |] ]
143 ebf38064 Iustin Pop
    )
144 fae980e5 Iustin Pop
  , (luxiReqAutoArchiveJobs,
145 88609f00 Iustin Pop
     [ simpleField "age"   [t| Int |]
146 88609f00 Iustin Pop
     , simpleField "tmout" [t| Int |]
147 ebf38064 Iustin Pop
     ])
148 fae980e5 Iustin Pop
  , (luxiReqCancelJob,
149 c48711d5 Iustin Pop
     [ simpleField "job" [t| JobId |] ]
150 ebf38064 Iustin Pop
    )
151 f63ffb37 Michael Hanselmann
  , (luxiReqChangeJobPriority,
152 c48711d5 Iustin Pop
     [ simpleField "job"      [t| JobId |]
153 f63ffb37 Michael Hanselmann
     , simpleField "priority" [t| Int |] ]
154 f63ffb37 Michael Hanselmann
    )
155 fae980e5 Iustin Pop
  , (luxiReqSetDrainFlag,
156 88609f00 Iustin Pop
     [ simpleField "flag" [t| Bool |] ]
157 ebf38064 Iustin Pop
    )
158 fae980e5 Iustin Pop
  , (luxiReqSetWatcherPause,
159 d819aba6 Klaus Aehlig
     [ optionalNullSerField
160 d819aba6 Klaus Aehlig
         $ simpleField "duration" [t| Double |] ]
161 ebf38064 Iustin Pop
    )
162 a0090487 Agata Murawska
  ])
163 6583e677 Iustin Pop
164 95d0d502 Iustin Pop
$(makeJSONInstance ''LuxiReq)
165 95d0d502 Iustin Pop
166 471b6c46 Iustin Pop
-- | List of all defined Luxi calls.
167 471b6c46 Iustin Pop
$(genAllConstr (drop 3) ''LuxiReq "allLuxiCalls")
168 471b6c46 Iustin Pop
169 6583e677 Iustin Pop
-- | The serialisation of LuxiOps into strings in messages.
170 a0090487 Agata Murawska
$(genStrOfOp ''LuxiOp "strOfOp")
171 6583e677 Iustin Pop
172 cdd495ae Iustin Pop
173 0fbc8447 Petr Pudlak
luxiConnectConfig :: ConnectConfig
174 0fbc8447 Petr Pudlak
luxiConnectConfig = ConnectConfig { connDaemon = GanetiLuxid
175 0fbc8447 Petr Pudlak
                                  , recvTmo    = luxiDefRwto
176 0fbc8447 Petr Pudlak
                                  , sendTmo    = luxiDefRwto
177 0fbc8447 Petr Pudlak
                                  }
178 0fbc8447 Petr Pudlak
179 0fbc8447 Petr Pudlak
-- | Connects to the master daemon and returns a luxi Client.
180 0fbc8447 Petr Pudlak
getLuxiClient :: String -> IO Client
181 0fbc8447 Petr Pudlak
getLuxiClient = connectClient luxiConnectConfig luxiDefCtmo
182 0fbc8447 Petr Pudlak
183 0fbc8447 Petr Pudlak
-- | Creates and returns a server endpoint.
184 0fbc8447 Petr Pudlak
getLuxiServer :: Bool -> FilePath -> IO Server
185 0fbc8447 Petr Pudlak
getLuxiServer = connectServer luxiConnectConfig
186 0fbc8447 Petr Pudlak
187 6583e677 Iustin Pop
-- | Serialize a request to String.
188 6583e677 Iustin Pop
buildCall :: LuxiOp  -- ^ The method
189 6583e677 Iustin Pop
          -> String  -- ^ The serialized form
190 683b1ca7 Iustin Pop
buildCall lo =
191 2cdaf225 Iustin Pop
  let ja = [ (strOfKey Method, J.showJSON $ strOfOp lo)
192 2cdaf225 Iustin Pop
           , (strOfKey Args, opToArgs lo)
193 ebf38064 Iustin Pop
           ]
194 ebf38064 Iustin Pop
      jo = toJSObject ja
195 ebf38064 Iustin Pop
  in encodeStrict jo
196 6583e677 Iustin Pop
197 cdd495ae Iustin Pop
198 cdd495ae Iustin Pop
-- | Converts Luxi call arguments into a 'LuxiOp' data structure.
199 d79a6502 Petr Pudlak
-- This is used for building a Luxi 'Handler'.
200 cdd495ae Iustin Pop
--
201 cdd495ae Iustin Pop
-- This is currently hand-coded until we make it more uniform so that
202 cdd495ae Iustin Pop
-- it can be generated using TH.
203 d79a6502 Petr Pudlak
decodeLuxiCall :: JSValue -> JSValue -> Result LuxiOp
204 d79a6502 Petr Pudlak
decodeLuxiCall method args = do
205 d79a6502 Petr Pudlak
  call <- fromJResult "Unable to parse LUXI request method" $ J.readJSON method
206 cdd495ae Iustin Pop
  case call of
207 cdd495ae Iustin Pop
    ReqQueryJobs -> do
208 c48711d5 Iustin Pop
              (jids, jargs) <- fromJVal args
209 d2970809 Iustin Pop
              jids' <- case jids of
210 d2970809 Iustin Pop
                         JSNull -> return []
211 d2970809 Iustin Pop
                         _ -> fromJVal jids
212 d2970809 Iustin Pop
              return $ QueryJobs jids' jargs
213 cdd495ae Iustin Pop
    ReqQueryInstances -> do
214 cdd495ae Iustin Pop
              (names, fields, locking) <- fromJVal args
215 cdd495ae Iustin Pop
              return $ QueryInstances names fields locking
216 cdd495ae Iustin Pop
    ReqQueryNodes -> do
217 cdd495ae Iustin Pop
              (names, fields, locking) <- fromJVal args
218 cdd495ae Iustin Pop
              return $ QueryNodes names fields locking
219 cdd495ae Iustin Pop
    ReqQueryGroups -> do
220 cdd495ae Iustin Pop
              (names, fields, locking) <- fromJVal args
221 cdd495ae Iustin Pop
              return $ QueryGroups names fields locking
222 5b11f8db Iustin Pop
    ReqQueryClusterInfo ->
223 cdd495ae Iustin Pop
              return QueryClusterInfo
224 795d035d Klaus Aehlig
    ReqQueryNetworks -> do
225 795d035d Klaus Aehlig
              (names, fields, locking) <- fromJVal args
226 795d035d Klaus Aehlig
              return $ QueryNetworks names fields locking
227 cdd495ae Iustin Pop
    ReqQuery -> do
228 9a94c848 Iustin Pop
              (what, fields, qfilter) <- fromJVal args
229 9a94c848 Iustin Pop
              return $ Query what fields qfilter
230 72295708 Iustin Pop
    ReqQueryFields -> do
231 72295708 Iustin Pop
              (what, fields) <- fromJVal args
232 72295708 Iustin Pop
              fields' <- case fields of
233 72295708 Iustin Pop
                           JSNull -> return []
234 72295708 Iustin Pop
                           _ -> fromJVal fields
235 72295708 Iustin Pop
              return $ QueryFields what fields'
236 cdd495ae Iustin Pop
    ReqSubmitJob -> do
237 cdd495ae Iustin Pop
              [ops1] <- fromJVal args
238 cdd495ae Iustin Pop
              ops2 <- mapM (fromJResult (luxiReqToRaw call) . J.readJSON) ops1
239 cdd495ae Iustin Pop
              return $ SubmitJob ops2
240 346c3037 Klaus Aehlig
    ReqSubmitJobToDrainedQueue -> do
241 346c3037 Klaus Aehlig
              [ops1] <- fromJVal args
242 346c3037 Klaus Aehlig
              ops2 <- mapM (fromJResult (luxiReqToRaw call) . J.readJSON) ops1
243 346c3037 Klaus Aehlig
              return $ SubmitJobToDrainedQueue ops2
244 cdd495ae Iustin Pop
    ReqSubmitManyJobs -> do
245 cdd495ae Iustin Pop
              [ops1] <- fromJVal args
246 cdd495ae Iustin Pop
              ops2 <- mapM (fromJResult (luxiReqToRaw call) . J.readJSON) ops1
247 cdd495ae Iustin Pop
              return $ SubmitManyJobs ops2
248 cdd495ae Iustin Pop
    ReqWaitForJobChange -> do
249 cdd495ae Iustin Pop
              (jid, fields, pinfo, pidx, wtmout) <-
250 cdd495ae Iustin Pop
                -- No instance for 5-tuple, code copied from the
251 cdd495ae Iustin Pop
                -- json sources and adapted
252 cdd495ae Iustin Pop
                fromJResult "Parsing WaitForJobChange message" $
253 cdd495ae Iustin Pop
                case args of
254 cdd495ae Iustin Pop
                  JSArray [a, b, c, d, e] ->
255 cdd495ae Iustin Pop
                    (,,,,) `fmap`
256 cdd495ae Iustin Pop
                    J.readJSON a `ap`
257 cdd495ae Iustin Pop
                    J.readJSON b `ap`
258 cdd495ae Iustin Pop
                    J.readJSON c `ap`
259 cdd495ae Iustin Pop
                    J.readJSON d `ap`
260 cdd495ae Iustin Pop
                    J.readJSON e
261 cdd495ae Iustin Pop
                  _ -> J.Error "Not enough values"
262 c48711d5 Iustin Pop
              return $ WaitForJobChange jid fields pinfo pidx wtmout
263 d9d1e541 Klaus Aehlig
    ReqPickupJob -> do
264 d9d1e541 Klaus Aehlig
              [jid] <- fromJVal args
265 d9d1e541 Klaus Aehlig
              return $ PickupJob jid
266 cdd495ae Iustin Pop
    ReqArchiveJob -> do
267 cdd495ae Iustin Pop
              [jid] <- fromJVal args
268 c48711d5 Iustin Pop
              return $ ArchiveJob jid
269 cdd495ae Iustin Pop
    ReqAutoArchiveJobs -> do
270 cdd495ae Iustin Pop
              (age, tmout) <- fromJVal args
271 cdd495ae Iustin Pop
              return $ AutoArchiveJobs age tmout
272 cdd495ae Iustin Pop
    ReqQueryExports -> do
273 cdd495ae Iustin Pop
              (nodes, lock) <- fromJVal args
274 cdd495ae Iustin Pop
              return $ QueryExports nodes lock
275 cdd495ae Iustin Pop
    ReqQueryConfigValues -> do
276 cdd495ae Iustin Pop
              [fields] <- fromJVal args
277 cdd495ae Iustin Pop
              return $ QueryConfigValues fields
278 cdd495ae Iustin Pop
    ReqQueryTags -> do
279 cdd495ae Iustin Pop
              (kind, name) <- fromJVal args
280 34af39e8 Jose A. Lopes
              return $ QueryTags kind name
281 cdd495ae Iustin Pop
    ReqCancelJob -> do
282 c48711d5 Iustin Pop
              [jid] <- fromJVal args
283 c48711d5 Iustin Pop
              return $ CancelJob jid
284 f63ffb37 Michael Hanselmann
    ReqChangeJobPriority -> do
285 c48711d5 Iustin Pop
              (jid, priority) <- fromJVal args
286 c48711d5 Iustin Pop
              return $ ChangeJobPriority jid priority
287 cdd495ae Iustin Pop
    ReqSetDrainFlag -> do
288 cdd495ae Iustin Pop
              [flag] <- fromJVal args
289 cdd495ae Iustin Pop
              return $ SetDrainFlag flag
290 cdd495ae Iustin Pop
    ReqSetWatcherPause -> do
291 906df9f1 Klaus Aehlig
              let duration = case args of
292 906df9f1 Klaus Aehlig
                               JSArray [JSRational _ x] 
293 906df9f1 Klaus Aehlig
                                 -> Just (fromRational x :: Double)
294 906df9f1 Klaus Aehlig
                               _ -> Nothing
295 cdd495ae Iustin Pop
              return $ SetWatcherPause duration
296 cdd495ae Iustin Pop
297 6583e677 Iustin Pop
-- | Check that luxi responses contain the required keys and that the
298 6583e677 Iustin Pop
-- call was successful.
299 7adb7dff Iustin Pop
validateResult :: String -> ErrorResult JSValue
300 6583e677 Iustin Pop
validateResult s = do
301 e821050d Iustin Pop
  when (UTF8.replacement_char `elem` s) $
302 e821050d Iustin Pop
       fail "Failed to decode UTF-8, detected replacement char after decoding"
303 7adb7dff Iustin Pop
  oarr <- fromJResult "Parsing LUXI response" (decodeStrict s)
304 262f3e6c Iustin Pop
  let arr = J.fromJSObject oarr
305 7adb7dff Iustin Pop
  status <- fromObj arr (strOfKey Success)
306 7adb7dff Iustin Pop
  result <- fromObj arr (strOfKey Result)
307 3603605a Iustin Pop
  if status
308 7adb7dff Iustin Pop
    then return result
309 7adb7dff Iustin Pop
    else decodeError result
310 7adb7dff Iustin Pop
311 7adb7dff Iustin Pop
-- | Try to decode an error from the server response. This function
312 7adb7dff Iustin Pop
-- will always fail, since it's called only on the error path (when
313 7adb7dff Iustin Pop
-- status is False).
314 7adb7dff Iustin Pop
decodeError :: JSValue -> ErrorResult JSValue
315 7adb7dff Iustin Pop
decodeError val =
316 7adb7dff Iustin Pop
  case fromJVal val of
317 7adb7dff Iustin Pop
    Ok e -> Bad e
318 7adb7dff Iustin Pop
    Bad msg -> Bad $ GenericError msg
319 6583e677 Iustin Pop
320 6583e677 Iustin Pop
-- | Generic luxi method call.
321 7adb7dff Iustin Pop
callMethod :: LuxiOp -> Client -> IO (ErrorResult JSValue)
322 683b1ca7 Iustin Pop
callMethod method s = do
323 683b1ca7 Iustin Pop
  sendMsg s $ buildCall method
324 6583e677 Iustin Pop
  result <- recvMsg s
325 6583e677 Iustin Pop
  let rval = validateResult result
326 6583e677 Iustin Pop
  return rval
327 9a2ff880 Iustin Pop
328 619e89c8 Iustin Pop
-- | Parse job submission result.
329 7adb7dff Iustin Pop
parseSubmitJobResult :: JSValue -> ErrorResult JobId
330 7adb7dff Iustin Pop
parseSubmitJobResult (JSArray [JSBool True, v]) =
331 c48711d5 Iustin Pop
  case J.readJSON v of
332 c48711d5 Iustin Pop
    J.Error msg -> Bad $ LuxiError msg
333 c48711d5 Iustin Pop
    J.Ok v' -> Ok v'
334 619e89c8 Iustin Pop
parseSubmitJobResult (JSArray [JSBool False, JSString x]) =
335 7adb7dff Iustin Pop
  Bad . LuxiError $ fromJSString x
336 7adb7dff Iustin Pop
parseSubmitJobResult v =
337 7adb7dff Iustin Pop
  Bad . LuxiError $ "Unknown result from the master daemon: " ++
338 7adb7dff Iustin Pop
      show (pp_value v)
339 619e89c8 Iustin Pop
340 9a2ff880 Iustin Pop
-- | Specialized submitManyJobs call.
341 7e723913 Iustin Pop
submitManyJobs :: Client -> [[MetaOpCode]] -> IO (ErrorResult [JobId])
342 9a2ff880 Iustin Pop
submitManyJobs s jobs = do
343 683b1ca7 Iustin Pop
  rval <- callMethod (SubmitManyJobs jobs) s
344 9a2ff880 Iustin Pop
  -- map each result (status, payload) pair into a nice Result ADT
345 9a2ff880 Iustin Pop
  return $ case rval of
346 9a2ff880 Iustin Pop
             Bad x -> Bad x
347 619e89c8 Iustin Pop
             Ok (JSArray r) -> mapM parseSubmitJobResult r
348 7adb7dff Iustin Pop
             x -> Bad . LuxiError $
349 7adb7dff Iustin Pop
                  "Cannot parse response from Ganeti: " ++ show x
350 9a2ff880 Iustin Pop
351 9a2ff880 Iustin Pop
-- | Custom queryJobs call.
352 7adb7dff Iustin Pop
queryJobsStatus :: Client -> [JobId] -> IO (ErrorResult [JobStatus])
353 9a2ff880 Iustin Pop
queryJobsStatus s jids = do
354 76b62028 Iustin Pop
  rval <- callMethod (QueryJobs jids ["status"]) s
355 9a2ff880 Iustin Pop
  return $ case rval of
356 9a2ff880 Iustin Pop
             Bad x -> Bad x
357 9a2ff880 Iustin Pop
             Ok y -> case J.readJSON y::(J.Result [[JobStatus]]) of
358 9a2ff880 Iustin Pop
                       J.Ok vals -> if any null vals
359 7adb7dff Iustin Pop
                                    then Bad $
360 7adb7dff Iustin Pop
                                         LuxiError "Missing job status field"
361 9a2ff880 Iustin Pop
                                    else Ok (map head vals)
362 7adb7dff Iustin Pop
                       J.Error x -> Bad $ LuxiError x