Commit 8c72f711 authored by Bernardo Dal Seno's avatar Bernardo Dal Seno

Refactor reading live data in htools

This simplifies different handling of individual items.
Signed-off-by: default avatarBernardo Dal Seno <bdalseno@google.com>
Reviewed-by: default avatarKlaus Aehlig <aehlig@google.com>
parent 06fb92cf
......@@ -133,22 +133,19 @@ parseNode ktg n a = do
vm_capable <- annotateResult desc $ maybeFromObj a "vm_capable"
let vm_capable' = fromMaybe True vm_capable
gidx <- lookupGroup ktg n guuid
node <- if offline || drained || not vm_capable'
then return $ Node.create n 0 0 0 0 0 0 True 0 gidx False
else do
mtotal <- extract "total_memory"
mnode <- extract "reserved_memory"
mfree <- extract "free_memory"
dtotal <- extract "total_disk"
dfree <- extract "free_disk"
ctotal <- extract "total_cpus"
ndparams <- extract "ndparams" >>= asJSObject
spindles <- tryFromObj desc (fromJSObject ndparams)
"spindle_count"
excl_stor <- tryFromObj desc (fromJSObject ndparams)
"exclusive_storage"
return $ Node.create n mtotal mnode mfree
dtotal dfree ctotal False spindles gidx excl_stor
ndparams <- extract "ndparams" >>= asJSObject
spindles <- tryFromObj desc (fromJSObject ndparams) "spindle_count"
excl_stor <- tryFromObj desc (fromJSObject ndparams) "exclusive_storage"
let live = not offline && not drained && vm_capable'
lvextract def = eitherLive live def . extract
mtotal <- lvextract 0.0 "total_memory"
mnode <- lvextract 0 "reserved_memory"
mfree <- lvextract 0 "free_memory"
dtotal <- lvextract 0.0 "total_disk"
dfree <- lvextract 0 "free_disk"
ctotal <- lvextract 0.0 "total_cpus"
let node = Node.create n mtotal mnode mfree dtotal dfree ctotal (not live)
spindles gidx excl_stor
return (n, node)
-- | Parses a group as found in the cluster group list.
......
......@@ -198,19 +198,17 @@ parseNode ktg [ name, mtotal, mnode, mfree, dtotal, dfree
xgdx <- convert "group.uuid" g_uuid >>= lookupGroup ktg xname
xtags <- convert "tags" tags
xexcl_stor <- convert "exclusive_storage" excl_stor
node <- if xoffline || xdrained || not xvm_capable
then return $
Node.create xname 0 0 0 0 0 0 True xspindles xgdx False
else do
xmtotal <- convert "mtotal" mtotal
xmnode <- convert "mnode" mnode
xmfree <- convert "mfree" mfree
xdtotal <- convert "dtotal" dtotal
xdfree <- convert "dfree" dfree
xctotal <- convert "ctotal" ctotal
return . flip Node.setNodeTags xtags $
Node.create xname xmtotal xmnode xmfree xdtotal xdfree
xctotal False xspindles xgdx xexcl_stor
let live = not xoffline && not xdrained && xvm_capable
lvconvert def n d = eitherLive live def $ convert n d
xmtotal <- lvconvert 0.0 "mtotal" mtotal
xmnode <- lvconvert 0 "mnode" mnode
xmfree <- lvconvert 0 "mfree" mfree
xdtotal <- lvconvert 0.0 "dtotal" dtotal
xdfree <- lvconvert 0 "dfree" dfree
xctotal <- lvconvert 0.0 "ctotal" ctotal
let node = flip Node.setNodeTags xtags $
Node.create xname xmtotal xmnode xmfree xdtotal xdfree
xctotal (not live) xspindles xgdx xexcl_stor
return (xname, node)
parseNode _ v = fail ("Invalid node query result: " ++ show v)
......
......@@ -160,19 +160,18 @@ parseNode ktg a = do
excl_stor <- tryFromObj desc (fromJSObject ndparams) "exclusive_storage"
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' False
else do
mtotal <- extract "mtotal"
mnode <- extract "mnode"
mfree <- extract "mfree"
dtotal <- extract "dtotal"
dfree <- extract "dfree"
ctotal <- extract "ctotal"
tags <- extract "tags"
return . flip Node.setNodeTags tags $
Node.create name mtotal mnode mfree dtotal dfree ctotal False
spindles guuid' excl_stor
let live = not offline && not drained && vm_cap'
lvextract def = eitherLive live def . extract
mtotal <- lvextract 0.0 "mtotal"
mnode <- lvextract 0 "mnode"
mfree <- lvextract 0 "mfree"
dtotal <- lvextract 0.0 "dtotal"
dfree <- lvextract 0 "dfree"
ctotal <- lvextract 0.0 "ctotal"
tags <- extract "tags"
let node = flip Node.setNodeTags tags $
Node.create name mtotal mnode mfree dtotal dfree ctotal (not live)
spindles guuid' excl_stor
return (name, node)
-- | Construct a group from a JSON object.
......
......@@ -34,6 +34,7 @@ module Ganeti.HTools.Loader
, lookupNode
, lookupInstance
, lookupGroup
, eitherLive
, commonSuffix
, RqType(..)
, Request(..)
......@@ -332,3 +333,8 @@ nodeIdsk node il =
let rfind = flip Container.find il
in sum . map (Instance.dsk . rfind)
$ Node.pList node ++ Node.sList node
-- | Get live information or a default value
eitherLive :: (Monad m) => Bool -> a -> m a -> m a
eitherLive True _ live_data = live_data
eitherLive False def_data _ = return def_data
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment