Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / CLI.hs @ 00152519

History | View | Annotate | Download (17.3 kB)

1 209b3711 Iustin Pop
{-| Implementation of command-line functions.
2 209b3711 Iustin Pop
3 525bfb36 Iustin Pop
This module holds the common command-line related functions for the
4 525bfb36 Iustin Pop
binaries, separated into this module since "Ganeti.HTools.Utils" is
5 525bfb36 Iustin Pop
used in many other places and this is more IO oriented.
6 209b3711 Iustin Pop
7 209b3711 Iustin Pop
-}
8 209b3711 Iustin Pop
9 e2fa2baf Iustin Pop
{-
10 e2fa2baf Iustin Pop
11 a69ff623 Iustin Pop
Copyright (C) 2009, 2010, 2011 Google Inc.
12 e2fa2baf Iustin Pop
13 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
14 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
15 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
16 e2fa2baf Iustin Pop
(at your option) any later version.
17 e2fa2baf Iustin Pop
18 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
19 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
20 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 e2fa2baf Iustin Pop
General Public License for more details.
22 e2fa2baf Iustin Pop
23 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
24 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
25 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 e2fa2baf Iustin Pop
02110-1301, USA.
27 e2fa2baf Iustin Pop
28 e2fa2baf Iustin Pop
-}
29 e2fa2baf Iustin Pop
30 209b3711 Iustin Pop
module Ganeti.HTools.CLI
31 0427285d Iustin Pop
    ( Options(..)
32 0427285d Iustin Pop
    , OptType
33 75d1edf8 Iustin Pop
    , parseOpts
34 e0eb63f0 Iustin Pop
    , shTemplate
35 ba9349b8 Iustin Pop
    , defaultLuxiSocket
36 417f6b50 Iustin Pop
    , maybePrintNodes
37 33e44f0c Iustin Pop
    , maybePrintInsts
38 0427285d Iustin Pop
    -- * The options
39 16c2369c Iustin Pop
    , oDataFile
40 df18fdfe Iustin Pop
    , oDiskMoves
41 ddef0585 Guido Trotter
    , oSelInst
42 8fcfb767 Guido Trotter
    , oInstMoves
43 df18fdfe Iustin Pop
    , oDynuFile
44 f0f21ec4 Iustin Pop
    , oEvacMode
45 10f396e1 Iustin Pop
    , oExInst
46 df18fdfe Iustin Pop
    , oExTags
47 0df5a1b4 Iustin Pop
    , oExecJobs
48 a423b510 Iustin Pop
    , oGroup
49 0427285d Iustin Pop
    , oIDisk
50 df18fdfe Iustin Pop
    , oIMem
51 0427285d Iustin Pop
    , oINodes
52 df18fdfe Iustin Pop
    , oIVcpus
53 df18fdfe Iustin Pop
    , oLuxiSocket
54 0427285d Iustin Pop
    , oMaxCpu
55 df18fdfe Iustin Pop
    , oMaxSolLength
56 0427285d Iustin Pop
    , oMinDisk
57 4f807a57 Iustin Pop
    , oMinGain
58 4f807a57 Iustin Pop
    , oMinGainLim
59 df18fdfe Iustin Pop
    , oMinScore
60 df18fdfe Iustin Pop
    , oNoHeaders
61 df18fdfe Iustin Pop
    , oNodeSim
62 df18fdfe Iustin Pop
    , oOfflineNode
63 df18fdfe Iustin Pop
    , oOneline
64 df18fdfe Iustin Pop
    , oOutputDir
65 df18fdfe Iustin Pop
    , oPrintCommands
66 df18fdfe Iustin Pop
    , oPrintInsts
67 df18fdfe Iustin Pop
    , oPrintNodes
68 df18fdfe Iustin Pop
    , oQuiet
69 df18fdfe Iustin Pop
    , oRapiMaster
70 509809db Iustin Pop
    , oReplay
71 02da9d07 Iustin Pop
    , oSaveCluster
72 0427285d Iustin Pop
    , oShowHelp
73 df18fdfe Iustin Pop
    , oShowVer
74 df18fdfe Iustin Pop
    , oTieredSpec
75 df18fdfe Iustin Pop
    , oVerbose
76 209b3711 Iustin Pop
    ) where
