Statistics
| Branch: | Tag: | Revision:

root / hail.hs @ dbba5246

History | View | Annotate | Download (3.5 kB)

1 585d4420 Iustin Pop
{-| Solver for N+1 cluster errors
2 585d4420 Iustin Pop
3 585d4420 Iustin Pop
-}
4 585d4420 Iustin Pop
5 585d4420 Iustin Pop
module Main (main) where
6 585d4420 Iustin Pop
7 585d4420 Iustin Pop
import Data.List
8 585d4420 Iustin Pop
import Data.Function
9 842e3764 Iustin Pop
import Data.Maybe (isJust, fromJust)
10 585d4420 Iustin Pop
import Monad
11 585d4420 Iustin Pop
import System
12 585d4420 Iustin Pop
import System.IO
13 585d4420 Iustin Pop
import System.Console.GetOpt
14 585d4420 Iustin Pop
import qualified System
15 585d4420 Iustin Pop
16 585d4420 Iustin Pop
import Text.Printf (printf)
17 585d4420 Iustin Pop
18 585d4420 Iustin Pop
import qualified Ganeti.HTools.Container as Container
19 585d4420 Iustin Pop
import qualified Ganeti.HTools.Cluster as Cluster
20 585d4420 Iustin Pop
import qualified Ganeti.HTools.Node as Node
21 ed41c179 Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
22 585d4420 Iustin Pop
import qualified Ganeti.HTools.CLI as CLI
23 585d4420 Iustin Pop
import Ganeti.HTools.IAlloc
24 e4c5beaf Iustin Pop
import Ganeti.HTools.Types
25 19f38ee8 Iustin Pop
import Ganeti.HTools.Loader (RqType(..), Request(..))
26 585d4420 Iustin Pop
27 585d4420 Iustin Pop
-- | Command line options structure.
28 585d4420 Iustin Pop
data Options = Options
29 842e3764 Iustin Pop
    { optShowVer   :: Bool           -- ^ Just show the program version
30 585d4420 Iustin Pop
    , optShowHelp  :: Bool           -- ^ Just show the help
31 585d4420 Iustin Pop
    } deriving Show
32 585d4420 Iustin Pop
33 585d4420 Iustin Pop
-- | Default values for the command line options.
34 585d4420 Iustin Pop
defaultOptions :: Options
35 585d4420 Iustin Pop
defaultOptions  = Options
36 842e3764 Iustin Pop
 { optShowVer   = False
37 585d4420 Iustin Pop
 , optShowHelp  = False
38 585d4420 Iustin Pop
 }
39 585d4420 Iustin Pop
40 842e3764 Iustin Pop
instance CLI.CLIOptions Options where
41 842e3764 Iustin Pop
    showVersion = optShowVer
42 842e3764 Iustin Pop
    showHelp    = optShowHelp
43 842e3764 Iustin Pop
44 585d4420 Iustin Pop
-- | Options list and functions
45 585d4420 Iustin Pop
options :: [OptDescr (Options -> Options)]
46 585d4420 Iustin Pop
options =
47 842e3764 Iustin Pop
    [ Option ['V']     ["version"]
48 585d4420 Iustin Pop
      (NoArg (\ opts -> opts { optShowVer = True}))
49 585d4420 Iustin Pop
      "show the version of the program"
50 585d4420 Iustin Pop
    , Option ['h']     ["help"]
51 585d4420 Iustin Pop
      (NoArg (\ opts -> opts { optShowHelp = True}))
52 585d4420 Iustin Pop
      "show help"
53 585d4420 Iustin Pop
    ]
54 585d4420 Iustin Pop
55 f826c5e0 Iustin Pop
56 262a08a2 Iustin Pop
filterFails :: (Monad m) => [(Maybe Node.List, [Node.Node])]
57 262a08a2 Iustin Pop
            -> m [(Node.List, [Node.Node])]
58 f826c5e0 Iustin Pop
filterFails sols =
59 f826c5e0 Iustin Pop
    if null sols then fail "No nodes onto which to allocate at all"
