root / src / Ganeti / HTools / CLI.hs @ 2207220d
History | View | Annotate | Download (21.9 kB)
1 |
{-| Implementation of command-line functions. |
---|---|
2 |
|
3 |
This module holds the common command-line related functions for the |
4 |
binaries, separated into this module since "Ganeti.Utils" is |
5 |
used in many other places and this is more IO oriented. |
6 |
|
7 |
-} |
8 |
|
9 |
{- |
10 |
|
11 |
Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc. |
12 |
|
13 |
This program is free software; you can redistribute it and/or modify |
14 |
it under the terms of the GNU General Public License as published by |
15 |
the Free Software Foundation; either version 2 of the License, or |
16 |
(at your option) any later version. |
17 |
|
18 |
This program is distributed in the hope that it will be useful, but |
19 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
20 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
21 |
General Public License for more details. |
22 |
|
23 |
You should have received a copy of the GNU General Public License |
24 |
along with this program; if not, write to the Free Software |
25 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
26 |
02110-1301, USA. |
27 |
|
28 |
-} |
29 |
|
30 |
module Ganeti.HTools.CLI |
31 |
( Options(..) |
32 |
, OptType |
33 |
, defaultOptions |
34 |
, Ganeti.HTools.CLI.parseOpts |
35 |
, parseOptsInner |
36 |
, parseYesNo |
37 |
, parseISpecString |
38 |
, shTemplate |
39 |
, maybePrintNodes |
40 |
, maybePrintInsts |
41 |
, maybeShowWarnings |
42 |
, printKeys |
43 |
, printFinal |
44 |
, setNodeStatus |
45 |
-- * The options |
46 |
, oDataFile |
47 |
, oDiskMoves |
48 |
, oDiskTemplate |
49 |
, oSpindleUse |
50 |
, oDynuFile |
51 |
, oEvacMode |
52 |
, oExInst |
53 |
, oExTags |
54 |
, oExecJobs |
55 |
, oForce |
56 |
, oGroup |
57 |
, oIAllocSrc |
58 |
, oInstMoves |
59 |
, oJobDelay |
60 |
, genOLuxiSocket |
61 |
, oLuxiSocket |
62 |
, oMachineReadable |
63 |
, oMaxCpu |
64 |
, oMaxSolLength |
65 |
, oMinDisk |
66 |
, oMinGain |
67 |
, oMinGainLim |
68 |
, oMinScore |
69 |
, oNoHeaders |
70 |
, oNoSimulation |
71 |
, oNodeSim |
72 |
, oNodeTags |
73 |
, oOfflineNode |
74 |
, oOneStepOnly |
75 |
, oOutputDir |
76 |
, oPrintCommands |
77 |
, oPrintInsts |
78 |
, oPrintNodes |
79 |
, oQuiet |
80 |
, oRapiMaster |
81 |
, oSaveCluster |
82 |
, oSelInst |
83 |
, oShowHelp |
84 |
, oShowVer |
85 |
, oShowComp |
86 |
, oStdSpec |
87 |
, oTieredSpec |
88 |
, oVerbose |
89 |
, oPriority |
90 |
, genericOpts |
91 |
) where |
92 |
|
93 |
import Control.Monad |
94 |
import Data.Char (toUpper) |
95 |
import Data.Maybe (fromMaybe) |
96 |
import System.Console.GetOpt |
97 |
import System.IO |
98 |
import Text.Printf (printf) |
99 |
|
100 |
import qualified Ganeti.HTools.Container as Container |
101 |
import qualified Ganeti.HTools.Node as Node |
102 |
import qualified Ganeti.Path as Path |
103 |
import Ganeti.HTools.Types |
104 |
import Ganeti.BasicTypes |
105 |
import Ganeti.Common as Common |
106 |
import Ganeti.Types |
107 |
import Ganeti.Utils |
108 |
|
109 |
-- * Data types |
110 |
|
111 |
-- | Command line options structure. |
112 |
data Options = Options |
113 |
{ optDataFile :: Maybe FilePath -- ^ Path to the cluster data file |
114 |
, optDiskMoves :: Bool -- ^ Allow disk moves |
115 |
, optInstMoves :: Bool -- ^ Allow instance moves |
116 |
, optDiskTemplate :: Maybe DiskTemplate -- ^ Override for the disk template |
117 |
, optSpindleUse :: Maybe Int -- ^ Override for the spindle usage |
118 |
, optDynuFile :: Maybe FilePath -- ^ Optional file with dynamic use data |
119 |
, optEvacMode :: Bool -- ^ Enable evacuation mode |
120 |
, optExInst :: [String] -- ^ Instances to be excluded |
121 |
, optExTags :: Maybe [String] -- ^ Tags to use for exclusion |
122 |
, optExecJobs :: Bool -- ^ Execute the commands via Luxi |
123 |
, optForce :: Bool -- ^ Force the execution |
124 |
, optGroup :: Maybe GroupID -- ^ The UUID of the group to process |
125 |
, optIAllocSrc :: Maybe FilePath -- ^ The iallocation spec |
126 |
, optSelInst :: [String] -- ^ Instances to be excluded |
127 |
, optLuxi :: Maybe FilePath -- ^ Collect data from Luxi |
128 |
, optJobDelay :: Double -- ^ Delay before executing first job |
129 |
, optMachineReadable :: Bool -- ^ Output machine-readable format |
130 |
, optMaster :: String -- ^ Collect data from RAPI |
131 |
, optMaxLength :: Int -- ^ Stop after this many steps |
132 |
, optMcpu :: Maybe Double -- ^ Override max cpu ratio for nodes |
133 |
, optMdsk :: Double -- ^ Max disk usage ratio for nodes |
134 |
, optMinGain :: Score -- ^ Min gain we aim for in a step |
135 |
, optMinGainLim :: Score -- ^ Limit below which we apply mingain |
136 |
, optMinScore :: Score -- ^ The minimum score we aim for |
137 |
, optNoHeaders :: Bool -- ^ Do not show a header line |
138 |
, optNoSimulation :: Bool -- ^ Skip the rebalancing dry-run |
139 |
, optNodeSim :: [String] -- ^ Cluster simulation mode |
140 |
, optNodeTags :: Maybe [String] -- ^ List of node tags to restrict to |
141 |
, optOffline :: [String] -- ^ Names of offline nodes |
142 |
, optOneStepOnly :: Bool -- ^ Only do the first step |
143 |
, optOutPath :: FilePath -- ^ Path to the output directory |
144 |
, optSaveCluster :: Maybe FilePath -- ^ Save cluster state to this file |
145 |
, optShowCmds :: Maybe FilePath -- ^ Whether to show the command list |
146 |
, optShowHelp :: Bool -- ^ Just show the help |
147 |
, optShowComp :: Bool -- ^ Just show the completion info |
148 |
, optShowInsts :: Bool -- ^ Whether to show the instance map |
149 |
, optShowNodes :: Maybe [String] -- ^ Whether to show node status |
150 |
, optShowVer :: Bool -- ^ Just show the program version |
151 |
, optStdSpec :: Maybe RSpec -- ^ Requested standard specs |
152 |
, optTestCount :: Maybe Int -- ^ Optional test count override |
153 |
, optTieredSpec :: Maybe RSpec -- ^ Requested specs for tiered mode |
154 |
, optReplay :: Maybe String -- ^ Unittests: RNG state |
155 |
, optVerbose :: Int -- ^ Verbosity level |
156 |
, optPriority :: Maybe OpSubmitPriority -- ^ OpCode submit priority |
157 |
} deriving Show |
158 |
|
159 |
-- | Default values for the command line options. |
160 |
defaultOptions :: Options |
161 |
defaultOptions = Options |
162 |
{ optDataFile = Nothing |
163 |
, optDiskMoves = True |
164 |
, optInstMoves = True |
165 |
, optDiskTemplate = Nothing |
166 |
, optSpindleUse = Nothing |
167 |
, optDynuFile = Nothing |
168 |
, optEvacMode = False |
169 |
, optExInst = [] |
170 |
, optExTags = Nothing |
171 |
, optExecJobs = False |
172 |
, optForce = False |
173 |
, optGroup = Nothing |
174 |
, optIAllocSrc = Nothing |
175 |
, optSelInst = [] |
176 |
, optLuxi = Nothing |
177 |
, optJobDelay = 10 |
178 |
, optMachineReadable = False |
179 |
, optMaster = "" |
180 |
, optMaxLength = -1 |
181 |
, optMcpu = Nothing |
182 |
, optMdsk = defReservedDiskRatio |
183 |
, optMinGain = 1e-2 |
184 |
, optMinGainLim = 1e-1 |
185 |
, optMinScore = 1e-9 |
186 |
, optNoHeaders = False |
187 |
, optNoSimulation = False |
188 |
, optNodeSim = [] |
189 |
, optNodeTags = Nothing |
190 |
, optOffline = [] |
191 |
, optOneStepOnly = False |
192 |
, optOutPath = "." |
193 |
, optSaveCluster = Nothing |
194 |
, optShowCmds = Nothing |
195 |
, optShowHelp = False |
196 |
, optShowComp = False |
197 |
, optShowInsts = False |
198 |
, optShowNodes = Nothing |
199 |
, optShowVer = False |
200 |
, optStdSpec = Nothing |
201 |
, optTestCount = Nothing |
202 |
, optTieredSpec = Nothing |
203 |
, optReplay = Nothing |
204 |
, optVerbose = 1 |
205 |
, optPriority = Nothing |
206 |
} |
207 |
|
208 |
-- | Abbreviation for the option type. |
209 |
type OptType = GenericOptType Options |
210 |
|
211 |
instance StandardOptions Options where |
212 |
helpRequested = optShowHelp |
213 |
verRequested = optShowVer |
214 |
compRequested = optShowComp |
215 |
requestHelp o = o { optShowHelp = True } |
216 |
requestVer o = o { optShowVer = True } |
217 |
requestComp o = o { optShowComp = True } |
218 |
|
219 |
-- * Helper functions |
220 |
|
221 |
parseISpecString :: String -> String -> Result RSpec |
222 |
parseISpecString descr inp = do |
223 |
let sp = sepSplit ',' inp |
224 |
err = Bad ("Invalid " ++ descr ++ " specification: '" ++ inp ++ |
225 |
"', expected disk,ram,cpu") |
226 |
when (length sp /= 3) err |
227 |
prs <- mapM (\(fn, val) -> fn val) $ |
228 |
zip [ annotateResult (descr ++ " specs disk") . parseUnit |
229 |
, annotateResult (descr ++ " specs memory") . parseUnit |
230 |
, tryRead (descr ++ " specs cpus") |
231 |
] sp |
232 |
case prs of |
233 |
[dsk, ram, cpu] -> return $ RSpec cpu ram dsk |
234 |
_ -> err |
235 |
|
236 |
-- | Disk template choices. |
237 |
optComplDiskTemplate :: OptCompletion |
238 |
optComplDiskTemplate = OptComplChoices $ |
239 |
map diskTemplateToRaw [minBound..maxBound] |
240 |
|
241 |
-- * Command line options |
242 |
|
243 |
oDataFile :: OptType |
244 |
oDataFile = |
245 |
(Option "t" ["text-data"] |
246 |
(ReqArg (\ f o -> Ok o { optDataFile = Just f }) "FILE") |
247 |
"the cluster data FILE", |
248 |
OptComplFile) |
249 |
|
250 |
oDiskMoves :: OptType |
251 |
oDiskMoves = |
252 |
(Option "" ["no-disk-moves"] |
253 |
(NoArg (\ opts -> Ok opts { optDiskMoves = False})) |
254 |
"disallow disk moves from the list of allowed instance changes,\ |
255 |
\ thus allowing only the 'cheap' failover/migrate operations", |
256 |
OptComplNone) |
257 |
|
258 |
oDiskTemplate :: OptType |
259 |
oDiskTemplate = |
260 |
(Option "" ["disk-template"] |
261 |
(reqWithConversion diskTemplateFromRaw |
262 |
(\dt opts -> Ok opts { optDiskTemplate = Just dt }) |
263 |
"TEMPLATE") "select the desired disk template", |
264 |
optComplDiskTemplate) |
265 |
|
266 |
oSpindleUse :: OptType |
267 |
oSpindleUse = |
268 |
(Option "" ["spindle-use"] |
269 |
(reqWithConversion (tryRead "parsing spindle-use") |
270 |
(\su opts -> do |
271 |
when (su < 0) $ |
272 |
fail "Invalid value of the spindle-use (expected >= 0)" |
273 |
return $ opts { optSpindleUse = Just su }) |
274 |
"SPINDLES") "select how many virtual spindle instances use\ |
275 |
\ [default read from cluster]", |
276 |
OptComplFloat) |
277 |
|
278 |
oSelInst :: OptType |
279 |
oSelInst = |
280 |
(Option "" ["select-instances"] |
281 |
(ReqArg (\ f opts -> Ok opts { optSelInst = sepSplit ',' f }) "INSTS") |
282 |
"only select given instances for any moves", |
283 |
OptComplManyInstances) |
284 |
|
285 |
oInstMoves :: OptType |
286 |
oInstMoves = |
287 |
(Option "" ["no-instance-moves"] |
288 |
(NoArg (\ opts -> Ok opts { optInstMoves = False})) |
289 |
"disallow instance (primary node) moves from the list of allowed,\ |
290 |
\ instance changes, thus allowing only slower, but sometimes\ |
291 |
\ safer, drbd secondary changes", |
292 |
OptComplNone) |
293 |
|
294 |
oDynuFile :: OptType |
295 |
oDynuFile = |
296 |
(Option "U" ["dynu-file"] |
297 |
(ReqArg (\ f opts -> Ok opts { optDynuFile = Just f }) "FILE") |
298 |
"Import dynamic utilisation data from the given FILE", |
299 |
OptComplFile) |
300 |
|
301 |
oEvacMode :: OptType |
302 |
oEvacMode = |
303 |
(Option "E" ["evac-mode"] |
304 |
(NoArg (\opts -> Ok opts { optEvacMode = True })) |
305 |
"enable evacuation mode, where the algorithm only moves\ |
306 |
\ instances away from offline and drained nodes", |
307 |
OptComplNone) |
308 |
|
309 |
oExInst :: OptType |
310 |
oExInst = |
311 |
(Option "" ["exclude-instances"] |
312 |
(ReqArg (\ f opts -> Ok opts { optExInst = sepSplit ',' f }) "INSTS") |
313 |
"exclude given instances from any moves", |
314 |
OptComplManyInstances) |
315 |
|
316 |
oExTags :: OptType |
317 |
oExTags = |
318 |
(Option "" ["exclusion-tags"] |
319 |
(ReqArg (\ f opts -> Ok opts { optExTags = Just $ sepSplit ',' f }) |
320 |
"TAG,...") "Enable instance exclusion based on given tag prefix", |
321 |
OptComplString) |
322 |
|
323 |
oExecJobs :: OptType |
324 |
oExecJobs = |
325 |
(Option "X" ["exec"] |
326 |
(NoArg (\ opts -> Ok opts { optExecJobs = True})) |
327 |
"execute the suggested moves via Luxi (only available when using\ |
328 |
\ it for data gathering)", |
329 |
OptComplNone) |
330 |
|
331 |
oForce :: OptType |
332 |
oForce = |
333 |
(Option "f" ["force"] |
334 |
(NoArg (\ opts -> Ok opts {optForce = True})) |
335 |
"force the execution of this program, even if warnings would\ |
336 |
\ otherwise prevent it", |
337 |
OptComplNone) |
338 |
|
339 |
oGroup :: OptType |
340 |
oGroup = |
341 |
(Option "G" ["group"] |
342 |
(ReqArg (\ f o -> Ok o { optGroup = Just f }) "ID") |
343 |
"the target node group (name or UUID)", |
344 |
OptComplOneGroup) |
345 |
|
346 |
oIAllocSrc :: OptType |
347 |
oIAllocSrc = |
348 |
(Option "I" ["ialloc-src"] |
349 |
(ReqArg (\ f opts -> Ok opts { optIAllocSrc = Just f }) "FILE") |
350 |
"Specify an iallocator spec as the cluster data source", |
351 |
OptComplFile) |
352 |
|
353 |
oJobDelay :: OptType |
354 |
oJobDelay = |
355 |
(Option "" ["job-delay"] |
356 |
(reqWithConversion (tryRead "job delay") |
357 |
(\d opts -> Ok opts { optJobDelay = d }) "SECONDS") |
358 |
"insert this much delay before the execution of repair jobs\ |
359 |
\ to allow the tool to continue processing instances", |
360 |
OptComplFloat) |
361 |
|
362 |
genOLuxiSocket :: String -> OptType |
363 |
genOLuxiSocket defSocket = |
364 |
(Option "L" ["luxi"] |
365 |
(OptArg ((\ f opts -> Ok opts { optLuxi = Just f }) . |
366 |
fromMaybe defSocket) "SOCKET") |
367 |
("collect data via Luxi, optionally using the given SOCKET path [" ++ |
368 |
defSocket ++ "]"), |
369 |
OptComplFile) |
370 |
|
371 |
oLuxiSocket :: IO OptType |
372 |
oLuxiSocket = liftM genOLuxiSocket Path.defaultLuxiSocket |
373 |
|
374 |
oMachineReadable :: OptType |
375 |
oMachineReadable = |
376 |
(Option "" ["machine-readable"] |
377 |
(OptArg (\ f opts -> do |
378 |
flag <- parseYesNo True f |
379 |
return $ opts { optMachineReadable = flag }) "CHOICE") |
380 |
"enable machine readable output (pass either 'yes' or 'no' to\ |
381 |
\ explicitly control the flag, or without an argument defaults to\ |
382 |
\ yes", |
383 |
optComplYesNo) |
384 |
|
385 |
oMaxCpu :: OptType |
386 |
oMaxCpu = |
387 |
(Option "" ["max-cpu"] |
388 |
(reqWithConversion (tryRead "parsing max-cpu") |
389 |
(\mcpu opts -> do |
390 |
when (mcpu <= 0) $ |
391 |
fail "Invalid value of the max-cpu ratio, expected >0" |
392 |
return $ opts { optMcpu = Just mcpu }) "RATIO") |
393 |
"maximum virtual-to-physical cpu ratio for nodes (from 0\ |
394 |
\ upwards) [default read from cluster]", |
395 |
OptComplFloat) |
396 |
|
397 |
oMaxSolLength :: OptType |
398 |
oMaxSolLength = |
399 |
(Option "l" ["max-length"] |
400 |
(reqWithConversion (tryRead "max solution length") |
401 |
(\i opts -> Ok opts { optMaxLength = i }) "N") |
402 |
"cap the solution at this many balancing or allocation\ |
403 |
\ rounds (useful for very unbalanced clusters or empty\ |
404 |
\ clusters)", |
405 |
OptComplInteger) |
406 |
|
407 |
oMinDisk :: OptType |
408 |
oMinDisk = |
409 |
(Option "" ["min-disk"] |
410 |
(reqWithConversion (tryRead "min free disk space") |
411 |
(\n opts -> Ok opts { optMdsk = n }) "RATIO") |
412 |
"minimum free disk space for nodes (between 0 and 1) [0]", |
413 |
OptComplFloat) |
414 |
|
415 |
oMinGain :: OptType |
416 |
oMinGain = |
417 |
(Option "g" ["min-gain"] |
418 |
(reqWithConversion (tryRead "min gain") |
419 |
(\g opts -> Ok opts { optMinGain = g }) "DELTA") |
420 |
"minimum gain to aim for in a balancing step before giving up", |
421 |
OptComplFloat) |
422 |
|
423 |
oMinGainLim :: OptType |
424 |
oMinGainLim = |
425 |
(Option "" ["min-gain-limit"] |
426 |
(reqWithConversion (tryRead "min gain limit") |
427 |
(\g opts -> Ok opts { optMinGainLim = g }) "SCORE") |
428 |
"minimum cluster score for which we start checking the min-gain", |
429 |
OptComplFloat) |
430 |
|
431 |
oMinScore :: OptType |
432 |
oMinScore = |
433 |
(Option "e" ["min-score"] |
434 |
(reqWithConversion (tryRead "min score") |
435 |
(\e opts -> Ok opts { optMinScore = e }) "EPSILON") |
436 |
"mininum score to aim for", |
437 |
OptComplFloat) |
438 |
|
439 |
oNoHeaders :: OptType |
440 |
oNoHeaders = |
441 |
(Option "" ["no-headers"] |
442 |
(NoArg (\ opts -> Ok opts { optNoHeaders = True })) |
443 |
"do not show a header line", |
444 |
OptComplNone) |
445 |
|
446 |
oNoSimulation :: OptType |
447 |
oNoSimulation = |
448 |
(Option "" ["no-simulation"] |
449 |
(NoArg (\opts -> Ok opts {optNoSimulation = True})) |
450 |
"do not perform rebalancing simulation", |
451 |
OptComplNone) |
452 |
|
453 |
oNodeSim :: OptType |
454 |
oNodeSim = |
455 |
(Option "" ["simulate"] |
456 |
(ReqArg (\ f o -> Ok o { optNodeSim = f:optNodeSim o }) "SPEC") |
457 |
"simulate an empty cluster, given as\ |
458 |
\ 'alloc_policy,num_nodes,disk,ram,cpu'", |
459 |
OptComplString) |
460 |
|
461 |
oNodeTags :: OptType |
462 |
oNodeTags = |
463 |
(Option "" ["node-tags"] |
464 |
(ReqArg (\ f opts -> Ok opts { optNodeTags = Just $ sepSplit ',' f }) |
465 |
"TAG,...") "Restrict to nodes with the given tags", |
466 |
OptComplString) |
467 |
|
468 |
oOfflineNode :: OptType |
469 |
oOfflineNode = |
470 |
(Option "O" ["offline"] |
471 |
(ReqArg (\ n o -> Ok o { optOffline = n:optOffline o }) "NODE") |
472 |
"set node as offline", |
473 |
OptComplOneNode) |
474 |
|
475 |
oOneStepOnly :: OptType |
476 |
oOneStepOnly = |
477 |
(Option "" ["one-step-only"] |
478 |
(NoArg (\ opts -> Ok opts {optOneStepOnly = True})) |
479 |
"Only do the first step", |
480 |
OptComplNone) |
481 |
|
482 |
oOutputDir :: OptType |
483 |
oOutputDir = |
484 |
(Option "d" ["output-dir"] |
485 |
(ReqArg (\ d opts -> Ok opts { optOutPath = d }) "PATH") |
486 |
"directory in which to write output files", |
487 |
OptComplDir) |
488 |
|
489 |
oPrintCommands :: OptType |
490 |
oPrintCommands = |
491 |
(Option "C" ["print-commands"] |
492 |
(OptArg ((\ f opts -> Ok opts { optShowCmds = Just f }) . |
493 |
fromMaybe "-") |
494 |
"FILE") |
495 |
"print the ganeti command list for reaching the solution,\ |
496 |
\ if an argument is passed then write the commands to a\ |
497 |
\ file named as such", |
498 |
OptComplNone) |
499 |
|
500 |
oPrintInsts :: OptType |
501 |
oPrintInsts = |
502 |
(Option "" ["print-instances"] |
503 |
(NoArg (\ opts -> Ok opts { optShowInsts = True })) |
504 |
"print the final instance map", |
505 |
OptComplNone) |
506 |
|
507 |
oPrintNodes :: OptType |
508 |
oPrintNodes = |
509 |
(Option "p" ["print-nodes"] |
510 |
(OptArg ((\ f opts -> |
511 |
let (prefix, realf) = case f of |
512 |
'+':rest -> (["+"], rest) |
513 |
_ -> ([], f) |
514 |
splitted = prefix ++ sepSplit ',' realf |
515 |
in Ok opts { optShowNodes = Just splitted }) . |
516 |
fromMaybe []) "FIELDS") |
517 |
"print the final node list", |
518 |
OptComplNone) |
519 |
|
520 |
oQuiet :: OptType |
521 |
oQuiet = |
522 |
(Option "q" ["quiet"] |
523 |
(NoArg (\ opts -> Ok opts { optVerbose = optVerbose opts - 1 })) |
524 |
"decrease the verbosity level", |
525 |
OptComplNone) |
526 |
|
527 |
oRapiMaster :: OptType |
528 |
oRapiMaster = |
529 |
(Option "m" ["master"] |
530 |
(ReqArg (\ m opts -> Ok opts { optMaster = m }) "ADDRESS") |
531 |
"collect data via RAPI at the given ADDRESS", |
532 |
OptComplHost) |
533 |
|
534 |
oSaveCluster :: OptType |
535 |
oSaveCluster = |
536 |
(Option "S" ["save"] |
537 |
(ReqArg (\ f opts -> Ok opts { optSaveCluster = Just f }) "FILE") |
538 |
"Save cluster state at the end of the processing to FILE", |
539 |
OptComplNone) |
540 |
|
541 |
oStdSpec :: OptType |
542 |
oStdSpec = |
543 |
(Option "" ["standard-alloc"] |
544 |
(ReqArg (\ inp opts -> do |
545 |
tspec <- parseISpecString "standard" inp |
546 |
return $ opts { optStdSpec = Just tspec } ) |
547 |
"STDSPEC") |
548 |
"enable standard specs allocation, given as 'disk,ram,cpu'", |
549 |
OptComplString) |
550 |
|
551 |
oTieredSpec :: OptType |
552 |
oTieredSpec = |
553 |
(Option "" ["tiered-alloc"] |
554 |
(ReqArg (\ inp opts -> do |
555 |
tspec <- parseISpecString "tiered" inp |
556 |
return $ opts { optTieredSpec = Just tspec } ) |
557 |
"TSPEC") |
558 |
"enable tiered specs allocation, given as 'disk,ram,cpu'", |
559 |
OptComplString) |
560 |
|
561 |
oVerbose :: OptType |
562 |
oVerbose = |
563 |
(Option "v" ["verbose"] |
564 |
(NoArg (\ opts -> Ok opts { optVerbose = optVerbose opts + 1 })) |
565 |
"increase the verbosity level", |
566 |
OptComplNone) |
567 |
|
568 |
oPriority :: OptType |
569 |
oPriority = |
570 |
(Option "" ["priority"] |
571 |
(ReqArg (\ inp opts -> do |
572 |
prio <- parseSubmitPriority inp |
573 |
Ok opts { optPriority = Just prio }) "PRIO") |
574 |
"set the priority of submitted jobs", |
575 |
OptComplChoices (map fmtSubmitPriority [minBound..maxBound])) |
576 |
|
577 |
-- | Generic options. |
578 |
genericOpts :: [GenericOptType Options] |
579 |
genericOpts = [ oShowVer |
580 |
, oShowHelp |
581 |
, oShowComp |
582 |
] |
583 |
|
584 |
-- * Functions |
585 |
|
586 |
-- | Wrapper over 'Common.parseOpts' with our custom options. |
587 |
parseOpts :: [String] -- ^ The command line arguments |
588 |
-> String -- ^ The program name |
589 |
-> [OptType] -- ^ The supported command line options |
590 |
-> [ArgCompletion] -- ^ The supported command line arguments |
591 |
-> IO (Options, [String]) -- ^ The resulting options and leftover |
592 |
-- arguments |
593 |
parseOpts = Common.parseOpts defaultOptions |
594 |
|
595 |
|
596 |
-- | A shell script template for autogenerated scripts. |
597 |
shTemplate :: String |
598 |
shTemplate = |
599 |
printf "#!/bin/sh\n\n\ |
600 |
\# Auto-generated script for executing cluster rebalancing\n\n\ |
601 |
\# To stop, touch the file /tmp/stop-htools\n\n\ |
602 |
\set -e\n\n\ |
603 |
\check() {\n\ |
604 |
\ if [ -f /tmp/stop-htools ]; then\n\ |
605 |
\ echo 'Stop requested, exiting'\n\ |
606 |
\ exit 0\n\ |
607 |
\ fi\n\ |
608 |
\}\n\n" |
609 |
|
610 |
-- | Optionally print the node list. |
611 |
maybePrintNodes :: Maybe [String] -- ^ The field list |
612 |
-> String -- ^ Informational message |
613 |
-> ([String] -> String) -- ^ Function to generate the listing |
614 |
-> IO () |
615 |
maybePrintNodes Nothing _ _ = return () |
616 |
maybePrintNodes (Just fields) msg fn = do |
617 |
hPutStrLn stderr "" |
618 |
hPutStrLn stderr (msg ++ " status:") |
619 |
hPutStrLn stderr $ fn fields |
620 |
|
621 |
-- | Optionally print the instance list. |
622 |
maybePrintInsts :: Bool -- ^ Whether to print the instance list |
623 |
-> String -- ^ Type of the instance map (e.g. initial) |
624 |
-> String -- ^ The instance data |
625 |
-> IO () |
626 |
maybePrintInsts do_print msg instdata = |
627 |
when do_print $ do |
628 |
hPutStrLn stderr "" |
629 |
hPutStrLn stderr $ msg ++ " instance map:" |
630 |
hPutStr stderr instdata |
631 |
|
632 |
-- | Function to display warning messages from parsing the cluster |
633 |
-- state. |
634 |
maybeShowWarnings :: [String] -- ^ The warning messages |
635 |
-> IO () |
636 |
maybeShowWarnings fix_msgs = |
637 |
unless (null fix_msgs) $ do |
638 |
hPutStrLn stderr "Warning: cluster has inconsistent data:" |
639 |
hPutStrLn stderr . unlines . map (printf " - %s") $ fix_msgs |
640 |
|
641 |
-- | Format a list of key, value as a shell fragment. |
642 |
printKeys :: String -- ^ Prefix to printed variables |
643 |
-> [(String, String)] -- ^ List of (key, value) pairs to be printed |
644 |
-> IO () |
645 |
printKeys prefix = |
646 |
mapM_ (\(k, v) -> |
647 |
printf "%s_%s=%s\n" prefix (map toUpper k) (ensureQuoted v)) |
648 |
|
649 |
-- | Prints the final @OK@ marker in machine readable output. |
650 |
printFinal :: String -- ^ Prefix to printed variable |
651 |
-> Bool -- ^ Whether output should be machine readable; |
652 |
-- note: if not, there is nothing to print |
653 |
-> IO () |
654 |
printFinal prefix True = |
655 |
-- this should be the final entry |
656 |
printKeys prefix [("OK", "1")] |
657 |
|
658 |
printFinal _ False = return () |
659 |
|
660 |
-- | Potentially set the node as offline based on passed offline list. |
661 |
setNodeOffline :: [Ndx] -> Node.Node -> Node.Node |
662 |
setNodeOffline offline_indices n = |
663 |
if Node.idx n `elem` offline_indices |
664 |
then Node.setOffline n True |
665 |
else n |
666 |
|
667 |
-- | Set node properties based on command line options. |
668 |
setNodeStatus :: Options -> Node.List -> IO Node.List |
669 |
setNodeStatus opts fixed_nl = do |
670 |
let offline_passed = optOffline opts |
671 |
all_nodes = Container.elems fixed_nl |
672 |
offline_lkp = map (lookupName (map Node.name all_nodes)) offline_passed |
673 |
offline_wrong = filter (not . goodLookupResult) offline_lkp |
674 |
offline_names = map lrContent offline_lkp |
675 |
offline_indices = map Node.idx $ |
676 |
filter (\n -> Node.name n `elem` offline_names) |
677 |
all_nodes |
678 |
m_cpu = optMcpu opts |
679 |
m_dsk = optMdsk opts |
680 |
|
681 |
unless (null offline_wrong) . |
682 |
exitErr $ printf "wrong node name(s) set as offline: %s\n" |
683 |
(commaJoin (map lrContent offline_wrong)) |
684 |
let setMCpuFn = case m_cpu of |
685 |
Nothing -> id |
686 |
Just new_mcpu -> flip Node.setMcpu new_mcpu |
687 |
let nm = Container.map (setNodeOffline offline_indices . |
688 |
flip Node.setMdsk m_dsk . |
689 |
setMCpuFn) fixed_nl |
690 |
return nm |