77 209b3711 Iustin Pop
78 cc532bdd Iustin Pop
import Control.Monad
79 e8f89bb6 Iustin Pop
import Data.Maybe (fromMaybe)
80 8e445e6d Iustin Pop
import qualified Data.Version
81 209b3711 Iustin Pop
import System.Console.GetOpt
82 209b3711 Iustin Pop
import System.IO
83 209b3711 Iustin Pop
import System.Info
84 209b3711 Iustin Pop
import System
85 e8f89bb6 Iustin Pop
import Text.Printf (printf)
86 209b3711 Iustin Pop
87 209b3711 Iustin Pop
import qualified Ganeti.HTools.Version as Version(version)
88 a69ff623 Iustin Pop
import qualified Ganeti.Constants as C
89 92e32d76 Iustin Pop
import Ganeti.HTools.Types
90 1f9066c0 Iustin Pop
import Ganeti.HTools.Utils
91 fae371cc Iustin Pop
92 525bfb36 Iustin Pop
-- * Constants
93 525bfb36 Iustin Pop
94 525bfb36 Iustin Pop
-- | The default value for the luxi socket.
95 525bfb36 Iustin Pop
--
96 525bfb36 Iustin Pop
-- This is re-exported from the "Ganeti.Constants" module.
97 8e445e6d Iustin Pop
defaultLuxiSocket :: FilePath
98 a69ff623 Iustin Pop
defaultLuxiSocket = C.masterSocket
99 8e445e6d Iustin Pop
100 525bfb36 Iustin Pop
-- * Data types
101 525bfb36 Iustin Pop
102 0427285d Iustin Pop
-- | Command line options structure.
103 0427285d Iustin Pop
data Options = Options
104 df18fdfe Iustin Pop
    { optDataFile    :: Maybe FilePath -- ^ Path to the cluster data file
105 df18fdfe Iustin Pop
    , optDiskMoves   :: Bool           -- ^ Allow disk moves
106 8fcfb767 Guido Trotter
    , optInstMoves   :: Bool           -- ^ Allow instance moves
107 df18fdfe Iustin Pop
    , optDynuFile    :: Maybe FilePath -- ^ Optional file with dynamic use data
108 f0f21ec4 Iustin Pop
    , optEvacMode    :: Bool           -- ^ Enable evacuation mode
109 10f396e1 Iustin Pop
    , optExInst      :: [String]       -- ^ Instances to be excluded
110 df18fdfe Iustin Pop
    , optExTags      :: Maybe [String] -- ^ Tags to use for exclusion
111 1f9066c0 Iustin Pop
    , optExecJobs    :: Bool           -- ^ Execute the commands via Luxi
112 a423b510 Iustin Pop
    , optGroup       :: Maybe GroupID  -- ^ The UUID of the group to process
113 ddef0585 Guido Trotter
    , optSelInst     :: [String]       -- ^ Instances to be excluded
114 1f9066c0 Iustin Pop
    , optINodes      :: Int            -- ^ Nodes required for an instance
115 1f9066c0 Iustin Pop
    , optISpec       :: RSpec          -- ^ Requested instance specs
116 df18fdfe Iustin Pop
    , optLuxi        :: Maybe FilePath -- ^ Collect data from Luxi
117 df18fdfe Iustin Pop
    , optMaster      :: String         -- ^ Collect data from RAPI
118 df18fdfe Iustin Pop
    , optMaxLength   :: Int            -- ^ Stop after this many steps
119 1f9066c0 Iustin Pop
    , optMcpu        :: Double         -- ^ Max cpu ratio for nodes
120 1f9066c0 Iustin Pop
    , optMdsk        :: Double         -- ^ Max disk usage ratio for nodes
121 4f807a57 Iustin Pop
    , optMinGain     :: Score          -- ^ Min gain we aim for in a step
122 4f807a57 Iustin Pop
    , optMinGainLim  :: Score          -- ^ Limit below which we apply mingain
123 df18fdfe Iustin Pop
    , optMinScore    :: Score          -- ^ The minimum score we aim for
124 df18fdfe Iustin Pop
    , optNoHeaders   :: Bool           -- ^ Do not show a header line
125 9983063b Iustin Pop
    , optNodeSim     :: [String]       -- ^ Cluster simulation mode
126 df18fdfe Iustin Pop
    , optOffline     :: [String]       -- ^ Names of offline nodes
127 df18fdfe Iustin Pop
    , optOneline     :: Bool           -- ^ Switch output to a single line
128 df18fdfe Iustin Pop
    , optOutPath     :: FilePath       -- ^ Path to the output directory
129 02da9d07 Iustin Pop
    , optSaveCluster :: Maybe FilePath -- ^ Save cluster state to this file
130 df18fdfe Iustin Pop
    , optShowCmds    :: Maybe FilePath -- ^ Whether to show the command list
131 1f9066c0 Iustin Pop
    , optShowHelp    :: Bool           -- ^ Just show the help
132 df18fdfe Iustin Pop
    , optShowInsts   :: Bool           -- ^ Whether to show the instance map
133 df18fdfe Iustin Pop
    , optShowNodes   :: Maybe [String] -- ^ Whether to show node status
134 df18fdfe Iustin Pop
    , optShowVer     :: Bool           -- ^ Just show the program version
135 df18fdfe Iustin Pop
    , optTieredSpec  :: Maybe RSpec    -- ^ Requested specs for tiered mode
136 509809db Iustin Pop
    , optReplay      :: Maybe String   -- ^ Unittests: RNG state
137 df18fdfe Iustin Pop
    , optVerbose     :: Int            -- ^ Verbosity level
138 0427285d Iustin Pop
    } deriving Show
