Revision ef9fa5b9

b/lib/cli.py
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)
b/lib/client/gnt_group.py
34 34
_LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt", "alloc_policy", "ndparams"]
35 35

  
36 36

  
37
_ENV_OVERRIDE = frozenset(["list"])
38

  
39

  
37 40
def AddGroup(opts, args):
38 41
  """Add a node group to the cluster.
39 42

  
......
258 261

  
259 262
def Main():
260 263
  return GenericMain(commands,
261
                     override={"tag_type": constants.TAG_NODEGROUP})
264
                     override={"tag_type": constants.TAG_NODEGROUP},
265
                     env_override=_ENV_OVERRIDE)
b/lib/client/gnt_instance.py
64 64
  ]
65 65

  
66 66

  
67
_ENV_OVERRIDE = frozenset(["list"])
68

  
69

  
67 70
def _ExpandMultiNames(mode, names, client=None):
68 71
  """Expand the given names using the passed mode.
69 72

  
......
1552 1555

  
1553 1556
def Main():
1554 1557
  return GenericMain(commands, aliases=aliases,
1555
                     override={"tag_type": constants.TAG_INSTANCE})
1558
                     override={"tag_type": constants.TAG_INSTANCE},
1559
                     env_override=_ENV_OVERRIDE)
b/lib/client/gnt_node.py
106 106
                              constants.OOB_POWER_CYCLE])
107 107

  
108 108

  
109
_ENV_OVERRIDE = frozenset(["list"])
110

  
111

  
109 112
NONODE_SETUP_OPT = cli_option("--no-node-setup", default=True,
110 113
                              action="store_false", dest="node_setup",
111 114
                              help=("Do not make initial SSH setup on remote"
......
963 966

  
964 967

  
965 968
def Main():
966
  return GenericMain(commands, override={"tag_type": constants.TAG_NODE})
969
  return GenericMain(commands, override={"tag_type": constants.TAG_NODE},
970
                     env_override=_ENV_OVERRIDE)

Also available in: Unified diff