60 f826c5e0 Iustin Pop
    else let sols' = filter (isJust . fst) sols
61 f826c5e0 Iustin Pop
         in if null sols' then
62 f826c5e0 Iustin Pop
                fail "No valid allocation solutions"
63 58709f92 Iustin Pop
            else
64 f826c5e0 Iustin Pop
                return $ map (\(x, y) -> (fromJust x, y)) sols'
65 f826c5e0 Iustin Pop
66 262a08a2 Iustin Pop
processResults :: (Monad m) => [(Node.List, [Node.Node])]
67 f826c5e0 Iustin Pop
               -> m (String, [Node.Node])
68 f826c5e0 Iustin Pop
processResults sols =
69 f826c5e0 Iustin Pop
    let sols' = map (\(nl', ns) -> (Cluster.compCV  nl', ns)) sols
70 f826c5e0 Iustin Pop
        sols'' = sortBy (compare `on` fst) sols'
71 f826c5e0 Iustin Pop
        (best, w) = head sols''
72 f826c5e0 Iustin Pop
        (worst, l) = last sols''
73 5e15f460 Iustin Pop
        info = printf "Valid results: %d, best score: %.8f for node(s) %s, \
74 5e15f460 Iustin Pop
                      \worst score: %.8f for node(s) %s" (length sols'')
75 f826c5e0 Iustin Pop
                      best (intercalate "/" . map Node.name $ w)
76 f826c5e0 Iustin Pop
                      worst (intercalate "/" . map Node.name $ l)
77 f826c5e0 Iustin Pop
    in return (info, w)
78 585d4420 Iustin Pop
79 585d4420 Iustin Pop
-- | Main function.
80 585d4420 Iustin Pop
main :: IO ()
81 585d4420 Iustin Pop
main = do
82 585d4420 Iustin Pop
  cmd_args <- System.getArgs
83 842e3764 Iustin Pop
  (_, args) <- CLI.parseOpts cmd_args "hail" options defaultOptions
84 585d4420 Iustin Pop
85 585d4420 Iustin Pop
  when (null args) $ do
86 585d4420 Iustin Pop
         hPutStrLn stderr "Error: this program needs an input file."
87 585d4420 Iustin Pop
         exitWith $ ExitFailure 1
88 585d4420 Iustin Pop
89 585d4420 Iustin Pop
  let input_file = head args
90 585d4420 Iustin Pop
  input_data <- readFile input_file
91 585d4420 Iustin Pop
92 585d4420 Iustin Pop
  request <- case (parseData input_data) of
93 585d4420 Iustin Pop
               Bad err -> do
94 585d4420 Iustin Pop
                 putStrLn $ "Error: " ++ err
95 585d4420 Iustin Pop
                 exitWith $ ExitFailure 1
96 585d4420 Iustin Pop
               Ok rq -> return rq
97 585d4420 Iustin Pop
98 ed41c179 Iustin Pop
  let Request rqtype nl il csf = request
99 ed41c179 Iustin Pop
      new_nodes = case rqtype of
100 dbba5246 Iustin Pop
                    Allocate xi reqn -> Cluster.tryAlloc nl il xi reqn
101 ed41c179 Iustin Pop
                    Relocate idx reqn exnodes ->
102 dbba5246 Iustin Pop
                        Cluster.tryReloc nl il idx reqn exnodes
103 f826c5e0 Iustin Pop
  let sols = new_nodes >>= filterFails >>= processResults
104 f826c5e0 Iustin Pop
  let (ok, info, rn) = case sols of
105 58709f92 Iustin Pop
               Ok (info, sn) -> (True, "Request successful: " ++ info,
106 262a08a2 Iustin Pop
                                     map ((++ csf) . Node.name) sn)
107 ed41c179 Iustin Pop
               Bad s -> (False, "Request failed: " ++ s, [])
108 ed41c179 Iustin Pop
      resp = formatResponse ok info rn
109 ed41c179 Iustin Pop
  putStrLn resp