139 0427285d Iustin Pop
140 0427285d Iustin Pop
-- | Default values for the command line options.
141 0427285d Iustin Pop
defaultOptions :: Options
142 0427285d Iustin Pop
defaultOptions  = Options
143 df18fdfe Iustin Pop
 { optDataFile    = Nothing
144 df18fdfe Iustin Pop
 , optDiskMoves   = True
145 8fcfb767 Guido Trotter
 , optInstMoves   = True
146 df18fdfe Iustin Pop
 , optDynuFile    = Nothing
147 f0f21ec4 Iustin Pop
 , optEvacMode    = False
148 10f396e1 Iustin Pop
 , optExInst      = []
149 df18fdfe Iustin Pop
 , optExTags      = Nothing
150 1f9066c0 Iustin Pop
 , optExecJobs    = False
151 a423b510 Iustin Pop
 , optGroup       = Nothing
152 ddef0585 Guido Trotter
 , optSelInst     = []
153 1f9066c0 Iustin Pop
 , optINodes      = 2
154 1f9066c0 Iustin Pop
 , optISpec       = RSpec 1 4096 102400
155 df18fdfe Iustin Pop
 , optLuxi        = Nothing
156 df18fdfe Iustin Pop
 , optMaster      = ""
157 df18fdfe Iustin Pop
 , optMaxLength   = -1
158 f4c0b8c5 Iustin Pop
 , optMcpu        = defVcpuRatio
159 f4c0b8c5 Iustin Pop
 , optMdsk        = defReservedDiskRatio
160 4f807a57 Iustin Pop
 , optMinGain     = 1e-2
161 4f807a57 Iustin Pop
 , optMinGainLim  = 1e-1
162 df18fdfe Iustin Pop
 , optMinScore    = 1e-9
163 df18fdfe Iustin Pop
 , optNoHeaders   = False
164 9983063b Iustin Pop
 , optNodeSim     = []
165 df18fdfe Iustin Pop
 , optOffline     = []
166 df18fdfe Iustin Pop
 , optOneline     = False
167 df18fdfe Iustin Pop
 , optOutPath     = "."
168 02da9d07 Iustin Pop
 , optSaveCluster = Nothing
169 df18fdfe Iustin Pop
 , optShowCmds    = Nothing
170 1f9066c0 Iustin Pop
 , optShowHelp    = False
171 df18fdfe Iustin Pop
 , optShowInsts   = False
172 df18fdfe Iustin Pop
 , optShowNodes   = Nothing
173 df18fdfe Iustin Pop
 , optShowVer     = False
174 df18fdfe Iustin Pop
 , optTieredSpec  = Nothing
175 509809db Iustin Pop
 , optReplay      = Nothing
176 df18fdfe Iustin Pop
 , optVerbose     = 1
177 0427285d Iustin Pop
 }
178 0427285d Iustin Pop
179 525bfb36 Iustin Pop
-- | Abrreviation for the option type.
180 2f567ac0 Iustin Pop
type OptType = OptDescr (Options -> Result Options)
181 0427285d Iustin Pop
182 525bfb36 Iustin Pop
-- * Command line options
183 525bfb36 Iustin Pop
184 16c2369c Iustin Pop
oDataFile :: OptType
185 16c2369c Iustin Pop
oDataFile = Option "t" ["text-data"]
186 16c2369c Iustin Pop
            (ReqArg (\ f o -> Ok o { optDataFile = Just f }) "FILE")
187 16c2369c Iustin Pop
            "the cluster data FILE"
188 0427285d Iustin Pop
189 df18fdfe Iustin Pop
oDiskMoves :: OptType
190 df18fdfe Iustin Pop
oDiskMoves = Option "" ["no-disk-moves"]
191 df18fdfe Iustin Pop
             (NoArg (\ opts -> Ok opts { optDiskMoves = False}))
192 df18fdfe Iustin Pop
             "disallow disk moves from the list of allowed instance changes,\
193 df18fdfe Iustin Pop
             \ thus allowing only the 'cheap' failover/migrate operations"
