Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / IAlloc.hs @ cabce2f4

History | View | Annotate | Download (11.5 kB)

1
{-| Implementation of the iallocator interface.
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 2009, 2010, 2011 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
    , readRequest
30
    , processRequest
31
    , processResults
32
    ) where
33

    
34
import Data.Either ()
35
import Data.Maybe (fromMaybe, isJust, fromJust)
36
import Data.List
37
import Control.Monad
38
import Text.JSON (JSObject, JSValue(JSBool, JSString, JSArray),
39
                  makeObj, encodeStrict, decodeStrict,
40
                  fromJSObject, toJSString)
41
import System (exitWith, ExitCode(..))
42
import System.IO
43

    
44
import qualified Ganeti.HTools.Cluster as Cluster
45
import qualified Ganeti.HTools.Container as Container
46
import qualified Ganeti.HTools.Group as Group
47
import qualified Ganeti.HTools.Node as Node
48
import qualified Ganeti.HTools.Instance as Instance
49
import qualified Ganeti.Constants as C
50
import Ganeti.HTools.CLI
51
import Ganeti.HTools.Loader
52
import Ganeti.HTools.ExtLoader (loadExternalData)
53
import Ganeti.HTools.Utils
54
import Ganeti.HTools.Types
55

    
56
-- | Parse the basic specifications of an instance.
57
--
58
-- Instances in the cluster instance list and the instance in an
59
-- 'Allocate' request share some common properties, which are read by
60
-- this function.
61
parseBaseInstance :: String
62
                  -> JSRecord
63
                  -> Result (String, Instance.Instance)
64
parseBaseInstance n a = do
65
  let extract x = tryFromObj ("invalid data for instance '" ++ n ++ "'") a x
66
  disk  <- extract "disk_space_total"
67
  mem   <- extract "memory"
68
  vcpus <- extract "vcpus"
69
  tags  <- extract "tags"
70
  let running = "running"
71
  return (n, Instance.create n mem disk vcpus running tags True 0 0)
72

    
73
-- | Parses an instance as found in the cluster instance list.
74
parseInstance :: NameAssoc -- ^ The node name-to-index association list
75
              -> String    -- ^ The name of the instance
76
              -> JSRecord  -- ^ The JSON object
77
              -> Result (String, Instance.Instance)
78
parseInstance ktn n a = do
79
  base <- parseBaseInstance n a
80
  nodes <- fromObj a "nodes"
81
  pnode <- if null nodes
82
           then Bad $ "empty node list for instance " ++ n
83
           else readEitherString $ head nodes
84
  pidx <- lookupNode ktn n pnode
85
  let snodes = tail nodes
86
  sidx <- (if null snodes then return Node.noSecondary
87
           else readEitherString (head snodes) >>= lookupNode ktn n)
88
  return (n, Instance.setBoth (snd base) pidx sidx)
89

    
90
-- | Parses a node as found in the cluster node list.
91
parseNode :: NameAssoc   -- ^ The group association
92
          -> String      -- ^ The node's name
93
          -> JSRecord    -- ^ The JSON object
94
          -> Result (String, Node.Node)
95
parseNode ktg n a = do
96
  let desc = "invalid data for node '" ++ n ++ "'"
97
      extract x = tryFromObj desc a x
98
  offline <- extract "offline"
99
  drained <- extract "drained"
100
  guuid   <- extract "group"
101
  vm_capable  <- annotateResult desc $ maybeFromObj a "vm_capable"
102
  let vm_capable' = fromMaybe True vm_capable
103
  gidx <- lookupGroup ktg n guuid
104
  node <- (if offline || drained || not vm_capable'
105
           then return $ Node.create n 0 0 0 0 0 0 True gidx
106
           else do
107
             mtotal <- extract "total_memory"
108
             mnode  <- extract "reserved_memory"
109
             mfree  <- extract "free_memory"
110
             dtotal <- extract "total_disk"
111
             dfree  <- extract "free_disk"
112
             ctotal <- extract "total_cpus"
113
             return $ Node.create n mtotal mnode mfree
114
                    dtotal dfree ctotal False gidx)
115
  return (n, node)
116

    
117
-- | Parses a group as found in the cluster group list.
118
parseGroup :: String     -- ^ The group UUID
119
           -> JSRecord   -- ^ The JSON object
