COT.cli module¶
CLI entry point for the Common OVF Tool (COT) suite.
Classes
CLI |
Command-line user interface for COT. |
-
class
CLI(terminal_width=None)[source]¶ Bases:
COT.ui_shared.UICommand-line user interface for COT.
confirmPrompt user to confirm the requested operation. create_parserCreate parserobject for globalcotcommand.create_subparsersPopulate the CLI sub-parsers for all known submodules. fill_examplesPretty-print a set of usage examples. fill_usagePretty-print a list of usage strings for a COT subcommand. formatterCreate formatter for log output. get_inputPrompt the user to enter a string. get_passwordGet password string from the user. mainMain worker function for COT when invoked from the CLI. parse_argsParse the given CLI arguments into a namespace object. runParse the given CLI args then run. set_verbosityEnable logging and/or change the logging verbosity level. terminal_widthThe width of the terminal in columns. -
confirm(prompt)[source]¶ Prompt user to confirm the requested operation.
Auto-accepts if
forceis set toTrue.Parameters: prompt (str) – Message to prompt the user with Returns: True(user confirms acceptance) orFalse(user declines)
-
create_parser()[source]¶ Create
parserobject for globalcotcommand.Includes a number of globally applicable CLI options.
-
create_subparsers()[source]¶ Populate the CLI sub-parsers for all known submodules.
Creates an instance of each
COTGenericSubmodulesubclass, then callscreate_subparser()for each.
-
fill_examples(example_list)[source]¶ Pretty-print a set of usage examples.
>>> fill_examples([ ... ("Deploy to vSphere/ESXi server 192.0.2.100 with credentials" ... " admin/admin, creating a VM named 'test_vm' from foo.ova.", ... 'cot deploy foo.ova esxi 192.0.2.100 -u admin -p admin' ... ' -n test_vm'), ... ("Deploy to vSphere/ESXi server 192.0.2.100, with username" ... " admin (prompting the user to input a password at runtime)," ... " creating a VM based on profile '1CPU-2.5GB' in foo.ova.", ... 'cot deploy foo.ova esxi 192.0.2.100 -u admin -c 1CPU-2.5GB') ... ]) Examples: Deploy to vSphere/ESXi server 192.0.2.100 with credentials admin/admin, creating a VM named 'test_vm' from foo.ova. cot deploy foo.ova esxi 192.0.2.100 -u admin -p admin \ -n test_vm Deploy to vSphere/ESXi server 192.0.2.100, with username admin (prompting the user to input a password at runtime), creating a VM based on profile '1CPU-2.5GB' in foo.ova. cot deploy foo.ova esxi 192.0.2.100 -u admin -c 1CPU-2.5GB
Parameters: example_list (list) – List of (description, CLI example) tuples. Returns: Examples wrapped appropriately to the terminal_width()value. CLI examples will be wrapped with backslashes and a hanging indent.
-
fill_usage(subcommand, usage_list)[source]¶ Pretty-print a list of usage strings for a COT subcommand.
Automatically prepends a
cot subcommand --helpusage string to the provided list.>>> fill_usage('add-file', ["FILE PACKAGE [-o OUTPUT] [-f FILE_ID]"]) cot add-file --help cot add-file FILE PACKAGE [-o OUTPUT] [-f FILE_ID]
Parameters: - subcommand (str) – Subcommand name/keyword
- usage_list (list) – List of usage strings for this subcommand.
Returns: String containing all usage strings, each appropriately wrapped to the
terminal_width()value.
-
formatter(verbosity=20)[source]¶ Create formatter for log output.
We offer different (more verbose) formatting when debugging is enabled, hence this need.
Parameters: verbosity – Logging level as defined by logging.Returns: Formatter object for use with logging.Return type: instance of colorlog.ColoredFormatter
-
get_input(prompt, default_value)[source]¶ Prompt the user to enter a string.
Auto-inputs the
default_valueifforceis set toTrue.Parameters: - prompt (str) – Message to prompt the user with
- default_value (str) – Default value to input if the user simply
hits Enter without entering a value, or if
force.
Returns: Input value
Return type: str
-
get_password(username, host)[source]¶ Get password string from the user.
Parameters: - username (str) – Username the password is associated with
- host (str) – Host the password is associated with
Raises InvalidInputError: if
forceisTrue(as there is no “default” password value)
-
main(args)[source]¶ Main worker function for COT when invoked from the CLI.
- Calls
set_verbosity()with the appropriate verbosity level derived from the args. - Looks up the appropriate
COTGenericSubmoduleinstance corresponding to the subcommand that was invoked. - Converts
argsto a dict and callsset_value()for each arg/value in the dict. - Calls
run()followed byfinished(). - Catches various exceptions and handles them appropriately.
Parameters: args – Parser namespace object returned from parse_args().Return type: int Returns: Exit code for the COT executable. - 0 on successful completion
- 1 on runtime error
- 2 on input error (parser error,
InvalidInputError, etc.)
- Calls
-
parse_args(argv)[source]¶ Parse the given CLI arguments into a namespace object.
Parameters: argv (list) – List of CLI arguments, not including argv0 Returns: Parser namespace object
-
run(argv)[source]¶ Parse the given CLI args then run.
Calls
parse_args()followed bymain().Parameters: argv (list) – The CLI argv value (not including argv[0]) Returns: Return code from main()
-
set_verbosity(level)[source]¶ Enable logging and/or change the logging verbosity level.
Will call
formatter()and associate the resulting formatter with logging.Parameters: level – Logging level as defined by logging
-
terminal_width¶ The width of the terminal in columns.
-