Package ganeti :: Module cli
[hide private]
[frames] | no frames]

Module cli

source code

Module dealing with command line parsing

Classes [hide private]
  CliOption
Custom option class for optparse.
  IdentKeyValOption
Custom option class for ident:key=val,key=val options.
  KeyValOption
Custom option class for key=val,key=val options.
  cli_option
Custom option class for optparse.
  ikv_option
Custom option class for ident:key=val,key=val options.
  keyval_option
Custom option class for key=val,key=val options.
  JobSubmittedException
Job was submitted, client should exit.
  JobExecutor
Class which manages the submission and execution of multiple jobs.
Functions [hide private]
 
_ExtractTagsObject(opts, args)
Extract the tag type object.
source code
 
_ExtendTags(opts, args)
Extend the args if a source file has been given.
source code
 
ListTags(opts, args)
List the tags on a given object.
source code
 
AddTags(opts, args)
Add tags on a given object.
source code
 
RemoveTags(opts, args)
Remove tags from a given object.
source code
 
ARGS_FIXED(val)
Macro-like function denoting a fixed number of arguments
source code
 
ARGS_ATLEAST(val)
Macro-like function denoting a minimum number of arguments
source code
 
check_unit(option, opt, value)
OptParsers custom converter for units.
source code
dict
_SplitKeyVal(opt, data)
Convert a KeyVal string into a dict.
source code
 
check_ident_key_val(option, opt, value)
Custom parser for the IdentKeyVal option type.
source code
 
check_key_val(option, opt, value)
Custom parser for the KeyVal option type.
source code
 
_ParseArgs(argv, commands, aliases)
Parser for the command line arguments.
source code
 
SplitNodeOption(value)
Splits the value of a --node option.
source code
 
UsesRPC(fn) source code
 
AskUser(text, choices=None)
Ask the user a question.
source code
 
SendJob(ops, cl=None)
Function to submit an opcode without waiting for the results.
source code
 
PollJob(job_id, cl=None, feedback_fn=None)
Function to poll for the result of a job.
source code
 
SubmitOpCode(op, cl=None, feedback_fn=None)
Legacy function to submit an opcode.
source code
 
SubmitOrSend(op, opts, cl=None, feedback_fn=None)
Wrapper around SubmitOpCode or SendJob.
source code
 
GetClient() source code
 
FormatError(err)
Return a formatted error message for a given error.
source code
 
GenericMain(commands, override=None, aliases=None)
Generic main function for all the gnt-* commands.
source code
 
GenerateTable(headers, fields, separator, data, numfields=None, unitfields=None, units=None)
Prints a table with headers and different fields.
source code
string
FormatTimestamp(ts)
Formats a given timestamp.
source code
 
ParseTimespec(value)
Parse a time specification.
source code
 
GetOnlineNodes(nodes, cl=None, nowarn=False)
Returns the names of online nodes.
source code
 
_ToStream(stream, txt, *args)
Write a message to a stream, bypassing the logging system
source code
 
ToStdout(txt, *args)
Write a message to stdout only, bypassing the logging system
source code
 
ToStderr(txt, *args)
Write a message to stderr only, bypassing the logging system
source code
Variables [hide private]
  DEBUG_OPT = make_option("-d", "--debug", default= False, actio...
  NOHDR_OPT = make_option("--no-headers", default= False, action...
  SEP_OPT = make_option("--separator", default= None, action= "s...
  USEUNITS_OPT = make_option("--units", default= None, dest= "un...
  FIELDS_OPT = make_option("-o", "--output", dest= "output", act...
  FORCE_OPT = make_option("-f", "--force", dest= "force", action...
  TAG_SRC_OPT = make_option("--from", dest= "tags_source", defau...
  SUBMIT_OPT = make_option("--submit", dest= "submit_only", defa...
  SYNC_OPT = make_option("--sync", dest= "do_locking", default= ...
  ARGS_NONE = None
  ARGS_ONE = ARGS_FIXED(1)
  ARGS_ANY = ARGS_ATLEAST(0)

Imports: sys, textwrap, os, copy, time, logging, StringIO, utils, errors, constants, opcodes, luxi, ssconf, rpc, OptionParser, make_option, TitledHelpFormatter, Option, OptionValueError


Function Details [hide private]

_ExtractTagsObject(opts, args)

source code 

Extract the tag type object.

Note that this function will modify its args parameter.

_ExtendTags(opts, args)

source code 

Extend the args if a source file has been given.

This function will extend the tags with the contents of the file passed in the 'tags_source' attribute of the opts parameter. A file named '-' will be replaced by stdin.

ListTags(opts, args)

source code 

List the tags on a given object.

This is a generic implementation that knows how to deal with all three cases of tag objects (cluster, node, instance). The opts argument is expected to contain a tag_type field denoting what object type we work on.

AddTags(opts, args)

source code 

Add tags on a given object.

This is a generic implementation that knows how to deal with all three cases of tag objects (cluster, node, instance). The opts argument is expected to contain a tag_type field denoting what object type we work on.

RemoveTags(opts, args)

source code 

Remove tags from a given object.

This is a generic implementation that knows how to deal with all three cases of tag objects (cluster, node, instance). The opts argument is expected to contain a tag_type field denoting what object type we work on.

_SplitKeyVal(opt, data)

source code 

Convert a KeyVal string into a dict.

This function will convert a key=val[,...] string into a dict. Empty values will be converted specially: keys which have the prefix 'no_' will have the value=False and the prefix stripped, the others will have value=True.

Parameters:
  • opt (string) - a string holding the option name for which we process the data, used in building error messages
  • data (string) - a string of the format key=val,key=val,...
Returns: dict
{key=val, key=val}
Raises:

_ParseArgs(argv, commands, aliases)

source code 

Parser for the command line arguments.

This function parses the arguments and returns the function which must be executed together with its (modified) arguments.

Parameters:
  • argv - the command line
  • commands - dictionary with special contents, see the design doc for cmdline handling
  • aliases - dictionary with command aliases {'alias': 'target, ...}

AskUser(text, choices=None)

source code 

Ask the user a question.

Parameters:
  • text - the question to ask
  • choices - list with elements tuples (input_char, return_value, description); if not given, it will default to: [('y', True, 'Perform the operation'), ('n', False, 'Do no do the operation')]; note that the '?' char is reserved for help
Returns:
one of the return values from the choices list; if input is not possible (i.e. not running with a tty, we return the last entry from the list

SendJob(ops, cl=None)

source code 

Function to submit an opcode without waiting for the results.

Parameters:
  • ops (list) - list of opcodes
  • cl (luxi.Client) - the luxi client to use for communicating with the master; if None, a new client will be created

PollJob(job_id, cl=None, feedback_fn=None)

source code 

Function to poll for the result of a job.

Parameters:
  • job_id (job identified) - the job to poll for results
  • cl (luxi.Client) - the luxi client to use for communicating with the master; if None, a new client will be created

SubmitOpCode(op, cl=None, feedback_fn=None)

source code 

Legacy function to submit an opcode.

This is just a simple wrapper over the construction of the processor instance. It should be extended to better handle feedback and interaction functions.

SubmitOrSend(op, opts, cl=None, feedback_fn=None)

source code 

Wrapper around SubmitOpCode or SendJob.

This function will decide, based on the 'opts' parameter, whether to submit and wait for the result of the opcode (and return it), or whether to just send the job and print its identifier. It is used in order to simplify the implementation of the '--submit' option.

FormatError(err)

source code 

Return a formatted error message for a given error.

This function takes an exception instance and returns a tuple consisting of two values: first, the recommended exit code, and second, a string describing the error message (not newline-terminated).

GenericMain(commands, override=None, aliases=None)

source code 

Generic main function for all the gnt-* commands.

Arguments:

  • commands: a dictionary with a special structure, see the design doc for command line handling.
  • override: if not None, we expect a dictionary with keys that will override command line options; this can be used to pass options from the scripts to generic functions
  • aliases: dictionary with command aliases {'alias': 'target, ...}

GenerateTable(headers, fields, separator, data, numfields=None, unitfields=None, units=None)

source code 

Prints a table with headers and different fields.

Parameters:
  • headers (dict) - dictionary mapping field names to headers for the table
  • fields (list) - the field names corresponding to each row in the data field
  • separator - the separator to be used; if this is None, the default 'smart' algorithm is used which computes optimal field width, otherwise just the separator is used between each field
  • data (list) - a list of lists, each sublist being one row to be output
  • numfields (list) - a list with the fields that hold numeric values and thus should be right-aligned
  • unitfields (list) - a list with the fields that hold numeric values that should be formatted with the units field
  • units (string or None) - the units we should use for formatting, or None for automatic choice (human-readable for non-separator usage, otherwise megabytes); this is a one-letter string

FormatTimestamp(ts)

source code 

Formats a given timestamp.

Parameters:
  • ts (timestamp) - a timeval-type timestamp, a tuple of seconds and microseconds
Returns: string
a string with the formatted timestamp

ParseTimespec(value)

source code 

Parse a time specification.

The following suffixed will be recognized:

  • s: seconds
  • m: minutes
  • h: hours
  • d: day
  • w: weeks

Without any suffix, the value will be taken to be in seconds.

GetOnlineNodes(nodes, cl=None, nowarn=False)

source code 

Returns the names of online nodes.

This function will also log a warning on stderr with the names of the online nodes.

Parameters:
  • nodes - if not empty, use only this subset of nodes (minus the offline ones)
  • cl - if not None, luxi client to use
  • nowarn (boolean) - by default, this function will output a note with the offline nodes that are skipped; if this parameter is True the note is not displayed

_ToStream(stream, txt, *args)

source code 

Write a message to a stream, bypassing the logging system

Parameters:
  • stream (file object) - the file to which we should write
  • txt (str) - the message

ToStdout(txt, *args)

source code 

Write a message to stdout only, bypassing the logging system

This is just a wrapper over _ToStream.

Parameters:
  • txt (str) - the message

ToStderr(txt, *args)

source code 

Write a message to stderr only, bypassing the logging system

This is just a wrapper over _ToStream.

Parameters:
  • txt (str) - the message

Variables Details [hide private]

DEBUG_OPT

Value:
make_option("-d", "--debug", default= False, action= "store_true", hel\
p= "Turn debugging on")

NOHDR_OPT

Value:
make_option("--no-headers", default= False, action= "store_true", dest\
= "no_headers", help= "Don't display column headers")

SEP_OPT

Value:
make_option("--separator", default= None, action= "store", dest= "sepa\
rator", help= "Separator between output fields" " (defaults to one spa\
ce)")

USEUNITS_OPT

Value:
make_option("--units", default= None, dest= "units", choices= ('h', 'm\
', 'g', 't'), help= "Specify units for output (one of hmgt)")

FIELDS_OPT

Value:
make_option("-o", "--output", dest= "output", action= "store", type= "\
string", help= "Comma separated list of" " output fields", metavar= "F\
IELDS")

FORCE_OPT

Value:
make_option("-f", "--force", dest= "force", action= "store_true", defa\
ult= False, help= "Force the operation")

TAG_SRC_OPT

Value:
make_option("--from", dest= "tags_source", default= None, help= "File \
with tag names")

SUBMIT_OPT

Value:
make_option("--submit", dest= "submit_only", default= False, action= "\
store_true", help= "Submit the job and return the job ID, but" " don't\
 wait for the job to finish")

SYNC_OPT

Value:
make_option("--sync", dest= "do_locking", default= False, action= "sto\
re_true", help= "Grab locks while doing the queries" " in order to ens\
ure more consistent results")