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