194 b2278348 Iustin Pop
195 ddef0585 Guido Trotter
oSelInst :: OptType
196 ddef0585 Guido Trotter
oSelInst = Option "" ["select-instances"]
197 ddef0585 Guido Trotter
          (ReqArg (\ f opts -> Ok opts { optSelInst = sepSplit ',' f }) "INSTS")
198 ddef0585 Guido Trotter
          "only select given instances for any moves"
199 ddef0585 Guido Trotter
200 8fcfb767 Guido Trotter
oInstMoves :: OptType
201 8fcfb767 Guido Trotter
oInstMoves = Option "" ["no-instance-moves"]
202 8fcfb767 Guido Trotter
             (NoArg (\ opts -> Ok opts { optInstMoves = False}))
203 8fcfb767 Guido Trotter
             "disallow instance (primary node) moves from the list of allowed,\
204 8fcfb767 Guido Trotter
             \ instance changes, thus allowing only slower, but sometimes\
205 8fcfb767 Guido Trotter
             \ safer, drbd secondary changes"
206 8fcfb767 Guido Trotter
207 df18fdfe Iustin Pop
oDynuFile :: OptType
208 df18fdfe Iustin Pop
oDynuFile = Option "U" ["dynu-file"]
209 df18fdfe Iustin Pop
            (ReqArg (\ f opts -> Ok opts { optDynuFile = Just f }) "FILE")
210 df18fdfe Iustin Pop
            "Import dynamic utilisation data from the given FILE"
211 0427285d Iustin Pop
212 f0f21ec4 Iustin Pop
oEvacMode :: OptType
213 f0f21ec4 Iustin Pop
oEvacMode = Option "E" ["evac-mode"]
214 f0f21ec4 Iustin Pop
            (NoArg (\opts -> Ok opts { optEvacMode = True }))
215 f0f21ec4 Iustin Pop
            "enable evacuation mode, where the algorithm only moves \
216 f0f21ec4 Iustin Pop
            \ instances away from offline and drained nodes"
217 f0f21ec4 Iustin Pop
218 10f396e1 Iustin Pop
oExInst :: OptType
219 10f396e1 Iustin Pop
oExInst = Option "" ["exclude-instances"]
220 10f396e1 Iustin Pop
          (ReqArg (\ f opts -> Ok opts { optExInst = sepSplit ',' f }) "INSTS")
221 6c30ce16 Guido Trotter
          "exclude given instances from any moves"
222 10f396e1 Iustin Pop
223 df18fdfe Iustin Pop
oExTags :: OptType
224 df18fdfe Iustin Pop
oExTags = Option "" ["exclusion-tags"]
225 df18fdfe Iustin Pop
            (ReqArg (\ f opts -> Ok opts { optExTags = Just $ sepSplit ',' f })
226 df18fdfe Iustin Pop
             "TAG,...") "Enable instance exclusion based on given tag prefix"
227 0427285d Iustin Pop
228 0df5a1b4 Iustin Pop
oExecJobs :: OptType
229 0df5a1b4 Iustin Pop
oExecJobs = Option "X" ["exec"]
230 2f567ac0 Iustin Pop
             (NoArg (\ opts -> Ok opts { optExecJobs = True}))
231 0df5a1b4 Iustin Pop
             "execute the suggested moves via Luxi (only available when using\
232 71e635f3 René Nussbaumer
             \ it for data gathering)"
233 0df5a1b4 Iustin Pop
234 a423b510 Iustin Pop
oGroup :: OptType
235 a423b510 Iustin Pop
oGroup = Option "G" ["group"]
236 a423b510 Iustin Pop
            (ReqArg (\ f o -> Ok o { optGroup = Just f }) "ID")
237 a423b510 Iustin Pop
            "the ID of the group to balance"
238 a423b510 Iustin Pop
239 df18fdfe Iustin Pop
oIDisk :: OptType
240 df18fdfe Iustin Pop
oIDisk = Option "" ["disk"]
241 df18fdfe Iustin Pop
         (ReqArg (\ d opts ->
242 df18fdfe Iustin Pop
                     let ospec = optISpec opts
243 df18fdfe Iustin Pop
                         nspec = ospec { rspecDsk = read d }
244 df18fdfe Iustin Pop
                     in Ok opts { optISpec = nspec }) "DISK")
245 df18fdfe Iustin Pop
         "disk size for instances"
246 0427285d Iustin Pop
247 0427285d Iustin Pop
oIMem :: OptType
248 0427285d Iustin Pop
oIMem = Option "" ["memory"]
249 1f9066c0 Iustin Pop
        (ReqArg (\ m opts ->
250 1f9066c0 Iustin Pop
                     let ospec = optISpec opts
251 1f9066c0 Iustin Pop
                         nspec = ospec { rspecMem = read m }
252 1f9066c0 Iustin Pop
                     in Ok opts { optISpec = nspec }) "MEMORY")
