module Ganeti.HTools.Rapi
( loadData
, parseData
) where
import Control.Exception
import Data.List (isPrefixOf)
import Data.Maybe (fromMaybe)
#ifndef NO_CURL
import Network.Curl
import Network.Curl.Types ()
#endif
import Control.Monad
import Text.JSON (JSObject, fromJSObject, decodeStrict)
import Text.JSON.Types (JSValue(..))
import Text.Printf (printf)
import System.FilePath
import Ganeti.HTools.Loader
import Ganeti.HTools.Types
import Ganeti.HTools.JSON
import qualified Ganeti.HTools.Group as Group
import qualified Ganeti.HTools.Node as Node
import qualified Ganeti.HTools.Instance as Instance
import qualified Ganeti.Constants as C
filePrefix :: String
filePrefix = "file://"
getUrl :: (Monad m) => String -> IO (m String)
#ifdef NO_CURL
getUrl _ = return $ fail "RAPI/curl backend disabled at compile time"
#else
curlOpts :: [CurlOption]
curlOpts = [ CurlSSLVerifyPeer False
, CurlSSLVerifyHost 0
, CurlTimeout (fromIntegral queryTimeout)
, CurlConnectTimeout (fromIntegral connTimeout)
]
getUrl url = do
(code, !body) <- curlGetString url curlOpts
return (case code of
CurlOK -> return body
_ -> fail $ printf "Curl error for '%s', error %s"
url (show code))
#endif
ioErrToResult :: IO a -> IO (Result a)
ioErrToResult ioaction =
Control.Exception.catch (ioaction >>= return . Ok)
(\e -> return . Bad . show $ (e::IOException))
formatHost :: String -> String
formatHost master =
if ':' `elem` master
then master
else "https://" ++ master ++ ":" ++ show C.defaultRapiPort
getInstances :: NameAssoc
-> String
-> Result [(String, Instance.Instance)]
getInstances ktn body =
loadJSArray "Parsing instance data" body >>=
mapM (parseInstance ktn . fromJSObject)
getNodes :: NameAssoc -> String -> Result [(String, Node.Node)]
getNodes ktg body = loadJSArray "Parsing node data" body >>=
mapM (parseNode ktg . fromJSObject)
getGroups :: String -> Result [(String, Group.Group)]
getGroups body = loadJSArray "Parsing group data" body >>=
mapM (parseGroup . fromJSObject)
parseInstance :: NameAssoc
-> JSRecord
-> Result (String, Instance.Instance)
parseInstance ktn a = do
name <- tryFromObj "Parsing new instance" a "name"
let owner_name = "Instance '" ++ name ++ "', error while parsing data"
let extract s x = tryFromObj owner_name x s
disk <- extract "disk_usage" a
beparams <- liftM fromJSObject (extract "beparams" a)
omem <- extract "oper_ram" a
mem <- case omem of
JSRational _ _ -> annotateResult owner_name (fromJVal omem)
_ -> extract "memory" beparams `mplus` extract "maxmem" beparams
vcpus <- extract "vcpus" beparams
pnode <- extract "pnode" a >>= lookupNode ktn name
snodes <- extract "snodes" a
snode <- if null snodes
then return Node.noSecondary
else readEitherString (head snodes) >>= lookupNode ktn name
running <- extract "status" a
tags <- extract "tags" a
auto_balance <- extract "auto_balance" beparams
dt <- extract "disk_template" a
su <- extract "spindle_use" beparams
let inst = Instance.create name mem disk vcpus running tags
auto_balance pnode snode dt su
return (name, inst)
parseNode :: NameAssoc -> JSRecord -> Result (String, Node.Node)
parseNode ktg a = do
name <- tryFromObj "Parsing new node" a "name"
let desc = "Node '" ++ name ++ "', error while parsing data"
extract s = tryFromObj desc a s
offline <- extract "offline"
drained <- extract "drained"
vm_cap <- annotateResult desc $ maybeFromObj a "vm_capable"
let vm_cap' = fromMaybe True vm_cap
ndparams <- extract "ndparams" >>= asJSObject
spindles <- tryFromObj desc (fromJSObject ndparams) "spindle_count"
guuid <- annotateResult desc $ maybeFromObj a "group.uuid"
guuid' <- lookupGroup ktg name (fromMaybe defaultGroupID guuid)
node <- if offline || drained || not vm_cap'
then return $ Node.create name 0 0 0 0 0 0 True 0 guuid'
else do
mtotal <- extract "mtotal"
mnode <- extract "mnode"
mfree <- extract "mfree"
dtotal <- extract "dtotal"
dfree <- extract "dfree"
ctotal <- extract "ctotal"
return $ Node.create name mtotal mnode mfree
dtotal dfree ctotal False spindles guuid'
return (name, node)
parseGroup :: JSRecord -> Result (String, Group.Group)
parseGroup a = do
name <- tryFromObj "Parsing new group" a "name"
let extract s = tryFromObj ("Group '" ++ name ++ "'") a s
uuid <- extract "uuid"
apol <- extract "alloc_policy"
ipol <- extract "ipolicy"
return (uuid, Group.create name uuid apol ipol)
parseCluster :: JSObject JSValue -> Result ([String], IPolicy)
parseCluster obj = do
let obj' = fromJSObject obj
extract s = tryFromObj "Parsing cluster data" obj' s
tags <- extract "tags"
ipolicy <- extract "ipolicy"
return (tags, ipolicy)
readDataHttp :: String
-> IO (Result String, Result String, Result String, Result String)
readDataHttp master = do
let url = formatHost master
group_body <- getUrl $ printf "%s/2/groups?bulk=1" url
node_body <- getUrl $ printf "%s/2/nodes?bulk=1" url
inst_body <- getUrl $ printf "%s/2/instances?bulk=1" url
info_body <- getUrl $ printf "%s/2/info" url
return (group_body, node_body, inst_body, info_body)
readDataFile:: String
-> IO (Result String, Result String, Result String, Result String)
readDataFile path = do
group_body <- ioErrToResult $ readFile $ path </> "groups.json"
node_body <- ioErrToResult $ readFile $ path </> "nodes.json"
inst_body <- ioErrToResult $ readFile $ path </> "instances.json"
info_body <- ioErrToResult $ readFile $ path </> "info.json"
return (group_body, node_body, inst_body, info_body)
readData :: String
-> IO (Result String, Result String, Result String, Result String)
readData url = do
if filePrefix `isPrefixOf` url
then readDataFile (drop (length filePrefix) url)
else readDataHttp url
parseData :: (Result String, Result String, Result String, Result String)
-> Result ClusterData
parseData (group_body, node_body, inst_body, info_body) = do
group_data <- group_body >>= getGroups
let (group_names, group_idx) = assignIndices group_data
node_data <- node_body >>= getNodes group_names
let (node_names, node_idx) = assignIndices node_data
inst_data <- inst_body >>= getInstances node_names
let (_, inst_idx) = assignIndices inst_data
(tags, ipolicy) <- info_body >>=
(fromJResult "Parsing cluster info" . decodeStrict) >>=
parseCluster
return (ClusterData group_idx node_idx inst_idx tags ipolicy)
loadData :: String
-> IO (Result ClusterData)
loadData = fmap parseData . readData