Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Luxi.hs @ d575c755

History | View | Annotate | Download (10 kB)

1 6583e677 Iustin Pop
{-| Implementation of the LUXI loader.
2 53ec9022 Iustin Pop
3 53ec9022 Iustin Pop
-}
4 53ec9022 Iustin Pop
5 53ec9022 Iustin Pop
{-
6 53ec9022 Iustin Pop
7 8bc34c7b Iustin Pop
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
8 53ec9022 Iustin Pop
9 53ec9022 Iustin Pop
This program is free software; you can redistribute it and/or modify
10 53ec9022 Iustin Pop
it under the terms of the GNU General Public License as published by
11 53ec9022 Iustin Pop
the Free Software Foundation; either version 2 of the License, or
12 53ec9022 Iustin Pop
(at your option) any later version.
13 53ec9022 Iustin Pop
14 53ec9022 Iustin Pop
This program is distributed in the hope that it will be useful, but
15 53ec9022 Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
16 53ec9022 Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 53ec9022 Iustin Pop
General Public License for more details.
18 53ec9022 Iustin Pop
19 53ec9022 Iustin Pop
You should have received a copy of the GNU General Public License
20 53ec9022 Iustin Pop
along with this program; if not, write to the Free Software
21 53ec9022 Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 53ec9022 Iustin Pop
02110-1301, USA.
23 53ec9022 Iustin Pop
24 53ec9022 Iustin Pop
-}
25 53ec9022 Iustin Pop
26 53ec9022 Iustin Pop
module Ganeti.HTools.Luxi
27 ebf38064 Iustin Pop
  ( loadData
28 ebf38064 Iustin Pop
  , parseData
29 ebf38064 Iustin Pop
  ) where
