Complete the instance OpCodes and parameters
[ganeti-local] / htools / Ganeti / Luxi.hs
index ec7d5e2..9b959fc 100644 (file)
@@ -27,26 +27,30 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
 module Ganeti.Luxi
   ( LuxiOp(..)
-  , QrViaLuxi(..)
-  , ResultStatus(..)
   , LuxiReq(..)
   , Client
   , JobId
-  , checkRS
+  , RecvResult(..)
+  , strOfOp
   , getClient
   , getServer
   , acceptClient
   , closeClient
+  , closeServer
   , callMethod
   , submitManyJobs
   , queryJobsStatus
   , buildCall
+  , buildResponse
   , validateCall
   , decodeCall
   , recvMsg
+  , recvMsgExt
   , sendMsg
+  , allLuxiCalls
   ) where
 
+import Control.Exception (catch)
 import Data.IORef
 import Data.Ratio (numerator, denominator)
 import qualified Data.ByteString as B
@@ -55,18 +59,22 @@ import Data.Word (Word8)
 import Control.Monad
 import Text.JSON (encodeStrict, decodeStrict)
 import qualified Text.JSON as J
+import Text.JSON.Pretty (pp_value)
 import Text.JSON.Types
+import System.Directory (removeFile)
 import System.IO (hClose, hFlush, hWaitForInput, Handle, IOMode(..))
+import System.IO.Error (isEOFError)
 import System.Timeout
 import qualified Network.Socket as S
 
-import Ganeti.HTools.JSON
-import Ganeti.HTools.Types
-import Ganeti.HTools.Utils
-
+import Ganeti.BasicTypes
 import Ganeti.Constants
+import Ganeti.Errors
+import Ganeti.JSON
 import Ganeti.Jobs (JobStatus)
-import Ganeti.OpCodes (OpCode)
+import Ganeti.OpCodes
+import Ganeti.Utils
+import qualified Ganeti.Query.Language as Qlang
 import Ganeti.THH
 
 -- * Utility functions
@@ -81,113 +89,103 @@ withTimeout secs descr action = do
 
 -- * Generic protocol functionality
 
+-- | Result of receiving a message from the socket.
+data RecvResult = RecvConnClosed    -- ^ Connection closed
+                | RecvError String  -- ^ Any other error
+                | RecvOk String     -- ^ Successfull receive
+                  deriving (Show, Read, Eq)
+
 -- | The Ganeti job type.
 type JobId = Int
 
-$(declareSADT "QrViaLuxi"
-  [ ("QRLock", 'qrLock)
-  , ("QRInstance", 'qrInstance)
-  , ("QRNode", 'qrNode)
-  , ("QRGroup", 'qrGroup)
-  , ("QROs", 'qrOs)
-  ])
-$(makeJSONInstance ''QrViaLuxi)
-
 -- | Currently supported Luxi operations and JSON serialization.
 $(genLuxiOp "LuxiOp"
-  [(luxiReqQuery,
-    [ ("what",    [t| QrViaLuxi |], [| id |])
-    , ("fields",  [t| [String]  |], [| id |])
-    , ("qfilter", [t| ()        |], [| const JSNull |])
+  [ (luxiReqQuery,
+    [ simpleField "what"    [t| Qlang.ItemType |]
+    , simpleField "fields"  [t| [String]  |]
+    , simpleField "qfilter" [t| Qlang.Filter Qlang.FilterField |]
+    ])
+  , (luxiReqQueryFields,
+    [ simpleField "what"    [t| Qlang.ItemType |]
+    , simpleField "fields"  [t| [String]  |]
     ])
   , (luxiReqQueryNodes,
-     [ ("names",  [t| [String] |], [| id |])
-     , ("fields", [t| [String] |], [| id |])
-     , ("lock",   [t| Bool     |], [| id |])
+     [ simpleField "names"  [t| [String] |]
+     , simpleField "fields" [t| [String] |]
+     , simpleField "lock"   [t| Bool     |]
      ])
   , (luxiReqQueryGroups,
-     [ ("names",  [t| [String] |], [| id |])
-     , ("fields", [t| [String] |], [| id |])
-     , ("lock",   [t| Bool     |], [| id |])
+     [ simpleField "names"  [t| [String] |]
+     , simpleField "fields" [t| [String] |]
+     , simpleField "lock"   [t| Bool     |]
      ])
   , (luxiReqQueryInstances,
-     [ ("names",  [t| [String] |], [| id |])
-     , ("fields", [t| [String] |], [| id |])
-     , ("lock",   [t| Bool     |], [| id |])
+     [ simpleField "names"  [t| [String] |]
+     , simpleField "fields" [t| [String] |]
+     , simpleField "lock"   [t| Bool     |]
      ])
   , (luxiReqQueryJobs,
-     [ ("ids",    [t| [Int]    |], [| id |])
-     , ("fields", [t| [String] |], [| id |])
+     [ simpleField "ids"    [t| [Int]    |]
+     , simpleField "fields" [t| [String] |]
      ])
   , (luxiReqQueryExports,
-     [ ("nodes", [t| [String] |], [| id |])
-     , ("lock",  [t| Bool     |], [| id |])
+     [ simpleField "nodes" [t| [String] |]
+     , simpleField "lock"  [t| Bool     |]
      ])
   , (luxiReqQueryConfigValues,
-     [ ("fields", [t| [String] |], [| id |]) ]
+     [ simpleField "fields" [t| [String] |] ]
     )
   , (luxiReqQueryClusterInfo, [])
   , (luxiReqQueryTags,
-     [ ("kind", [t| String |], [| id |])
-     , ("name", [t| String |], [| id |])
+     [ customField 'decodeTagObject 'encodeTagObject $
+       simpleField "kind" [t| TagObject |]
      ])
   , (luxiReqSubmitJob,
-     [ ("job", [t| [OpCode] |], [| id |]) ]
+     [ simpleField "job" [t| [OpCode] |] ]
     )
   , (luxiReqSubmitManyJobs,
-     [ ("ops", [t| [[OpCode]] |], [| id |]) ]
+     [ simpleField "ops" [t| [[OpCode]] |] ]
     )
   , (luxiReqWaitForJobChange,
-     [ ("job",      [t| Int     |], [| id |])
-     , ("fields",   [t| [String]|], [| id |])
-     , ("prev_job", [t| JSValue |], [| id |])
-     , ("prev_log", [t| JSValue |], [| id |])
-     , ("tmout",    [t| Int     |], [| id |])
+     [ simpleField "job"      [t| Int     |]
+     , simpleField "fields"   [t| [String]|]
+     , simpleField "prev_job" [t| JSValue |]
+     , simpleField "prev_log" [t| JSValue |]
+     , simpleField "tmout"    [t| Int     |]
      ])
   , (luxiReqArchiveJob,
-     [ ("job", [t| Int |], [| id |]) ]
+     [ simpleField "job" [t| Int |] ]
     )
   , (luxiReqAutoArchiveJobs,
-     [ ("age",   [t| Int |], [| id |])
-     , ("tmout", [t| Int |], [| id |])
+     [ simpleField "age"   [t| Int |]
+     , simpleField "tmout" [t| Int |]
      ])
   , (luxiReqCancelJob,
-     [ ("job", [t| Int |], [| id |]) ]
+     [ simpleField "job" [t| Int |] ]
+    )
+  , (luxiReqChangeJobPriority,
+     [ simpleField "job" [t| Int |]
+     , simpleField "priority" [t| Int |] ]
     )
   , (luxiReqSetDrainFlag,
-     [ ("flag", [t| Bool |], [| id |]) ]
+     [ simpleField "flag" [t| Bool |] ]
     )
   , (luxiReqSetWatcherPause,
-     [ ("duration", [t| Double |], [| id |]) ]
+     [ simpleField "duration" [t| Double |] ]
     )
   ])
 
 $(makeJSONInstance ''LuxiReq)
 
+-- | List of all defined Luxi calls.
+$(genAllConstr (drop 3) ''LuxiReq "allLuxiCalls")
+
 -- | The serialisation of LuxiOps into strings in messages.
 $(genStrOfOp ''LuxiOp "strOfOp")
 
-$(declareIADT "ResultStatus"
-  [ ("RSNormal", 'rsNormal)
-  , ("RSUnknown", 'rsUnknown)
-  , ("RSNoData", 'rsNodata)
-  , ("RSUnavailable", 'rsUnavail)
-  , ("RSOffline", 'rsOffline)
-  ])
-
-$(makeJSONInstance ''ResultStatus)
-
 -- | Type holding the initial (unparsed) Luxi call.
 data LuxiCall = LuxiCall LuxiReq JSValue
 
--- | Check that ResultStatus is success or fail with descriptive message.
-checkRS :: (Monad m) => ResultStatus -> a -> m a
-checkRS RSNormal val    = return val
-checkRS RSUnknown _     = fail "Unknown field"
-checkRS RSNoData _      = fail "No data for a field"
-checkRS RSUnavailable _ = fail "Ganeti reports unavailable data"
-checkRS RSOffline _     = fail "Ganeti reports resource as offline"
-
 -- | The end-of-message separator.
 eOM :: Word8
 eOM = 3
@@ -214,7 +212,7 @@ data Client = Client { socket :: Handle           -- ^ The socket of the client
 getClient :: String -> IO Client
 getClient path = do
   s <- S.socket S.AF_UNIX S.Stream S.defaultProtocol
-  withTimeout connTimeout "creating luxi connection" $
+  withTimeout luxiDefCtmo "creating luxi connection" $
               S.connect s (S.SockAddrUnix path)
   rf <- newIORef B.empty
   h <- S.socketToHandle s ReadWriteMode
@@ -228,6 +226,13 @@ getServer path = do
   S.listen s 5 -- 5 is the max backlog
   return s
 
+-- | Closes a server endpoint.
+-- FIXME: this should be encapsulated into a nicer type.
+closeServer :: FilePath -> S.Socket -> IO ()
+closeServer path sock = do
+  S.sClose sock
+  removeFile path
+
 -- | Accepts a client
 acceptClient :: S.Socket -> IO Client
 acceptClient s = do
@@ -243,7 +248,7 @@ closeClient = hClose . socket
 
 -- | Sends a message over a luxi transport.
 sendMsg :: Client -> String -> IO ()
-sendMsg s buf = withTimeout queryTimeout "sending luxi message" $ do
+sendMsg s buf = withTimeout luxiDefRwto "sending luxi message" $ do
   let encoded = UTF8.fromString buf
       handle = socket s
   B.hPut handle encoded
@@ -255,7 +260,7 @@ sendMsg s buf = withTimeout queryTimeout "sending luxi message" $ do
 -- message and the leftover buffer contents.
 recvUpdate :: Handle -> B.ByteString -> IO (B.ByteString, B.ByteString)
 recvUpdate handle obuf = do
-  nbuf <- withTimeout queryTimeout "reading luxi response" $ do
+  nbuf <- withTimeout luxiDefRwto "reading luxi response" $ do
             _ <- hWaitForInput handle (-1)
             B.hGetNonBlocking handle 4096
   let (msg, remaining) = B.break (eOM ==) nbuf
@@ -276,20 +281,39 @@ recvMsg s = do
   writeIORef (rbuf s) nbuf
   return $ UTF8.toString msg
 
+-- | Extended wrapper over recvMsg.
+recvMsgExt :: Client -> IO RecvResult
+recvMsgExt s =
+  Control.Exception.catch (liftM RecvOk (recvMsg s)) $ \e ->
+    return $ if isEOFError e
+               then RecvConnClosed
+               else RecvError (show e)
+
 -- | Serialize a request to String.
 buildCall :: LuxiOp  -- ^ The method
           -> String  -- ^ The serialized form
 buildCall lo =
-  let ja = [ (strOfKey Method, JSString $ toJSString $ strOfOp lo::JSValue)
-           , (strOfKey Args, opToArgs lo::JSValue)
+  let ja = [ (strOfKey Method, J.showJSON $ strOfOp lo)
+           , (strOfKey Args, opToArgs lo)
            ]
       jo = toJSObject ja
   in encodeStrict jo
 
+-- | Serialize the response to String.
+buildResponse :: Bool    -- ^ Success
+              -> JSValue -- ^ The arguments
+              -> String  -- ^ The serialized form
+buildResponse success args =
+  let ja = [ (strOfKey Success, JSBool success)
+           , (strOfKey Result, args)]
+      jo = toJSObject ja
+  in encodeStrict jo
+
 -- | Check that luxi request contains the required keys and parse it.
 validateCall :: String -> Result LuxiCall
 validateCall s = do
-  arr <- fromJResult "luxi call" $ decodeStrict s::Result (JSObject JSValue)
+  arr <- fromJResult "parsing top-level luxi message" $
+         decodeStrict s::Result (JSObject JSValue)
   let aobj = fromJSObject arr
   call <- fromObj aobj (strOfKey Method)::Result LuxiReq
   args <- fromObj aobj (strOfKey Args)
@@ -316,12 +340,17 @@ decodeCall (LuxiCall call args) =
     ReqQueryGroups -> do
               (names, fields, locking) <- fromJVal args
               return $ QueryGroups names fields locking
-    ReqQueryClusterInfo -> do
+    ReqQueryClusterInfo ->
               return QueryClusterInfo
     ReqQuery -> do
-              (what, fields, _) <-
-                fromJVal args::Result (QrViaLuxi, [String], JSValue)
-              return $ Query what fields ()
+              (what, fields, qfilter) <- fromJVal args
+              return $ Query what fields qfilter
+    ReqQueryFields -> do
+              (what, fields) <- fromJVal args
+              fields' <- case fields of
+                           JSNull -> return []
+                           _ -> fromJVal fields
+              return $ QueryFields what fields'
     ReqSubmitJob -> do
               [ops1] <- fromJVal args
               ops2 <- mapM (fromJResult (luxiReqToRaw call) . J.readJSON) ops1
@@ -361,11 +390,16 @@ decodeCall (LuxiCall call args) =
               return $ QueryConfigValues fields
     ReqQueryTags -> do
               (kind, name) <- fromJVal args
-              return $ QueryTags kind name
+              item <- tagObjectFrom kind name
+              return $ QueryTags item
     ReqCancelJob -> do
               [job] <- fromJVal args
               rid <- parseJobId job
               return $ CancelJob rid
+    ReqChangeJobPriority -> do
+              (job, priority) <- fromJVal args
+              rid <- parseJobId job
+              return $ ChangeJobPriority rid priority
     ReqSetDrainFlag -> do
               [flag] <- fromJVal args
               return $ SetDrainFlag flag
@@ -375,21 +409,29 @@ decodeCall (LuxiCall call args) =
 
 -- | Check that luxi responses contain the required keys and that the
 -- call was successful.
-validateResult :: String -> Result JSValue
+validateResult :: String -> ErrorResult JSValue
 validateResult s = do
   when (UTF8.replacement_char `elem` s) $
        fail "Failed to decode UTF-8, detected replacement char after decoding"
-  oarr <- fromJResult "Parsing LUXI response"
-          (decodeStrict s)::Result (JSObject JSValue)
+  oarr <- fromJResult "Parsing LUXI response" (decodeStrict s)
   let arr = J.fromJSObject oarr
-  status <- fromObj arr (strOfKey Success)::Result Bool
-  let rkey = strOfKey Result
+  status <- fromObj arr (strOfKey Success)
+  result <- fromObj arr (strOfKey Result)
   if status
-    then fromObj arr rkey
-    else fromObj arr rkey >>= fail
+    then return result
+    else decodeError result
+
+-- | Try to decode an error from the server response. This function
+-- will always fail, since it's called only on the error path (when
+-- status is False).
+decodeError :: JSValue -> ErrorResult JSValue
+decodeError val =
+  case fromJVal val of
+    Ok e -> Bad e
+    Bad msg -> Bad $ GenericError msg
 
 -- | Generic luxi method call.
-callMethod :: LuxiOp -> Client -> IO (Result JSValue)
+callMethod :: LuxiOp -> Client -> IO (ErrorResult JSValue)
 callMethod method s = do
   sendMsg s $ buildCall method
   result <- recvMsg s
@@ -407,31 +449,37 @@ parseJobId (JSRational _ x) =
 parseJobId x = Bad $ "Wrong type/value for job id: " ++ show x
 
 -- | Parse job submission result.
-parseSubmitJobResult :: JSValue -> Result JobId
-parseSubmitJobResult (JSArray [JSBool True, v]) = parseJobId v
+parseSubmitJobResult :: JSValue -> ErrorResult JobId
+parseSubmitJobResult (JSArray [JSBool True, v]) =
+  case parseJobId v of
+    Bad msg -> Bad $ LuxiError msg
+    Ok v' -> Ok v'
 parseSubmitJobResult (JSArray [JSBool False, JSString x]) =
-  Bad (fromJSString x)
-parseSubmitJobResult v = Bad $ "Unknown result from the master daemon" ++
-                         show v
+  Bad . LuxiError $ fromJSString x
+parseSubmitJobResult v =
+  Bad . LuxiError $ "Unknown result from the master daemon: " ++
+      show (pp_value v)
 
 -- | Specialized submitManyJobs call.
-submitManyJobs :: Client -> [[OpCode]] -> IO (Result [JobId])
+submitManyJobs :: Client -> [[OpCode]] -> IO (ErrorResult [JobId])
 submitManyJobs s jobs = do
   rval <- callMethod (SubmitManyJobs jobs) s
   -- map each result (status, payload) pair into a nice Result ADT
   return $ case rval of
              Bad x -> Bad x
              Ok (JSArray r) -> mapM parseSubmitJobResult r
-             x -> Bad ("Cannot parse response from Ganeti: " ++ show x)
+             x -> Bad . LuxiError $
+                  "Cannot parse response from Ganeti: " ++ show x
 
 -- | Custom queryJobs call.
-queryJobsStatus :: Client -> [JobId] -> IO (Result [JobStatus])
+queryJobsStatus :: Client -> [JobId] -> IO (ErrorResult [JobStatus])
 queryJobsStatus s jids = do
   rval <- callMethod (QueryJobs jids ["status"]) s
   return $ case rval of
              Bad x -> Bad x
              Ok y -> case J.readJSON y::(J.Result [[JobStatus]]) of
                        J.Ok vals -> if any null vals
-                                    then Bad "Missing job status field"
+                                    then Bad $
+                                         LuxiError "Missing job status field"
                                     else Ok (map head vals)
-                       J.Error x -> Bad x
+                       J.Error x -> Bad $ LuxiError x