Commit c3f8cb12 authored by René Nussbaumer's avatar René Nussbaumer
Browse files

htools: Refactor some of IAlloc.readRequest to Hail



This makes it possible to reuse that function when we want to use IAlloc
as a data source backend
Signed-off-by: default avatarRené Nussbaumer <rn@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 14fbd75b
......@@ -30,7 +30,7 @@ module Ganeti.HTools.IAlloc
) 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 +46,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
......@@ -325,24 +324,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)
......
......@@ -26,15 +26,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
module Ganeti.HTools.Program.Hail (main, options) where
import Control.Monad
import Data.Maybe (fromMaybe)
import Data.Maybe (fromMaybe, isJust)
import System.IO
import System.Exit
import qualified Ganeti.HTools.Cluster as Cluster
import Ganeti.HTools.CLI
import Ganeti.HTools.IAlloc
import Ganeti.HTools.Loader (Request(..), ClusterData(..))
import Ganeti.HTools.ExtLoader (maybeSaveData)
import Ganeti.HTools.ExtLoader (maybeSaveData, loadExternalData)
-- | Options list and functions.
options :: [OptType]
......@@ -48,6 +49,21 @@ options =
, oShowHelp
]
wrapReadRequest :: Options -> [String] -> IO Request
wrapReadRequest opts args = do
when (null args) $ do
hPutStrLn stderr "Error: this program needs an input file."
exitWith $ ExitFailure 1
r1 <- readRequest (head args)
if isJust (optDataFile opts) || (not . null . optNodeSim) opts
then do
cdata <- loadExternalData opts
let Request rqt _ = r1
return $ Request rqt cdata
else return r1
-- | Main function.
main :: Options -> [String] -> IO ()
main opts args = do
......@@ -55,7 +71,7 @@ main opts args = do
verbose = optVerbose opts
savecluster = optSaveCluster opts
request <- readRequest opts args
request <- wrapReadRequest opts args
let Request rq cdata = request
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment