root / hail.hs @ f3d53161
History | View | Annotate | Download (3.2 kB)
1 | 585d4420 | Iustin Pop | {-| Solver for N+1 cluster errors |
---|---|---|---|
2 | 585d4420 | Iustin Pop | |
3 | 585d4420 | Iustin Pop | -} |
4 | 585d4420 | Iustin Pop | |
5 | e2fa2baf | Iustin Pop | {- |
6 | e2fa2baf | Iustin Pop | |
7 | e2fa2baf | Iustin Pop | Copyright (C) 2009 Google Inc. |
8 | e2fa2baf | Iustin Pop | |
9 | e2fa2baf | Iustin Pop | This program is free software; you can redistribute it and/or modify |
10 | e2fa2baf | Iustin Pop | it under the terms of the GNU General Public License as published by |
11 | e2fa2baf | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
12 | e2fa2baf | Iustin Pop | (at your option) any later version. |
13 | e2fa2baf | Iustin Pop | |
14 | e2fa2baf | Iustin Pop | This program is distributed in the hope that it will be useful, but |
15 | e2fa2baf | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | e2fa2baf | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | e2fa2baf | Iustin Pop | General Public License for more details. |
18 | e2fa2baf | Iustin Pop | |
19 | e2fa2baf | Iustin Pop | You should have received a copy of the GNU General Public License |
20 | e2fa2baf | Iustin Pop | along with this program; if not, write to the Free Software |
21 | e2fa2baf | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 | e2fa2baf | Iustin Pop | 02110-1301, USA. |
23 | e2fa2baf | Iustin Pop | |
24 | e2fa2baf | Iustin Pop | -} |
25 | e2fa2baf | Iustin Pop | |
26 | 585d4420 | Iustin Pop | module Main (main) where |
27 | 585d4420 | Iustin Pop | |
28 | 585d4420 | Iustin Pop | import Data.List |
29 | 585d4420 | Iustin Pop | import Data.Function |
30 | f3d53161 | Iustin Pop | import Data.Maybe (isJust, fromJust) |
31 | 585d4420 | Iustin Pop | import Monad |
32 | 585d4420 | Iustin Pop | import System |
33 | 585d4420 | Iustin Pop | import System.IO |
34 | 585d4420 | Iustin Pop | import qualified System |
35 | 585d4420 | Iustin Pop | |
36 | 585d4420 | Iustin Pop | import Text.Printf (printf) |
37 | 585d4420 | Iustin Pop | |
38 | 585d4420 | Iustin Pop | import qualified Ganeti.HTools.Cluster as Cluster |
39 | 585d4420 | Iustin Pop | import qualified Ganeti.HTools.Node as Node |
40 | 0427285d | Iustin Pop | |
41 | 0427285d | Iustin Pop | import Ganeti.HTools.CLI |
42 | 585d4420 | Iustin Pop | import Ganeti.HTools.IAlloc |
43 | e4c5beaf | Iustin Pop | import Ganeti.HTools.Types |
44 | 19f38ee8 | Iustin Pop | import Ganeti.HTools.Loader (RqType(..), Request(..)) |
45 | 585d4420 | Iustin Pop | |
46 | 585d4420 | Iustin Pop | -- | Options list and functions |
47 | 0427285d | Iustin Pop | options :: [OptType] |
48 | f3d53161 | Iustin Pop | options = [oPrintNodes, oShowVer, oShowHelp] |
49 | f826c5e0 | Iustin Pop | |
50 | 478df686 | Iustin Pop | processResults :: (Monad m) => Cluster.AllocSolution -> m (String, [Node.Node]) |
51 | fbb95f28 | Iustin Pop | processResults (fstats, successes, sols) = |
52 | 478df686 | Iustin Pop | case sols of |
53 | 478df686 | Iustin Pop | Nothing -> fail "No valid allocation solutions" |
54 | 478df686 | Iustin Pop | Just (best, (_, _, w)) -> |
55 | 478df686 | Iustin Pop | let tfails = length fstats |
56 | 478df686 | Iustin Pop | info = printf "successes %d, failures %d,\ |
57 | 478df686 | Iustin Pop | \ best score: %.8f for node(s) %s" |
58 | fbb95f28 | Iustin Pop | successes tfails |
59 | 478df686 | Iustin Pop | best (intercalate "/" . map Node.name $ w)::String |
60 | 478df686 | Iustin Pop | in return (info, w) |
61 | 585d4420 | Iustin Pop | |
62 | 78694255 | Iustin Pop | -- | Process a request and return new node lists |
63 | f2280553 | Iustin Pop | processRequest :: Request |
64 | 478df686 | Iustin Pop | -> Result Cluster.AllocSolution |
65 | 78694255 | Iustin Pop | processRequest request = |
66 | 669ea132 | Iustin Pop | let Request rqtype nl il _ _ = request |
67 | 78694255 | Iustin Pop | in case rqtype of |
68 | 78694255 | Iustin Pop | Allocate xi reqn -> Cluster.tryAlloc nl il xi reqn |
69 | 78694255 | Iustin Pop | Relocate idx reqn exnodes -> Cluster.tryReloc nl il idx reqn exnodes |
70 | 78694255 | Iustin Pop | |
71 | 585d4420 | Iustin Pop | -- | Main function. |
72 | 585d4420 | Iustin Pop | main :: IO () |
73 | 585d4420 | Iustin Pop | main = do |
74 | 585d4420 | Iustin Pop | cmd_args <- System.getArgs |
75 | f3d53161 | Iustin Pop | (opts, args) <- parseOpts cmd_args "hail" options |
76 | 585d4420 | Iustin Pop | |
77 | 585d4420 | Iustin Pop | when (null args) $ do |
78 | 585d4420 | Iustin Pop | hPutStrLn stderr "Error: this program needs an input file." |
79 | 585d4420 | Iustin Pop | exitWith $ ExitFailure 1 |
80 | 585d4420 | Iustin Pop | |
81 | 585d4420 | Iustin Pop | let input_file = head args |
82 | f3d53161 | Iustin Pop | shownodes = optShowNodes opts |
83 | 585d4420 | Iustin Pop | input_data <- readFile input_file |
84 | 585d4420 | Iustin Pop | |
85 | 585d4420 | Iustin Pop | request <- case (parseData input_data) of |
86 | 585d4420 | Iustin Pop | Bad err -> do |
87 | 2795466b | Iustin Pop | hPutStrLn stderr $ "Error: " ++ err |
88 | 585d4420 | Iustin Pop | exitWith $ ExitFailure 1 |
89 | 585d4420 | Iustin Pop | Ok rq -> return rq |
90 | 585d4420 | Iustin Pop | |
91 | f3d53161 | Iustin Pop | let Request _ nl _ _ csf = request |
92 | f3d53161 | Iustin Pop | |
93 | f3d53161 | Iustin Pop | when (isJust shownodes) $ do |
94 | f3d53161 | Iustin Pop | hPutStrLn stderr "Initial cluster status:" |
95 | f3d53161 | Iustin Pop | hPutStrLn stderr $ Cluster.printNodes nl (fromJust shownodes) |
96 | f3d53161 | Iustin Pop | |
97 | f3d53161 | Iustin Pop | let sols = processRequest request >>= processResults |
98 | 478df686 | Iustin Pop | let (ok, info, rn) = |
99 | 478df686 | Iustin Pop | case sols of |
100 | fbb95f28 | Iustin Pop | Ok (ginfo, sn) -> (True, "Request successful: " ++ ginfo, |
101 | fbb95f28 | Iustin Pop | map ((++ csf) . Node.name) sn) |
102 | 478df686 | Iustin Pop | Bad s -> (False, "Request failed: " ++ s, []) |
103 | ed41c179 | Iustin Pop | resp = formatResponse ok info rn |
104 | ed41c179 | Iustin Pop | putStrLn resp |