IAlloc.hs 5.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 49 50 51
parseBaseInstance :: String
                  -> JSObject JSValue
                  -> 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
  let running = "running"
Iustin Pop's avatar
Iustin Pop committed
56
  return (n, Instance.create n mem disk vcpus running 0 0)
Iustin Pop's avatar
Iustin Pop committed
57

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

Iustin Pop's avatar
Iustin Pop committed
73 74 75 76
-- | Parses a node as found in the cluster node list.
parseNode :: String           -- ^ The node's name
          -> JSObject JSValue -- ^ The JSON object
          -> Result (String, Node.Node)
Iustin Pop's avatar
Iustin Pop committed
77 78 79
parseNode n a = do
    let name = n
    offline <- fromObj "offline" a
80
    drained <- fromObj "drained" a
81
    node <- (if offline || drained
Iustin Pop's avatar
Iustin Pop committed
82 83 84 85 86 87 88 89 90
             then return $ Node.create name 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
91
                      dtotal dfree ctotal False)
92
    return (name, node)
93

Iustin Pop's avatar
Iustin Pop committed
94 95 96
-- | 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
97 98 99 100 101 102 103 104 105
parseData body = do
  decoded <- fromJResult $ decodeStrict body
  let obj = decoded
  -- request parser
  request <- fromObj "request" obj
  rname <- fromObj "name" request
  -- existing node parsing
  nlist <- fromObj "nodes" obj
  let ndata = fromJSObject nlist
Iustin Pop's avatar
Iustin Pop committed
106
  nobj <- mapM (\(x,y) -> asJSObject y >>= parseNode x) ndata
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
Iustin Pop's avatar
Iustin Pop committed
111
  iobj <- mapM (\(x,y) -> asJSObject y >>= parseInstance ktn x) idata
112
  let (kti, il) = assignIndices iobj
113
  (map_n, map_i, csf) <- mergeData [] (nl, il)
114
  req_nodes <- fromObj "required_nodes" request
Iustin Pop's avatar
Iustin Pop committed
115 116 117 118 119 120
  optype <- fromObj "type" request
  rqtype <-
      case optype of
        "allocate" ->
            do
              inew <- parseBaseInstance rname request
121 122
              let io = snd inew
              return $ Allocate io req_nodes
Iustin Pop's avatar
Iustin Pop committed
123 124
        "relocate" ->
            do
125
              ridx <- lookupInstance kti rname
126 127
              ex_nodes <- fromObj "relocate_from" request
              let ex_nodes' = map (stripSuffix $ length csf) ex_nodes
128
              ex_idex <- mapM (Container.findByName map_n) ex_nodes'
129
              return $ Relocate ridx req_nodes (map Node.idx ex_idex)
Iustin Pop's avatar
Iustin Pop committed
130
        other -> fail ("Invalid request type '" ++ other ++ "'")
131
  return $ Request rqtype map_n map_i csf
132

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