Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Luxi.hs @ b9202225

History | View | Annotate | Download (11.8 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 ed7f7fd9 Petr Pudlak
import Control.Applicative (optional)
56 6583e677 Iustin Pop
import Control.Monad
57 71a4c605 Petr Pudlak
import qualified Data.ByteString.UTF8 as UTF8
58 0903280b Iustin Pop
import Text.JSON (encodeStrict, decodeStrict)
59 6583e677 Iustin Pop
import qualified Text.JSON as J
60 7adb7dff Iustin Pop
import Text.JSON.Pretty (pp_value)
61 6583e677 Iustin Pop
import Text.JSON.Types
62 6583e677 Iustin Pop
63 4cd79ca8 Iustin Pop
import Ganeti.BasicTypes
64 92678b3c Iustin Pop
import Ganeti.Constants
65 7adb7dff Iustin Pop
import Ganeti.Errors
66 7adb7dff Iustin Pop
import Ganeti.JSON
67 71a4c605 Petr Pudlak
import Ganeti.UDSServer
68 fa10983e Iustin Pop
import Ganeti.OpParams (pTagsObject)
69 367c4241 Dato Simó
import Ganeti.OpCodes
70 4cab6703 Iustin Pop
import qualified Ganeti.Query.Language as Qlang
71 0fbc8447 Petr Pudlak
import Ganeti.Runtime (GanetiDaemon(..))
72 a0090487 Agata Murawska
import Ganeti.THH
73 c48711d5 Iustin Pop
import Ganeti.Types
74 6583e677 Iustin Pop
75 0aff2293 Iustin Pop
76 a0090487 Agata Murawska
-- | Currently supported Luxi operations and JSON serialization.
77 a0090487 Agata Murawska
$(genLuxiOp "LuxiOp"
78 72295708 Iustin Pop
  [ (luxiReqQuery,
79 88609f00 Iustin Pop
    [ simpleField "what"    [t| Qlang.ItemType |]
80 88609f00 Iustin Pop
    , simpleField "fields"  [t| [String]  |]
81 88609f00 Iustin Pop
    , simpleField "qfilter" [t| Qlang.Filter Qlang.FilterField |]
82 ebf38064 Iustin Pop
    ])
83 72295708 Iustin Pop
  , (luxiReqQueryFields,
84 88609f00 Iustin Pop
    [ simpleField "what"    [t| Qlang.ItemType |]
85 88609f00 Iustin Pop
    , simpleField "fields"  [t| [String]  |]
86 72295708 Iustin Pop
    ])
87 fae980e5 Iustin Pop
  , (luxiReqQueryNodes,
88 88609f00 Iustin Pop
     [ simpleField "names"  [t| [String] |]
89 88609f00 Iustin Pop
     , simpleField "fields" [t| [String] |]
90 88609f00 Iustin Pop
     , simpleField "lock"   [t| Bool     |]
91 ebf38064 Iustin Pop
     ])
92 fae980e5 Iustin Pop
  , (luxiReqQueryGroups,
93 88609f00 Iustin Pop
     [ simpleField "names"  [t| [String] |]
94 88609f00 Iustin Pop
     , simpleField "fields" [t| [String] |]
95 88609f00 Iustin Pop
     , simpleField "lock"   [t| Bool     |]
96 ebf38064 Iustin Pop
     ])
97 795d035d Klaus Aehlig
  , (luxiReqQueryNetworks,
98 795d035d Klaus Aehlig
     [ simpleField "names"  [t| [String] |]
99 795d035d Klaus Aehlig
     , simpleField "fields" [t| [String] |]
100 795d035d Klaus Aehlig
     , simpleField "lock"   [t| Bool     |]
101 795d035d Klaus Aehlig
     ])
102 fae980e5 Iustin Pop
  , (luxiReqQueryInstances,
103 88609f00 Iustin Pop
     [ simpleField "names"  [t| [String] |]
104 88609f00 Iustin Pop
     , simpleField "fields" [t| [String] |]
105 88609f00 Iustin Pop
     , simpleField "lock"   [t| Bool     |]
106 ebf38064 Iustin Pop
     ])
107 fae980e5 Iustin Pop
  , (luxiReqQueryJobs,
108 c48711d5 Iustin Pop
     [ simpleField "ids"    [t| [JobId]  |]
109 88609f00 Iustin Pop
     , simpleField "fields" [t| [String] |]
110 ebf38064 Iustin Pop
     ])
111 fae980e5 Iustin Pop
  , (luxiReqQueryExports,
112 88609f00 Iustin Pop
     [ simpleField "nodes" [t| [String] |]
113 88609f00 Iustin Pop
     , simpleField "lock"  [t| Bool     |]
114 ebf38064 Iustin Pop
     ])
115 fae980e5 Iustin Pop
  , (luxiReqQueryConfigValues,
116 88609f00 Iustin Pop
     [ simpleField "fields" [t| [String] |] ]
117 ebf38064 Iustin Pop
    )
118 fae980e5 Iustin Pop
  , (luxiReqQueryClusterInfo, [])
119 fae980e5 Iustin Pop
  , (luxiReqQueryTags,
120 9131274c Jose A. Lopes
     [ pTagsObject
121 34af39e8 Jose A. Lopes
     , simpleField "name" [t| String |]
122 34af39e8 Jose A. Lopes
     ])
123 fae980e5 Iustin Pop
  , (luxiReqSubmitJob,
124 7e723913 Iustin Pop
     [ simpleField "job" [t| [MetaOpCode] |] ]
125 ebf38064 Iustin Pop
    )
126 346c3037 Klaus Aehlig
  , (luxiReqSubmitJobToDrainedQueue,
127 346c3037 Klaus Aehlig
     [ simpleField "job" [t| [MetaOpCode] |] ]
128 346c3037 Klaus Aehlig
    )
129 fae980e5 Iustin Pop
  , (luxiReqSubmitManyJobs,
130 7e723913 Iustin Pop
     [ simpleField "ops" [t| [[MetaOpCode]] |] ]
131 ebf38064 Iustin Pop
    )
132 fae980e5 Iustin Pop
  , (luxiReqWaitForJobChange,
133 c48711d5 Iustin Pop
     [ simpleField "job"      [t| JobId   |]
134 88609f00 Iustin Pop
     , simpleField "fields"   [t| [String]|]
135 88609f00 Iustin Pop
     , simpleField "prev_job" [t| JSValue |]
136 88609f00 Iustin Pop
     , simpleField "prev_log" [t| JSValue |]
137 88609f00 Iustin Pop
     , simpleField "tmout"    [t| Int     |]
138 ebf38064 Iustin Pop
     ])
139 d9d1e541 Klaus Aehlig
  , (luxiReqPickupJob,
140 d9d1e541 Klaus Aehlig
     [ simpleField "job" [t| JobId |] ]
141 d9d1e541 Klaus Aehlig
    )
142 fae980e5 Iustin Pop
  , (luxiReqArchiveJob,
143 c48711d5 Iustin Pop
     [ simpleField "job" [t| JobId |] ]
144 ebf38064 Iustin Pop
    )
145 fae980e5 Iustin Pop
  , (luxiReqAutoArchiveJobs,
146 88609f00 Iustin Pop
     [ simpleField "age"   [t| Int |]
147 88609f00 Iustin Pop
     , simpleField "tmout" [t| Int |]
148 ebf38064 Iustin Pop
     ])
149 fae980e5 Iustin Pop
  , (luxiReqCancelJob,
150 c48711d5 Iustin Pop
     [ simpleField "job" [t| JobId |] ]
151 ebf38064 Iustin Pop
    )
152 f63ffb37 Michael Hanselmann
  , (luxiReqChangeJobPriority,
153 c48711d5 Iustin Pop
     [ simpleField "job"      [t| JobId |]
154 f63ffb37 Michael Hanselmann
     , simpleField "priority" [t| Int |] ]
155 f63ffb37 Michael Hanselmann
    )
156 fae980e5 Iustin Pop
  , (luxiReqSetDrainFlag,
157 88609f00 Iustin Pop
     [ simpleField "flag" [t| Bool |] ]
158 ebf38064 Iustin Pop
    )
159 fae980e5 Iustin Pop
  , (luxiReqSetWatcherPause,
160 d819aba6 Klaus Aehlig
     [ optionalNullSerField
161 ed7f7fd9 Petr Pudlak
         $ timeAsDoubleField "duration" ]
162 ebf38064 Iustin Pop
    )
163 a0090487 Agata Murawska
  ])
164 6583e677 Iustin Pop
165 95d0d502 Iustin Pop
$(makeJSONInstance ''LuxiReq)
166 95d0d502 Iustin Pop
167 471b6c46 Iustin Pop
-- | List of all defined Luxi calls.
168 471b6c46 Iustin Pop
$(genAllConstr (drop 3) ''LuxiReq "allLuxiCalls")
169 471b6c46 Iustin Pop
170 6583e677 Iustin Pop
-- | The serialisation of LuxiOps into strings in messages.
171 a0090487 Agata Murawska
$(genStrOfOp ''LuxiOp "strOfOp")
172 6583e677 Iustin Pop
173 cdd495ae Iustin Pop
174 0fbc8447 Petr Pudlak
luxiConnectConfig :: ConnectConfig
175 0fbc8447 Petr Pudlak
luxiConnectConfig = ConnectConfig { connDaemon = GanetiLuxid
176 0fbc8447 Petr Pudlak
                                  , recvTmo    = luxiDefRwto
177 0fbc8447 Petr Pudlak
                                  , sendTmo    = luxiDefRwto
178 0fbc8447 Petr Pudlak
                                  }
179 0fbc8447 Petr Pudlak
180 0fbc8447 Petr Pudlak
-- | Connects to the master daemon and returns a luxi Client.
181 0fbc8447 Petr Pudlak
getLuxiClient :: String -> IO Client
182 0fbc8447 Petr Pudlak
getLuxiClient = connectClient luxiConnectConfig luxiDefCtmo
183 0fbc8447 Petr Pudlak
184 0fbc8447 Petr Pudlak
-- | Creates and returns a server endpoint.
185 0fbc8447 Petr Pudlak
getLuxiServer :: Bool -> FilePath -> IO Server
186 0fbc8447 Petr Pudlak
getLuxiServer = connectServer luxiConnectConfig
187 0fbc8447 Petr Pudlak
188 6583e677 Iustin Pop
-- | Serialize a request to String.
189 6583e677 Iustin Pop
buildCall :: LuxiOp  -- ^ The method
190 6583e677 Iustin Pop
          -> String  -- ^ The serialized form
191 683b1ca7 Iustin Pop
buildCall lo =
192 2cdaf225 Iustin Pop
  let ja = [ (strOfKey Method, J.showJSON $ strOfOp lo)
193 2cdaf225 Iustin Pop
           , (strOfKey Args, opToArgs lo)
194 ebf38064 Iustin Pop
           ]
195 ebf38064 Iustin Pop
      jo = toJSObject ja
196 ebf38064 Iustin Pop
  in encodeStrict jo
197 6583e677 Iustin Pop
198 cdd495ae Iustin Pop
199 cdd495ae Iustin Pop
-- | Converts Luxi call arguments into a 'LuxiOp' data structure.
200 d79a6502 Petr Pudlak
-- This is used for building a Luxi 'Handler'.
201 cdd495ae Iustin Pop
--
202 cdd495ae Iustin Pop
-- This is currently hand-coded until we make it more uniform so that
203 cdd495ae Iustin Pop
-- it can be generated using TH.
204 d79a6502 Petr Pudlak
decodeLuxiCall :: JSValue -> JSValue -> Result LuxiOp
205 d79a6502 Petr Pudlak
decodeLuxiCall method args = do
206 d79a6502 Petr Pudlak
  call <- fromJResult "Unable to parse LUXI request method" $ J.readJSON method
207 cdd495ae Iustin Pop
  case call of
208 cdd495ae Iustin Pop
    ReqQueryJobs -> do
209 c48711d5 Iustin Pop
              (jids, jargs) <- fromJVal args
210 d2970809 Iustin Pop
              jids' <- case jids of
211 d2970809 Iustin Pop
                         JSNull -> return []
212 d2970809 Iustin Pop
                         _ -> fromJVal jids
213 d2970809 Iustin Pop
              return $ QueryJobs jids' jargs
214 cdd495ae Iustin Pop
    ReqQueryInstances -> do
215 cdd495ae Iustin Pop
              (names, fields, locking) <- fromJVal args
216 cdd495ae Iustin Pop
              return $ QueryInstances names fields locking
217 cdd495ae Iustin Pop
    ReqQueryNodes -> do
218 cdd495ae Iustin Pop
              (names, fields, locking) <- fromJVal args
219 cdd495ae Iustin Pop
              return $ QueryNodes names fields locking
220 cdd495ae Iustin Pop
    ReqQueryGroups -> do
221 cdd495ae Iustin Pop
              (names, fields, locking) <- fromJVal args
222 cdd495ae Iustin Pop
              return $ QueryGroups names fields locking
223 5b11f8db Iustin Pop
    ReqQueryClusterInfo ->
224 cdd495ae Iustin Pop
              return QueryClusterInfo
225 795d035d Klaus Aehlig
    ReqQueryNetworks -> do
226 795d035d Klaus Aehlig
              (names, fields, locking) <- fromJVal args
227 795d035d Klaus Aehlig
              return $ QueryNetworks names fields locking
228 cdd495ae Iustin Pop
    ReqQuery -> do
229 9a94c848 Iustin Pop
              (what, fields, qfilter) <- fromJVal args
230 9a94c848 Iustin Pop
              return $ Query what fields qfilter
231 72295708 Iustin Pop
    ReqQueryFields -> do
232 72295708 Iustin Pop
              (what, fields) <- fromJVal args
233 72295708 Iustin Pop
              fields' <- case fields of
234 72295708 Iustin Pop
                           JSNull -> return []
235 72295708 Iustin Pop
                           _ -> fromJVal fields
236 72295708 Iustin Pop
              return $ QueryFields what fields'
237 cdd495ae Iustin Pop
    ReqSubmitJob -> do
238 cdd495ae Iustin Pop
              [ops1] <- fromJVal args
239 cdd495ae Iustin Pop
              ops2 <- mapM (fromJResult (luxiReqToRaw call) . J.readJSON) ops1
240 cdd495ae Iustin Pop
              return $ SubmitJob ops2
241 346c3037 Klaus Aehlig
    ReqSubmitJobToDrainedQueue -> do
242 346c3037 Klaus Aehlig
              [ops1] <- fromJVal args
243 346c3037 Klaus Aehlig
              ops2 <- mapM (fromJResult (luxiReqToRaw call) . J.readJSON) ops1
244 346c3037 Klaus Aehlig
              return $ SubmitJobToDrainedQueue ops2
245 cdd495ae Iustin Pop
    ReqSubmitManyJobs -> do
246 cdd495ae Iustin Pop
              [ops1] <- fromJVal args
247 cdd495ae Iustin Pop
              ops2 <- mapM (fromJResult (luxiReqToRaw call) . J.readJSON) ops1
248 cdd495ae Iustin Pop
              return $ SubmitManyJobs ops2
249 cdd495ae Iustin Pop
    ReqWaitForJobChange -> do
250 cdd495ae Iustin Pop
              (jid, fields, pinfo, pidx, wtmout) <-
251 cdd495ae Iustin Pop
                -- No instance for 5-tuple, code copied from the
252 cdd495ae Iustin Pop
                -- json sources and adapted
253 cdd495ae Iustin Pop
                fromJResult "Parsing WaitForJobChange message" $
254 cdd495ae Iustin Pop
                case args of
255 cdd495ae Iustin Pop
                  JSArray [a, b, c, d, e] ->
256 cdd495ae Iustin Pop
                    (,,,,) `fmap`
257 cdd495ae Iustin Pop
                    J.readJSON a `ap`
258 cdd495ae Iustin Pop
                    J.readJSON b `ap`
259 cdd495ae Iustin Pop
                    J.readJSON c `ap`
260 cdd495ae Iustin Pop
                    J.readJSON d `ap`
261 cdd495ae Iustin Pop
                    J.readJSON e
262 cdd495ae Iustin Pop
                  _ -> J.Error "Not enough values"
263 c48711d5 Iustin Pop
              return $ WaitForJobChange jid fields pinfo pidx wtmout
264 d9d1e541 Klaus Aehlig
    ReqPickupJob -> do
265 d9d1e541 Klaus Aehlig
              [jid] <- fromJVal args
266 d9d1e541 Klaus Aehlig
              return $ PickupJob jid
267 cdd495ae Iustin Pop
    ReqArchiveJob -> do
268 cdd495ae Iustin Pop
              [jid] <- fromJVal args
269 c48711d5 Iustin Pop
              return $ ArchiveJob jid
270 cdd495ae Iustin Pop
    ReqAutoArchiveJobs -> do
271 cdd495ae Iustin Pop
              (age, tmout) <- fromJVal args
272 cdd495ae Iustin Pop
              return $ AutoArchiveJobs age tmout
273 cdd495ae Iustin Pop
    ReqQueryExports -> do
274 cdd495ae Iustin Pop
              (nodes, lock) <- fromJVal args
275 cdd495ae Iustin Pop
              return $ QueryExports nodes lock
276 cdd495ae Iustin Pop
    ReqQueryConfigValues -> do
277 cdd495ae Iustin Pop
              [fields] <- fromJVal args
278 cdd495ae Iustin Pop
              return $ QueryConfigValues fields
279 cdd495ae Iustin Pop
    ReqQueryTags -> do
280 cdd495ae Iustin Pop
              (kind, name) <- fromJVal args
281 34af39e8 Jose A. Lopes
              return $ QueryTags kind name
282 cdd495ae Iustin Pop
    ReqCancelJob -> do
283 c48711d5 Iustin Pop
              [jid] <- fromJVal args
284 c48711d5 Iustin Pop
              return $ CancelJob jid
285 f63ffb37 Michael Hanselmann
    ReqChangeJobPriority -> do
286 c48711d5 Iustin Pop
              (jid, priority) <- fromJVal args
287 c48711d5 Iustin Pop
              return $ ChangeJobPriority jid priority
288 cdd495ae Iustin Pop
    ReqSetDrainFlag -> do
289 cdd495ae Iustin Pop
              [flag] <- fromJVal args
290 cdd495ae Iustin Pop
              return $ SetDrainFlag flag
291 cdd495ae Iustin Pop
    ReqSetWatcherPause -> do
292 ed7f7fd9 Petr Pudlak
              duration <- optional $ do
293 ed7f7fd9 Petr Pudlak
                [x] <- fromJVal args
294 ed7f7fd9 Petr Pudlak
                liftM unTimeAsDoubleJSON $ fromJVal x
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