Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Luxi.hs @ 05ac718f

History | View | Annotate | Download (14.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 fae980e5 Iustin Pop
Copyright (C) 2009, 2010, 2011, 2012 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 ccc817a2 Iustin Pop
  , JobId
33 0aff2293 Iustin Pop
  , RecvResult(..)
34 be747966 Iustin Pop
  , TagObject(..)
35 0aff2293 Iustin Pop
  , strOfOp
36 ebf38064 Iustin Pop
  , getClient
37 13f2321c Iustin Pop
  , getServer
38 13f2321c Iustin Pop
  , acceptClient
39 ebf38064 Iustin Pop
  , closeClient
40 0aff2293 Iustin Pop
  , closeServer
41 ebf38064 Iustin Pop
  , callMethod
42 ebf38064 Iustin Pop
  , submitManyJobs
43 ebf38064 Iustin Pop
  , queryJobsStatus
44 cdd495ae Iustin Pop
  , buildCall
45 0aff2293 Iustin Pop
  , buildResponse
46 cdd495ae Iustin Pop
  , validateCall
47 cdd495ae Iustin Pop
  , decodeCall
48 13f2321c Iustin Pop
  , recvMsg
49 0aff2293 Iustin Pop
  , recvMsgExt
50 13f2321c Iustin Pop
  , sendMsg
51 ebf38064 Iustin Pop
  ) where
52 6583e677 Iustin Pop
53 0aff2293 Iustin Pop
import Control.Exception (catch)
54 6583e677 Iustin Pop
import Data.IORef
55 76b62028 Iustin Pop
import Data.Ratio (numerator, denominator)
56 e821050d Iustin Pop
import qualified Data.ByteString as B
57 e821050d Iustin Pop
import qualified Data.ByteString.UTF8 as UTF8
58 e821050d Iustin Pop
import Data.Word (Word8)
59 6583e677 Iustin Pop
import Control.Monad
60 0aff2293 Iustin Pop
import Prelude hiding (catch)
61 0903280b Iustin Pop
import Text.JSON (encodeStrict, decodeStrict)
62 6583e677 Iustin Pop
import qualified Text.JSON as J
63 6583e677 Iustin Pop
import Text.JSON.Types
64 0aff2293 Iustin Pop
import System.Directory (removeFile)
65 e821050d Iustin Pop
import System.IO (hClose, hFlush, hWaitForInput, Handle, IOMode(..))
66 0aff2293 Iustin Pop
import System.IO.Error (isEOFError)
67 6583e677 Iustin Pop
import System.Timeout
68 6583e677 Iustin Pop
import qualified Network.Socket as S
69 6583e677 Iustin Pop
70 b69be409 Iustin Pop
import Ganeti.HTools.JSON
71 6583e677 Iustin Pop
import Ganeti.HTools.Types
72 cdd495ae Iustin Pop
import Ganeti.HTools.Utils
73 6583e677 Iustin Pop
74 92678b3c Iustin Pop
import Ganeti.Constants
75 9a2ff880 Iustin Pop
import Ganeti.Jobs (JobStatus)
76 683b1ca7 Iustin Pop
import Ganeti.OpCodes (OpCode)
77 dc6a0f82 Iustin Pop
import qualified Ganeti.Qlang as Qlang
78 a0090487 Agata Murawska
import Ganeti.THH
79 9a2ff880 Iustin Pop
80 6583e677 Iustin Pop
-- * Utility functions
81 6583e677 Iustin Pop
82 6583e677 Iustin Pop
-- | Wrapper over System.Timeout.timeout that fails in the IO monad.
83 6583e677 Iustin Pop
withTimeout :: Int -> String -> IO a -> IO a
84 6583e677 Iustin Pop
withTimeout secs descr action = do
85 ebf38064 Iustin Pop
  result <- timeout (secs * 1000000) action
86 3603605a Iustin Pop
  case result of
87 3603605a Iustin Pop
    Nothing -> fail $ "Timeout in " ++ descr
88 3603605a Iustin Pop
    Just v -> return v
