29 |
29 |
import logging
|
30 |
30 |
import errno
|
31 |
31 |
import itertools
|
|
32 |
import shlex
|
32 |
33 |
from cStringIO import StringIO
|
33 |
34 |
|
34 |
35 |
from ganeti import utils
|
... | ... | |
1297 |
1298 |
]
|
1298 |
1299 |
|
1299 |
1300 |
|
1300 |
|
def _ParseArgs(argv, commands, aliases):
|
|
1301 |
def _ParseArgs(argv, commands, aliases, env_override):
|
1301 |
1302 |
"""Parser for the command line arguments.
|
1302 |
1303 |
|
1303 |
1304 |
This function parses the arguments and returns the function which
|
... | ... | |
1307 |
1308 |
@param commands: dictionary with special contents, see the design
|
1308 |
1309 |
doc for cmdline handling
|
1309 |
1310 |
@param aliases: dictionary with command aliases {'alias': 'target, ...}
|
|
1311 |
@param env_override: list of env variables allowed for default args
|
1310 |
1312 |
|
1311 |
1313 |
"""
|
|
1314 |
assert not (env_override - set(commands))
|
|
1315 |
|
1312 |
1316 |
if len(argv) == 0:
|
1313 |
1317 |
binary = "<command>"
|
1314 |
1318 |
else:
|
... | ... | |
1362 |
1366 |
|
1363 |
1367 |
cmd = aliases[cmd]
|
1364 |
1368 |
|
|
1369 |
if cmd in env_override:
|
|
1370 |
args_env_name = ("%s_%s" % (binary.replace("-", "_"), cmd)).upper()
|
|
1371 |
env_args = os.environ.get(args_env_name)
|
|
1372 |
if env_args:
|
|
1373 |
argv = utils.InsertAtPos(argv, 1, shlex.split(env_args))
|
|
1374 |
|
1365 |
1375 |
func, args_def, parser_opts, usage, description = commands[cmd]
|
1366 |
1376 |
parser = OptionParser(option_list=parser_opts + COMMON_OPTS,
|
1367 |
1377 |
description=description,
|
1368 |
1378 |
formatter=TitledHelpFormatter(),
|
1369 |
1379 |
usage="%%prog %s %s" % (cmd, usage))
|
1370 |
1380 |
parser.disable_interspersed_args()
|
1371 |
|
options, args = parser.parse_args()
|
|
1381 |
options, args = parser.parse_args(args=argv[1:])
|
1372 |
1382 |
|
1373 |
1383 |
if not _CheckArguments(cmd, args_def, args):
|
1374 |
1384 |
return None, None, None
|
... | ... | |
2002 |
2012 |
return retcode, obuf.getvalue().rstrip("\n")
|
2003 |
2013 |
|
2004 |
2014 |
|
2005 |
|
def GenericMain(commands, override=None, aliases=None):
|
|
2015 |
def GenericMain(commands, override=None, aliases=None,
|
|
2016 |
env_override=frozenset()):
|
2006 |
2017 |
"""Generic main function for all the gnt-* commands.
|
2007 |
2018 |
|
2008 |
|
Arguments:
|
2009 |
|
- commands: a dictionary with a special structure, see the design doc
|
2010 |
|
for command line handling.
|
2011 |
|
- override: if not None, we expect a dictionary with keys that will
|
2012 |
|
override command line options; this can be used to pass
|
2013 |
|
options from the scripts to generic functions
|
2014 |
|
- aliases: dictionary with command aliases {'alias': 'target, ...}
|
|
2019 |
@param commands: a dictionary with a special structure, see the design doc
|
|
2020 |
for command line handling.
|
|
2021 |
@param override: if not None, we expect a dictionary with keys that will
|
|
2022 |
override command line options; this can be used to pass
|
|
2023 |
options from the scripts to generic functions
|
|
2024 |
@param aliases: dictionary with command aliases {'alias': 'target, ...}
|
|
2025 |
@param env_override: list of environment names which are allowed to submit
|
|
2026 |
default args for commands
|
2015 |
2027 |
|
2016 |
2028 |
"""
|
2017 |
2029 |
# save the program name and the entire command line for later logging
|
... | ... | |
2030 |
2042 |
aliases = {}
|
2031 |
2043 |
|
2032 |
2044 |
try:
|
2033 |
|
func, options, args = _ParseArgs(sys.argv, commands, aliases)
|
|
2045 |
func, options, args = _ParseArgs(sys.argv, commands, aliases, env_override)
|
2034 |
2046 |
except errors.ParameterError, err:
|
2035 |
2047 |
result, err_msg = FormatError(err)
|
2036 |
2048 |
ToStderr(err_msg)
|