120
           -> Result (String, Group.Group)
121
parseGroup u a = do
122
  let extract x = tryFromObj ("invalid data for group '" ++ u ++ "'") a x
123
  name <- extract "name"
124
  apol <- extract "alloc_policy"
125
  return (u, Group.create name u apol)
126

    
127
parseTargetGroups :: JSRecord      -- ^ The JSON object (request dict)
128
                  -> Group.List    -- ^ The existing groups
129
                  -> Result [Gdx]
130
parseTargetGroups req map_g = do
131
  group_uuids <- fromObjWithDefault req "target_groups" []
132
  mapM (liftM Group.idx . Container.findByName map_g) group_uuids
133

    
134
-- | Top-level parser.
135
parseData :: String         -- ^ The JSON message as received from Ganeti
136
          -> Result Request -- ^ A (possible valid) request
137
parseData body = do
138
  decoded <- fromJResult "Parsing input IAllocator message" (decodeStrict body)
139
  let obj = fromJSObject decoded
140
      extrObj x = tryFromObj "invalid iallocator message" obj x
141
  -- request parser
142
  request <- liftM fromJSObject (extrObj "request")
143
  let extrReq x = tryFromObj "invalid request dict" request x
144
  -- existing group parsing
145
  glist <- liftM fromJSObject (extrObj "nodegroups")
146
  gobj <- mapM (\(x, y) -> asJSObject y >>= parseGroup x . fromJSObject) glist
147
  let (ktg, gl) = assignIndices gobj
148
  -- existing node parsing
149
  nlist <- liftM fromJSObject (extrObj "nodes")
150
  nobj <- mapM (\(x,y) ->
151
                    asJSObject y >>= parseNode ktg x . fromJSObject) nlist
152
  let (ktn, nl) = assignIndices nobj
153
  -- existing instance parsing
154
  ilist <- extrObj "instances"
155
  let idata = fromJSObject ilist
156
  iobj <- mapM (\(x,y) ->
157
                    asJSObject y >>= parseInstance ktn x . fromJSObject) idata
158
  let (kti, il) = assignIndices iobj
159
  -- cluster tags
160
  ctags <- extrObj "cluster_tags"
161
  cdata <- mergeData [] [] [] [] (ClusterData gl nl il ctags)
162
  let map_n = cdNodes cdata
163
      map_i = cdInstances cdata
164
      map_g = cdGroups cdata
165
  optype <- extrReq "type"
166
  rqtype <-
167
      case () of
168
        _ | optype == C.iallocatorModeAlloc ->
169
              do
170
                rname     <- extrReq "name"
171
                req_nodes <- extrReq "required_nodes"
172
                inew      <- parseBaseInstance rname request
173
                let io = snd inew
174
                return $ Allocate io req_nodes
175
          | optype == C.iallocatorModeReloc ->
176
              do
177
                rname     <- extrReq "name"
178
                ridx      <- lookupInstance kti rname
179
                req_nodes <- extrReq "required_nodes"
180
                ex_nodes  <- extrReq "relocate_from"
181
                ex_idex   <- mapM (Container.findByName map_n) ex_nodes
182
                return $ Relocate ridx req_nodes (map Node.idx ex_idex)
183
          | optype == C.iallocatorModeMevac ->
184
              do
185
                ex_names <- extrReq "evac_nodes"
186
                ex_nodes <- mapM (Container.findByName map_n) ex_names
187
                let ex_ndx = map Node.idx ex_nodes
188
                return $ Evacuate ex_ndx
189
          | optype == C.iallocatorModeMreloc ->
190
              do
191
                rl_names <- extrReq "instances"
192
                rl_insts <- mapM (Container.findByName map_i) rl_names
193
                let rl_idx = map Instance.idx rl_insts
194
                rl_mode <-
195
                   case extrReq "reloc_mode" of
196
                     Ok s | s == C.iallocatorMrelocKeep -> return KeepGroup
197
                          | s == C.iallocatorMrelocChange ->
198
                              do
199
                                tg_groups <- parseTargetGroups request map_g
200
                                return $ ChangeGroup tg_groups
201
                          | s == C.iallocatorMrelocAny -> return AnyGroup
202
                          | otherwise -> Bad $ "Invalid relocate mode " ++ s
203
                     Bad x -> Bad x
