[PATCH master 1/5] htools: Refactor some of IAlloc.readRequest to Hail

0 views
Skip to first unread message

René Nussbaumer

unread,
Mar 2, 2012, 7:33:57 AM3/2/12
to ganeti...@googlegroups.com
This makes it possible to reuse that function when we want to use IAlloc
as a data source backend
---
htools/Ganeti/HTools/IAlloc.hs | 29 +++++++++--------------------
htools/Ganeti/HTools/Program/Hail.hs | 22 +++++++++++++++++++---
2 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/htools/Ganeti/HTools/IAlloc.hs b/htools/Ganeti/HTools/IAlloc.hs
index 9ccc8ec..a6a5b14 100644
--- a/htools/Ganeti/HTools/IAlloc.hs
+++ b/htools/Ganeti/HTools/IAlloc.hs
@@ -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)
diff --git a/htools/Ganeti/HTools/Program/Hail.hs b/htools/Ganeti/HTools/Program/Hail.hs
index 426def4..4fc016a 100644
--- a/htools/Ganeti/HTools/Program/Hail.hs
+++ b/htools/Ganeti/HTools/Program/Hail.hs
@@ -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

--
1.7.7.3

Iustin Pop

unread,
Mar 2, 2012, 10:48:22 AM3/2/12
to René Nussbaumer, ganeti...@googlegroups.com
On Fri, Mar 02, 2012 at 01:33:57PM +0100, Ren� Nussbaumer wrote:
> This makes it possible to reuse that function when we want to use IAlloc
> as a data source backend

LGTM, thanks.

iustin

Reply all
Reply to author
Forward
0 new messages