Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / Luxi.hs @ ea017cbc

History | View | Annotate | Download (5.4 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 53ec9022 Iustin Pop
Copyright (C) 2009 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 53ec9022 Iustin Pop
    ) where
30 53ec9022 Iustin Pop
31 53ec9022 Iustin Pop
import Data.List
32 53ec9022 Iustin Pop
import qualified Control.Exception as E
33 53ec9022 Iustin Pop
import Control.Monad
34 53ec9022 Iustin Pop
import Text.JSON.Types
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 53ec9022 Iustin Pop
import qualified Ganeti.HTools.Node as Node
40 53ec9022 Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
41 117dc2d8 Iustin Pop
import Ganeti.HTools.Utils (fromJVal, annotateResult)
42 53ec9022 Iustin Pop
43 53ec9022 Iustin Pop
-- * Utility functions
44 53ec9022 Iustin Pop
45 53ec9022 Iustin Pop
-- | Ensure a given JSValue is actually a JSArray.
46 53ec9022 Iustin Pop
toArray :: (Monad m) => JSValue -> m [JSValue]
47 53ec9022 Iustin Pop
toArray v =
48 53ec9022 Iustin Pop
    case v of
49 53ec9022 Iustin Pop
      JSArray arr -> return arr
50 53ec9022 Iustin Pop
      o -> fail ("Invalid input, expected array but got " ++ show o)
51 53ec9022 Iustin Pop
52 53ec9022 Iustin Pop
-- * Data querying functionality
53 53ec9022 Iustin Pop
54 53ec9022 Iustin Pop
-- | The input data for node query.
55 53ec9022 Iustin Pop
queryNodesMsg :: JSValue
56 53ec9022 Iustin Pop
queryNodesMsg =
57 53ec9022 Iustin Pop
    let nnames = JSArray []
58 53ec9022 Iustin Pop
        fnames = ["name",
59 53ec9022 Iustin Pop
                  "mtotal", "mnode", "mfree",
60 53ec9022 Iustin Pop
                  "dtotal", "dfree",
61 53ec9022 Iustin Pop
                  "ctotal",
62 53ec9022 Iustin Pop
                  "offline", "drained"]
63 53ec9022 Iustin Pop
        fields = JSArray $ map (JSString . toJSString) fnames
64 53ec9022 Iustin Pop
        use_locking = JSBool False
65 53ec9022 Iustin Pop
    in JSArray [nnames, fields, use_locking]
66 53ec9022 Iustin Pop
67 53ec9022 Iustin Pop
-- | The input data for instance query.
68 53ec9022 Iustin Pop
queryInstancesMsg :: JSValue
69 53ec9022 Iustin Pop
queryInstancesMsg =
70 53ec9022 Iustin Pop
    let nnames = JSArray []
71 53ec9022 Iustin Pop
        fnames = ["name",
72 53ec9022 Iustin Pop
                  "disk_usage", "be/memory", "be/vcpus",
73 17e7af2b Iustin Pop
                  "status", "pnode", "snodes", "tags"]
74 53ec9022 Iustin Pop
        fields = JSArray $ map (JSString . toJSString) fnames
75 53ec9022 Iustin Pop
        use_locking = JSBool False
76 53ec9022 Iustin Pop
    in JSArray [nnames, fields, use_locking]
77 53ec9022 Iustin Pop
78 53ec9022 Iustin Pop
-- | Wraper over callMethod doing node query.
79 6583e677 Iustin Pop
queryNodes :: L.Client -> IO (Result JSValue)
80 6583e677 Iustin Pop
queryNodes = L.callMethod L.QueryNodes queryNodesMsg
81 53ec9022 Iustin Pop
82 53ec9022 Iustin Pop
-- | Wraper over callMethod doing instance query.
83 6583e677 Iustin Pop
queryInstances :: L.Client -> IO (Result JSValue)
84 6583e677 Iustin Pop
queryInstances = L.callMethod L.QueryInstances queryInstancesMsg
85 53ec9022 Iustin Pop
86 53ec9022 Iustin Pop
-- | Parse a instance list in JSON format.
87 53ec9022 Iustin Pop
getInstances :: NameAssoc
88 53ec9022 Iustin Pop
             -> JSValue
89 53ec9022 Iustin Pop
             -> Result [(String, Instance.Instance)]
90 53ec9022 Iustin Pop
getInstances ktn arr = toArray arr >>= mapM (parseInstance ktn)
91 53ec9022 Iustin Pop
92 53ec9022 Iustin Pop
-- | Construct an instance from a JSON object.
93 53ec9022 Iustin Pop
parseInstance :: [(String, Ndx)]
94 53ec9022 Iustin Pop
              -> JSValue
95 53ec9022 Iustin Pop
              -> Result (String, Instance.Instance)
96 27671a61 Iustin Pop
parseInstance ktn (JSArray [ name, disk, mem, vcpus
97 17e7af2b Iustin Pop
                           , status, pnode, snodes, tags ]) = do
98 117dc2d8 Iustin Pop
  xname <- annotateResult "Parsing new instance" (fromJVal name)
