Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / IAlloc.hs @ 734b1ff1

History | View | Annotate | Download (5.3 kB)

1
{-| Implementation of the iallocator 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.IAlloc
27
    ( parseData
28
    , formatResponse
29
    ) where
30

    
31
import Data.Either ()
32
import Control.Monad
33
import Text.JSON (JSObject, JSValue(JSBool, JSString, JSArray),
34
                  makeObj, encodeStrict, decodeStrict,
35
                  fromJSObject, toJSString)
36
import qualified Ganeti.HTools.Container as Container
37
import qualified Ganeti.HTools.Node as Node
38
import qualified Ganeti.HTools.Instance as Instance
39
import Ganeti.HTools.Loader
40
import Ganeti.HTools.Utils
41
import Ganeti.HTools.Types
42

    
43
-- | Parse the basic specifications of an instance.
44
--
45
-- Instances in the cluster instance list and the instance in an
46
-- 'Allocate' request share some common properties, which are read by
47
-- this function.
48
parseBaseInstance :: String
49
                  -> JSObject JSValue
50
                  -> Result (String, Instance.Instance)
51
parseBaseInstance n a = do
52
  disk <- fromObj "disk_space_total" a
53
  mem <- fromObj "memory" a
54
  let running = "running"
55
  return $ (n, Instance.create n mem disk running 0 0)
56

    
57
-- | Parses an instance as found in the cluster instance list.
58
parseInstance :: NameAssoc        -- ^ The node name-to-index association list
59
              -> String           -- ^ The name of the instance
60
              -> JSObject JSValue -- ^ The JSON object
61
              -> Result (String, Instance.Instance)
62
parseInstance ktn n a = do
63
    base <- parseBaseInstance n a
64
    nodes <- fromObj "nodes" a
65
    pnode <- readEitherString $ head nodes
66
    pidx <- lookupNode ktn n pnode
67
    let snodes = tail nodes
68
    sidx <- (if null snodes then return Node.noSecondary
69
             else (readEitherString $ head snodes) >>= lookupNode ktn n)
70
    return (n, Instance.setBoth (snd base) pidx sidx)
71

    
72
-- | Parses a node as found in the cluster node list.
73
parseNode :: String           -- ^ The node's name
74
          -> JSObject JSValue -- ^ The JSON object
75
          -> Result (String, Node.Node)
76
parseNode n a = do
77
    let name = n
78
    offline <- fromObj "offline" a
79
    drained <- fromObj "drained" a
80
    node <- (case offline of
81
               True -> return $ Node.create name 0 0 0 0 0 True
82
               _ -> do
83
                 mtotal <- fromObj "total_memory" a
84
                 mnode <- fromObj "reserved_memory" a
85
                 mfree <- fromObj "free_memory" a
86
                 dtotal <- fromObj "total_disk" a
87
                 dfree <- fromObj "free_disk" a
88
                 return $ Node.create n mtotal mnode mfree
89
                        dtotal dfree (offline || drained))
90
    return (name, node)
91

    
92
-- | Top-level parser.
93
parseData :: String         -- ^ The JSON message as received from Ganeti
94
          -> Result Request -- ^ A (possible valid) request
95
parseData body = do
96
  decoded <- fromJResult $ decodeStrict body
97
  let obj = decoded
98
  -- request parser
99
  request <- fromObj "request" obj
100
  rname <- fromObj "name" request
101
  -- existing node parsing
102
  nlist <- fromObj "nodes" obj
103
  let ndata = fromJSObject nlist
104
  nobj <- (mapM (\(x,y) -> asJSObject y >>= parseNode x)) ndata
105
  let (ktn, nl) = assignIndices nobj
106
  -- existing instance parsing
107
  ilist <- fromObj "instances" obj
108
  let idata = fromJSObject ilist
109
  iobj <- (mapM (\(x,y) -> asJSObject y >>= parseInstance ktn x)) idata
110
  let (kti, il) = assignIndices iobj
111
  (map_n, map_i, csf) <- mergeData (nl, il)
112
  req_nodes <- fromObj "required_nodes" request
113
  optype <- fromObj "type" request
114
  rqtype <-
115
      case optype of
116
        "allocate" ->
117
            do
118
              inew <- parseBaseInstance rname request
119
              let io = snd inew
120
              return $ Allocate io req_nodes
121
        "relocate" ->
122
            do
123
              ridx <- lookupInstance kti rname
124
              ex_nodes <- fromObj "relocate_from" request
125
              let ex_nodes' = map (stripSuffix $ length csf) ex_nodes
126
              ex_idex <- mapM (Container.findByName map_n) ex_nodes'
127
              return $ Relocate ridx req_nodes ex_idex
128
        other -> fail $ ("Invalid request type '" ++ other ++ "'")
129
  return $ Request rqtype map_n map_i csf
130

    
131
-- | Formats the response into a valid IAllocator response message.
132
formatResponse :: Bool     -- ^ Whether the request was successful
133
               -> String   -- ^ Information text
134
               -> [String] -- ^ The list of chosen nodes
135
               -> String   -- ^ The JSON-formatted message
136
formatResponse success info nodes =
137
    let
138
        e_success = ("success", JSBool success)
139
        e_info = ("info", JSString . toJSString $ info)
140
        e_nodes = ("nodes", JSArray $ map (JSString . toJSString) nodes)
141
    in encodeStrict $ makeObj [e_success, e_info, e_nodes]