Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Luxi.hs @ 3158250d

History | View | Annotate | Download (8.1 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 39420403 Iustin Pop
Copyright (C) 2009, 2010, 2011 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 53ec9022 Iustin Pop
    (
28 53ec9022 Iustin Pop
      loadData
29 b3f0710c Iustin Pop
    , parseData
30 53ec9022 Iustin Pop
    ) where
31 53ec9022 Iustin Pop
32 53ec9022 Iustin Pop
import qualified Control.Exception as E
33 53ec9022 Iustin Pop
import Text.JSON.Types
34 39420403 Iustin Pop
import qualified Text.JSON
35 53ec9022 Iustin Pop
36 6583e677 Iustin Pop
import qualified Ganeti.Luxi as L
37 53ec9022 Iustin Pop
import Ganeti.HTools.Loader
38 53ec9022 Iustin Pop
import Ganeti.HTools.Types
39 a679e9dc Iustin Pop
import qualified Ganeti.HTools.Group as Group
40 53ec9022 Iustin Pop
import qualified Ganeti.HTools.Node as Node
41 53ec9022 Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
42 f5ed8632 Iustin Pop
import Ganeti.HTools.Utils (fromJVal, annotateResult, tryFromObj, asJSObject)
43 53ec9022 Iustin Pop
44 53ec9022 Iustin Pop
-- * Utility functions
45 53ec9022 Iustin Pop
46 53ec9022 Iustin Pop
-- | Ensure a given JSValue is actually a JSArray.
47 53ec9022 Iustin Pop
toArray :: (Monad m) => JSValue -> m [JSValue]
48 53ec9022 Iustin Pop
toArray v =
49 53ec9022 Iustin Pop
    case v of
50 53ec9022 Iustin Pop
      JSArray arr -> return arr
51 53ec9022 Iustin Pop
      o -> fail ("Invalid input, expected array but got " ++ show o)
52 53ec9022 Iustin Pop
53 39420403 Iustin Pop
-- | Annotate errors when converting values with owner/attribute for
54 39420403 Iustin Pop
-- better debugging.
55 39420403 Iustin Pop
genericConvert :: (Text.JSON.JSON a) =>
56 39420403 Iustin Pop
                  String     -- ^ The object type
57 39420403 Iustin Pop
               -> String     -- ^ The object name
58 39420403 Iustin Pop
               -> String     -- ^ The attribute we're trying to convert
59 39420403 Iustin Pop
               -> JSValue    -- ^ The value we try to convert
60 39420403 Iustin Pop
               -> Result a   -- ^ The annotated result
61 39420403 Iustin Pop
genericConvert otype oname oattr =
62 6a062ff9 Iustin Pop
    annotateResult (otype ++ " '" ++ oname ++
63 6a062ff9 Iustin Pop
                    "', error while reading attribute '" ++
64 39420403 Iustin Pop
                    oattr ++ "'") . fromJVal
65 39420403 Iustin Pop
66 53ec9022 Iustin Pop
-- * Data querying functionality
67 53ec9022 Iustin Pop
68 53ec9022 Iustin Pop
-- | The input data for node query.
69 683b1ca7 Iustin Pop
queryNodesMsg :: L.LuxiOp
70 53ec9022 Iustin Pop
queryNodesMsg =
71 683b1ca7 Iustin Pop
  L.QueryNodes [] ["name", "mtotal", "mnode", "mfree", "dtotal", "dfree",
72 f5ed8632 Iustin Pop
                   "ctotal", "offline", "drained", "vm_capable",
73 f5ed8632 Iustin Pop
                   "group.uuid"] False
74 53ec9022 Iustin Pop
75 53ec9022 Iustin Pop
-- | The input data for instance query.
76 683b1ca7 Iustin Pop
queryInstancesMsg :: L.LuxiOp
77 53ec9022 Iustin Pop
queryInstancesMsg =
78 683b1ca7 Iustin Pop
  L.QueryInstances [] ["name", "disk_usage", "be/memory", "be/vcpus",
79 6880526c Iustin Pop
                       "status", "pnode", "snodes", "tags", "oper_ram",
80 b3c5e8de Iustin Pop
                       "be/auto_balance", "disk_template"] False
81 53ec9022 Iustin Pop
82 a679e9dc Iustin Pop
-- | The input data for cluster query.
83 683b1ca7 Iustin Pop
queryClusterInfoMsg :: L.LuxiOp
84 683b1ca7 Iustin Pop
queryClusterInfoMsg = L.QueryClusterInfo
85 f89235f1 Iustin Pop
86 a679e9dc Iustin Pop
-- | The input data for node group query.
87 a679e9dc Iustin Pop
queryGroupsMsg :: L.LuxiOp
88 a679e9dc Iustin Pop
queryGroupsMsg =
89 c4c37257 Iustin Pop
  L.QueryGroups [] ["uuid", "name", "alloc_policy"] False
90 a679e9dc Iustin Pop
91 53ec9022 Iustin Pop
-- | Wraper over callMethod doing node query.
92 6583e677 Iustin Pop
queryNodes :: L.Client -> IO (Result JSValue)
93 683b1ca7 Iustin Pop
queryNodes = L.callMethod queryNodesMsg
94 53ec9022 Iustin Pop
95 53ec9022 Iustin Pop
-- | Wraper over callMethod doing instance query.
96 6583e677 Iustin Pop
queryInstances :: L.Client -> IO (Result JSValue)
97 683b1ca7 Iustin Pop
queryInstances = L.callMethod queryInstancesMsg
98 53ec9022 Iustin Pop
99 f89235f1 Iustin Pop
queryClusterInfo :: L.Client -> IO (Result JSValue)
100 683b1ca7 Iustin Pop
queryClusterInfo = L.callMethod queryClusterInfoMsg
101 f89235f1 Iustin Pop
102 a679e9dc Iustin Pop
-- | Wrapper over callMethod doing group query.
103 a679e9dc Iustin Pop
queryGroups :: L.Client -> IO (Result JSValue)
104 a679e9dc Iustin Pop
queryGroups = L.callMethod queryGroupsMsg
105 a679e9dc Iustin Pop
106 53ec9022 Iustin Pop
-- | Parse a instance list in JSON format.
107 53ec9022 Iustin Pop
getInstances :: NameAssoc
108 53ec9022 Iustin Pop
             -> JSValue
109 53ec9022 Iustin Pop
             -> Result [(String, Instance.Instance)]
110 53ec9022 Iustin Pop
getInstances ktn arr = toArray arr >>= mapM (parseInstance ktn)
111 53ec9022 Iustin Pop
112 53ec9022 Iustin Pop
-- | Construct an instance from a JSON object.
113 6ff78049 Iustin Pop
parseInstance :: NameAssoc
114 53ec9022 Iustin Pop
              -> JSValue
115 53ec9022 Iustin Pop
              -> Result (String, Instance.Instance)
116 27671a61 Iustin Pop
parseInstance ktn (JSArray [ name, disk, mem, vcpus
117 6880526c Iustin Pop
                           , status, pnode, snodes, tags, oram
118 b3c5e8de Iustin Pop
                           , auto_balance, disk_template ]) = do
119 117dc2d8 Iustin Pop
  xname <- annotateResult "Parsing new instance" (fromJVal name)
120 39420403 Iustin Pop
  let convert a = genericConvert "Instance" xname a
121 39420403 Iustin Pop
  xdisk <- convert "disk_usage" disk
122 6402a260 Iustin Pop
  xmem <- (case oram of
123 39420403 Iustin Pop
             JSRational _ _ -> convert "oper_ram" oram
124 39420403 Iustin Pop
             _ -> convert "be/memory" mem)
125 39420403 Iustin Pop
  xvcpus <- convert "be/vcpus" vcpus
126 39420403 Iustin Pop
  xpnode <- convert "pnode" pnode >>= lookupNode ktn xname
127 39420403 Iustin Pop
  xsnodes <- convert "snodes" snodes::Result [JSString]
128 53ec9022 Iustin Pop
  snode <- (if null xsnodes then return Node.noSecondary
129 53ec9022 Iustin Pop
            else lookupNode ktn xname (fromJSString $ head xsnodes))
130 39420403 Iustin Pop
  xrunning <- convert "status" status
131 39420403 Iustin Pop
  xtags <- convert "tags" tags
132 6880526c Iustin Pop
  xauto_balance <- convert "auto_balance" auto_balance
133 b3c5e8de Iustin Pop
  xdt <- convert "disk_template" disk_template
134 17e7af2b Iustin Pop
  let inst = Instance.create xname xmem xdisk xvcpus
135 b3c5e8de Iustin Pop
             xrunning xtags xauto_balance xpnode snode xdt
136 53ec9022 Iustin Pop
  return (xname, inst)
137 53ec9022 Iustin Pop
138 53ec9022 Iustin Pop
parseInstance _ v = fail ("Invalid instance query result: " ++ show v)
139 53ec9022 Iustin Pop
140 53ec9022 Iustin Pop
-- | Parse a node list in JSON format.
141 10ef6b4e Iustin Pop
getNodes :: NameAssoc -> JSValue -> Result [(String, Node.Node)]
142 10ef6b4e Iustin Pop
getNodes ktg arr = toArray arr >>= mapM (parseNode ktg)
143 53ec9022 Iustin Pop
144 53ec9022 Iustin Pop
-- | Construct a node from a JSON object.
145 10ef6b4e Iustin Pop
parseNode :: NameAssoc -> JSValue -> Result (String, Node.Node)
146 10ef6b4e Iustin Pop
parseNode ktg (JSArray [ name, mtotal, mnode, mfree, dtotal, dfree
147 10ef6b4e Iustin Pop
                       , ctotal, offline, drained, vm_capable, g_uuid ])
148 53ec9022 Iustin Pop
    = do
149 117dc2d8 Iustin Pop
  xname <- annotateResult "Parsing new node" (fromJVal name)
150 39420403 Iustin Pop
  let convert a = genericConvert "Node" xname a
151 39420403 Iustin Pop
  xoffline <- convert "offline" offline
152 39420403 Iustin Pop
  xdrained <- convert "drained" drained
153 39420403 Iustin Pop
  xvm_capable <- convert "vm_capable" vm_capable
154 39420403 Iustin Pop
  xgdx   <- convert "group.uuid" g_uuid >>= lookupGroup ktg xname
155 9d775204 Iustin Pop
  node <- (if xoffline || xdrained || not xvm_capable
156 10ef6b4e Iustin Pop
           then return $ Node.create xname 0 0 0 0 0 0 True xgdx
157 53ec9022 Iustin Pop
           else do
158 39420403 Iustin Pop
             xmtotal  <- convert "mtotal" mtotal
159 39420403 Iustin Pop
             xmnode   <- convert "mnode" mnode
160 39420403 Iustin Pop
             xmfree   <- convert "mfree" mfree
161 39420403 Iustin Pop
             xdtotal  <- convert "dtotal" dtotal
162 39420403 Iustin Pop
             xdfree   <- convert "dfree" dfree
163 39420403 Iustin Pop
             xctotal  <- convert "ctotal" ctotal
164 53ec9022 Iustin Pop
             return $ Node.create xname xmtotal xmnode xmfree
165 10ef6b4e Iustin Pop
                    xdtotal xdfree xctotal False xgdx)
166 53ec9022 Iustin Pop
  return (xname, node)
167 53ec9022 Iustin Pop
168 10ef6b4e Iustin Pop
parseNode _ v = fail ("Invalid node query result: " ++ show v)
169 53ec9022 Iustin Pop
170 f89235f1 Iustin Pop
getClusterTags :: JSValue -> Result [String]
171 f89235f1 Iustin Pop
getClusterTags v = do
172 f89235f1 Iustin Pop
  let errmsg = "Parsing cluster info"
173 f89235f1 Iustin Pop
  obj <- annotateResult errmsg $ asJSObject v
174 5182e970 Iustin Pop
  tryFromObj errmsg (fromJSObject obj) "tags"
175 f89235f1 Iustin Pop
176 a679e9dc Iustin Pop
getGroups :: JSValue -> Result [(String, Group.Group)]
177 a679e9dc Iustin Pop
getGroups arr = toArray arr >>= mapM parseGroup
178 a679e9dc Iustin Pop
179 a679e9dc Iustin Pop
parseGroup :: JSValue -> Result (String, Group.Group)
180 c4c37257 Iustin Pop
parseGroup (JSArray [ uuid, name, apol ]) = do
181 a679e9dc Iustin Pop
  xname <- annotateResult "Parsing new group" (fromJVal name)
182 39420403 Iustin Pop
  let convert a = genericConvert "Group" xname a
183 39420403 Iustin Pop
  xuuid <- convert "uuid" uuid
184 39420403 Iustin Pop
  xapol <- convert "alloc_policy" apol
185 d5072e4c Iustin Pop
  return (xuuid, Group.create xname xuuid xapol)
186 a679e9dc Iustin Pop
187 a679e9dc Iustin Pop
parseGroup v = fail ("Invalid group query result: " ++ show v)
188 a679e9dc Iustin Pop
189 53ec9022 Iustin Pop
-- * Main loader functionality
190 53ec9022 Iustin Pop
191 525bfb36 Iustin Pop
-- | Builds the cluster data by querying a given socket name.
192 b3f0710c Iustin Pop
readData :: String -- ^ Unix socket to use as source
193 a679e9dc Iustin Pop
         -> IO (Result JSValue, Result JSValue, Result JSValue, Result JSValue)
194 b3f0710c Iustin Pop
readData master =
195 53ec9022 Iustin Pop
  E.bracket
196 6583e677 Iustin Pop
       (L.getClient master)
197 6583e677 Iustin Pop
       L.closeClient
198 53ec9022 Iustin Pop
       (\s -> do
199 53ec9022 Iustin Pop
          nodes <- queryNodes s
200 53ec9022 Iustin Pop
          instances <- queryInstances s
201 f89235f1 Iustin Pop
          cinfo <- queryClusterInfo s
202 a679e9dc Iustin Pop
          groups <- queryGroups s
203 a679e9dc Iustin Pop
          return (groups, nodes, instances, cinfo)
204 53ec9022 Iustin Pop
       )
205 b3f0710c Iustin Pop
206 525bfb36 Iustin Pop
-- | Converts the output of 'readData' into the internal cluster
207 525bfb36 Iustin Pop
-- representation.
208 a679e9dc Iustin Pop
parseData :: (Result JSValue, Result JSValue, Result JSValue, Result JSValue)
209 f4f6eb0b Iustin Pop
          -> Result ClusterData
210 a679e9dc Iustin Pop
parseData (groups, nodes, instances, cinfo) = do
211 a679e9dc Iustin Pop
  group_data <- groups >>= getGroups
212 10ef6b4e Iustin Pop
  let (group_names, group_idx) = assignIndices group_data
213 10ef6b4e Iustin Pop
  node_data <- nodes >>= getNodes group_names
214 b3f0710c Iustin Pop
  let (node_names, node_idx) = assignIndices node_data
215 b3f0710c Iustin Pop
  inst_data <- instances >>= getInstances node_names
216 b3f0710c Iustin Pop
  let (_, inst_idx) = assignIndices inst_data
217 b3f0710c Iustin Pop
  ctags <- cinfo >>= getClusterTags
218 f4f6eb0b Iustin Pop
  return (ClusterData group_idx node_idx inst_idx ctags)
219 b3f0710c Iustin Pop
220 525bfb36 Iustin Pop
-- | Top level function for data loading.
221 b3f0710c Iustin Pop
loadData :: String -- ^ Unix socket to use as source
222 f4f6eb0b Iustin Pop
         -> IO (Result ClusterData)
223 2a8e2dc9 Iustin Pop
loadData = fmap parseData . readData