99 117dc2d8 Iustin Pop
  let convert v = annotateResult ("Instance '" ++ xname ++ "'") (fromJVal v)
100 117dc2d8 Iustin Pop
  xdisk <- convert disk
101 117dc2d8 Iustin Pop
  xmem <- convert mem
102 117dc2d8 Iustin Pop
  xvcpus <- convert vcpus
103 117dc2d8 Iustin Pop
  xpnode <- convert pnode >>= lookupNode ktn xname
104 117dc2d8 Iustin Pop
  xsnodes <- convert snodes::Result [JSString]
105 53ec9022 Iustin Pop
  snode <- (if null xsnodes then return Node.noSecondary
106 53ec9022 Iustin Pop
            else lookupNode ktn xname (fromJSString $ head xsnodes))
107 117dc2d8 Iustin Pop
  xrunning <- convert status
108 17e7af2b Iustin Pop
  xtags <- convert tags
109 17e7af2b Iustin Pop
  let inst = Instance.create xname xmem xdisk xvcpus
110 17e7af2b Iustin Pop
             xrunning xtags xpnode snode
111 53ec9022 Iustin Pop
  return (xname, inst)
112 53ec9022 Iustin Pop
113 53ec9022 Iustin Pop
parseInstance _ v = fail ("Invalid instance query result: " ++ show v)
114 53ec9022 Iustin Pop
115 53ec9022 Iustin Pop
-- | Parse a node list in JSON format.
116 53ec9022 Iustin Pop
getNodes :: JSValue -> Result [(String, Node.Node)]
117 53ec9022 Iustin Pop
getNodes arr = toArray arr >>= mapM parseNode
118 53ec9022 Iustin Pop
119 53ec9022 Iustin Pop
-- | Construct a node from a JSON object.
120 53ec9022 Iustin Pop
parseNode :: JSValue -> Result (String, Node.Node)
121 27671a61 Iustin Pop
parseNode (JSArray [ name, mtotal, mnode, mfree, dtotal, dfree
122 27671a61 Iustin Pop
                   , ctotal, offline, drained ])
123 53ec9022 Iustin Pop
    = do
124 117dc2d8 Iustin Pop
  xname <- annotateResult "Parsing new node" (fromJVal name)
125 117dc2d8 Iustin Pop
  let convert v = annotateResult ("Node '" ++ xname ++ "'") (fromJVal v)
126 117dc2d8 Iustin Pop
  xoffline <- convert offline
127 53ec9022 Iustin Pop
  node <- (if xoffline
128 53ec9022 Iustin Pop
           then return $ Node.create xname 0 0 0 0 0 0 True
129 53ec9022 Iustin Pop
           else do
130 117dc2d8 Iustin Pop
             xdrained <- convert drained
131 117dc2d8 Iustin Pop
             xmtotal  <- convert mtotal
132 117dc2d8 Iustin Pop
             xmnode   <- convert mnode
133 117dc2d8 Iustin Pop
             xmfree   <- convert mfree
134 117dc2d8 Iustin Pop
             xdtotal  <- convert dtotal
135 117dc2d8 Iustin Pop
             xdfree   <- convert dfree
136 117dc2d8 Iustin Pop
             xctotal  <- convert ctotal
137 53ec9022 Iustin Pop
             return $ Node.create xname xmtotal xmnode xmfree
138 53ec9022 Iustin Pop
                    xdtotal xdfree xctotal (xoffline || xdrained))
139 53ec9022 Iustin Pop
  return (xname, node)
140 53ec9022 Iustin Pop
141 53ec9022 Iustin Pop
parseNode v = fail ("Invalid node query result: " ++ show v)
142 53ec9022 Iustin Pop
143 53ec9022 Iustin Pop
-- * Main loader functionality
144 53ec9022 Iustin Pop
145 53ec9022 Iustin Pop
-- | Builds the cluster data from an URL.
146 53ec9022 Iustin Pop
loadData :: String -- ^ Unix socket to use as source
147 94e05c32 Iustin Pop
         -> IO (Result (Node.AssocList, Instance.AssocList, [String]))
148 2485487d Iustin Pop
loadData master =
149 53ec9022 Iustin Pop
  E.bracket
150 6583e677 Iustin Pop
       (L.getClient master)
151 6583e677 Iustin Pop
       L.closeClient
152 53ec9022 Iustin Pop
       (\s -> do
153 53ec9022 Iustin Pop
          nodes <- queryNodes s
154 53ec9022 Iustin Pop
          instances <- queryInstances s
155 53ec9022 Iustin Pop
          return $ do -- Result monad
156 53ec9022 Iustin Pop
            node_data <- nodes >>= getNodes
157 53ec9022 Iustin Pop
            let (node_names, node_idx) = assignIndices node_data
158 53ec9022 Iustin Pop
            inst_data <- instances >>= getInstances node_names
159 53ec9022 Iustin Pop
            let (_, inst_idx) = assignIndices inst_data
160 94e05c32 Iustin Pop
            return (node_idx, inst_idx, [])
161 53ec9022 Iustin Pop
       )