Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (13.1 kB)

1 43643696 Iustin Pop
{-| Implementation of the iallocator interface.
2 43643696 Iustin Pop
3 43643696 Iustin Pop
-}
4 43643696 Iustin Pop
5 e2fa2baf Iustin Pop
{-
6 e2fa2baf Iustin Pop
7 e8230242 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 43643696 Iustin Pop
module Ganeti.HTools.IAlloc
27 00152519 Iustin Pop
    ( readRequest
28 00152519 Iustin Pop
    , runIAllocator
29 43643696 Iustin Pop
    ) where
30 43643696 Iustin Pop
31 43643696 Iustin Pop
import Data.Either ()
32 00152519 Iustin Pop
import Data.Maybe (fromMaybe, isJust)
33 cabce2f4 Iustin Pop
import Data.List
34 43643696 Iustin Pop
import Control.Monad
35 34c5a24a Iustin Pop
import Text.JSON (JSObject, JSValue(JSArray),
36 34c5a24a Iustin Pop
                  makeObj, encodeStrict, decodeStrict, fromJSObject, showJSON)
37 cabce2f4 Iustin Pop
import System (exitWith, ExitCode(..))
38 cabce2f4 Iustin Pop
import System.IO
39 cabce2f4 Iustin Pop
40 cabce2f4 Iustin Pop
import qualified Ganeti.HTools.Cluster as Cluster
41 262a08a2 Iustin Pop
import qualified Ganeti.HTools.Container as Container
42 a679e9dc Iustin Pop
import qualified Ganeti.HTools.Group as Group
43 942403e6 Iustin Pop
import qualified Ganeti.HTools.Node as Node
44 942403e6 Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
45 df5227dc Iustin Pop
import qualified Ganeti.Constants as C
46 cabce2f4 Iustin Pop
import Ganeti.HTools.CLI
47 e4c5beaf Iustin Pop
import Ganeti.HTools.Loader
48 cabce2f4 Iustin Pop
import Ganeti.HTools.ExtLoader (loadExternalData)
49 e4c5beaf Iustin Pop
import Ganeti.HTools.Utils
50 e4c5beaf Iustin Pop
import Ganeti.HTools.Types
51 43643696 Iustin Pop
52 7c14b50a Iustin Pop
-- | Type alias for the result of an IAllocator call.
53 f9283686 Iustin Pop
type IAllocResult = (String, JSValue, Node.List, Instance.List)
54 7c14b50a Iustin Pop
55 9188aeef Iustin Pop
-- | Parse the basic specifications of an instance.
56 9188aeef Iustin Pop
--
57 9188aeef Iustin Pop
-- Instances in the cluster instance list and the instance in an
58 9188aeef Iustin Pop
-- 'Allocate' request share some common properties, which are read by
59 9188aeef Iustin Pop
-- this function.
60 e4c5beaf Iustin Pop
parseBaseInstance :: String
61 28f19313 Iustin Pop
                  -> JSRecord
62 e4c5beaf Iustin Pop
                  -> Result (String, Instance.Instance)
63 e4c5beaf Iustin Pop
parseBaseInstance n a = do
64 e8230242 Iustin Pop
  let extract x = tryFromObj ("invalid data for instance '" ++ n ++ "'") a x
65 e8230242 Iustin Pop
  disk  <- extract "disk_space_total"
66 e8230242 Iustin Pop
  mem   <- extract "memory"
67 e8230242 Iustin Pop
  vcpus <- extract "vcpus"
68 e8230242 Iustin Pop
  tags  <- extract "tags"
69 5a4a3b7f Iustin Pop
  dt    <- extract "disk_template"
70 e4c5beaf Iustin Pop
  let running = "running"
71 5a4a3b7f Iustin Pop
  return (n, Instance.create n mem disk vcpus running tags True 0 0 dt)
72 585d4420 Iustin Pop
73 525bfb36 Iustin Pop
-- | Parses an instance as found in the cluster instance list.
74 28f19313 Iustin Pop
parseInstance :: NameAssoc -- ^ The node name-to-index association list
75 28f19313 Iustin Pop
              -> String    -- ^ The name of the instance
76 28f19313 Iustin Pop
              -> JSRecord  -- ^ The JSON object
77 e4c5beaf Iustin Pop
              -> Result (String, Instance.Instance)
78 e4c5beaf Iustin Pop
parseInstance ktn n a = do
79 262f3e6c Iustin Pop
  base <- parseBaseInstance n a
80 e8230242 Iustin Pop
  nodes <- fromObj a "nodes"
81 e41f4ba0 Iustin Pop
  pnode <- if null nodes
82 e41f4ba0 Iustin Pop
           then Bad $ "empty node list for instance " ++ n
83 e41f4ba0 Iustin Pop
           else readEitherString $ head nodes
84 262f3e6c Iustin Pop
  pidx <- lookupNode ktn n pnode
85 262f3e6c Iustin Pop
  let snodes = tail nodes
86 262f3e6c Iustin Pop
  sidx <- (if null snodes then return Node.noSecondary
87 262f3e6c Iustin Pop
           else readEitherString (head snodes) >>= lookupNode ktn n)
88 262f3e6c Iustin Pop
  return (n, Instance.setBoth (snd base) pidx sidx)
89 585d4420 Iustin Pop
90 9188aeef Iustin Pop
-- | Parses a node as found in the cluster node list.
91 28f19313 Iustin Pop
parseNode :: NameAssoc   -- ^ The group association
92 28f19313 Iustin Pop
          -> String      -- ^ The node's name
93 28f19313 Iustin Pop
          -> JSRecord    -- ^ The JSON object
94 9188aeef Iustin Pop
          -> Result (String, Node.Node)
95 10ef6b4e Iustin Pop
parseNode ktg n a = do
96 3eeea90f Iustin Pop
  let desc = "invalid data for node '" ++ n ++ "'"
97 3eeea90f Iustin Pop
      extract x = tryFromObj desc a x
98 e8230242 Iustin Pop
  offline <- extract "offline"
99 e8230242 Iustin Pop
  drained <- extract "drained"
100 e8230242 Iustin Pop
  guuid   <- extract "group"
101 3eeea90f Iustin Pop
  vm_capable  <- annotateResult desc $ maybeFromObj a "vm_capable"
102 3eeea90f Iustin Pop
  let vm_capable' = fromMaybe True vm_capable
103 10ef6b4e Iustin Pop
  gidx <- lookupGroup ktg n guuid
104 3eeea90f Iustin Pop
  node <- (if offline || drained || not vm_capable'
105 10ef6b4e Iustin Pop
           then return $ Node.create n 0 0 0 0 0 0 True gidx
106 262f3e6c Iustin Pop
           else do
107 e8230242 Iustin Pop
             mtotal <- extract "total_memory"
108 e8230242 Iustin Pop
             mnode  <- extract "reserved_memory"
109 e8230242 Iustin Pop
             mfree  <- extract "free_memory"
110 e8230242 Iustin Pop
             dtotal <- extract "total_disk"
111 e8230242 Iustin Pop
             dfree  <- extract "free_disk"
112 e8230242 Iustin Pop
             ctotal <- extract "total_cpus"
113 262f3e6c Iustin Pop
             return $ Node.create n mtotal mnode mfree
114 10ef6b4e Iustin Pop
                    dtotal dfree ctotal False gidx)
115 262f3e6c Iustin Pop
  return (n, node)
116 144f190b Iustin Pop
117 a679e9dc Iustin Pop
-- | Parses a group as found in the cluster group list.
118 28f19313 Iustin Pop
parseGroup :: String     -- ^ The group UUID
119 28f19313 Iustin Pop
           -> JSRecord   -- ^ The JSON object
120 a679e9dc Iustin Pop
           -> Result (String, Group.Group)
121 a679e9dc Iustin Pop
parseGroup u a = do
122 1b2cb110 Iustin Pop
  let extract x = tryFromObj ("invalid data for group '" ++ u ++ "'") a x
123 1b2cb110 Iustin Pop
  name <- extract "name"
124 1b2cb110 Iustin Pop
  apol <- extract "alloc_policy"
125 1b2cb110 Iustin Pop
  return (u, Group.create name u apol)
126 a679e9dc Iustin Pop
127 9188aeef Iustin Pop
-- | Top-level parser.
128 96a12113 Iustin Pop
--
129 96a12113 Iustin Pop
-- The result is a tuple of eventual warning messages and the parsed
130 96a12113 Iustin Pop
-- request; if parsing the input data fails, we'll return a 'Bad'
131 96a12113 Iustin Pop
-- value.
132 96a12113 Iustin Pop
parseData :: String -- ^ The JSON message as received from Ganeti
133 96a12113 Iustin Pop
          -> Result ([String], Request) -- ^ Result tuple
134 e4c5beaf Iustin Pop
parseData body = do
135 c96d44df Iustin Pop
  decoded <- fromJResult "Parsing input IAllocator message" (decodeStrict body)
136 262f3e6c Iustin Pop
  let obj = fromJSObject decoded
137 e8230242 Iustin Pop
      extrObj x = tryFromObj "invalid iallocator message" obj x
138 e4c5beaf Iustin Pop
  -- request parser
139 e8230242 Iustin Pop
  request <- liftM fromJSObject (extrObj "request")
140 e8230242 Iustin Pop
  let extrReq x = tryFromObj "invalid request dict" request x
141 a679e9dc Iustin Pop
  -- existing group parsing
142 e8230242 Iustin Pop
  glist <- liftM fromJSObject (extrObj "nodegroups")
143 a679e9dc Iustin Pop
  gobj <- mapM (\(x, y) -> asJSObject y >>= parseGroup x . fromJSObject) glist
144 10ef6b4e Iustin Pop
  let (ktg, gl) = assignIndices gobj
145 e4c5beaf Iustin Pop
  -- existing node parsing
146 e8230242 Iustin Pop
  nlist <- liftM fromJSObject (extrObj "nodes")
147 10ef6b4e Iustin Pop
  nobj <- mapM (\(x,y) ->
148 10ef6b4e Iustin Pop
                    asJSObject y >>= parseNode ktg x . fromJSObject) nlist
149 497e30a1 Iustin Pop
  let (ktn, nl) = assignIndices nobj
150 e4c5beaf Iustin Pop
  -- existing instance parsing
151 e8230242 Iustin Pop
  ilist <- extrObj "instances"
152 e4c5beaf Iustin Pop
  let idata = fromJSObject ilist
153 262f3e6c Iustin Pop
  iobj <- mapM (\(x,y) ->
154 262f3e6c Iustin Pop
                    asJSObject y >>= parseInstance ktn x . fromJSObject) idata
155 497e30a1 Iustin Pop
  let (kti, il) = assignIndices iobj
156 669ea132 Iustin Pop
  -- cluster tags
157 e8230242 Iustin Pop
  ctags <- extrObj "cluster_tags"
158 96a12113 Iustin Pop
  cdata1 <- mergeData [] [] [] [] (ClusterData gl nl il ctags)
159 96a12113 Iustin Pop
  let (msgs, fix_nl) = checkData (cdNodes cdata1) (cdInstances cdata1)
160 96a12113 Iustin Pop
      cdata = cdata1 { cdNodes = fix_nl }
161 96a12113 Iustin Pop
      map_n = cdNodes cdata
162 695c1bab Iustin Pop
      map_i = cdInstances cdata
163 695c1bab Iustin Pop
      map_g = cdGroups cdata
164 e8230242 Iustin Pop
  optype <- extrReq "type"
165 e4c5beaf Iustin Pop
  rqtype <-
166 df5227dc Iustin Pop
      case () of
167 df5227dc Iustin Pop
        _ | optype == C.iallocatorModeAlloc ->
168 df5227dc Iustin Pop
              do
169 df5227dc Iustin Pop
                rname     <- extrReq "name"
170 df5227dc Iustin Pop
                req_nodes <- extrReq "required_nodes"
171 df5227dc Iustin Pop
                inew      <- parseBaseInstance rname request
172 df5227dc Iustin Pop
                let io = snd inew
173 df5227dc Iustin Pop
                return $ Allocate io req_nodes
174 df5227dc Iustin Pop
          | optype == C.iallocatorModeReloc ->
175 df5227dc Iustin Pop
              do
176 df5227dc Iustin Pop
                rname     <- extrReq "name"
177 df5227dc Iustin Pop
                ridx      <- lookupInstance kti rname
178 df5227dc Iustin Pop
                req_nodes <- extrReq "required_nodes"
179 df5227dc Iustin Pop
                ex_nodes  <- extrReq "relocate_from"
180 df5227dc Iustin Pop
                ex_idex   <- mapM (Container.findByName map_n) ex_nodes
181 df5227dc Iustin Pop
                return $ Relocate ridx req_nodes (map Node.idx ex_idex)
182 df5227dc Iustin Pop
          | optype == C.iallocatorModeMevac ->
183 df5227dc Iustin Pop
              do
184 df5227dc Iustin Pop
                ex_names <- extrReq "evac_nodes"
185 df5227dc Iustin Pop
                ex_nodes <- mapM (Container.findByName map_n) ex_names
186 df5227dc Iustin Pop
                let ex_ndx = map Node.idx ex_nodes
187 df5227dc Iustin Pop
                return $ Evacuate ex_ndx
188 57f07ff2 Iustin Pop
          | optype == C.iallocatorModeChgGroup ->
189 695c1bab Iustin Pop
              do
190 695c1bab Iustin Pop
                rl_names <- extrReq "instances"
191 57f07ff2 Iustin Pop
                rl_insts <- mapM (liftM Instance.idx .
192 57f07ff2 Iustin Pop
                                  Container.findByName map_i) rl_names
193 57f07ff2 Iustin Pop
                gr_uuids <- extrReq "target_groups"
194 57f07ff2 Iustin Pop
                gr_idxes <- mapM (liftM Group.idx .
195 57f07ff2 Iustin Pop
                                  Container.findByName map_g) gr_uuids
196 57f07ff2 Iustin Pop
                return $ ChangeGroup rl_insts gr_idxes
197 4e84ca27 Iustin Pop
          | optype == C.iallocatorModeNodeEvac ->
198 4e84ca27 Iustin Pop
              do
199 4e84ca27 Iustin Pop
                rl_names <- extrReq "instances"
200 4e84ca27 Iustin Pop
                rl_insts <- mapM (Container.findByName map_i) rl_names
201 4e84ca27 Iustin Pop
                let rl_idx = map Instance.idx rl_insts
202 4e84ca27 Iustin Pop
                rl_mode <-
203 4e84ca27 Iustin Pop
                   case extrReq "evac_mode" of
204 4e84ca27 Iustin Pop
                     Ok s | s == C.iallocatorNevacAll -> return ChangeAll
205 4e84ca27 Iustin Pop
                          | s == C.iallocatorNevacPri -> return ChangePrimary
206 4e84ca27 Iustin Pop
                          | s == C.iallocatorNevacSec -> return ChangeSecondary
207 4e84ca27 Iustin Pop
                          | otherwise -> Bad $ "Invalid evacuate mode " ++ s
208 4e84ca27 Iustin Pop
                     Bad x -> Bad x
209 4e84ca27 Iustin Pop
                return $ NodeEvacuate rl_idx rl_mode
210 695c1bab Iustin Pop
211 df5227dc Iustin Pop
          | otherwise -> fail ("Invalid request type '" ++ optype ++ "'")
212 96a12113 Iustin Pop
  return $ (msgs, Request rqtype cdata)
213 942403e6 Iustin Pop
214 d6cf394e Iustin Pop
-- | Formats the result into a valid IAllocator response message.
215 9188aeef Iustin Pop
formatResponse :: Bool     -- ^ Whether the request was successful
216 9188aeef Iustin Pop
               -> String   -- ^ Information text
217 d6cf394e Iustin Pop
               -> JSValue  -- ^ The JSON encoded result
218 d6cf394e Iustin Pop
               -> String   -- ^ The full JSON-formatted message
219 d6cf394e Iustin Pop
formatResponse success info result =
220 43643696 Iustin Pop
    let
221 34c5a24a Iustin Pop
        e_success = ("success", showJSON success)
222 34c5a24a Iustin Pop
        e_info = ("info", showJSON info)
223 d6cf394e Iustin Pop
        e_result = ("result", result)
224 b5cec17a Iustin Pop
    in encodeStrict $ makeObj [e_success, e_info, e_result]
225 cabce2f4 Iustin Pop
226 7c14b50a Iustin Pop
-- | Flatten the log of a solution into a string.
227 7c14b50a Iustin Pop
describeSolution :: Cluster.AllocSolution -> String
228 7c14b50a Iustin Pop
describeSolution = intercalate ", " . Cluster.asLog
229 cabce2f4 Iustin Pop
230 7c14b50a Iustin Pop
-- | Convert evacuation results into the result format.
231 f9283686 Iustin Pop
formatEvacuate :: Instance.List -> Cluster.AllocSolution -> Result IAllocResult
232 f9283686 Iustin Pop
formatEvacuate il as = do
233 7c14b50a Iustin Pop
  let info = describeSolution as
234 7c14b50a Iustin Pop
      elems = Cluster.asSolutions as
235 7c14b50a Iustin Pop
  when (null elems) $ fail info
236 7c14b50a Iustin Pop
  let sols = map (\(_, inst, nl, _) -> Instance.name inst : map Node.name nl)
237 7c14b50a Iustin Pop
             elems
238 ce6a0b53 Iustin Pop
      -- FIXME: head elems is certainly not correct here, since we
239 ce6a0b53 Iustin Pop
      -- don't always concat the elems and lists in the same order;
240 ce6a0b53 Iustin Pop
      -- however, as the old evacuate mode is deprecated, we can leave
241 ce6a0b53 Iustin Pop
      -- it like this for the moment
242 ce6a0b53 Iustin Pop
      (head_nl, _, _, _) = head elems
243 f9283686 Iustin Pop
      il' = foldl' (\ilist (_, inst, _, _) ->
244 f9283686 Iustin Pop
                        Container.add (Instance.idx inst) inst ilist)
245 f9283686 Iustin Pop
            il elems
246 f9283686 Iustin Pop
  return (info, showJSON sols, head_nl, il')
247 cabce2f4 Iustin Pop
248 7c14b50a Iustin Pop
-- | Convert allocation/relocation results into the result format.
249 f9283686 Iustin Pop
formatAllocate :: Instance.List -> Cluster.AllocSolution -> Result IAllocResult
250 f9283686 Iustin Pop
formatAllocate il as = do
251 7c14b50a Iustin Pop
  let info = describeSolution as
252 7c14b50a Iustin Pop
  case Cluster.asSolutions as of
253 7c14b50a Iustin Pop
    [] -> fail info
254 f9283686 Iustin Pop
    (nl, inst, nodes, _):[] ->
255 f9283686 Iustin Pop
        do
256 f9283686 Iustin Pop
          let il' = Container.add (Instance.idx inst) inst il
257 f9283686 Iustin Pop
          return (info, showJSON $ map (Node.name) nodes, nl, il')
258 7c14b50a Iustin Pop
    _ -> fail "Internal error: multiple allocation solutions"
259 cabce2f4 Iustin Pop
260 47eed3f4 Iustin Pop
-- | Convert a node-evacuation/change group result.
261 5440c877 Iustin Pop
formatNodeEvac :: Group.List
262 5440c877 Iustin Pop
               -> Node.List
263 5440c877 Iustin Pop
               -> Instance.List
264 4036f63a Iustin Pop
               -> (Node.List, Instance.List, Cluster.EvacSolution)
265 5440c877 Iustin Pop
               -> Result IAllocResult
266 f9283686 Iustin Pop
formatNodeEvac gl nl il (fin_nl, fin_il, es) =
267 5440c877 Iustin Pop
    let iname = Instance.name . flip Container.find il
268 5440c877 Iustin Pop
        nname = Node.name . flip Container.find nl
269 5440c877 Iustin Pop
        gname = Group.name . flip Container.find gl
270 5440c877 Iustin Pop
        fes = map (\(idx, msg) -> (iname idx, msg)) $ Cluster.esFailed es
271 5440c877 Iustin Pop
        mes = map (\(idx, gdx, ndxs) -> (iname idx, gname gdx, map nname ndxs))
272 5440c877 Iustin Pop
              $ Cluster.esMoved es
273 47eed3f4 Iustin Pop
        failed = length fes
274 47eed3f4 Iustin Pop
        moved  = length mes
275 47eed3f4 Iustin Pop
        info = show failed ++ " instances failed to move and " ++ show moved ++
276 47eed3f4 Iustin Pop
               " were moved successfully"
277 f9283686 Iustin Pop
    in Ok (info, showJSON (mes, fes, Cluster.esOpCodes es), fin_nl, fin_il)
278 47eed3f4 Iustin Pop
279 cabce2f4 Iustin Pop
-- | Process a request and return new node lists
280 7c14b50a Iustin Pop
processRequest :: Request -> Result IAllocResult
281 cabce2f4 Iustin Pop
processRequest request =
282 cabce2f4 Iustin Pop
  let Request rqtype (ClusterData gl nl il _) = request
283 cabce2f4 Iustin Pop
  in case rqtype of
284 7c14b50a Iustin Pop
       Allocate xi reqn ->
285 f9283686 Iustin Pop
           Cluster.tryMGAlloc gl nl il xi reqn >>= formatAllocate il
286 7c14b50a Iustin Pop
       Relocate idx reqn exnodes ->
287 f9283686 Iustin Pop
           Cluster.tryMGReloc gl nl il idx reqn exnodes >>= formatAllocate il
288 7c14b50a Iustin Pop
       Evacuate exnodes ->
289 f9283686 Iustin Pop
           Cluster.tryMGEvac gl nl il exnodes >>= formatEvacuate il
290 20b376ff Iustin Pop
       ChangeGroup gdxs idxs ->
291 5440c877 Iustin Pop
           Cluster.tryChangeGroup gl nl il idxs gdxs >>=
292 5440c877 Iustin Pop
                  formatNodeEvac gl nl il
293 47eed3f4 Iustin Pop
       NodeEvacuate xi mode ->
294 5440c877 Iustin Pop
           Cluster.tryNodeEvac gl nl il mode xi >>=
295 5440c877 Iustin Pop
                  formatNodeEvac gl nl il
296 cabce2f4 Iustin Pop
297 cabce2f4 Iustin Pop
-- | Reads the request from the data file(s)
298 cabce2f4 Iustin Pop
readRequest :: Options -> [String] -> IO Request
299 cabce2f4 Iustin Pop
readRequest opts args = do
300 cabce2f4 Iustin Pop
  when (null args) $ do
301 cabce2f4 Iustin Pop
         hPutStrLn stderr "Error: this program needs an input file."
302 cabce2f4 Iustin Pop
         exitWith $ ExitFailure 1
303 cabce2f4 Iustin Pop
304 cabce2f4 Iustin Pop
  input_data <- readFile (head args)
305 cabce2f4 Iustin Pop
  r1 <- case parseData input_data of
306 cabce2f4 Iustin Pop
          Bad err -> do
307 cabce2f4 Iustin Pop
            hPutStrLn stderr $ "Error: " ++ err
308 cabce2f4 Iustin Pop
            exitWith $ ExitFailure 1
309 96a12113 Iustin Pop
          Ok (fix_msgs, rq) -> maybeShowWarnings fix_msgs >> return rq
310 cabce2f4 Iustin Pop
  (if isJust (optDataFile opts) ||  (not . null . optNodeSim) opts
311 cabce2f4 Iustin Pop
   then do
312 cabce2f4 Iustin Pop
     cdata <- loadExternalData opts
313 cabce2f4 Iustin Pop
     let Request rqt _ = r1
314 cabce2f4 Iustin Pop
     return $ Request rqt cdata
315 cabce2f4 Iustin Pop
   else return r1)
316 00152519 Iustin Pop
317 00152519 Iustin Pop
-- | Main iallocator pipeline.
318 f9283686 Iustin Pop
runIAllocator :: Request -> (Maybe (Node.List, Instance.List), String)
319 00152519 Iustin Pop
runIAllocator request =
320 f9283686 Iustin Pop
  let (ok, info, result, cdata) =
321 7c14b50a Iustin Pop
          case processRequest request of
322 f9283686 Iustin Pop
            Ok (msg, r, nl, il) -> (True, "Request successful: " ++ msg, r,
323 f9283686 Iustin Pop
                                    Just (nl, il))
324 ce6a0b53 Iustin Pop
            Bad msg -> (False, "Request failed: " ++ msg, JSArray [], Nothing)
325 ce6a0b53 Iustin Pop
      rstring = formatResponse ok info result
326 f9283686 Iustin Pop
  in (cdata, rstring)