30 53ec9022 Iustin Pop
31 53ec9022 Iustin Pop
import qualified Control.Exception as E
32 53ec9022 Iustin Pop
import Text.JSON.Types
33 39420403 Iustin Pop
import qualified Text.JSON
34 53ec9022 Iustin Pop
35 6583e677 Iustin Pop
import qualified Ganeti.Luxi as L
36 53ec9022 Iustin Pop
import Ganeti.HTools.Loader
37 53ec9022 Iustin Pop
import Ganeti.HTools.Types
38 a679e9dc Iustin Pop
import qualified Ganeti.HTools.Group as Group
39 53ec9022 Iustin Pop
import qualified Ganeti.HTools.Node as Node
40 53ec9022 Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
41 b69be409 Iustin Pop
import Ganeti.HTools.JSON
42 53ec9022 Iustin Pop
43 3603605a Iustin Pop
{-# ANN module "HLint: ignore Eta reduce" #-}
44 3603605a Iustin Pop
45 53ec9022 Iustin Pop
-- * Utility functions
46 53ec9022 Iustin Pop
47 92678b3c Iustin Pop
-- | Get values behind \"data\" part of the result.
48 92678b3c Iustin Pop
getData :: (Monad m) => JSValue -> m JSValue
49 d12f50b2 Iustin Pop
getData (JSObject o) = fromObj (fromJSObject o) "data"
50 d12f50b2 Iustin Pop
getData x = fail $ "Invalid input, expected dict entry but got " ++ show x
51 d12f50b2 Iustin Pop
52 d12f50b2 Iustin Pop
-- | Converts a (status, value) into m value, if possible.
53 d12f50b2 Iustin Pop
parseQueryField :: (Monad m) => JSValue -> m (JSValue, JSValue)
54 d12f50b2 Iustin Pop
parseQueryField (JSArray [status, result]) = return (status, result)
55 d12f50b2 Iustin Pop
parseQueryField o =
56 ebf38064 Iustin Pop
  fail $ "Invalid query field, expected (status, value) but got " ++ show o
57 d12f50b2 Iustin Pop
58 d12f50b2 Iustin Pop
-- | Parse a result row.
59 d12f50b2 Iustin Pop
parseQueryRow :: (Monad m) => JSValue -> m [(JSValue, JSValue)]
60 d12f50b2 Iustin Pop
parseQueryRow (JSArray arr) = mapM parseQueryField arr
61 d12f50b2 Iustin Pop
parseQueryRow o =
62 ebf38064 Iustin Pop
  fail $ "Invalid query row result, expected array but got " ++ show o
63 d12f50b2 Iustin Pop
64 d12f50b2 Iustin Pop
-- | Parse an overall query result and get the [(status, value)] list
65 d12f50b2 Iustin Pop
-- for each element queried.
66 d12f50b2 Iustin Pop
parseQueryResult :: (Monad m) => JSValue -> m [[(JSValue, JSValue)]]
67 d12f50b2 Iustin Pop
parseQueryResult (JSArray arr) = mapM parseQueryRow arr
68 d12f50b2 Iustin Pop
parseQueryResult o =
69 ebf38064 Iustin Pop
  fail $ "Invalid query result, expected array but got " ++ show o
70 92678b3c Iustin Pop
71 92678b3c Iustin Pop
-- | Prepare resulting output as parsers expect it.
72 260d0bda Agata Murawska
extractArray :: (Monad m) => JSValue -> m [[(JSValue, JSValue)]]
73 d12f50b2 Iustin Pop
extractArray v =
74 260d0bda Agata Murawska
  getData v >>= parseQueryResult
75 260d0bda Agata Murawska
76 260d0bda Agata Murawska
-- | Testing result status for more verbose error message.
77 260d0bda Agata Murawska
fromJValWithStatus :: (Text.JSON.JSON a, Monad m) => (JSValue, JSValue) -> m a
78 260d0bda Agata Murawska
fromJValWithStatus (st, v) = do
79 ebf38064 Iustin Pop
  st' <- fromJVal st
80 ebf38064 Iustin Pop
  L.checkRS st' v >>= fromJVal
81 92678b3c Iustin Pop
82 39420403 Iustin Pop
-- | Annotate errors when converting values with owner/attribute for
83 39420403 Iustin Pop
-- better debugging.
84 39420403 Iustin Pop
genericConvert :: (Text.JSON.JSON a) =>
85 260d0bda Agata Murawska
                  String             -- ^ The object type
86 260d0bda Agata Murawska
               -> String             -- ^ The object name
87 260d0bda Agata Murawska
               -> String             -- ^ The attribute we're trying to convert
88 260d0bda Agata Murawska
               -> (JSValue, JSValue) -- ^ The value we're trying to convert
89 260d0bda Agata Murawska
               -> Result a           -- ^ The annotated result
90 39420403 Iustin Pop
genericConvert otype oname oattr =
91 ebf38064 Iustin Pop
  annotateResult (otype ++ " '" ++ oname ++
92 ebf38064 Iustin Pop
                  "', error while reading attribute '" ++
93 ebf38064 Iustin Pop
                  oattr ++ "'") . fromJValWithStatus
94 39420403 Iustin Pop
95 53ec9022 Iustin Pop
-- * Data querying functionality
96 53ec9022 Iustin Pop
97 53ec9022 Iustin Pop
-- | The input data for node query.
98 683b1ca7 Iustin Pop
queryNodesMsg :: L.LuxiOp
99 53ec9022 Iustin Pop
queryNodesMsg =
100 92678b3c Iustin Pop
  L.Query L.QRNode ["name", "mtotal", "mnode", "mfree", "dtotal", "dfree",
101 92678b3c Iustin Pop
                    "ctotal", "offline", "drained", "vm_capable",
102 8bc34c7b Iustin Pop
                    "ndp/spindle_count", "group.uuid"] ()
103 53ec9022 Iustin Pop
104 53ec9022 Iustin Pop
-- | The input data for instance query.
105 683b1ca7 Iustin Pop
queryInstancesMsg :: L.LuxiOp
106 53ec9022 Iustin Pop
queryInstancesMsg =
107 ebf38064 Iustin Pop
  L.Query L.QRInstance ["name", "disk_usage", "be/memory", "be/vcpus",
108 ebf38064 Iustin Pop
                        "status", "pnode", "snodes", "tags", "oper_ram",
109 b003b8c0 René Nussbaumer
                        "be/auto_balance", "disk_template",
110 ec629280 René Nussbaumer
                        "be/spindle_use"] ()
111 53ec9022 Iustin Pop
112 a679e9dc Iustin Pop
-- | The input data for cluster query.
113 683b1ca7 Iustin Pop
queryClusterInfoMsg :: L.LuxiOp
114 683b1ca7 Iustin Pop
queryClusterInfoMsg = L.QueryClusterInfo
115 f89235f1 Iustin Pop
116 a679e9dc Iustin Pop
-- | The input data for node group query.
117 a679e9dc Iustin Pop
queryGroupsMsg :: L.LuxiOp
118 a679e9dc Iustin Pop
queryGroupsMsg =
119 6cff91f5 Iustin Pop
  L.Query L.QRGroup ["uuid", "name", "alloc_policy", "ipolicy"] ()
120 a679e9dc Iustin Pop
121 179c0828 Iustin Pop
-- | Wraper over 'callMethod' doing node query.
122 6583e677 Iustin Pop
queryNodes :: L.Client -> IO (Result JSValue)
123 683b1ca7 Iustin Pop
queryNodes = L.callMethod queryNodesMsg
124 53ec9022 Iustin Pop
125 179c0828 Iustin Pop
-- | Wraper over 'callMethod' doing instance query.
126 6583e677 Iustin Pop
queryInstances :: L.Client -> IO (Result JSValue)
127 683b1ca7 Iustin Pop
queryInstances = L.callMethod queryInstancesMsg
128 53ec9022 Iustin Pop
129 179c0828 Iustin Pop
-- | Wrapper over 'callMethod' doing cluster information query.
130 f89235f1 Iustin Pop
queryClusterInfo :: L.Client -> IO (Result JSValue)
131 683b1ca7 Iustin Pop
queryClusterInfo = L.callMethod queryClusterInfoMsg
132 f89235f1 Iustin Pop
133 a679e9dc Iustin Pop
-- | Wrapper over callMethod doing group query.
134 a679e9dc Iustin Pop
queryGroups :: L.Client -> IO (Result JSValue)
135 a679e9dc Iustin Pop
queryGroups = L.callMethod queryGroupsMsg
136 a679e9dc Iustin Pop
137 53ec9022 Iustin Pop
-- | Parse a instance list in JSON format.
138 53ec9022 Iustin Pop
getInstances :: NameAssoc
139 53ec9022 Iustin Pop
             -> JSValue
140 53ec9022 Iustin Pop
             -> Result [(String, Instance.Instance)]
141 92678b3c Iustin Pop
getInstances ktn arr = extractArray arr >>= mapM (parseInstance ktn)
142 53ec9022 Iustin Pop
143 53ec9022 Iustin Pop
-- | Construct an instance from a JSON object.
144 6ff78049 Iustin Pop
parseInstance :: NameAssoc
145 260d0bda Agata Murawska
              -> [(JSValue, JSValue)]
146 53ec9022 Iustin Pop
              -> Result (String, Instance.Instance)
147 260d0bda Agata Murawska
parseInstance ktn [ name, disk, mem, vcpus
148 260d0bda Agata Murawska
                  , status, pnode, snodes, tags, oram
149 b003b8c0 René Nussbaumer
                  , auto_balance, disk_template, su ] = do
150 260d0bda Agata Murawska
  xname <- annotateResult "Parsing new instance" (fromJValWithStatus name)
151 39420403 Iustin Pop
  let convert a = genericConvert "Instance" xname a
152 39420403 Iustin Pop
  xdisk <- convert "disk_usage" disk
153 3603605a Iustin Pop
  xmem <- case oram of -- FIXME: remove the "guessing"
154 3603605a Iustin Pop
            (_, JSRational _ _) -> convert "oper_ram" oram
155 3603605a Iustin Pop
            _ -> convert "be/memory" mem
156 39420403 Iustin Pop
  xvcpus <- convert "be/vcpus" vcpus
157 39420403 Iustin Pop
  xpnode <- convert "pnode" pnode >>= lookupNode ktn xname
158 39420403 Iustin Pop
  xsnodes <- convert "snodes" snodes::Result [JSString]
159 3603605a Iustin Pop
  snode <- if null xsnodes
160 3603605a Iustin Pop
             then return Node.noSecondary
161 3603605a Iustin Pop
             else lookupNode ktn xname (fromJSString $ head xsnodes)
162 39420403 Iustin Pop
  xrunning <- convert "status" status
163 39420403 Iustin Pop
  xtags <- convert "tags" tags
164 6880526c Iustin Pop
  xauto_balance <- convert "auto_balance" auto_balance
165 b3c5e8de Iustin Pop
  xdt <- convert "disk_template" disk_template
166 ec629280 René Nussbaumer
  xsu <- convert "be/spindle_use" su
167 17e7af2b Iustin Pop
  let inst = Instance.create xname xmem xdisk xvcpus
168 b003b8c0 René Nussbaumer
             xrunning xtags xauto_balance xpnode snode xdt xsu
169 53ec9022 Iustin Pop
  return (xname, inst)
170 53ec9022 Iustin Pop
171 53ec9022 Iustin Pop
parseInstance _ v = fail ("Invalid instance query result: " ++ show v)
172 53ec9022 Iustin Pop
173 53ec9022 Iustin Pop
-- | Parse a node list in JSON format.
174 10ef6b4e Iustin Pop
getNodes :: NameAssoc -> JSValue -> Result [(String, Node.Node)]
175 92678b3c Iustin Pop
getNodes ktg arr = extractArray arr >>= mapM (parseNode ktg)
176 53ec9022 Iustin Pop
177 53ec9022 Iustin Pop
-- | Construct a node from a JSON object.
178 260d0bda Agata Murawska
parseNode :: NameAssoc -> [(JSValue, JSValue)] -> Result (String, Node.Node)
179 260d0bda Agata Murawska
parseNode ktg [ name, mtotal, mnode, mfree, dtotal, dfree
180 8bc34c7b Iustin Pop
              , ctotal, offline, drained, vm_capable, spindles, g_uuid ]
181 53ec9022 Iustin Pop
    = do
182 260d0bda Agata Murawska
  xname <- annotateResult "Parsing new node" (fromJValWithStatus name)
183 39420403 Iustin Pop
  let convert a = genericConvert "Node" xname a
184 39420403 Iustin Pop
  xoffline <- convert "offline" offline
185 39420403 Iustin Pop
  xdrained <- convert "drained" drained
186 39420403 Iustin Pop
  xvm_capable <- convert "vm_capable" vm_capable
187 8bc34c7b Iustin Pop
  xspindles <- convert "spindles" spindles
188 39420403 Iustin Pop
  xgdx   <- convert "group.uuid" g_uuid >>= lookupGroup ktg xname
189 3603605a Iustin Pop
  node <- if xoffline || xdrained || not xvm_capable
190 8bc34c7b Iustin Pop
            then return $ Node.create xname 0 0 0 0 0 0 True xspindles xgdx
191 3603605a Iustin Pop
            else do
192 3603605a Iustin Pop
              xmtotal  <- convert "mtotal" mtotal
193 3603605a Iustin Pop
              xmnode   <- convert "mnode" mnode
194 3603605a Iustin Pop
              xmfree   <- convert "mfree" mfree
195 3603605a Iustin Pop
              xdtotal  <- convert "dtotal" dtotal
196 3603605a Iustin Pop
              xdfree   <- convert "dfree" dfree
197 3603605a Iustin Pop
              xctotal  <- convert "ctotal" ctotal
198 3603605a Iustin Pop
              return $ Node.create xname xmtotal xmnode xmfree
199 8bc34c7b Iustin Pop
                     xdtotal xdfree xctotal False xspindles xgdx
200 53ec9022 Iustin Pop
  return (xname, node)
201 53ec9022 Iustin Pop
202 10ef6b4e Iustin Pop
parseNode _ v = fail ("Invalid node query result: " ++ show v)
203 53ec9022 Iustin Pop
204 179c0828 Iustin Pop
-- | Parses the cluster tags.
205 e77bc89b Iustin Pop
getClusterData :: JSValue -> Result ([String], IPolicy)
206 e77bc89b Iustin Pop
getClusterData (JSObject obj) = do
207 f89235f1 Iustin Pop
  let errmsg = "Parsing cluster info"
208 e77bc89b Iustin Pop
      obj' = fromJSObject obj
209 e77bc89b Iustin Pop
  ctags <- tryFromObj errmsg obj' "tags"
210 e77bc89b Iustin Pop
  cpol <- tryFromObj errmsg obj' "ipolicy"
211 e77bc89b Iustin Pop
  return (ctags, cpol)
212 e77bc89b Iustin Pop
213 e77bc89b Iustin Pop
getClusterData _ = Bad $ "Cannot parse cluster info, not a JSON record"
214 f89235f1 Iustin Pop
215 179c0828 Iustin Pop
-- | Parses the cluster groups.
216 a679e9dc Iustin Pop
getGroups :: JSValue -> Result [(String, Group.Group)]
217 92678b3c Iustin Pop
getGroups jsv = extractArray jsv >>= mapM parseGroup
218 a679e9dc Iustin Pop
219 179c0828 Iustin Pop
-- | Parses a given group information.
220 260d0bda Agata Murawska
parseGroup :: [(JSValue, JSValue)] -> Result (String, Group.Group)
221 6cff91f5 Iustin Pop
parseGroup [uuid, name, apol, ipol] = do
222 260d0bda Agata Murawska
  xname <- annotateResult "Parsing new group" (fromJValWithStatus name)
223 39420403 Iustin Pop
  let convert a = genericConvert "Group" xname a
224 39420403 Iustin Pop
  xuuid <- convert "uuid" uuid
225 39420403 Iustin Pop
  xapol <- convert "alloc_policy" apol
226 6cff91f5 Iustin Pop
  xipol <- convert "ipolicy" ipol
227 6cff91f5 Iustin Pop
  return (xuuid, Group.create xname xuuid xapol xipol)
228 a679e9dc Iustin Pop
229 a679e9dc Iustin Pop
parseGroup v = fail ("Invalid group query result: " ++ show v)
230 a679e9dc Iustin Pop
231 53ec9022 Iustin Pop
-- * Main loader functionality
232 53ec9022 Iustin Pop
233 525bfb36 Iustin Pop
-- | Builds the cluster data by querying a given socket name.
234 b3f0710c Iustin Pop
readData :: String -- ^ Unix socket to use as source
235 a679e9dc Iustin Pop
         -> IO (Result JSValue, Result JSValue, Result JSValue, Result JSValue)
236 b3f0710c Iustin Pop
readData master =
237 53ec9022 Iustin Pop
  E.bracket
238 6583e677 Iustin Pop
       (L.getClient master)
239 6583e677 Iustin Pop
       L.closeClient
240 53ec9022 Iustin Pop
       (\s -> do
241 53ec9022 Iustin Pop
          nodes <- queryNodes s
242 53ec9022 Iustin Pop
          instances <- queryInstances s
243 f89235f1 Iustin Pop
          cinfo <- queryClusterInfo s
244 a679e9dc Iustin Pop
          groups <- queryGroups s
245 a679e9dc Iustin Pop
          return (groups, nodes, instances, cinfo)
246 53ec9022 Iustin Pop
       )
247 b3f0710c Iustin Pop
248 525bfb36 Iustin Pop
-- | Converts the output of 'readData' into the internal cluster
249 525bfb36 Iustin Pop
-- representation.
250 a679e9dc Iustin Pop
parseData :: (Result JSValue, Result JSValue, Result JSValue, Result JSValue)
251 f4f6eb0b Iustin Pop
          -> Result ClusterData
252 a679e9dc Iustin Pop
parseData (groups, nodes, instances, cinfo) = do
253 a679e9dc Iustin Pop
  group_data <- groups >>= getGroups
254 10ef6b4e Iustin Pop
  let (group_names, group_idx) = assignIndices group_data
255 10ef6b4e Iustin Pop
  node_data <- nodes >>= getNodes group_names
256 b3f0710c Iustin Pop
  let (node_names, node_idx) = assignIndices node_data
257 b3f0710c Iustin Pop
  inst_data <- instances >>= getInstances node_names
258 b3f0710c Iustin Pop
  let (_, inst_idx) = assignIndices inst_data
259 e77bc89b Iustin Pop
  (ctags, cpol) <- cinfo >>= getClusterData
260 e77bc89b Iustin Pop
  return (ClusterData group_idx node_idx inst_idx ctags cpol)
261 b3f0710c Iustin Pop
262 525bfb36 Iustin Pop
-- | Top level function for data loading.
263 b3f0710c Iustin Pop
loadData :: String -- ^ Unix socket to use as source
264 f4f6eb0b Iustin Pop
         -> IO (Result ClusterData)
265 2a8e2dc9 Iustin Pop
loadData = fmap parseData . readData