Do not rely on no-secondary for movable tests
[ganeti-local] / htools / Ganeti / HTools / IAlloc.hs
index cbec6a9..e2076e2 100644 (file)
@@ -4,7 +4,7 @@
 
 {-
 
-Copyright (C) 2009, 2010, 2011 Google Inc.
+Copyright (C) 2009, 2010, 2011, 2012 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
@@ -27,10 +27,11 @@ module Ganeti.HTools.IAlloc
   ( readRequest
   , runIAllocator
   , processRelocate
+  , loadData
   ) where
 
 import Data.Either ()
-import Data.Maybe (fromMaybe, isJust)
+import Data.Maybe (fromMaybe)
 import Data.List
 import Control.Monad
 import Text.JSON (JSObject, JSValue(JSArray),
@@ -46,7 +47,6 @@ import qualified Ganeti.HTools.Instance as Instance
 import qualified Ganeti.Constants as C
 import Ganeti.HTools.CLI
 import Ganeti.HTools.Loader
-import Ganeti.HTools.ExtLoader (loadExternalData)
 import Ganeti.HTools.JSON
 import Ganeti.HTools.Types
 
@@ -105,7 +105,7 @@ parseNode ktg n a = do
   let vm_capable' = fromMaybe True vm_capable
   gidx <- lookupGroup ktg n guuid
   node <- if offline || drained || not vm_capable'
-            then return $ Node.create n 0 0 0 0 0 0 True gidx
+            then return $ Node.create n 0 0 0 0 0 0 True 0 gidx
             else do
               mtotal <- extract "total_memory"
               mnode  <- extract "reserved_memory"
@@ -113,8 +113,11 @@ parseNode ktg n a = do
               dtotal <- extract "total_disk"
               dfree  <- extract "free_disk"
               ctotal <- extract "total_cpus"
+              ndparams <- extract "ndparams" >>= asJSObject
+              spindles <- tryFromObj desc (fromJSObject ndparams)
+                          "spindle_count"
               return $ Node.create n mtotal mnode mfree
-                     dtotal dfree ctotal False gidx
+                     dtotal dfree ctotal False spindles gidx
   return (n, node)
 
 -- | Parses a group as found in the cluster group list.
@@ -125,7 +128,8 @@ parseGroup u a = do
   let extract x = tryFromObj ("invalid data for group '" ++ u ++ "'") a x
   name <- extract "name"
   apol <- extract "alloc_policy"
-  return (u, Group.create name u apol)
+  ipol <- extract "ipolicy"
+  return (u, Group.create name u apol ipol)
 
 -- | Top-level parser.
 --
@@ -321,24 +325,14 @@ processRequest request =
                 formatNodeEvac gl nl il
 
 -- | Reads the request from the data file(s).
-readRequest :: Options -> [String] -> IO Request
-readRequest opts args = do
-  when (null args) $ do
-    hPutStrLn stderr "Error: this program needs an input file."
-    exitWith $ ExitFailure 1
-
-  input_data <- readFile (head args)
-  r1 <- case parseData input_data of
-          Bad err -> do
-            hPutStrLn stderr $ "Error: " ++ err
-            exitWith $ ExitFailure 1
-          Ok (fix_msgs, rq) -> maybeShowWarnings fix_msgs >> return rq
-  if isJust (optDataFile opts) ||  (not . null . optNodeSim) opts
-    then do
-      cdata <- loadExternalData opts
-      let Request rqt _ = r1
-      return $ Request rqt cdata
-    else return r1
+readRequest :: FilePath -> IO Request
+readRequest fp = do
+  input_data <- readFile fp
+  case parseData input_data of
+    Bad err -> do
+      hPutStrLn stderr $ "Error: " ++ err
+      exitWith $ ExitFailure 1
+    Ok (fix_msgs, rq) -> maybeShowWarnings fix_msgs >> return rq
 
 -- | Main iallocator pipeline.
 runIAllocator :: Request -> (Maybe (Node.List, Instance.List), String)
@@ -350,3 +344,10 @@ runIAllocator request =
           Bad msg -> (False, "Request failed: " ++ msg, JSArray [], Nothing)
       rstring = formatResponse ok info result
   in (cdata, rstring)
+
+-- | Load the data from an iallocation request file
+loadData :: FilePath -- ^ The path to the file
+         -> IO (Result ClusterData)
+loadData fp = do
+  Request _ cdata <- readRequest fp
+  return $ Ok cdata