253 0427285d Iustin Pop
        "memory size for instances"
254 0427285d Iustin Pop
255 df18fdfe Iustin Pop
oINodes :: OptType
256 df18fdfe Iustin Pop
oINodes = Option "" ["req-nodes"]
257 df18fdfe Iustin Pop
          (ReqArg (\ n opts -> Ok opts { optINodes = read n }) "NODES")
258 df18fdfe Iustin Pop
          "number of nodes for the new instances (1=plain, 2=mirrored)"
259 0427285d Iustin Pop
260 0427285d Iustin Pop
oIVcpus :: OptType
261 0427285d Iustin Pop
oIVcpus = Option "" ["vcpus"]
262 1f9066c0 Iustin Pop
          (ReqArg (\ p opts ->
263 1f9066c0 Iustin Pop
                       let ospec = optISpec opts
264 1f9066c0 Iustin Pop
                           nspec = ospec { rspecCpu = read p }
265 1f9066c0 Iustin Pop
                       in Ok opts { optISpec = nspec }) "NUM")
266 0427285d Iustin Pop
          "number of virtual cpus for instances"
267 0427285d Iustin Pop
268 df18fdfe Iustin Pop
oLuxiSocket :: OptType
269 df18fdfe Iustin Pop
oLuxiSocket = Option "L" ["luxi"]
270 df18fdfe Iustin Pop
              (OptArg ((\ f opts -> Ok opts { optLuxi = Just f }) .
271 df18fdfe Iustin Pop
                       fromMaybe defaultLuxiSocket) "SOCKET")
272 df18fdfe Iustin Pop
              "collect data via Luxi, optionally using the given SOCKET path"
273 0427285d Iustin Pop
274 0427285d Iustin Pop
oMaxCpu :: OptType
275 0427285d Iustin Pop
oMaxCpu = Option "" ["max-cpu"]
276 2f567ac0 Iustin Pop
          (ReqArg (\ n opts -> Ok opts { optMcpu = read n }) "RATIO")
277 f4c0b8c5 Iustin Pop
          "maximum virtual-to-physical cpu ratio for nodes (from 1\
278 f4c0b8c5 Iustin Pop
          \ upwards) [64]"
279 0427285d Iustin Pop
280 df18fdfe Iustin Pop
oMaxSolLength :: OptType
281 df18fdfe Iustin Pop
oMaxSolLength = Option "l" ["max-length"]
282 df18fdfe Iustin Pop
                (ReqArg (\ i opts -> Ok opts { optMaxLength = read i }) "N")
283 df18fdfe Iustin Pop
                "cap the solution at this many moves (useful for very\
284 df18fdfe Iustin Pop
                \ unbalanced clusters)"
285 df18fdfe Iustin Pop
286 0427285d Iustin Pop
oMinDisk :: OptType
287 0427285d Iustin Pop
oMinDisk = Option "" ["min-disk"]
288 2f567ac0 Iustin Pop
           (ReqArg (\ n opts -> Ok opts { optMdsk = read n }) "RATIO")
289 f4c0b8c5 Iustin Pop
           "minimum free disk space for nodes (between 0 and 1) [0]"
290 0427285d Iustin Pop
291 4f807a57 Iustin Pop
oMinGain :: OptType
292 4f807a57 Iustin Pop
oMinGain = Option "g" ["min-gain"]
293 4f807a57 Iustin Pop
            (ReqArg (\ g opts -> Ok opts { optMinGain = read g }) "DELTA")
294 4f807a57 Iustin Pop
            "minimum gain to aim for in a balancing step before giving up"
295 4f807a57 Iustin Pop
296 4f807a57 Iustin Pop
oMinGainLim :: OptType
297 4f807a57 Iustin Pop
oMinGainLim = Option "" ["min-gain-limit"]
298 4f807a57 Iustin Pop
            (ReqArg (\ g opts -> Ok opts { optMinGainLim = read g }) "SCORE")
299 4f807a57 Iustin Pop
            "minimum cluster score for which we start checking the min-gain"
300 4f807a57 Iustin Pop
301 df18fdfe Iustin Pop
oMinScore :: OptType
302 df18fdfe Iustin Pop
oMinScore = Option "e" ["min-score"]
303 df18fdfe Iustin Pop
            (ReqArg (\ e opts -> Ok opts { optMinScore = read e }) "EPSILON")
304 4f807a57 Iustin Pop
            "mininum score to aim for"
305 c0501c69 Iustin Pop
306 df18fdfe Iustin Pop
oNoHeaders :: OptType
307 df18fdfe Iustin Pop
oNoHeaders = Option "" ["no-headers"]
308 df18fdfe Iustin Pop
             (NoArg (\ opts -> Ok opts { optNoHeaders = True }))
309 df18fdfe Iustin Pop
             "do not show a header line"
310 4f83a560 Iustin Pop
311 df18fdfe Iustin Pop
oNodeSim :: OptType
312 df18fdfe Iustin Pop
oNodeSim = Option "" ["simulate"]
313 9983063b Iustin Pop
            (ReqArg (\ f o -> Ok o { optNodeSim = f:optNodeSim o }) "SPEC")
314 df18fdfe Iustin Pop
            "simulate an empty cluster, given as 'num_nodes,disk,ram,cpu'"
315 df18fdfe Iustin Pop
316 df18fdfe Iustin Pop
oOfflineNode :: OptType
317 df18fdfe Iustin Pop
oOfflineNode = Option "O" ["offline"]
318 df18fdfe Iustin Pop
               (ReqArg (\ n o -> Ok o { optOffline = n:optOffline o }) "NODE")
319 df18fdfe Iustin Pop
               "set node as offline"
320 df18fdfe Iustin Pop
321 df18fdfe Iustin Pop
oOneline :: OptType
322 df18fdfe Iustin Pop
oOneline = Option "o" ["oneline"]
323 df18fdfe Iustin Pop
           (NoArg (\ opts -> Ok opts { optOneline = True }))
324 df18fdfe Iustin Pop
           "print the ganeti command list for reaching the solution"
325 df18fdfe Iustin Pop
326 df18fdfe Iustin Pop
oOutputDir :: OptType
327 df18fdfe Iustin Pop
oOutputDir = Option "d" ["output-dir"]
328 df18fdfe Iustin Pop
             (ReqArg (\ d opts -> Ok opts { optOutPath = d }) "PATH")
329 df18fdfe Iustin Pop
             "directory in which to write output files"
330 df18fdfe Iustin Pop
331 df18fdfe Iustin Pop
oPrintCommands :: OptType
332 df18fdfe Iustin Pop
oPrintCommands = Option "C" ["print-commands"]
333 df18fdfe Iustin Pop
                 (OptArg ((\ f opts -> Ok opts { optShowCmds = Just f }) .
334 df18fdfe Iustin Pop
                          fromMaybe "-")
335 df18fdfe Iustin Pop
                  "FILE")
336 df18fdfe Iustin Pop
                 "print the ganeti command list for reaching the solution,\
337 df18fdfe Iustin Pop
                 \ if an argument is passed then write the commands to a\
338 df18fdfe Iustin Pop
                 \ file named as such"
339 df18fdfe Iustin Pop
340 df18fdfe Iustin Pop
oPrintInsts :: OptType
341 df18fdfe Iustin Pop
oPrintInsts = Option "" ["print-instances"]
342 df18fdfe Iustin Pop
              (NoArg (\ opts -> Ok opts { optShowInsts = True }))
343 df18fdfe Iustin Pop
              "print the final instance map"
344 df18fdfe Iustin Pop
345 df18fdfe Iustin Pop
oPrintNodes :: OptType
346 df18fdfe Iustin Pop
oPrintNodes = Option "p" ["print-nodes"]
347 df18fdfe Iustin Pop
              (OptArg ((\ f opts ->
348 6dfa04fd Iustin Pop
                            let (prefix, realf) = case f of
349 6dfa04fd Iustin Pop
                                  '+':rest -> (["+"], rest)
350 6dfa04fd Iustin Pop
                                  _ -> ([], f)
351 6dfa04fd Iustin Pop
                                splitted = prefix ++ sepSplit ',' realf
352 df18fdfe Iustin Pop
                            in Ok opts { optShowNodes = Just splitted }) .
353 df18fdfe Iustin Pop
                       fromMaybe []) "FIELDS")
354 df18fdfe Iustin Pop
              "print the final node list"
355 df18fdfe Iustin Pop
356 df18fdfe Iustin Pop
oQuiet :: OptType
357 df18fdfe Iustin Pop
oQuiet = Option "q" ["quiet"]
358 df18fdfe Iustin Pop
         (NoArg (\ opts -> Ok opts { optVerbose = optVerbose opts - 1 }))
359 df18fdfe Iustin Pop
         "decrease the verbosity level"
360 df18fdfe Iustin Pop
361 df18fdfe Iustin Pop
oRapiMaster :: OptType
362 df18fdfe Iustin Pop
oRapiMaster = Option "m" ["master"]
363 df18fdfe Iustin Pop
              (ReqArg (\ m opts -> Ok opts { optMaster = m }) "ADDRESS")
364 df18fdfe Iustin Pop
              "collect data via RAPI at the given ADDRESS"
365 df18fdfe Iustin Pop
366 02da9d07 Iustin Pop
oSaveCluster :: OptType
367 02da9d07 Iustin Pop
oSaveCluster = Option "S" ["save"]
368 02da9d07 Iustin Pop
            (ReqArg (\ f opts -> Ok opts { optSaveCluster = Just f }) "FILE")
369 02da9d07 Iustin Pop
            "Save cluster state at the end of the processing to FILE"
370 02da9d07 Iustin Pop
371 df18fdfe Iustin Pop
oShowHelp :: OptType
372 df18fdfe Iustin Pop
oShowHelp = Option "h" ["help"]
373 df18fdfe Iustin Pop
            (NoArg (\ opts -> Ok opts { optShowHelp = True}))
374 df18fdfe Iustin Pop
            "show help"
375 df18fdfe Iustin Pop
376 df18fdfe Iustin Pop
oShowVer :: OptType
377 df18fdfe Iustin Pop
oShowVer = Option "V" ["version"]
378 df18fdfe Iustin Pop
           (NoArg (\ opts -> Ok opts { optShowVer = True}))
379 df18fdfe Iustin Pop
           "show the version of the program"
380 0f15cc76 Iustin Pop
381 1f9066c0 Iustin Pop
oTieredSpec :: OptType
382 1f9066c0 Iustin Pop
oTieredSpec = Option "" ["tiered-alloc"]
383 1f9066c0 Iustin Pop
             (ReqArg (\ inp opts -> do
384 1f9066c0 Iustin Pop
                          let sp = sepSplit ',' inp
385 1f9066c0 Iustin Pop
                          prs <- mapM (tryRead "tiered specs") sp
386 1f9066c0 Iustin Pop
                          tspec <-
387 1f9066c0 Iustin Pop
                              case prs of
388 7f4e37f0 Iustin Pop
                                [dsk, ram, cpu] -> return $ RSpec cpu ram dsk
389 03c6d8fa Iustin Pop
                                _ -> Bad $ "Invalid specification: " ++ inp ++
390 03c6d8fa Iustin Pop
                                     ", expected disk,ram,cpu"
391 1f9066c0 Iustin Pop
                          return $ opts { optTieredSpec = Just tspec } )
392 1f9066c0 Iustin Pop
              "TSPEC")
393 7f4e37f0 Iustin Pop
             "enable tiered specs allocation, given as 'disk,ram,cpu'"
394 1f9066c0 Iustin Pop
395 509809db Iustin Pop
oReplay :: OptType
396 509809db Iustin Pop
oReplay = Option "" ["replay"]
397 509809db Iustin Pop
          (ReqArg (\ stat opts -> Ok opts { optReplay = Just stat } ) "STATE")
398 509809db Iustin Pop
          "Pre-seed the random number generator with STATE"
399 509809db Iustin Pop
400 df18fdfe Iustin Pop
oVerbose :: OptType
401 df18fdfe Iustin Pop
oVerbose = Option "v" ["verbose"]
402 df18fdfe Iustin Pop
           (NoArg (\ opts -> Ok opts { optVerbose = optVerbose opts + 1 }))
403 df18fdfe Iustin Pop
           "increase the verbosity level"
404 fae371cc Iustin Pop
405 525bfb36 Iustin Pop
-- * Functions
406 525bfb36 Iustin Pop
407 525bfb36 Iustin Pop
-- | Usage info.
408 0427285d Iustin Pop
usageHelp :: String -> [OptType] -> String
409 9f6dcdea Iustin Pop
usageHelp progname =
410 78694255 Iustin Pop
    usageInfo (printf "%s %s\nUsage: %s [OPTION...]"
411 9f6dcdea Iustin Pop
               progname Version.version progname)
412 78694255 Iustin Pop
413 525bfb36 Iustin Pop
-- | Command line parser, using the 'Options' structure.
414 0427285d Iustin Pop
parseOpts :: [String]               -- ^ The command line arguments
415 0427285d Iustin Pop
          -> String                 -- ^ The program name
416 0427285d Iustin Pop
          -> [OptType]              -- ^ The supported command line options
417 0427285d Iustin Pop
          -> IO (Options, [String]) -- ^ The resulting options and leftover
418 0427285d Iustin Pop
                                    -- arguments
419 0427285d Iustin Pop
parseOpts argv progname options =
420 209b3711 Iustin Pop
    case getOpt Permute options argv of
421 209b3711 Iustin Pop
      (o, n, []) ->
422 209b3711 Iustin Pop
          do
423 2f567ac0 Iustin Pop
            let (pr, args) = (foldM (flip id) defaultOptions o, n)
424 2f567ac0 Iustin Pop
            po <- (case pr of
425 2f567ac0 Iustin Pop
                     Bad msg -> do
426 2f567ac0 Iustin Pop
                       hPutStrLn stderr "Error while parsing command\
427 2f567ac0 Iustin Pop
                                        \line arguments:"
428 2f567ac0 Iustin Pop
                       hPutStrLn stderr msg
429 2f567ac0 Iustin Pop
                       exitWith $ ExitFailure 1
430 2f567ac0 Iustin Pop
                     Ok val -> return val)
431 0427285d Iustin Pop
            when (optShowHelp po) $ do
432 78694255 Iustin Pop
              putStr $ usageHelp progname options
433 209b3711 Iustin Pop
              exitWith ExitSuccess
434 0427285d Iustin Pop
            when (optShowVer po) $ do
435 75d1edf8 Iustin Pop
              printf "%s %s\ncompiled with %s %s\nrunning on %s %s\n"
436 75d1edf8 Iustin Pop
                     progname Version.version
437 75d1edf8 Iustin Pop
                     compilerName (Data.Version.showVersion compilerVersion)
438 c939b58e Iustin Pop
                     os arch :: IO ()
439 75d1edf8 Iustin Pop
              exitWith ExitSuccess
440 2f567ac0 Iustin Pop
            return (po, args)
441 f723de38 Iustin Pop
      (_, _, errs) -> do
442 f723de38 Iustin Pop
        hPutStrLn stderr $ "Command line error: "  ++ concat errs
443 f723de38 Iustin Pop
        hPutStrLn stderr $ usageHelp progname options
444 f723de38 Iustin Pop
        exitWith $ ExitFailure 2
445 209b3711 Iustin Pop
446 9188aeef Iustin Pop
-- | A shell script template for autogenerated scripts.
447 e0eb63f0 Iustin Pop
shTemplate :: String
448 e0eb63f0 Iustin Pop
shTemplate =
449 e0eb63f0 Iustin Pop
    printf "#!/bin/sh\n\n\
450 e0eb63f0 Iustin Pop
           \# Auto-generated script for executing cluster rebalancing\n\n\
451 e0eb63f0 Iustin Pop
           \# To stop, touch the file /tmp/stop-htools\n\n\
452 e0eb63f0 Iustin Pop
           \set -e\n\n\
453 e0eb63f0 Iustin Pop
           \check() {\n\
454 e0eb63f0 Iustin Pop
           \  if [ -f /tmp/stop-htools ]; then\n\
455 e0eb63f0 Iustin Pop
           \    echo 'Stop requested, exiting'\n\
456 e0eb63f0 Iustin Pop
           \    exit 0\n\
457 e0eb63f0 Iustin Pop
           \  fi\n\
458 e0eb63f0 Iustin Pop
           \}\n\n"
459 417f6b50 Iustin Pop
460 417f6b50 Iustin Pop
-- | Optionally print the node list.
461 417f6b50 Iustin Pop
maybePrintNodes :: Maybe [String]       -- ^ The field list
462 417f6b50 Iustin Pop
                -> String               -- ^ Informational message
463 417f6b50 Iustin Pop
                -> ([String] -> String) -- ^ Function to generate the listing
464 417f6b50 Iustin Pop
                -> IO ()
465 417f6b50 Iustin Pop
maybePrintNodes Nothing _ _ = return ()
466 417f6b50 Iustin Pop
maybePrintNodes (Just fields) msg fn = do
467 417f6b50 Iustin Pop
  hPutStrLn stderr ""
468 417f6b50 Iustin Pop
  hPutStrLn stderr (msg ++ " status:")
469 417f6b50 Iustin Pop
  hPutStrLn stderr $ fn fields
470 33e44f0c Iustin Pop
471 33e44f0c Iustin Pop
472 33e44f0c Iustin Pop
-- | Optionally print the instance list.
473 33e44f0c Iustin Pop
maybePrintInsts :: Bool   -- ^ Whether to print the instance list
474 33e44f0c Iustin Pop
                -> String -- ^ Type of the instance map (e.g. initial)
475 33e44f0c Iustin Pop
                -> String -- ^ The instance data
476 33e44f0c Iustin Pop
                -> IO ()
477 33e44f0c Iustin Pop
maybePrintInsts do_print msg instdata =
478 33e44f0c Iustin Pop
  when do_print $ do
479 33e44f0c Iustin Pop
    hPutStrLn stderr ""
480 33e44f0c Iustin Pop
    hPutStrLn stderr $ msg ++ " instance map:"
481 33e44f0c Iustin Pop
    hPutStr stderr instdata