Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / Rapi.hs @ 8bcdde0c

History | View | Annotate | Download (4.8 kB)

1
{-| Implementation of the RAPI client interface.
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 2009 Google Inc.
8

    
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
13

    
14
This program is distributed in the hope that it will be useful, but
15
WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
General Public License for more details.
18

    
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
02110-1301, USA.
23

    
24
-}
25

    
26
module Ganeti.HTools.Rapi
27
    (
28
      loadData
29
    ) where
30

    
31
import Network.Curl
32
import Network.Curl.Types ()
33
import Control.Monad
34
import Text.JSON (JSObject, JSValue, fromJSObject, decodeStrict)
35
import Text.JSON.Types (JSValue(..))
36
import Text.Printf (printf)
37

    
38
import Ganeti.HTools.Utils
39
import Ganeti.HTools.Loader
40
import Ganeti.HTools.Types
41
import qualified Ganeti.HTools.Node as Node
42
import qualified Ganeti.HTools.Instance as Instance
43

    
44
-- | Read an URL via curl and return the body if successful.
45
getUrl :: (Monad m) => String -> IO (m String)
46
getUrl url = do
47
  (code, body) <- curlGetString url [CurlSSLVerifyPeer False,
48
                                     CurlSSLVerifyHost 0,
49
                                     CurlTimeout (fromIntegral queryTimeout),
50
                                     CurlConnectTimeout
51
                                     (fromIntegral connTimeout)]
52
  return (case code of
53
            CurlOK -> return body
54
            _ -> fail $ printf "Curl error for '%s', error %s"
55
                 url (show code))
56

    
57
-- | Append the default port if not passed in.
58
formatHost :: String -> String
59
formatHost master =
60
    if ':' `elem` master then  master
61
    else "https://" ++ master ++ ":5080"
62

    
63
-- | Parse a instance list in JSON format.
64
getInstances :: NameAssoc
65
             -> String
66
             -> Result [(String, Instance.Instance)]
67
getInstances ktn body =
68
    loadJSArray body >>= mapM (parseInstance ktn . fromJSObject)
69

    
70
-- | Parse a node list in JSON format.
71
getNodes :: String -> Result [(String, Node.Node)]
72
getNodes body = loadJSArray body >>= mapM (parseNode . fromJSObject)
73

    
74
-- | Construct an instance from a JSON object.
75
parseInstance :: [(String, Ndx)]
76
              -> [(String, JSValue)]
77
              -> Result (String, Instance.Instance)
78
parseInstance ktn a = do
79
  name <- tryFromObj "Parsing new instance" a "name"
80
  let owner_name = "Instance '" ++ name ++ "'"
81
  let extract s x = tryFromObj owner_name x s
82
  disk <- extract "disk_usage" a
83
  beparams <- liftM fromJSObject (extract "beparams" a)
84
  omem <- extract "oper_ram" a
85
  mem <- (case omem of
86
            JSRational _ _ -> annotateResult owner_name (fromJVal omem)
87
            _ -> extract "memory" beparams)
88
  vcpus <- extract "vcpus" beparams
89
  pnode <- extract "pnode" a >>= lookupNode ktn name
90
  snodes <- extract "snodes" a
91
  snode <- (if null snodes then return Node.noSecondary
92
            else readEitherString (head snodes) >>= lookupNode ktn name)
93
  running <- extract "status" a
94
  tags <- extract "tags" a
95
  let inst = Instance.create name mem disk vcpus running tags pnode snode
96
  return (name, inst)
97

    
98
-- | Construct a node from a JSON object.
99
parseNode :: [(String, JSValue)] -> Result (String, Node.Node)
100
parseNode a = do
101
  name <- tryFromObj "Parsing new node" a "name"
102
  let extract s = tryFromObj ("Node '" ++ name ++ "'") a s
103
  offline <- extract "offline"
104
  drained <- extract "drained"
105
  node <- (if offline || drained
106
           then return $ Node.create name 0 0 0 0 0 0 True
107
           else do
108
             mtotal  <- extract "mtotal"
109
             mnode   <- extract "mnode"
110
             mfree   <- extract "mfree"
111
             dtotal  <- extract "dtotal"
112
             dfree   <- extract "dfree"
113
             ctotal  <- extract "ctotal"
114
             return $ Node.create name mtotal mnode mfree
115
                    dtotal dfree ctotal False)
116
  return (name, node)
117

    
118
-- | Builds the cluster data from an URL.
119
loadData :: String -- ^ Cluster or URL to use as source
120
         -> IO (Result (Node.AssocList, Instance.AssocList, [String]))
121
loadData master = do -- IO monad
122
  let url = formatHost master
123
  node_body <- getUrl $ printf "%s/2/nodes?bulk=1" url
124
  inst_body <- getUrl $ printf "%s/2/instances?bulk=1" url
125
  tags_body <- getUrl $ printf "%s/2/tags" url
126
  return $ do -- Result monad
127
    node_data <- node_body >>= getNodes
128
    let (node_names, node_idx) = assignIndices node_data
129
    inst_data <- inst_body >>= getInstances node_names
130
    let (_, inst_idx) = assignIndices inst_data
131
    tags_data <- tags_body >>= (fromJResult . decodeStrict)
132
    return (node_idx, inst_idx, tags_data)