89 6583e677 Iustin Pop
90 6583e677 Iustin Pop
-- * Generic protocol functionality
91 6583e677 Iustin Pop
92 0aff2293 Iustin Pop
-- | Result of receiving a message from the socket.
93 0aff2293 Iustin Pop
data RecvResult = RecvConnClosed    -- ^ Connection closed
94 0aff2293 Iustin Pop
                | RecvError String  -- ^ Any other error
95 0aff2293 Iustin Pop
                | RecvOk String     -- ^ Successfull receive
96 0aff2293 Iustin Pop
                  deriving (Show, Read, Eq)
97 0aff2293 Iustin Pop
98 ccc817a2 Iustin Pop
-- | The Ganeti job type.
99 76b62028 Iustin Pop
type JobId = Int
100 ccc817a2 Iustin Pop
101 be747966 Iustin Pop
-- | Data type representing what items do the tag operations apply to.
102 be747966 Iustin Pop
$(declareSADT "TagObject"
103 be747966 Iustin Pop
  [ ("TagInstance", 'tagInstance)
104 be747966 Iustin Pop
  , ("TagNode",     'tagNode)
105 be747966 Iustin Pop
  , ("TagGroup",    'tagNodegroup)
106 be747966 Iustin Pop
  , ("TagCluster",  'tagCluster)
107 be747966 Iustin Pop
  ])
108 be747966 Iustin Pop
$(makeJSONInstance ''TagObject)
109 be747966 Iustin Pop
110 a0090487 Agata Murawska
-- | Currently supported Luxi operations and JSON serialization.
111 a0090487 Agata Murawska
$(genLuxiOp "LuxiOp"
112 72295708 Iustin Pop
  [ (luxiReqQuery,
113 8a9ee1e9 Iustin Pop
    [ ("what",    [t| Qlang.ItemType |])
114 4b71f30c Iustin Pop
    , ("fields",  [t| [String]  |])
115 05ac718f Iustin Pop
    , ("qfilter", [t| Qlang.Filter Qlang.FilterField |])
116 ebf38064 Iustin Pop
    ])
117 72295708 Iustin Pop
  , (luxiReqQueryFields,
118 72295708 Iustin Pop
    [ ("what",    [t| Qlang.ItemType |])
119 72295708 Iustin Pop
    , ("fields",  [t| [String]  |])
120 72295708 Iustin Pop
    ])
121 fae980e5 Iustin Pop
  , (luxiReqQueryNodes,
122 4b71f30c Iustin Pop
     [ ("names",  [t| [String] |])
123 4b71f30c Iustin Pop
     , ("fields", [t| [String] |])
124 4b71f30c Iustin Pop
     , ("lock",   [t| Bool     |])
125 ebf38064 Iustin Pop
     ])
126 fae980e5 Iustin Pop
  , (luxiReqQueryGroups,
127 4b71f30c Iustin Pop
     [ ("names",  [t| [String] |])
128 4b71f30c Iustin Pop
     , ("fields", [t| [String] |])
129 4b71f30c Iustin Pop
     , ("lock",   [t| Bool     |])
130 ebf38064 Iustin Pop
     ])
131 fae980e5 Iustin Pop
  , (luxiReqQueryInstances,
132 4b71f30c Iustin Pop
     [ ("names",  [t| [String] |])
133 4b71f30c Iustin Pop
     , ("fields", [t| [String] |])
134 4b71f30c Iustin Pop
     , ("lock",   [t| Bool     |])
135 ebf38064 Iustin Pop
     ])
136 fae980e5 Iustin Pop
  , (luxiReqQueryJobs,
137 4b71f30c Iustin Pop
     [ ("ids",    [t| [Int]    |])
138 4b71f30c Iustin Pop
     , ("fields", [t| [String] |])
139 ebf38064 Iustin Pop
     ])
140 fae980e5 Iustin Pop
  , (luxiReqQueryExports,
141 4b71f30c Iustin Pop
     [ ("nodes", [t| [String] |])
142 4b71f30c Iustin Pop
     , ("lock",  [t| Bool     |])
143 ebf38064 Iustin Pop
     ])
144 fae980e5 Iustin Pop
  , (luxiReqQueryConfigValues,
145 4b71f30c Iustin Pop
     [ ("fields", [t| [String] |]) ]
146 ebf38064 Iustin Pop
    )
147 fae980e5 Iustin Pop
  , (luxiReqQueryClusterInfo, [])
148 fae980e5 Iustin Pop
  , (luxiReqQueryTags,
149 be747966 Iustin Pop
     [ ("kind", [t| TagObject |])
150 4b71f30c Iustin Pop
     , ("name", [t| String |])
151 ebf38064 Iustin Pop
     ])
152 fae980e5 Iustin Pop
  , (luxiReqSubmitJob,
153 4b71f30c Iustin Pop
     [ ("job", [t| [OpCode] |]) ]
154 ebf38064 Iustin Pop
    )
155 fae980e5 Iustin Pop
  , (luxiReqSubmitManyJobs,
156 4b71f30c Iustin Pop
     [ ("ops", [t| [[OpCode]] |]) ]
157 ebf38064 Iustin Pop
    )
158 fae980e5 Iustin Pop
  , (luxiReqWaitForJobChange,
159 4b71f30c Iustin Pop
     [ ("job",      [t| Int     |])
160 4b71f30c Iustin Pop
     , ("fields",   [t| [String]|])
161 4b71f30c Iustin Pop
     , ("prev_job", [t| JSValue |])
162 4b71f30c Iustin Pop
     , ("prev_log", [t| JSValue |])
163 4b71f30c Iustin Pop
     , ("tmout",    [t| Int     |])
164 ebf38064 Iustin Pop
     ])
165 fae980e5 Iustin Pop
  , (luxiReqArchiveJob,
166 4b71f30c Iustin Pop
     [ ("job", [t| Int |]) ]
167 ebf38064 Iustin Pop
    )
168 fae980e5 Iustin Pop
  , (luxiReqAutoArchiveJobs,
169 4b71f30c Iustin Pop
     [ ("age",   [t| Int |])
170 4b71f30c Iustin Pop
     , ("tmout", [t| Int |])
171 ebf38064 Iustin Pop
     ])
172 fae980e5 Iustin Pop
  , (luxiReqCancelJob,
173 4b71f30c Iustin Pop
     [ ("job", [t| Int |]) ]
174 ebf38064 Iustin Pop
    )
175 fae980e5 Iustin Pop
  , (luxiReqSetDrainFlag,
176 4b71f30c Iustin Pop
     [ ("flag", [t| Bool |]) ]
177 ebf38064 Iustin Pop
    )
178 fae980e5 Iustin Pop
  , (luxiReqSetWatcherPause,
179 4b71f30c Iustin Pop
     [ ("duration", [t| Double |]) ]
180 ebf38064 Iustin Pop
    )
181 a0090487 Agata Murawska
  ])
182 6583e677 Iustin Pop
183 95d0d502 Iustin Pop
$(makeJSONInstance ''LuxiReq)
184 95d0d502 Iustin Pop
185 6583e677 Iustin Pop
-- | The serialisation of LuxiOps into strings in messages.
186 a0090487 Agata Murawska
$(genStrOfOp ''LuxiOp "strOfOp")
187 6583e677 Iustin Pop
188 cdd495ae Iustin Pop
-- | Type holding the initial (unparsed) Luxi call.
189 cdd495ae Iustin Pop
data LuxiCall = LuxiCall LuxiReq JSValue
190 cdd495ae Iustin Pop
191 6583e677 Iustin Pop
-- | The end-of-message separator.
192 e821050d Iustin Pop
eOM :: Word8
193 e821050d Iustin Pop
eOM = 3
194 e821050d Iustin Pop
195 e821050d Iustin Pop
-- | The end-of-message encoded as a ByteString.
196 e821050d Iustin Pop
bEOM :: B.ByteString
197 e821050d Iustin Pop
bEOM = B.singleton eOM
198 6583e677 Iustin Pop
199 6583e677 Iustin Pop
-- | Valid keys in the requests and responses.
200 6583e677 Iustin Pop
data MsgKeys = Method
201 6583e677 Iustin Pop
             | Args
202 6583e677 Iustin Pop
             | Success
203 6583e677 Iustin Pop
             | Result
204 6583e677 Iustin Pop
205 6583e677 Iustin Pop
-- | The serialisation of MsgKeys into strings in messages.
206 a0090487 Agata Murawska
$(genStrOfKey ''MsgKeys "strOfKey")
207 6583e677 Iustin Pop
208 6583e677 Iustin Pop
-- | Luxi client encapsulation.
209 e821050d Iustin Pop
data Client = Client { socket :: Handle           -- ^ The socket of the client
210 e821050d Iustin Pop
                     , rbuf :: IORef B.ByteString -- ^ Already received buffer
211 6583e677 Iustin Pop
                     }
212 6583e677 Iustin Pop
213 6583e677 Iustin Pop
-- | Connects to the master daemon and returns a luxi Client.
214 6583e677 Iustin Pop
getClient :: String -> IO Client
215 6583e677 Iustin Pop
getClient path = do
216 ebf38064 Iustin Pop
  s <- S.socket S.AF_UNIX S.Stream S.defaultProtocol
217 ebf38064 Iustin Pop
  withTimeout connTimeout "creating luxi connection" $
218 ebf38064 Iustin Pop
              S.connect s (S.SockAddrUnix path)
219 e821050d Iustin Pop
  rf <- newIORef B.empty
220 e821050d Iustin Pop
  h <- S.socketToHandle s ReadWriteMode
221 e821050d Iustin Pop
  return Client { socket=h, rbuf=rf }
222 6583e677 Iustin Pop
223 13f2321c Iustin Pop
-- | Creates and returns a server endpoint.
224 13f2321c Iustin Pop
getServer :: FilePath -> IO S.Socket
225 13f2321c Iustin Pop
getServer path = do
226 13f2321c Iustin Pop
  s <- S.socket S.AF_UNIX S.Stream S.defaultProtocol
227 13f2321c Iustin Pop
  S.bindSocket s (S.SockAddrUnix path)
228 13f2321c Iustin Pop
  S.listen s 5 -- 5 is the max backlog
229 13f2321c Iustin Pop
  return s
230 13f2321c Iustin Pop
231 0aff2293 Iustin Pop
-- | Closes a server endpoint.
232 0aff2293 Iustin Pop
-- FIXME: this should be encapsulated into a nicer type.
233 0aff2293 Iustin Pop
closeServer :: FilePath -> S.Socket -> IO ()
234 0aff2293 Iustin Pop
closeServer path sock = do
235 0aff2293 Iustin Pop
  S.sClose sock
236 0aff2293 Iustin Pop
  removeFile path
237 0aff2293 Iustin Pop
238 13f2321c Iustin Pop
-- | Accepts a client
239 13f2321c Iustin Pop
acceptClient :: S.Socket -> IO Client
240 13f2321c Iustin Pop
acceptClient s = do
241 13f2321c Iustin Pop
  -- second return is the address of the client, which we ignore here
242 13f2321c Iustin Pop
  (client_socket, _) <- S.accept s
243 13f2321c Iustin Pop
  new_buffer <- newIORef B.empty
244 13f2321c Iustin Pop
  handle <- S.socketToHandle client_socket ReadWriteMode
245 13f2321c Iustin Pop
  return Client { socket=handle, rbuf=new_buffer }
246 13f2321c Iustin Pop
247 6583e677 Iustin Pop
-- | Closes the client socket.
248 6583e677 Iustin Pop
closeClient :: Client -> IO ()
249 e821050d Iustin Pop
closeClient = hClose . socket
250 6583e677 Iustin Pop
251 6583e677 Iustin Pop
-- | Sends a message over a luxi transport.
252 6583e677 Iustin Pop
sendMsg :: Client -> String -> IO ()
253 e821050d Iustin Pop
sendMsg s buf = withTimeout queryTimeout "sending luxi message" $ do
254 e821050d Iustin Pop
  let encoded = UTF8.fromString buf
255 e821050d Iustin Pop
      handle = socket s
256 e821050d Iustin Pop
  B.hPut handle encoded
257 e821050d Iustin Pop
  B.hPut handle bEOM
258 e821050d Iustin Pop
  hFlush handle
259 e821050d Iustin Pop
260 e821050d Iustin Pop
-- | Given a current buffer and the handle, it will read from the
261 e821050d Iustin Pop
-- network until we get a full message, and it will return that
262 e821050d Iustin Pop
-- message and the leftover buffer contents.
263 e821050d Iustin Pop
recvUpdate :: Handle -> B.ByteString -> IO (B.ByteString, B.ByteString)
264 e821050d Iustin Pop
recvUpdate handle obuf = do
265 e821050d Iustin Pop
  nbuf <- withTimeout queryTimeout "reading luxi response" $ do
266 e821050d Iustin Pop
            _ <- hWaitForInput handle (-1)
267 e821050d Iustin Pop
            B.hGetNonBlocking handle 4096
268 e821050d Iustin Pop
  let (msg, remaining) = B.break (eOM ==) nbuf
269 e821050d Iustin Pop
      newbuf = B.append obuf msg
270 e821050d Iustin Pop
  if B.null remaining
271 e821050d Iustin Pop
    then recvUpdate handle newbuf
272 e821050d Iustin Pop
    else return (newbuf, B.tail remaining)
273 6583e677 Iustin Pop
274 6583e677 Iustin Pop
-- | Waits for a message over a luxi transport.
275 6583e677 Iustin Pop
recvMsg :: Client -> IO String
276 6583e677 Iustin Pop
recvMsg s = do
277 6583e677 Iustin Pop
  cbuf <- readIORef $ rbuf s
278 e821050d Iustin Pop
  let (imsg, ibuf) = B.break (eOM ==) cbuf
279 95f490de Iustin Pop
  (msg, nbuf) <-
280 e821050d Iustin Pop
    if B.null ibuf      -- if old buffer didn't contain a full message
281 e821050d Iustin Pop
      then recvUpdate (socket s) cbuf   -- then we read from network
282 e821050d Iustin Pop
      else return (imsg, B.tail ibuf)   -- else we return data from our buffer
283 6583e677 Iustin Pop
  writeIORef (rbuf s) nbuf
284 e821050d Iustin Pop
  return $ UTF8.toString msg
285 6583e677 Iustin Pop
286 0aff2293 Iustin Pop
-- | Extended wrapper over recvMsg.
287 0aff2293 Iustin Pop
recvMsgExt :: Client -> IO RecvResult
288 0aff2293 Iustin Pop
recvMsgExt s =
289 0aff2293 Iustin Pop
  catch (liftM RecvOk (recvMsg s)) $ \e ->
290 0aff2293 Iustin Pop
    if isEOFError e
291 0aff2293 Iustin Pop
      then return RecvConnClosed
292 0aff2293 Iustin Pop
      else return $ RecvError (show e)
293 0aff2293 Iustin Pop
294 6583e677 Iustin Pop
-- | Serialize a request to String.
295 6583e677 Iustin Pop
buildCall :: LuxiOp  -- ^ The method
296 6583e677 Iustin Pop
          -> String  -- ^ The serialized form
297 683b1ca7 Iustin Pop
buildCall lo =
298 2cdaf225 Iustin Pop
  let ja = [ (strOfKey Method, J.showJSON $ strOfOp lo)
299 2cdaf225 Iustin Pop
           , (strOfKey Args, opToArgs lo)
300 ebf38064 Iustin Pop
           ]
301 ebf38064 Iustin Pop
      jo = toJSObject ja
302 ebf38064 Iustin Pop
  in encodeStrict jo
303 6583e677 Iustin Pop
304 0aff2293 Iustin Pop
-- | Serialize the response to String.
305 0aff2293 Iustin Pop
buildResponse :: Bool    -- ^ Success
306 0aff2293 Iustin Pop
              -> JSValue -- ^ The arguments
307 0aff2293 Iustin Pop
              -> String  -- ^ The serialized form
308 0aff2293 Iustin Pop
buildResponse success args =
309 0aff2293 Iustin Pop
  let ja = [ (strOfKey Success, JSBool success)
310 0aff2293 Iustin Pop
           , (strOfKey Result, args)]
311 0aff2293 Iustin Pop
      jo = toJSObject ja
312 0aff2293 Iustin Pop
  in encodeStrict jo
313 0aff2293 Iustin Pop
314 cdd495ae Iustin Pop
-- | Check that luxi request contains the required keys and parse it.
315 cdd495ae Iustin Pop
validateCall :: String -> Result LuxiCall
316 cdd495ae Iustin Pop
validateCall s = do
317 0aff2293 Iustin Pop
  arr <- fromJResult "parsing top-level luxi message" $
318 0aff2293 Iustin Pop
         decodeStrict s::Result (JSObject JSValue)
319 cdd495ae Iustin Pop
  let aobj = fromJSObject arr
320 cdd495ae Iustin Pop
  call <- fromObj aobj (strOfKey Method)::Result LuxiReq
321 cdd495ae Iustin Pop
  args <- fromObj aobj (strOfKey Args)
322 cdd495ae Iustin Pop
  return (LuxiCall call args)
323 cdd495ae Iustin Pop
324 cdd495ae Iustin Pop
-- | Converts Luxi call arguments into a 'LuxiOp' data structure.
325 cdd495ae Iustin Pop
--
326 cdd495ae Iustin Pop
-- This is currently hand-coded until we make it more uniform so that
327 cdd495ae Iustin Pop
-- it can be generated using TH.
328 cdd495ae Iustin Pop
decodeCall :: LuxiCall -> Result LuxiOp
329 cdd495ae Iustin Pop
decodeCall (LuxiCall call args) =
330 cdd495ae Iustin Pop
  case call of
331 cdd495ae Iustin Pop
    ReqQueryJobs -> do
332 cdd495ae Iustin Pop
              (jid, jargs) <- fromJVal args
333 76b62028 Iustin Pop
              rid <- mapM parseJobId jid
334 cdd495ae Iustin Pop
              let rargs = map fromJSString jargs
335 cdd495ae Iustin Pop
              return $ QueryJobs rid rargs
336 cdd495ae Iustin Pop
    ReqQueryInstances -> do
337 cdd495ae Iustin Pop
              (names, fields, locking) <- fromJVal args
338 cdd495ae Iustin Pop
              return $ QueryInstances names fields locking
339 cdd495ae Iustin Pop
    ReqQueryNodes -> do
340 cdd495ae Iustin Pop
              (names, fields, locking) <- fromJVal args
341 cdd495ae Iustin Pop
              return $ QueryNodes names fields locking
342 cdd495ae Iustin Pop
    ReqQueryGroups -> do
343 cdd495ae Iustin Pop
              (names, fields, locking) <- fromJVal args
344 cdd495ae Iustin Pop
              return $ QueryGroups names fields locking
345 cdd495ae Iustin Pop
    ReqQueryClusterInfo -> do
346 cdd495ae Iustin Pop
              return QueryClusterInfo
347 cdd495ae Iustin Pop
    ReqQuery -> do
348 9a94c848 Iustin Pop
              (what, fields, qfilter) <- fromJVal args
349 9a94c848 Iustin Pop
              return $ Query what fields qfilter
350 72295708 Iustin Pop
    ReqQueryFields -> do
351 72295708 Iustin Pop
              (what, fields) <- fromJVal args
352 72295708 Iustin Pop
              fields' <- case fields of
353 72295708 Iustin Pop
                           JSNull -> return []
354 72295708 Iustin Pop
                           _ -> fromJVal fields
355 72295708 Iustin Pop
              return $ QueryFields what fields'
356 cdd495ae Iustin Pop
    ReqSubmitJob -> do
357 cdd495ae Iustin Pop
              [ops1] <- fromJVal args
358 cdd495ae Iustin Pop
              ops2 <- mapM (fromJResult (luxiReqToRaw call) . J.readJSON) ops1
359 cdd495ae Iustin Pop
              return $ SubmitJob ops2
360 cdd495ae Iustin Pop
    ReqSubmitManyJobs -> do
361 cdd495ae Iustin Pop
              [ops1] <- fromJVal args
362 cdd495ae Iustin Pop
              ops2 <- mapM (fromJResult (luxiReqToRaw call) . J.readJSON) ops1
363 cdd495ae Iustin Pop
              return $ SubmitManyJobs ops2
364 cdd495ae Iustin Pop
    ReqWaitForJobChange -> do
365 cdd495ae Iustin Pop
              (jid, fields, pinfo, pidx, wtmout) <-
366 cdd495ae Iustin Pop
                -- No instance for 5-tuple, code copied from the
367 cdd495ae Iustin Pop
                -- json sources and adapted
368 cdd495ae Iustin Pop
                fromJResult "Parsing WaitForJobChange message" $
369 cdd495ae Iustin Pop
                case args of
370 cdd495ae Iustin Pop
                  JSArray [a, b, c, d, e] ->
371 cdd495ae Iustin Pop
                    (,,,,) `fmap`
372 cdd495ae Iustin Pop
                    J.readJSON a `ap`
373 cdd495ae Iustin Pop
                    J.readJSON b `ap`
374 cdd495ae Iustin Pop
                    J.readJSON c `ap`
375 cdd495ae Iustin Pop
                    J.readJSON d `ap`
376 cdd495ae Iustin Pop
                    J.readJSON e
377 cdd495ae Iustin Pop
                  _ -> J.Error "Not enough values"
378 76b62028 Iustin Pop
              rid <- parseJobId jid
379 cdd495ae Iustin Pop
              return $ WaitForJobChange rid fields pinfo pidx wtmout
380 cdd495ae Iustin Pop
    ReqArchiveJob -> do
381 cdd495ae Iustin Pop
              [jid] <- fromJVal args
382 76b62028 Iustin Pop
              rid <- parseJobId jid
383 cdd495ae Iustin Pop
              return $ ArchiveJob rid
384 cdd495ae Iustin Pop
    ReqAutoArchiveJobs -> do
385 cdd495ae Iustin Pop
              (age, tmout) <- fromJVal args
386 cdd495ae Iustin Pop
              return $ AutoArchiveJobs age tmout
387 cdd495ae Iustin Pop
    ReqQueryExports -> do
388 cdd495ae Iustin Pop
              (nodes, lock) <- fromJVal args
389 cdd495ae Iustin Pop
              return $ QueryExports nodes lock
390 cdd495ae Iustin Pop
    ReqQueryConfigValues -> do
391 cdd495ae Iustin Pop
              [fields] <- fromJVal args
392 cdd495ae Iustin Pop
              return $ QueryConfigValues fields
393 cdd495ae Iustin Pop
    ReqQueryTags -> do
394 cdd495ae Iustin Pop
              (kind, name) <- fromJVal args
395 cdd495ae Iustin Pop
              return $ QueryTags kind name
396 cdd495ae Iustin Pop
    ReqCancelJob -> do
397 cdd495ae Iustin Pop
              [job] <- fromJVal args
398 76b62028 Iustin Pop
              rid <- parseJobId job
399 cdd495ae Iustin Pop
              return $ CancelJob rid
400 cdd495ae Iustin Pop
    ReqSetDrainFlag -> do
401 cdd495ae Iustin Pop
              [flag] <- fromJVal args
402 cdd495ae Iustin Pop
              return $ SetDrainFlag flag
403 cdd495ae Iustin Pop
    ReqSetWatcherPause -> do
404 cdd495ae Iustin Pop
              [duration] <- fromJVal args
405 cdd495ae Iustin Pop
              return $ SetWatcherPause duration
406 cdd495ae Iustin Pop
407 6583e677 Iustin Pop
-- | Check that luxi responses contain the required keys and that the
408 6583e677 Iustin Pop
-- call was successful.
409 6583e677 Iustin Pop
validateResult :: String -> Result JSValue
410 6583e677 Iustin Pop
validateResult s = do
411 e821050d Iustin Pop
  when (UTF8.replacement_char `elem` s) $
412 e821050d Iustin Pop
       fail "Failed to decode UTF-8, detected replacement char after decoding"
413 c96d44df Iustin Pop
  oarr <- fromJResult "Parsing LUXI response"
414 c96d44df Iustin Pop
          (decodeStrict s)::Result (JSObject JSValue)
415 262f3e6c Iustin Pop
  let arr = J.fromJSObject oarr
416 e8230242 Iustin Pop
  status <- fromObj arr (strOfKey Success)::Result Bool
417 6583e677 Iustin Pop
  let rkey = strOfKey Result
418 3603605a Iustin Pop
  if status
419 3603605a Iustin Pop
    then fromObj arr rkey
420 3603605a Iustin Pop
    else fromObj arr rkey >>= fail
421 6583e677 Iustin Pop
422 6583e677 Iustin Pop
-- | Generic luxi method call.
423 683b1ca7 Iustin Pop
callMethod :: LuxiOp -> Client -> IO (Result JSValue)
424 683b1ca7 Iustin Pop
callMethod method s = do
425 683b1ca7 Iustin Pop
  sendMsg s $ buildCall method
426 6583e677 Iustin Pop
  result <- recvMsg s
427 6583e677 Iustin Pop
  let rval = validateResult result
428 6583e677 Iustin Pop
  return rval
429 9a2ff880 Iustin Pop
430 619e89c8 Iustin Pop
-- | Parses a job ID.
431 619e89c8 Iustin Pop
parseJobId :: JSValue -> Result JobId
432 76b62028 Iustin Pop
parseJobId (JSString x) = tryRead "parsing job id" . fromJSString $ x
433 76b62028 Iustin Pop
parseJobId (JSRational _ x) =
434 76b62028 Iustin Pop
  if denominator x /= 1
435 76b62028 Iustin Pop
    then Bad $ "Got fractional job ID from master daemon?! Value:" ++ show x
436 76b62028 Iustin Pop
    -- FIXME: potential integer overflow here on 32-bit platforms
437 76b62028 Iustin Pop
    else Ok . fromIntegral . numerator $ x
438 619e89c8 Iustin Pop
parseJobId x = Bad $ "Wrong type/value for job id: " ++ show x
439 619e89c8 Iustin Pop
440 619e89c8 Iustin Pop
-- | Parse job submission result.
441 619e89c8 Iustin Pop
parseSubmitJobResult :: JSValue -> Result JobId
442 619e89c8 Iustin Pop
parseSubmitJobResult (JSArray [JSBool True, v]) = parseJobId v
443 619e89c8 Iustin Pop
parseSubmitJobResult (JSArray [JSBool False, JSString x]) =
444 619e89c8 Iustin Pop
  Bad (fromJSString x)
445 619e89c8 Iustin Pop
parseSubmitJobResult v = Bad $ "Unknown result from the master daemon" ++
446 619e89c8 Iustin Pop
                         show v
447 619e89c8 Iustin Pop
448 9a2ff880 Iustin Pop
-- | Specialized submitManyJobs call.
449 ccc817a2 Iustin Pop
submitManyJobs :: Client -> [[OpCode]] -> IO (Result [JobId])
450 9a2ff880 Iustin Pop
submitManyJobs s jobs = do
451 683b1ca7 Iustin Pop
  rval <- callMethod (SubmitManyJobs jobs) s
452 9a2ff880 Iustin Pop
  -- map each result (status, payload) pair into a nice Result ADT
453 9a2ff880 Iustin Pop
  return $ case rval of
454 9a2ff880 Iustin Pop
             Bad x -> Bad x
455 619e89c8 Iustin Pop
             Ok (JSArray r) -> mapM parseSubmitJobResult r
456 9a2ff880 Iustin Pop
             x -> Bad ("Cannot parse response from Ganeti: " ++ show x)
457 9a2ff880 Iustin Pop
458 9a2ff880 Iustin Pop
-- | Custom queryJobs call.
459 ccc817a2 Iustin Pop
queryJobsStatus :: Client -> [JobId] -> IO (Result [JobStatus])
460 9a2ff880 Iustin Pop
queryJobsStatus s jids = do
461 76b62028 Iustin Pop
  rval <- callMethod (QueryJobs jids ["status"]) s
462 9a2ff880 Iustin Pop
  return $ case rval of
463 9a2ff880 Iustin Pop
             Bad x -> Bad x
464 9a2ff880 Iustin Pop
             Ok y -> case J.readJSON y::(J.Result [[JobStatus]]) of
465 9a2ff880 Iustin Pop
                       J.Ok vals -> if any null vals
466 9a2ff880 Iustin Pop
                                    then Bad "Missing job status field"
467 9a2ff880 Iustin Pop
                                    else Ok (map head vals)
468 9a2ff880 Iustin Pop
                       J.Error x -> Bad x