Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Rapi.hs @ d5072e4c

History | View | Annotate | Download (6.7 kB)

1 a7654563 Iustin Pop
{-| Implementation of the RAPI client interface.
2 a7654563 Iustin Pop
3 a7654563 Iustin Pop
-}
4 a7654563 Iustin Pop
5 e2fa2baf Iustin Pop
{-
6 e2fa2baf Iustin Pop
7 4d8e5008 Iustin Pop
Copyright (C) 2009, 2010, 2011 Google Inc.
8 e2fa2baf Iustin Pop
9 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
10 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
11 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
12 e2fa2baf Iustin Pop
(at your option) any later version.
13 e2fa2baf Iustin Pop
14 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
15 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
16 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 e2fa2baf Iustin Pop
General Public License for more details.
18 e2fa2baf Iustin Pop
19 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
20 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
21 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 e2fa2baf Iustin Pop
02110-1301, USA.
23 e2fa2baf Iustin Pop
24 e2fa2baf Iustin Pop
-}
25 e2fa2baf Iustin Pop
26 c9224fa4 Iustin Pop
{-# LANGUAGE BangPatterns #-}
27 c9224fa4 Iustin Pop
28 669d7e3d Iustin Pop
module Ganeti.HTools.Rapi
29 dd4c56ed Iustin Pop
    (
30 040afc35 Iustin Pop
      loadData
31 748bfcc2 Iustin Pop
    , parseData
32 dd4c56ed Iustin Pop
    ) where
33 a7654563 Iustin Pop
34 3986684e Iustin Pop
import Data.Maybe (fromMaybe)
35 a7654563 Iustin Pop
import Network.Curl
36 b8b9a53c Iustin Pop
import Network.Curl.Types ()
37 a7654563 Iustin Pop
import Control.Monad
38 ea017cbc Iustin Pop
import Text.JSON (JSObject, JSValue, fromJSObject, decodeStrict)
39 6402a260 Iustin Pop
import Text.JSON.Types (JSValue(..))
40 a7654563 Iustin Pop
import Text.Printf (printf)
41 040afc35 Iustin Pop
42 9ba5c28f Iustin Pop
import Ganeti.HTools.Utils
43 040afc35 Iustin Pop
import Ganeti.HTools.Loader
44 e4c5beaf Iustin Pop
import Ganeti.HTools.Types
45 a679e9dc Iustin Pop
import qualified Ganeti.HTools.Group as Group
46 040afc35 Iustin Pop
import qualified Ganeti.HTools.Node as Node
47 040afc35 Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
48 a7654563 Iustin Pop
49 4d8e5008 Iustin Pop
-- | The curl options we use
50 4d8e5008 Iustin Pop
curlOpts :: [CurlOption]
51 4d8e5008 Iustin Pop
curlOpts = [ CurlSSLVerifyPeer False
52 4d8e5008 Iustin Pop
           , CurlSSLVerifyHost 0
53 4d8e5008 Iustin Pop
           , CurlTimeout (fromIntegral queryTimeout)
54 4d8e5008 Iustin Pop
           , CurlConnectTimeout (fromIntegral connTimeout)
55 4d8e5008 Iustin Pop
           ]
56 4d8e5008 Iustin Pop
57 9188aeef Iustin Pop
-- | Read an URL via curl and return the body if successful.
58 ba00ad4d Iustin Pop
getUrl :: (Monad m) => String -> IO (m String)
59 a7654563 Iustin Pop
getUrl url = do
60 c9224fa4 Iustin Pop
  (code, !body) <- curlGetString url curlOpts
61 a7654563 Iustin Pop
  return (case code of
62 ba00ad4d Iustin Pop
            CurlOK -> return body
63 ba00ad4d Iustin Pop
            _ -> fail $ printf "Curl error for '%s', error %s"
64 aab26f2d Iustin Pop
                 url (show code))
65 aab26f2d Iustin Pop
66 9188aeef Iustin Pop
-- | Append the default port if not passed in.
67 e015b554 Iustin Pop
formatHost :: String -> String
68 e015b554 Iustin Pop
formatHost master =
69 5182e970 Iustin Pop
    if ':' `elem` master then  master
70 e015b554 Iustin Pop
    else "https://" ++ master ++ ":5080"
71 e015b554 Iustin Pop
72 9188aeef Iustin Pop
-- | Parse a instance list in JSON format.
73 040afc35 Iustin Pop
getInstances :: NameAssoc
74 040afc35 Iustin Pop
             -> String
75 040afc35 Iustin Pop
             -> Result [(String, Instance.Instance)]
76 262f3e6c Iustin Pop
getInstances ktn body =
77 c8b662f1 Iustin Pop
    loadJSArray "Parsing instance data" body >>=
78 c8b662f1 Iustin Pop
    mapM (parseInstance ktn . fromJSObject)
79 a7654563 Iustin Pop
80 9188aeef Iustin Pop
-- | Parse a node list in JSON format.
81 10ef6b4e Iustin Pop
getNodes :: NameAssoc -> String -> Result [(String, Node.Node)]
82 10ef6b4e Iustin Pop
getNodes ktg body = loadJSArray "Parsing node data" body >>=
83 10ef6b4e Iustin Pop
                mapM (parseNode ktg . fromJSObject)
84 a7654563 Iustin Pop
85 a679e9dc Iustin Pop
-- | Parse a group list in JSON format.
86 a679e9dc Iustin Pop
getGroups :: String -> Result [(String, Group.Group)]
87 a679e9dc Iustin Pop
getGroups body = loadJSArray "Parsing group data" body >>=
88 a679e9dc Iustin Pop
                mapM (parseGroup . fromJSObject)
89 a679e9dc Iustin Pop
90 fd3fe74d Iustin Pop
getFakeGroups :: Result [(String, Group.Group)]
91 fd3fe74d Iustin Pop
getFakeGroups =
92 d5072e4c Iustin Pop
  return [(defaultGroupID,
93 d5072e4c Iustin Pop
           Group.create "default" defaultGroupID AllocPreferred)]
94 fd3fe74d Iustin Pop
95 9188aeef Iustin Pop
-- | Construct an instance from a JSON object.
96 6ff78049 Iustin Pop
parseInstance :: NameAssoc
97 262f3e6c Iustin Pop
              -> [(String, JSValue)]
98 040afc35 Iustin Pop
              -> Result (String, Instance.Instance)
99 040afc35 Iustin Pop
parseInstance ktn a = do
100 117dc2d8 Iustin Pop
  name <- tryFromObj "Parsing new instance" a "name"
101 6402a260 Iustin Pop
  let owner_name = "Instance '" ++ name ++ "'"
102 6402a260 Iustin Pop
  let extract s x = tryFromObj owner_name x s
103 117dc2d8 Iustin Pop
  disk <- extract "disk_usage" a
104 117dc2d8 Iustin Pop
  beparams <- liftM fromJSObject (extract "beparams" a)
105 6402a260 Iustin Pop
  omem <- extract "oper_ram" a
106 6402a260 Iustin Pop
  mem <- (case omem of
107 6402a260 Iustin Pop
            JSRational _ _ -> annotateResult owner_name (fromJVal omem)
108 6402a260 Iustin Pop
            _ -> extract "memory" beparams)
109 117dc2d8 Iustin Pop
  vcpus <- extract "vcpus" beparams
110 117dc2d8 Iustin Pop
  pnode <- extract "pnode" a >>= lookupNode ktn name
111 117dc2d8 Iustin Pop
  snodes <- extract "snodes" a
112 040afc35 Iustin Pop
  snode <- (if null snodes then return Node.noSecondary
113 040afc35 Iustin Pop
            else readEitherString (head snodes) >>= lookupNode ktn name)
114 117dc2d8 Iustin Pop
  running <- extract "status" a
115 17e7af2b Iustin Pop
  tags <- extract "tags" a
116 17e7af2b Iustin Pop
  let inst = Instance.create name mem disk vcpus running tags pnode snode
117 040afc35 Iustin Pop
  return (name, inst)
118 a7654563 Iustin Pop
119 9188aeef Iustin Pop
-- | Construct a node from a JSON object.
120 10ef6b4e Iustin Pop
parseNode :: NameAssoc -> [(String, JSValue)] -> Result (String, Node.Node)
121 10ef6b4e Iustin Pop
parseNode ktg a = do
122 117dc2d8 Iustin Pop
  name <- tryFromObj "Parsing new node" a "name"
123 3986684e Iustin Pop
  let desc = "Node '" ++ name ++ "'"
124 3986684e Iustin Pop
      extract s = tryFromObj desc a s
125 117dc2d8 Iustin Pop
  offline <- extract "offline"
126 b45222ce Iustin Pop
  drained <- extract "drained"
127 3986684e Iustin Pop
  vm_cap  <- annotateResult desc $ maybeFromObj a "vm_capable"
128 3986684e Iustin Pop
  let vm_cap' = fromMaybe True vm_cap
129 3986684e Iustin Pop
  guuid   <- annotateResult desc $ maybeFromObj a "group.uuid"
130 3986684e Iustin Pop
  guuid' <-  lookupGroup ktg name (fromMaybe defaultGroupID guuid)
131 3986684e Iustin Pop
  node <- (if offline || drained || not vm_cap'
132 3986684e Iustin Pop
           then return $ Node.create name 0 0 0 0 0 0 True guuid'
133 262f3e6c Iustin Pop
           else do
134 117dc2d8 Iustin Pop
             mtotal  <- extract "mtotal"
135 117dc2d8 Iustin Pop
             mnode   <- extract "mnode"
136 117dc2d8 Iustin Pop
             mfree   <- extract "mfree"
137 117dc2d8 Iustin Pop
             dtotal  <- extract "dtotal"
138 117dc2d8 Iustin Pop
             dfree   <- extract "dfree"
139 117dc2d8 Iustin Pop
             ctotal  <- extract "ctotal"
140 262f3e6c Iustin Pop
             return $ Node.create name mtotal mnode mfree
141 3986684e Iustin Pop
                    dtotal dfree ctotal False guuid')
142 262f3e6c Iustin Pop
  return (name, node)
143 00b15752 Iustin Pop
144 a679e9dc Iustin Pop
-- | Construct a group from a JSON object.
145 a679e9dc Iustin Pop
parseGroup :: [(String, JSValue)] -> Result (String, Group.Group)
146 a679e9dc Iustin Pop
parseGroup a = do
147 a679e9dc Iustin Pop
  name <- tryFromObj "Parsing new group" a "name"
148 a679e9dc Iustin Pop
  let extract s = tryFromObj ("Group '" ++ name ++ "'") a s
149 a679e9dc Iustin Pop
  uuid <- extract "uuid"
150 2ddabf4f Iustin Pop
  apol <- extract "alloc_policy"
151 2ddabf4f Iustin Pop
  return (uuid, Group.create name uuid apol)
152 a679e9dc Iustin Pop
153 748bfcc2 Iustin Pop
-- | Loads the raw cluster data from an URL.
154 748bfcc2 Iustin Pop
readData :: String -- ^ Cluster or URL to use as source
155 a679e9dc Iustin Pop
         -> IO (Result String, Result String, Result String, Result String)
156 748bfcc2 Iustin Pop
readData master = do
157 040afc35 Iustin Pop
  let url = formatHost master
158 a679e9dc Iustin Pop
  group_body <- getUrl $ printf "%s/2/groups?bulk=1" url
159 040afc35 Iustin Pop
  node_body <- getUrl $ printf "%s/2/nodes?bulk=1" url
160 040afc35 Iustin Pop
  inst_body <- getUrl $ printf "%s/2/instances?bulk=1" url
161 ea017cbc Iustin Pop
  tags_body <- getUrl $ printf "%s/2/tags" url
162 a679e9dc Iustin Pop
  return (group_body, node_body, inst_body, tags_body)
163 748bfcc2 Iustin Pop
164 748bfcc2 Iustin Pop
-- | Builds the cluster data from the raw Rapi content
165 a679e9dc Iustin Pop
parseData :: (Result String, Result String, Result String, Result String)
166 f4f6eb0b Iustin Pop
          -> Result ClusterData
167 a679e9dc Iustin Pop
parseData (group_body, node_body, inst_body, tags_body) = do
168 fd3fe74d Iustin Pop
  group_data <-
169 fd3fe74d Iustin Pop
      -- TODO: handle different ganeti versions properly, not via "all
170 fd3fe74d Iustin Pop
      -- errors mean Ganeti 2.3"
171 fd3fe74d Iustin Pop
      case group_body of
172 fd3fe74d Iustin Pop
        Bad _ -> getFakeGroups
173 fd3fe74d Iustin Pop
        Ok v -> getGroups v
174 10ef6b4e Iustin Pop
  let (group_names, group_idx) = assignIndices group_data
175 10ef6b4e Iustin Pop
  node_data <- node_body >>= getNodes group_names
176 748bfcc2 Iustin Pop
  let (node_names, node_idx) = assignIndices node_data
177 748bfcc2 Iustin Pop
  inst_data <- inst_body >>= getInstances node_names
178 748bfcc2 Iustin Pop
  let (_, inst_idx) = assignIndices inst_data
179 748bfcc2 Iustin Pop
  tags_data <- tags_body >>= (fromJResult "Parsing tags data" . decodeStrict)
180 f4f6eb0b Iustin Pop
  return (ClusterData group_idx node_idx inst_idx tags_data)
181 748bfcc2 Iustin Pop
182 748bfcc2 Iustin Pop
-- | Top level function for data loading
183 748bfcc2 Iustin Pop
loadData :: String -- ^ Cluster or URL to use as source
184 f4f6eb0b Iustin Pop
         -> IO (Result ClusterData)
185 748bfcc2 Iustin Pop
loadData master = readData master >>= return . parseData