Commit 0eb83d70 authored by Bernardo Dal Seno's avatar Bernardo Dal Seno Committed by Jose A. Lopes
Browse files

Refactor reading live data in htools

This simplifies different handling of individual items.

Cherry-picked from 8c72f711

.
Signed-off-by: default avatarJose A. Lopes <jabolopes@google.com>
Reviewed-by: default avatarThomas Thrainer <thomasth@google.com>
parent 9d7e8218
......@@ -130,20 +130,18 @@ 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
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"
return $ Node.create n mtotal mnode mfree
dtotal dfree ctotal False spindles gidx
ndparams <- extract "ndparams" >>= asJSObject
spindles <- tryFromObj desc (fromJSObject ndparams) "spindle_count"
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
return (n, node)
-- | Parses a group as found in the cluster group list.
......
......@@ -194,17 +194,16 @@ parseNode ktg [ name, mtotal, mnode, mfree, dtotal, dfree
xvm_capable <- convert "vm_capable" vm_capable
xspindles <- convert "spindles" spindles
xgdx <- convert "group.uuid" g_uuid >>= lookupGroup ktg xname
node <- if xoffline || xdrained || not xvm_capable
then return $ Node.create xname 0 0 0 0 0 0 True xspindles xgdx
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 $ Node.create xname xmtotal xmnode xmfree
xdtotal xdfree xctotal False xspindles xgdx
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 = Node.create xname xmtotal xmnode xmfree xdtotal xdfree
xctotal (not live) xspindles xgdx
return (xname, node)
parseNode _ v = fail ("Invalid node query result: " ++ show v)
......
......@@ -157,17 +157,16 @@ parseNode ktg a = do
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'
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"
let node = Node.create name mtotal mnode mfree dtotal dfree ctotal (not live)
spindles guuid'
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