Revision 73702ee7 lib/cli.py
b/lib/cli.py | ||
---|---|---|
26 | 26 |
import textwrap |
27 | 27 |
import os.path |
28 | 28 |
import copy |
29 |
from cStringIO import StringIO |
|
29 | 30 |
|
30 | 31 |
from ganeti import utils |
31 | 32 |
from ganeti import logger |
... | ... | |
42 | 43 |
"ARGS_NONE", "ARGS_FIXED", "ARGS_ATLEAST", "ARGS_ANY", "ARGS_ONE", |
43 | 44 |
"USEUNITS_OPT", "FIELDS_OPT", "FORCE_OPT", |
44 | 45 |
"ListTags", "AddTags", "RemoveTags", "TAG_SRC_OPT", |
46 |
"FormatError", |
|
45 | 47 |
] |
46 | 48 |
|
47 | 49 |
|
... | ... | |
351 | 353 |
return proc.ExecOpCode(op, feedback_fn) |
352 | 354 |
|
353 | 355 |
|
356 |
def FormatError(err): |
|
357 |
"""Return a formatted error message for a given error. |
|
358 |
|
|
359 |
This function takes an exception instance and returns a tuple |
|
360 |
consisting of two values: first, the recommended exit code, and |
|
361 |
second, a string describing the error message (not |
|
362 |
newline-terminated). |
|
363 |
|
|
364 |
""" |
|
365 |
retcode = 1 |
|
366 |
obuf = StringIO() |
|
367 |
if isinstance(err, errors.ConfigurationError): |
|
368 |
msg = "Corrupt configuration file: %s" % err |
|
369 |
logger.Error(msg) |
|
370 |
obuf.write(msg + "\n") |
|
371 |
obuf.write("Aborting.") |
|
372 |
retcode = 2 |
|
373 |
elif isinstance(err, errors.HooksAbort): |
|
374 |
obuf.write("Failure: hooks execution failed:\n") |
|
375 |
for node, script, out in err.args[0]: |
|
376 |
if out: |
|
377 |
obuf.write(" node: %s, script: %s, output: %s\n" % |
|
378 |
(node, script, out)) |
|
379 |
else: |
|
380 |
obuf.write(" node: %s, script: %s (no output)\n" % |
|
381 |
(node, script)) |
|
382 |
elif isinstance(err, errors.HooksFailure): |
|
383 |
obuf.write("Failure: hooks general failure: %s" % str(err)) |
|
384 |
elif isinstance(err, errors.ResolverError): |
|
385 |
this_host = utils.HostInfo.SysName() |
|
386 |
if err.args[0] == this_host: |
|
387 |
msg = "Failure: can't resolve my own hostname ('%s')" |
|
388 |
else: |
|
389 |
msg = "Failure: can't resolve hostname '%s'" |
|
390 |
obuf.write(msg % err.args[0]) |
|
391 |
elif isinstance(err, errors.OpPrereqError): |
|
392 |
obuf.write("Failure: prerequisites not met for this" |
|
393 |
" operation:\n%s" % str(err)) |
|
394 |
elif isinstance(err, errors.OpExecError): |
|
395 |
obuf.write("Failure: command execution error:\n%s" % str(err)) |
|
396 |
elif isinstance(err, errors.TagError): |
|
397 |
obuf.write("Failure: invalid tag(s) given:\n%s" % str(err)) |
|
398 |
elif isinstance(err, errors.GenericError): |
|
399 |
obuf.write("Unhandled Ganeti error: %s" % str(err)) |
|
400 |
else: |
|
401 |
obuf.write("Unhandled exception: %s" % str(err)) |
|
402 |
return retcode, obuf.getvalue().rstrip('\n') |
|
403 |
|
|
404 |
|
|
354 | 405 |
def GenericMain(commands, override=None): |
355 | 406 |
"""Generic main function for all the gnt-* commands. |
356 | 407 |
|
... | ... | |
398 | 449 |
try: |
399 | 450 |
try: |
400 | 451 |
result = func(options, args) |
401 |
except errors.ConfigurationError, err: |
|
402 |
logger.Error("Corrupt configuration file: %s" % err) |
|
403 |
logger.ToStderr("Aborting.") |
|
404 |
result = 2 |
|
405 |
except errors.HooksAbort, err: |
|
406 |
logger.ToStderr("Failure: hooks execution failed:") |
|
407 |
for node, script, out in err.args[0]: |
|
408 |
if out: |
|
409 |
logger.ToStderr(" node: %s, script: %s, output: %s" % |
|
410 |
(node, script, out)) |
|
411 |
else: |
|
412 |
logger.ToStderr(" node: %s, script: %s (no output)" % |
|
413 |
(node, script)) |
|
414 |
result = 1 |
|
415 |
except errors.HooksFailure, err: |
|
416 |
logger.ToStderr("Failure: hooks general failure: %s" % str(err)) |
|
417 |
result = 1 |
|
418 |
except errors.ResolverError, err: |
|
419 |
this_host = utils.HostInfo.SysName() |
|
420 |
if err.args[0] == this_host: |
|
421 |
msg = "Failure: can't resolve my own hostname ('%s')" |
|
422 |
else: |
|
423 |
msg = "Failure: can't resolve hostname '%s'" |
|
424 |
logger.ToStderr(msg % err.args[0]) |
|
425 |
result = 1 |
|
426 |
except errors.OpPrereqError, err: |
|
427 |
logger.ToStderr("Failure: prerequisites not met for this" |
|
428 |
" operation:\n%s" % str(err)) |
|
429 |
result = 1 |
|
430 |
except errors.OpExecError, err: |
|
431 |
logger.ToStderr("Failure: command execution error:\n%s" % str(err)) |
|
432 |
result = 1 |
|
433 |
except errors.TagError, err: |
|
434 |
logger.ToStderr("Failure: invalid tag(s) given:\n%s" % str(err)) |
|
435 |
result = 1 |
|
452 |
except errors.GenericError, err: |
|
453 |
result, err_msg = FormatError(err) |
|
454 |
logger.ToStderr(err_msg) |
|
436 | 455 |
finally: |
437 | 456 |
utils.Unlock('cmd') |
438 | 457 |
utils.LockCleanup() |
Also available in: Unified diff