Package CedarBackup2 :: Module cli
[show private | hide private]
[frames | no frames]

Module CedarBackup2.cli

Provides command-line interface implementation for the cback script.

Summary

The functionality in this module encapsulates the command-line interface for the cback script. The cback script itself is very short, basically just an invokation of one function implemented here. That, in turn, makes it simpler to validate the command line interface (for instance, it's easier to run pychecker against a module, and unit tests are easier, too).

The objects and functions implemented in this module are probably not useful to any code external to Cedar Backup. Anyone else implementing their own command-line interface would have to reimplement (or at least enhance) all of this anyway.

Backwards Compatibility

The command line interface has changed between Cedar Backup 1.x and Cedar Backup 2.x. Some new switches have been added, and the actions have become simple arguments rather than switches (which is a much more standard command line format). Old 1.x command lines are generally no longer valid.

Author: Kenneth J. Pronovici <pronovic@ieee.org>

Classes
Options Class representing command-line options for the cback script.

Function Summary
  cli()
Implements the command-line interface for the cback script.
  setupLogging(options)
Set up logging based on command-line options.
  setupPathResolver(config)
Set up the path resolver singleton based on configuration.

Variable Summary
str DEFAULT_CONFIG: The default configuration file.
str DEFAULT_LOGFILE: The default log file path.
list DEFAULT_OWNERSHIP: Default ownership for the logfile.
int DEFAULT_MODE: Default file permissions mode on the logfile.
list VALID_ACTIONS: List of valid actions.
list COMBINE_ACTIONS: List of actions which can be combined with other actions.
list NONCOMBINE_ACTIONS: List of actions which cannot be combined with other actions.
int COLLECT_INDEX = 100                                                                   
str DATE_FORMAT = '%Y-%m-%dT%H:%M:%S %Z'
str DISK_LOG_FORMAT = '%(asctime)s --> [%(levelname)-7s] %(m...
str DISK_OUTPUT_FORMAT = '%(message)s'
Logger logger = <logging.Logger instance at 0x402dd3ec>
list LONG_SWITCHES = ['help', 'version', 'verbose', 'quiet', ...
int PURGE_INDEX = 400                                                                   
str SCREEN_LOG_FORMAT = '%(message)s'
_DevNull SCREEN_LOG_STREAM = <epydoc.imports._DevNull instance at...
str SHORT_SWITCHES = 'hVbqc:fl:o:m:Ods'
int STAGE_INDEX = 200                                                                   
int STORE_INDEX = 300                                                                   

Function Details

cli()

Implements the command-line interface for the cback script.

Essentially, this is the "main routine" for the cback script. It does all of the argument processing for the script, and then sets about executing the indicated actions.

As a general rule, only the actions indicated on the command line will be executed. We will accept any of the built-in actions and any of the configured extended actions (which makes action list verification a two- step process).

The 'all' action has a special meaning: it means that the built-in set of actions (collect, stage, store, purge) will all be executed, in that order. Extended actions will be ignored as part of the 'all' action.

Raised exceptions always result in an immediate return. Otherwise, we generally return when all specified actions have been completed. Actions are ignored if the help, version or validate flags are set.

A different error code is returned for each type of failure:
  • 1: The Python interpreter version is < 2.3
  • 2: Error processing command-line arguments
  • 3: Error configuring logging
  • 4: Error parsing indicated configuration file
  • 5: Backup was interrupted with a CTRL-C or similar
  • 6: Error executing specified backup actions
Returns:
Error code as described above.

Notes:

  • This function contains a good amount of logging at the INFO level, because this is the right place to document high-level flow of control (i.e. what the command-line options were, what config file was being used, etc.)
  • We assume that anything that must be seen on the screen is logged at the ERROR level. Errors that occur before logging can be configured are written to sys.stderr.

setupLogging(options)

Set up logging based on command-line options.

There are two kinds of logging: flow logging and output logging. Output logging contains information about system commands executed by Cedar Backup, for instance the calls to mkisofs or mount, etc. Flow logging contains error and informational messages used to understand program flow. Flow log messages and output log messages are written to two different loggers target (CedarBackup2.log and CedarBackup2.output). Flow log messages are written at the ERROR, INFO and DEBUG log levels, while output log messages are generally only written at the INFO log level.

By default, output logging is disabled. When the options.output or options.debug flags are set, output logging will be written to the configured logfile. Output logging is never written to the screen.

By default, flow logging is enabled at the ERROR level to the screen and at the INFO level to the configured logfile. If the options.quiet flag is set, flow logging is enabled at the INFO level to the configured logfile only (i.e. no output will be sent to the screen). If the options.verbose flag is set, flow logging is enabled at the INFO level to both the screen and the configured logfile. If the options.debug flag is set, flow logging is enabled at the DEBUG level to both the screen and the configured logfile.
Parameters:
options - Command-line options.
           (type=Options object)
Returns:
Path to logfile on disk.

setupPathResolver(config)

Set up the path resolver singleton based on configuration.

Cedar Backup's path resolver is implemented in terms of a singleton, the PathResolverSingleton class. This function takes options configuration, converts it into the dictionary form needed by the singleton, and then initializes the singleton. After that, any function that needs to resolve the path of a command can use the singleton.
Parameters:
config - Configuration
           (type=Config object)

Variable Details

DEFAULT_CONFIG

The default configuration file.
Type:
str
Value:
'/etc/cback.conf'                                                      

DEFAULT_LOGFILE

The default log file path.
Type:
str
Value:
'/var/log/cback.log'                                                   

DEFAULT_OWNERSHIP

Default ownership for the logfile.
Type:
list
Value:
['root', 'adm']                                                        

DEFAULT_MODE

Default file permissions mode on the logfile.
Type:
int
Value:
416                                                                   

VALID_ACTIONS

List of valid actions.
Type:
list
Value:
['collect',
 'stage',
 'store',
 'purge',
 'rebuild',
 'validate',
 'initialize',
 'all']                                                                

COMBINE_ACTIONS

List of actions which can be combined with other actions.
Type:
list
Value:
['collect', 'stage', 'store', 'purge']                                 

NONCOMBINE_ACTIONS

List of actions which cannot be combined with other actions.
Type:
list
Value:
['rebuild', 'validate', 'initialize', 'all']                           

COLLECT_INDEX

Type:
int
Value:
100                                                                   

DATE_FORMAT

Type:
str
Value:
'%Y-%m-%dT%H:%M:%S %Z'                                                 

DISK_LOG_FORMAT

Type:
str
Value:
'%(asctime)s --> [%(levelname)-7s] %(message)s'                        

DISK_OUTPUT_FORMAT

Type:
str
Value:
'%(message)s'                                                          

logger

Type:
Logger
Value:
<logging.Logger instance at 0x402dd3ec>                                

LONG_SWITCHES

Type:
list
Value:
['help',
 'version',
 'verbose',
 'quiet',
 'config=',
 'full',
 'logfile=',
 'owner=',
...                                                                    

PURGE_INDEX

Type:
int
Value:
400                                                                   

SCREEN_LOG_FORMAT

Type:
str
Value:
'%(message)s'                                                          

SCREEN_LOG_STREAM

Type:
_DevNull
Value:
<epydoc.imports._DevNull instance at 0x401ff88c>                       

SHORT_SWITCHES

Type:
str
Value:
'hVbqc:fl:o:m:Ods'                                                     

STAGE_INDEX

Type:
int
Value:
200                                                                   

STORE_INDEX

Type:
int
Value:
300                                                                   

Generated by Epydoc 2.1 on Thu Mar 29 20:58:29 2007 http://epydoc.sf.net