204
                return $ MultiReloc rl_idx rl_mode
205
          | optype == C.iallocatorModeNodeEvac ->
206
              do
207
                rl_names <- extrReq "instances"
208
                rl_insts <- mapM (Container.findByName map_i) rl_names
209
                let rl_idx = map Instance.idx rl_insts
210
                rl_mode <-
211
                   case extrReq "evac_mode" of
212
                     Ok s | s == C.iallocatorNevacAll -> return ChangeAll
213
                          | s == C.iallocatorNevacPri -> return ChangePrimary
214
                          | s == C.iallocatorNevacSec -> return ChangeSecondary
215
                          | otherwise -> Bad $ "Invalid evacuate mode " ++ s
216
                     Bad x -> Bad x
217
                return $ NodeEvacuate rl_idx rl_mode
218

    
219
          | otherwise -> fail ("Invalid request type '" ++ optype ++ "'")
220
  return $ Request rqtype cdata
221

    
222
-- | Format the result
223
formatRVal :: RqType -> [Node.AllocElement] -> JSValue
224
formatRVal _ [] = JSArray []
225

    
226
formatRVal (Evacuate _) elems =
227
    let sols = map (\(_, inst, nl, _) -> Instance.name inst : map Node.name nl)
228
               elems
229
        jsols = map (JSArray . map (JSString . toJSString)) sols
230
    in JSArray jsols
231

    
232
formatRVal _ elems =
233
    let (_, _, nodes, _) = head elems
234
        nodes' = map Node.name nodes
235
    in JSArray $ map (JSString . toJSString) nodes'
236

    
237
-- | Formats the response into a valid IAllocator response message.
238
formatResponse :: Bool     -- ^ Whether the request was successful
239
               -> String   -- ^ Information text
240
               -> RqType   -- ^ Request type
241
               -> [Node.AllocElement] -- ^ The resulting allocations
242
               -> String   -- ^ The JSON-formatted message
243
formatResponse success info rq elems =
244
    let
245
        e_success = ("success", JSBool success)
246
        e_info = ("info", JSString . toJSString $ info)
247
        e_result = ("result", formatRVal rq elems)
248
    in encodeStrict $ makeObj [e_success, e_info, e_result]
249

    
250
processResults :: (Monad m) =>
251
                  RqType -> Cluster.AllocSolution
252
               -> m Cluster.AllocSolution
253
processResults _ (Cluster.AllocSolution { Cluster.asSolutions = [],
254
                                          Cluster.asLog = msgs }) =
255
  fail $ intercalate ", " msgs
256

    
257
processResults (Evacuate _) as = return as
258

    
259
processResults _ as =
260
    case Cluster.asSolutions as of
261
      _:[] -> return as
262
      _ -> fail "Internal error: multiple allocation solutions"
263

    
264
-- | Process a request and return new node lists
265
processRequest :: Request
266
               -> Result Cluster.AllocSolution
267
processRequest request =
268
  let Request rqtype (ClusterData gl nl il _) = request
269
  in case rqtype of
270
       Allocate xi reqn -> Cluster.tryMGAlloc gl nl il xi reqn
271
       Relocate idx reqn exnodes -> Cluster.tryMGReloc gl nl il
272
                                    idx reqn exnodes
273
       Evacuate exnodes -> Cluster.tryMGEvac gl nl il exnodes
274
       MultiReloc _ _ -> fail "multi-reloc not handled"
275
       NodeEvacuate _ _ -> fail "node-evacuate not handled"
276

    
277
-- | Reads the request from the data file(s)
278
readRequest :: Options -> [String] -> IO Request
279
readRequest opts args = do
280
  when (null args) $ do
281
         hPutStrLn stderr "Error: this program needs an input file."
282
         exitWith $ ExitFailure 1
283

    
284
  input_data <- readFile (head args)
285
  r1 <- case parseData input_data of
286
          Bad err -> do
287
            hPutStrLn stderr $ "Error: " ++ err
288
            exitWith $ ExitFailure 1
289
          Ok rq -> return rq
290
  (if isJust (optDataFile opts) ||  (not . null . optNodeSim) opts
291
   then do
292
     cdata <- loadExternalData opts
293
     let Request rqt _ = r1
294
     return $ Request rqt cdata
295
   else return r1)