IAlloc.hs 6.35 KB
Newer Older
1
2
3
4
{-| Implementation of the iallocator interface.

-}

Iustin Pop's avatar
Iustin Pop committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{-

Copyright (C) 2009 Google Inc.

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

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

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

-}

26
module Ganeti.HTools.IAlloc
27
    ( parseData
28
29
30
31
32
    , formatResponse
    ) where

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

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

59
-- | Parses an instance as found in the cluster instance listg.
Iustin Pop's avatar
Iustin Pop committed
60
61
parseInstance :: NameAssoc        -- ^ The node name-to-index association list
              -> String           -- ^ The name of the instance
62
              -> [(String, JSValue)] -- ^ The JSON object
Iustin Pop's avatar
Iustin Pop committed
63
64
              -> Result (String, Instance.Instance)
parseInstance ktn n a = do
65
66
  base <- parseBaseInstance n a
  nodes <- fromObj "nodes" a
67
68
69
  pnode <- if null nodes
           then Bad $ "empty node list for instance " ++ n
           else readEitherString $ head nodes
70
71
72
73
74
  pidx <- lookupNode ktn n pnode
  let snodes = tail nodes
  sidx <- (if null snodes then return Node.noSecondary
           else readEitherString (head snodes) >>= lookupNode ktn n)
  return (n, Instance.setBoth (snd base) pidx sidx)
Iustin Pop's avatar
Iustin Pop committed
75

Iustin Pop's avatar
Iustin Pop committed
76
77
-- | Parses a node as found in the cluster node list.
parseNode :: String           -- ^ The node's name
78
          -> [(String, JSValue)] -- ^ The JSON object
Iustin Pop's avatar
Iustin Pop committed
79
          -> Result (String, Node.Node)
Iustin Pop's avatar
Iustin Pop committed
80
parseNode n a = do
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  offline <- fromObj "offline" a
  drained <- fromObj "drained" a
  node <- (if offline || drained
           then return $ Node.create n 0 0 0 0 0 0 True
           else do
             mtotal <- fromObj "total_memory" a
             mnode  <- fromObj "reserved_memory" a
             mfree  <- fromObj "free_memory"  a
             dtotal <- fromObj "total_disk"   a
             dfree  <- fromObj "free_disk"    a
             ctotal <- fromObj "total_cpus"   a
             return $ Node.create n mtotal mnode mfree
                    dtotal dfree ctotal False)
  return (n, node)
95

Iustin Pop's avatar
Iustin Pop committed
96
97
98
-- | Top-level parser.
parseData :: String         -- ^ The JSON message as received from Ganeti
          -> Result Request -- ^ A (possible valid) request
Iustin Pop's avatar
Iustin Pop committed
99
100
parseData body = do
  decoded <- fromJResult $ decodeStrict body
101
  let obj = fromJSObject decoded
Iustin Pop's avatar
Iustin Pop committed
102
  -- request parser
103
  request <- liftM fromJSObject (fromObj "request" obj)
Iustin Pop's avatar
Iustin Pop committed
104
  -- existing node parsing
105
106
  nlist <- liftM fromJSObject (fromObj "nodes" obj)
  nobj <- mapM (\(x,y) -> asJSObject y >>= parseNode x . fromJSObject) nlist
107
  let (ktn, nl) = assignIndices nobj
Iustin Pop's avatar
Iustin Pop committed
108
109
110
  -- existing instance parsing
  ilist <- fromObj "instances" obj
  let idata = fromJSObject ilist
111
112
  iobj <- mapM (\(x,y) ->
                    asJSObject y >>= parseInstance ktn x . fromJSObject) idata
113
  let (kti, il) = assignIndices iobj
114
115
  -- cluster tags
  ctags <- fromObj "cluster_tags" obj
116
  (map_n, map_i, ptags) <- mergeData [] [] [] (nl, il, ctags)
Iustin Pop's avatar
Iustin Pop committed
117
118
119
120
121
  optype <- fromObj "type" request
  rqtype <-
      case optype of
        "allocate" ->
            do
122
123
              rname <- fromObj "name" request
              req_nodes <- fromObj "required_nodes" request
Iustin Pop's avatar
Iustin Pop committed
124
              inew <- parseBaseInstance rname request
125
126
              let io = snd inew
              return $ Allocate io req_nodes
Iustin Pop's avatar
Iustin Pop committed
127
128
        "relocate" ->
            do
129
              rname <- fromObj "name" request
130
              ridx <- lookupInstance kti rname
131
              req_nodes <- fromObj "required_nodes" request
132
              ex_nodes <- fromObj "relocate_from" request
133
              ex_idex <- mapM (Container.findByName map_n) ex_nodes
134
              return $ Relocate ridx req_nodes (map Node.idx ex_idex)
135
136
137
        "multi-evacuate" ->
            do
              ex_names <- fromObj "evac_nodes" request
138
              ex_nodes <- mapM (Container.findByName map_n) ex_names
139
140
              let ex_ndx = map Node.idx ex_nodes
              return $ Evacuate ex_ndx
Iustin Pop's avatar
Iustin Pop committed
141
        other -> fail ("Invalid request type '" ++ other ++ "'")
142
  return $ Request rqtype map_n map_i ptags
143

144
-- | Format the result
145
146
formatRVal :: RqType -> [Node.AllocElement] -> JSValue
formatRVal _ [] = JSArray []
147

148
149
150
formatRVal (Evacuate _) elems =
    let sols = map (\(_, inst, nl) -> Instance.name inst : map Node.name nl)
               elems
151
152
153
        jsols = map (JSArray . map (JSString . toJSString)) sols
    in JSArray jsols

154
formatRVal _ elems =
155
    let (_, _, nodes) = head elems
156
        nodes' = map Node.name nodes
157
158
    in JSArray $ map (JSString . toJSString) nodes'

Iustin Pop's avatar
Iustin Pop committed
159
160
161
-- | Formats the response into a valid IAllocator response message.
formatResponse :: Bool     -- ^ Whether the request was successful
               -> String   -- ^ Information text
162
163
               -> RqType   -- ^ Request type
               -> [Node.AllocElement] -- ^ The resulting allocations
Iustin Pop's avatar
Iustin Pop committed
164
               -> String   -- ^ The JSON-formatted message
165
formatResponse success info rq elems =
166
167
168
    let
        e_success = ("success", JSBool success)
        e_info = ("info", JSString . toJSString $ info)
169
        e_nodes = ("nodes", formatRVal rq elems)
170
    in encodeStrict $ makeObj [e_success, e_info, e_nodes]