Statistics
| Branch: | Tag: | Revision:

root / lib / cli.py @ 5c947f38

History | View | Annotate | Download (10.2 kB)

1 a8083063 Iustin Pop
#!/usr/bin/python
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 a8083063 Iustin Pop
# Copyright (C) 2006, 2007 Google Inc.
5 a8083063 Iustin Pop
#
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
#
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
#
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 a8083063 Iustin Pop
"""Module dealing with command line parsing"""
23 a8083063 Iustin Pop
24 a8083063 Iustin Pop
25 a8083063 Iustin Pop
import sys
26 a8083063 Iustin Pop
import textwrap
27 a8083063 Iustin Pop
import os.path
28 a8083063 Iustin Pop
import copy
29 a8083063 Iustin Pop
30 a8083063 Iustin Pop
from ganeti import utils
31 a8083063 Iustin Pop
from ganeti import logger
32 a8083063 Iustin Pop
from ganeti import errors
33 a8083063 Iustin Pop
from ganeti import mcpu
34 a8083063 Iustin Pop
from ganeti import constants
35 a8083063 Iustin Pop
36 a8083063 Iustin Pop
from optparse import (OptionParser, make_option, TitledHelpFormatter,
37 a8083063 Iustin Pop
                      Option, OptionValueError, SUPPRESS_HELP)
38 a8083063 Iustin Pop
39 a8083063 Iustin Pop
__all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain", "SubmitOpCode",
40 137161c9 Michael Hanselmann
           "cli_option", "OutputTable",
41 a8083063 Iustin Pop
           "ARGS_NONE", "ARGS_FIXED", "ARGS_ATLEAST", "ARGS_ANY", "ARGS_ONE",
42 fe7b0351 Michael Hanselmann
           "USEUNITS_OPT", "FIELDS_OPT", "FORCE_OPT"]
43 a8083063 Iustin Pop
44 a8083063 Iustin Pop
DEBUG_OPT = make_option("-d", "--debug", default=False,
45 a8083063 Iustin Pop
                        action="store_true",
46 a8083063 Iustin Pop
                        help="Turn debugging on")
47 a8083063 Iustin Pop
48 a8083063 Iustin Pop
NOHDR_OPT = make_option("--no-headers", default=False,
49 a8083063 Iustin Pop
                        action="store_true", dest="no_headers",
50 a8083063 Iustin Pop
                        help="Don't display column headers")
51 a8083063 Iustin Pop
52 137161c9 Michael Hanselmann
SEP_OPT = make_option("--separator", default=None,
53 a8083063 Iustin Pop
                      action="store", dest="separator",
54 a8083063 Iustin Pop
                      help="Separator between output fields"
55 a8083063 Iustin Pop
                      " (defaults to one space)")
56 a8083063 Iustin Pop
57 a8083063 Iustin Pop
USEUNITS_OPT = make_option("--human-readable", default=False,
58 a8083063 Iustin Pop
                           action="store_true", dest="human_readable",
59 a8083063 Iustin Pop
                           help="Print sizes in human readable format")
60 a8083063 Iustin Pop
61 dcb93971 Michael Hanselmann
FIELDS_OPT = make_option("-o", "--output", dest="output", action="store",
62 dcb93971 Michael Hanselmann
                         type="string", help="Select output fields",
63 dcb93971 Michael Hanselmann
                         metavar="FIELDS")
64 dcb93971 Michael Hanselmann
65 fe7b0351 Michael Hanselmann
FORCE_OPT = make_option("-f", "--force", dest="force", action="store_true",
66 fe7b0351 Michael Hanselmann
                        default=False, help="Force the operation")
67 fe7b0351 Michael Hanselmann
68 a8083063 Iustin Pop
_LOCK_OPT = make_option("--lock-retries", default=None,
69 a8083063 Iustin Pop
                        type="int", help=SUPPRESS_HELP)
70 a8083063 Iustin Pop
71 a8083063 Iustin Pop
72 a8083063 Iustin Pop
def ARGS_FIXED(val):
73 a8083063 Iustin Pop
  """Macro-like function denoting a fixed number of arguments"""
74 a8083063 Iustin Pop
  return -val
75 a8083063 Iustin Pop
76 a8083063 Iustin Pop
77 a8083063 Iustin Pop
def ARGS_ATLEAST(val):
78 a8083063 Iustin Pop
  """Macro-like function denoting a minimum number of arguments"""
79 a8083063 Iustin Pop
  return val
80 a8083063 Iustin Pop
81 a8083063 Iustin Pop
82 a8083063 Iustin Pop
ARGS_NONE = None
83 a8083063 Iustin Pop
ARGS_ONE = ARGS_FIXED(1)
84 a8083063 Iustin Pop
ARGS_ANY = ARGS_ATLEAST(0)
85 a8083063 Iustin Pop
86 a8083063 Iustin Pop
87 a8083063 Iustin Pop
def check_unit(option, opt, value):
88 a8083063 Iustin Pop
  try:
89 a8083063 Iustin Pop
    return utils.ParseUnit(value)
90 a8083063 Iustin Pop
  except errors.UnitParseError, err:
91 a8083063 Iustin Pop
    raise OptionValueError, ("option %s: %s" % (opt, err))
92 a8083063 Iustin Pop
93 a8083063 Iustin Pop
94 a8083063 Iustin Pop
class CliOption(Option):
95 a8083063 Iustin Pop
  TYPES = Option.TYPES + ("unit",)
96 a8083063 Iustin Pop
  TYPE_CHECKER = copy.copy(Option.TYPE_CHECKER)
97 a8083063 Iustin Pop
  TYPE_CHECKER["unit"] = check_unit
98 a8083063 Iustin Pop
99 a8083063 Iustin Pop
100 a8083063 Iustin Pop
# optparse.py sets make_option, so we do it for our own option class, too
101 a8083063 Iustin Pop
cli_option = CliOption
102 a8083063 Iustin Pop
103 a8083063 Iustin Pop
104 a8083063 Iustin Pop
def _ParseArgs(argv, commands):
105 a8083063 Iustin Pop
  """Parses the command line and return the function which must be
106 a8083063 Iustin Pop
  executed together with its arguments
107 a8083063 Iustin Pop

108 a8083063 Iustin Pop
  Arguments:
109 a8083063 Iustin Pop
    argv: the command line
110 a8083063 Iustin Pop

111 a8083063 Iustin Pop
    commands: dictionary with special contents, see the design doc for
112 a8083063 Iustin Pop
    cmdline handling
113 098c0958 Michael Hanselmann

114 a8083063 Iustin Pop
  """
115 a8083063 Iustin Pop
  if len(argv) == 0:
116 a8083063 Iustin Pop
    binary = "<command>"
117 a8083063 Iustin Pop
  else:
118 a8083063 Iustin Pop
    binary = argv[0].split("/")[-1]
119 a8083063 Iustin Pop
120 a8083063 Iustin Pop
  if len(argv) > 1 and argv[1] == "--version":
121 a8083063 Iustin Pop
    print "%s (ganeti) %s" % (binary, constants.RELEASE_VERSION)
122 a8083063 Iustin Pop
    # Quit right away. That way we don't have to care about this special
123 a8083063 Iustin Pop
    # argument. optparse.py does it the same.
124 a8083063 Iustin Pop
    sys.exit(0)
125 a8083063 Iustin Pop
126 a8083063 Iustin Pop
  if len(argv) < 2 or argv[1] not in commands.keys():
127 a8083063 Iustin Pop
    # let's do a nice thing
128 a8083063 Iustin Pop
    sortedcmds = commands.keys()
129 a8083063 Iustin Pop
    sortedcmds.sort()
130 a8083063 Iustin Pop
    print ("Usage: %(bin)s {command} [options...] [argument...]"
131 a8083063 Iustin Pop
           "\n%(bin)s <command> --help to see details, or"
132 a8083063 Iustin Pop
           " man %(bin)s\n" % {"bin": binary})
133 a8083063 Iustin Pop
    # compute the max line length for cmd + usage
134 a8083063 Iustin Pop
    mlen = max([len(" %s %s" % (cmd, commands[cmd][3])) for cmd in commands])
135 a8083063 Iustin Pop
    mlen = min(60, mlen) # should not get here...
136 a8083063 Iustin Pop
    # and format a nice command list
137 a8083063 Iustin Pop
    print "Commands:"
138 a8083063 Iustin Pop
    for cmd in sortedcmds:
139 a8083063 Iustin Pop
      cmdstr = " %s %s" % (cmd, commands[cmd][3])
140 a8083063 Iustin Pop
      help_text = commands[cmd][4]
141 a8083063 Iustin Pop
      help_lines = textwrap.wrap(help_text, 79-3-mlen)
142 a8083063 Iustin Pop
      print "%-*s - %s" % (mlen, cmdstr,
143 a8083063 Iustin Pop
                                          help_lines.pop(0))
144 a8083063 Iustin Pop
      for line in help_lines:
145 a8083063 Iustin Pop
        print "%-*s   %s" % (mlen, "", line)
146 a8083063 Iustin Pop
    print
147 a8083063 Iustin Pop
    return None, None, None
148 a8083063 Iustin Pop
  cmd = argv.pop(1)
149 a8083063 Iustin Pop
  func, nargs, parser_opts, usage, description = commands[cmd]
150 a8083063 Iustin Pop
  parser_opts.append(_LOCK_OPT)
151 a8083063 Iustin Pop
  parser = OptionParser(option_list=parser_opts,
152 a8083063 Iustin Pop
                        description=description,
153 a8083063 Iustin Pop
                        formatter=TitledHelpFormatter(),
154 a8083063 Iustin Pop
                        usage="%%prog %s %s" % (cmd, usage))
155 a8083063 Iustin Pop
  parser.disable_interspersed_args()
156 a8083063 Iustin Pop
  options, args = parser.parse_args()
157 a8083063 Iustin Pop
  if nargs is None:
158 a8083063 Iustin Pop
    if len(args) != 0:
159 a8083063 Iustin Pop
      print >> sys.stderr, ("Error: Command %s expects no arguments" % cmd)
160 a8083063 Iustin Pop
      return None, None, None
161 a8083063 Iustin Pop
  elif nargs < 0 and len(args) != -nargs:
162 a8083063 Iustin Pop
    print >> sys.stderr, ("Error: Command %s expects %d argument(s)" %
163 a8083063 Iustin Pop
                         (cmd, -nargs))
164 a8083063 Iustin Pop
    return None, None, None
165 a8083063 Iustin Pop
  elif nargs >= 0 and len(args) < nargs:
166 a8083063 Iustin Pop
    print >> sys.stderr, ("Error: Command %s expects at least %d argument(s)" %
167 a8083063 Iustin Pop
                         (cmd, nargs))
168 a8083063 Iustin Pop
    return None, None, None
169 a8083063 Iustin Pop
170 a8083063 Iustin Pop
  return func, options, args
171 a8083063 Iustin Pop
172 a8083063 Iustin Pop
173 a8083063 Iustin Pop
def _AskUser(text):
174 a8083063 Iustin Pop
  """Ask the user a yes/no question.
175 a8083063 Iustin Pop

176 a8083063 Iustin Pop
  Args:
177 a8083063 Iustin Pop
    questionstring - the question to ask.
178 a8083063 Iustin Pop

179 a8083063 Iustin Pop
  Returns:
180 a8083063 Iustin Pop
    True or False depending on answer (No for False is default).
181 a8083063 Iustin Pop

182 a8083063 Iustin Pop
  """
183 a8083063 Iustin Pop
  try:
184 a8083063 Iustin Pop
    f = file("/dev/tty", "r+")
185 a8083063 Iustin Pop
  except IOError:
186 a8083063 Iustin Pop
    return False
187 a8083063 Iustin Pop
  answer = False
188 a8083063 Iustin Pop
  try:
189 a8083063 Iustin Pop
    f.write(textwrap.fill(text))
190 a8083063 Iustin Pop
    f.write('\n')
191 a8083063 Iustin Pop
    f.write("y/[n]: ")
192 a8083063 Iustin Pop
    line = f.readline(16).strip().lower()
193 a8083063 Iustin Pop
    answer = line in ('y', 'yes')
194 a8083063 Iustin Pop
  finally:
195 a8083063 Iustin Pop
    f.close()
196 a8083063 Iustin Pop
  return answer
197 a8083063 Iustin Pop
198 a8083063 Iustin Pop
199 a8083063 Iustin Pop
def SubmitOpCode(op):
200 a8083063 Iustin Pop
  """Function to submit an opcode.
201 a8083063 Iustin Pop

202 a8083063 Iustin Pop
  This is just a simple wrapper over the construction of the processor
203 a8083063 Iustin Pop
  instance. It should be extended to better handle feedback and
204 a8083063 Iustin Pop
  interaction functions.
205 a8083063 Iustin Pop

206 a8083063 Iustin Pop
  """
207 a8083063 Iustin Pop
  proc = mcpu.Processor()
208 a8083063 Iustin Pop
  return proc.ExecOpCode(op, logger.ToStdout)
209 a8083063 Iustin Pop
210 a8083063 Iustin Pop
211 a8083063 Iustin Pop
def GenericMain(commands):
212 a8083063 Iustin Pop
  """Generic main function for all the gnt-* commands.
213 a8083063 Iustin Pop

214 a8083063 Iustin Pop
  Argument: a dictionary with a special structure, see the design doc
215 a8083063 Iustin Pop
  for command line handling.
216 a8083063 Iustin Pop

217 a8083063 Iustin Pop
  """
218 a8083063 Iustin Pop
  # save the program name and the entire command line for later logging
219 a8083063 Iustin Pop
  if sys.argv:
220 a8083063 Iustin Pop
    binary = os.path.basename(sys.argv[0]) or sys.argv[0]
221 a8083063 Iustin Pop
    if len(sys.argv) >= 2:
222 a8083063 Iustin Pop
      binary += " " + sys.argv[1]
223 a8083063 Iustin Pop
      old_cmdline = " ".join(sys.argv[2:])
224 a8083063 Iustin Pop
    else:
225 a8083063 Iustin Pop
      old_cmdline = ""
226 a8083063 Iustin Pop
  else:
227 a8083063 Iustin Pop
    binary = "<unknown program>"
228 a8083063 Iustin Pop
    old_cmdline = ""
229 a8083063 Iustin Pop
230 a8083063 Iustin Pop
  func, options, args = _ParseArgs(sys.argv, commands)
231 a8083063 Iustin Pop
  if func is None: # parse error
232 a8083063 Iustin Pop
    return 1
233 a8083063 Iustin Pop
234 a8083063 Iustin Pop
  options._ask_user = _AskUser
235 a8083063 Iustin Pop
236 a8083063 Iustin Pop
  logger.SetupLogging(debug=options.debug, program=binary)
237 a8083063 Iustin Pop
238 a8083063 Iustin Pop
  try:
239 a8083063 Iustin Pop
    utils.Lock('cmd', max_retries=options.lock_retries, debug=options.debug)
240 a8083063 Iustin Pop
  except errors.LockError, err:
241 a8083063 Iustin Pop
    logger.ToStderr(str(err))
242 a8083063 Iustin Pop
    return 1
243 a8083063 Iustin Pop
244 a8083063 Iustin Pop
  if old_cmdline:
245 a8083063 Iustin Pop
    logger.Info("run with arguments '%s'" % old_cmdline)
246 a8083063 Iustin Pop
  else:
247 a8083063 Iustin Pop
    logger.Info("run with no arguments")
248 a8083063 Iustin Pop
249 a8083063 Iustin Pop
  try:
250 a8083063 Iustin Pop
    try:
251 a8083063 Iustin Pop
      result = func(options, args)
252 a8083063 Iustin Pop
    except errors.ConfigurationError, err:
253 a8083063 Iustin Pop
      logger.Error("Corrupt configuration file: %s" % err)
254 a8083063 Iustin Pop
      logger.ToStderr("Aborting.")
255 a8083063 Iustin Pop
      result = 2
256 a8083063 Iustin Pop
    except errors.HooksAbort, err:
257 a8083063 Iustin Pop
      logger.ToStderr("Failure: hooks execution failed:")
258 a8083063 Iustin Pop
      for node, script, out in err.args[0]:
259 a8083063 Iustin Pop
        if out:
260 a8083063 Iustin Pop
          logger.ToStderr("  node: %s, script: %s, output: %s" %
261 a8083063 Iustin Pop
                          (node, script, out))
262 a8083063 Iustin Pop
        else:
263 a8083063 Iustin Pop
          logger.ToStderr("  node: %s, script: %s (no output)" %
264 a8083063 Iustin Pop
                          (node, script))
265 a8083063 Iustin Pop
      result = 1
266 a8083063 Iustin Pop
    except errors.HooksFailure, err:
267 a8083063 Iustin Pop
      logger.ToStderr("Failure: hooks general failure: %s" % str(err))
268 a8083063 Iustin Pop
      result = 1
269 a8083063 Iustin Pop
    except errors.OpPrereqError, err:
270 a8083063 Iustin Pop
      logger.ToStderr("Failure: prerequisites not met for this"
271 a8083063 Iustin Pop
                      " operation:\n%s" % str(err))
272 a8083063 Iustin Pop
      result = 1
273 a8083063 Iustin Pop
    except errors.OpExecError, err:
274 a8083063 Iustin Pop
      logger.ToStderr("Failure: command execution error:\n%s" % str(err))
275 a8083063 Iustin Pop
      result = 1
276 a8083063 Iustin Pop
  finally:
277 a8083063 Iustin Pop
    utils.Unlock('cmd')
278 a8083063 Iustin Pop
    utils.LockCleanup()
279 a8083063 Iustin Pop
280 a8083063 Iustin Pop
  return result
281 137161c9 Michael Hanselmann
282 137161c9 Michael Hanselmann
283 137161c9 Michael Hanselmann
def OutputTable(headers, fields, separator, data,
284 137161c9 Michael Hanselmann
                numfields=None, unitfields=None):
285 137161c9 Michael Hanselmann
  """Prints a table with headers and different fields.
286 137161c9 Michael Hanselmann

287 137161c9 Michael Hanselmann
  Args:
288 137161c9 Michael Hanselmann
    headers: Dict of header titles or None if no headers should be shown
289 137161c9 Michael Hanselmann
    fields: List of fields to show
290 137161c9 Michael Hanselmann
    separator: String used to separate fields or None for spaces
291 137161c9 Michael Hanselmann
    data: Data to be printed
292 137161c9 Michael Hanselmann
    numfields: List of fields to be aligned to right
293 137161c9 Michael Hanselmann
    unitfields: List of fields to be formatted as units
294 137161c9 Michael Hanselmann

295 137161c9 Michael Hanselmann
  """
296 137161c9 Michael Hanselmann
  if numfields is None:
297 137161c9 Michael Hanselmann
    numfields = []
298 137161c9 Michael Hanselmann
  if unitfields is None:
299 137161c9 Michael Hanselmann
    unitfields = []
300 137161c9 Michael Hanselmann
301 137161c9 Michael Hanselmann
  format_fields = []
302 137161c9 Michael Hanselmann
  for field in fields:
303 137161c9 Michael Hanselmann
    if separator is not None:
304 137161c9 Michael Hanselmann
      format_fields.append("%s")
305 137161c9 Michael Hanselmann
    elif field in numfields:
306 137161c9 Michael Hanselmann
      format_fields.append("%*s")
307 137161c9 Michael Hanselmann
    else:
308 137161c9 Michael Hanselmann
      format_fields.append("%-*s")
309 137161c9 Michael Hanselmann
310 137161c9 Michael Hanselmann
  if separator is None:
311 137161c9 Michael Hanselmann
    mlens = [0 for name in fields]
312 137161c9 Michael Hanselmann
    format = ' '.join(format_fields)
313 137161c9 Michael Hanselmann
  else:
314 137161c9 Michael Hanselmann
    format = separator.replace("%", "%%").join(format_fields)
315 137161c9 Michael Hanselmann
316 137161c9 Michael Hanselmann
  for row in data:
317 137161c9 Michael Hanselmann
    for idx, val in enumerate(row):
318 137161c9 Michael Hanselmann
      if fields[idx] in unitfields:
319 137161c9 Michael Hanselmann
        try:
320 137161c9 Michael Hanselmann
          val = int(val)
321 137161c9 Michael Hanselmann
        except ValueError:
322 137161c9 Michael Hanselmann
          pass
323 137161c9 Michael Hanselmann
        else:
324 137161c9 Michael Hanselmann
          val = row[idx] = utils.FormatUnit(val)
325 137161c9 Michael Hanselmann
      if separator is None:
326 137161c9 Michael Hanselmann
        mlens[idx] = max(mlens[idx], len(val))
327 137161c9 Michael Hanselmann
328 137161c9 Michael Hanselmann
  if headers:
329 137161c9 Michael Hanselmann
    args = []
330 137161c9 Michael Hanselmann
    for idx, name in enumerate(fields):
331 137161c9 Michael Hanselmann
      hdr = headers[name]
332 137161c9 Michael Hanselmann
      if separator is None:
333 137161c9 Michael Hanselmann
        mlens[idx] = max(mlens[idx], len(hdr))
334 137161c9 Michael Hanselmann
        args.append(mlens[idx])
335 137161c9 Michael Hanselmann
      args.append(hdr)
336 137161c9 Michael Hanselmann
    logger.ToStdout(format % tuple(args))
337 137161c9 Michael Hanselmann
338 137161c9 Michael Hanselmann
  for line in data:
339 137161c9 Michael Hanselmann
    args = []
340 137161c9 Michael Hanselmann
    for idx in xrange(len(fields)):
341 137161c9 Michael Hanselmann
      if separator is None:
342 137161c9 Michael Hanselmann
        args.append(mlens[idx])
343 137161c9 Michael Hanselmann
      args.append(line[idx])
344 137161c9 Michael Hanselmann
    logger.ToStdout(format % tuple(args))