Previous topic

The nova.objectstore.s3server Module

Next topic

The nova.openstack.common.iniparser Module

This Page

Psst... hey. You're reading the latest content, but it might be out of sync with code. You can read Nova 2011.2 docs or all OpenStack docs too.

The nova.openstack.common.cfg Module

Configuration options which may be set on the command line or in config files.

The schema for each option is defined using the Opt sub-classes, e.g.:

common_opts = [
    cfg.StrOpt('bind_host',
               default='0.0.0.0',
               help='IP address to listen on'),
    cfg.IntOpt('bind_port',
               default=9292,
               help='Port number to listen on')
]

Options can be strings, integers, floats, booleans, lists or ‘multi strings’:

enabled_apis_opt = cfg.ListOpt('enabled_apis',
                               default=['ec2', 'osapi_compute'],
                               help='List of APIs to enable by default')

DEFAULT_EXTENSIONS = [
    'nova.api.openstack.compute.contrib.standard_extensions'
]
osapi_compute_extension_opt = cfg.MultiStrOpt('osapi_compute_extension',
                                              default=DEFAULT_EXTENSIONS)

Option schemas are registered with with the config manager at runtime, but before the option is referenced:

class ExtensionManager(object):

    enabled_apis_opt = cfg.ListOpt(...)

    def __init__(self, conf):
        self.conf = conf
        self.conf.register_opt(enabled_apis_opt)
        ...

    def _load_extensions(self):
        for ext_factory in self.conf.osapi_compute_extension:
            ....

A common usage pattern is for each option schema to be defined in the module or class which uses the option:

opts = ...

def add_common_opts(conf):
    conf.register_opts(opts)

def get_bind_host(conf):
    return conf.bind_host

def get_bind_port(conf):
    return conf.bind_port

An option may optionally be made available via the command line. Such options must registered with the config manager before the command line is parsed (for the purposes of –help and CLI arg validation):

cli_opts = [
    cfg.BoolOpt('verbose',
                short='v',
                default=False,
                help='Print more verbose output'),
    cfg.BoolOpt('debug',
                short='d',
                default=False,
                help='Print debugging output'),
]

def add_common_opts(conf):
    conf.register_cli_opts(cli_opts)

The config manager has a single CLI option defined by default, –config-file:

class ConfigOpts(object):

    config_file_opt = MultiStrOpt('config-file',
                                  ...

    def __init__(self, ...):
        ...
        self.register_cli_opt(self.config_file_opt)

Option values are parsed from any supplied config files using openstack.common.iniparser. If none are specified, a default set is used e.g. glance-api.conf and glance-common.conf:

glance-api.conf:
  [DEFAULT]
  bind_port = 9292

glance-common.conf:
  [DEFAULT]
  bind_host = 0.0.0.0

Option values in config files override those on the command line. Config files are parsed in order, with values in later files overriding those in earlier files.

The parsing of CLI args and config files is initiated by invoking the config manager e.g.:

conf = ConfigOpts()
conf.register_opt(BoolOpt('verbose', ...))
conf(sys.argv[1:])
if conf.verbose:
    ...

Options can be registered as belonging to a group:

rabbit_group = cfg.OptionGroup(name='rabbit',
                               title='RabbitMQ options')

rabbit_host_opt = cfg.StrOpt('host',
                             default='localhost',
                             help='IP/hostname to listen on'),
rabbit_port_opt = cfg.IntOpt('port',
                             default=5672,
                             help='Port number to listen on')

def register_rabbit_opts(conf):
    conf.register_group(rabbit_group)
    # options can be registered under a group in either of these ways:
    conf.register_opt(rabbit_host_opt, group=rabbit_group)
    conf.register_opt(rabbit_port_opt, group='rabbit')

If no group is specified, options belong to the ‘DEFAULT’ section of config files:

glance-api.conf:
  [DEFAULT]
  bind_port = 9292
  ...

  [rabbit]
  host = localhost
  port = 5672
  use_ssl = False
  userid = guest
  password = guest
  virtual_host = /

Command-line options in a group are automatically prefixed with the group name:

--rabbit-host localhost --rabbit-port 9999

Option values in the default group are referenced as attributes/properties on the config manager; groups are also attributes on the config manager, with attributes for each of the options associated with the group:

server.start(app, conf.bind_port, conf.bind_host, conf)

self.connection = kombu.connection.BrokerConnection(
    hostname=conf.rabbit.host,
    port=conf.rabbit.port,
    ...)

Option values may reference other values using PEP 292 string substitution:

opts = [
    cfg.StrOpt('state_path',
               default=os.path.join(os.path.dirname(__file__), '../'),
               help='Top-level directory for maintaining nova state'),
    cfg.StrOpt('sqlite_db',
               default='nova.sqlite',
               help='file name for sqlite'),
    cfg.StrOpt('sql_connection',
               default='sqlite:///$state_path/$sqlite_db',
               help='connection string for sql database'),
]

Note that interpolation can be avoided by using ‘$$’.

For command line utilities that dispatch to other command line utilities, the disable_interspersed_args() method is available. If this this method is called, then parsing e.g.:

script --verbose cmd --debug /tmp/mything

will no longer return:

['cmd', '/tmp/mything']

as the leftover arguments, but will instead return:

['cmd', '--debug', '/tmp/mything']

i.e. argument parsing is stopped at the first non-option argument.

Options may be declared as secret so that their values are not leaked into log files:

opts = [
cfg.StrOpt(‘s3_store_access_key’, secret=True), cfg.StrOpt(‘s3_store_secret_key’, secret=True), ...

]

exception ArgsAlreadyParsedError(msg=None)

Bases: nova.openstack.common.cfg.Error

Raised if a CLI opt is registered after parsing.

class BoolOpt(name, dest=None, short=None, default=None, metavar=None, help=None, secret=False)

Bases: nova.openstack.common.cfg.Opt

Bool opts are set to True or False on the command line using –optname or –noopttname respectively.

In config files, boolean values are case insensitive and can be set using 1/0, yes/no, true/false or on/off.

class CommonConfigOpts(**kwargs)

Bases: nova.openstack.common.cfg.ConfigOpts

DEFAULT_LOG_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
DEFAULT_LOG_FORMAT = '%(asctime)s %(levelname)8s [%(name)s] %(message)s'
common_cli_opts = [<nova.openstack.common.cfg.BoolOpt object at 0x1d4b050>, <nova.openstack.common.cfg.BoolOpt object at 0x1d4b090>]
logging_cli_opts = [<nova.openstack.common.cfg.StrOpt object at 0x1d4b0d0>, <nova.openstack.common.cfg.StrOpt object at 0x1d4b110>, <nova.openstack.common.cfg.StrOpt object at 0x1d4b150>, <nova.openstack.common.cfg.StrOpt object at 0x1d4b190>, <nova.openstack.common.cfg.StrOpt object at 0x1d4b1d0>, <nova.openstack.common.cfg.BoolOpt object at 0x1d4b210>, <nova.openstack.common.cfg.StrOpt object at 0x1d4b250>]
exception ConfigFileParseError(config_file, msg)

Bases: nova.openstack.common.cfg.Error

Raised if there is an error parsing a config file.

exception ConfigFileValueError(msg=None)

Bases: nova.openstack.common.cfg.Error

Raised if a config file value does not match its opt type.

exception ConfigFilesNotFoundError(config_files)

Bases: nova.openstack.common.cfg.Error

Raised if one or more config files are not found.

class ConfigOpts(project=None, prog=None, version=None, usage=None, default_config_files=None)

Bases: _abcoll.Mapping

Config options which may be set on the command line or in config files.

ConfigOpts is a configuration option manager with APIs for registering option schemas, grouping options, parsing option values and retrieving the values of options.

class GroupAttr(conf, group)

Bases: _abcoll.Mapping

A helper class representing the option values of a group as a mapping and attributes.

class ConfigOpts.StrSubWrapper(conf)

Bases: object

A helper class exposing opt values as a dict for string substitution.

ConfigOpts.disable_interspersed_args()

Set parsing to stop on the first non-option.

If this this method is called, then parsing e.g.

script –verbose cmd –debug /tmp/mything

will no longer return:

[‘cmd’, ‘/tmp/mything’]

as the leftover arguments, but will instead return:

[‘cmd’, ‘–debug’, ‘/tmp/mything’]

i.e. argument parsing is stopped at the first non-option argument.

ConfigOpts.enable_interspersed_args()

Set parsing to not stop on the first non-option.

This it the default behaviour.

ConfigOpts.log_opt_values(logger, lvl)

Log the value of all registered opts.

It’s often useful for an app to log its configuration to a log file at startup for debugging. This method dumps to the entire config state to the supplied logger at a given log level.

Parameters:
  • logger – a logging.Logger object
  • lvl – the log level (e.g. logging.DEBUG) arg to logger.log()
ConfigOpts.print_help(file=None)

Print the help message for the current program.

ConfigOpts.print_usage(file=None)

Print the usage message for the current program.

ConfigOpts.register_cli_opt(opt, group=None)

Register a CLI option schema.

CLI option schemas must be registered before the command line and config files are parsed. This is to ensure that all CLI options are show in –help and option validation works as expected.

Parameters:
  • opt – an instance of an Opt sub-class
  • group – an optional OptGroup object or group name
Returns:

False if the opt was already register, True otherwise

Raises :

DuplicateOptError, ArgsAlreadyParsedError

ConfigOpts.register_cli_opts(opts, group=None)

Register multiple CLI option schemas at once.

ConfigOpts.register_group(group)

Register an option group.

An option group must be registered before options can be registered with the group.

Parameters:group – an OptGroup object
ConfigOpts.register_opt(opt, group=None)

Register an option schema.

Registering an option schema makes any option value which is previously or subsequently parsed from the command line or config files available as an attribute of this object.

Parameters:
  • opt – an instance of an Opt sub-class
  • group – an optional OptGroup object or group name
Returns:

False if the opt was already register, True otherwise

Raises :

DuplicateOptError

ConfigOpts.register_opts(opts, group=None)

Register multiple option schemas at once.

ConfigOpts.reset()

Reset the state of the object to before it was called.

ConfigOpts.set_default(name, default, group=None)

Override an opt’s default value.

Override the default value of given option. A command line or config file value will still take precedence over this default.

Parameters:
  • name – the name/dest of the opt
  • default – the default value
  • group – an option OptGroup object or group name
Raises :

NoSuchOptError, NoSuchGroupError

ConfigOpts.set_override(name, override, group=None)

Override an opt value.

Override the command line, config file and default values of a given option.

Parameters:
  • name – the name/dest of the opt
  • override – the override value
  • group – an option OptGroup object or group name
Raises :

NoSuchOptError, NoSuchGroupError

class ConfigParser(filename, sections)

Bases: nova.openstack.common.iniparser.BaseParser

assignment(key, value)
error_no_section()
new_section(section)
parse()
parse_exc(msg, lineno, line=None)
exception DuplicateOptError(opt_name)

Bases: nova.openstack.common.cfg.Error

Raised if multiple opts with the same name are registered.

exception Error(msg=None)

Bases: exceptions.Exception

Base class for cfg exceptions.

class FloatOpt(name, dest=None, short=None, default=None, metavar=None, help=None, secret=False)

Bases: nova.openstack.common.cfg.Opt

Float opt values are converted to floats using the float() builtin.

class IntOpt(name, dest=None, short=None, default=None, metavar=None, help=None, secret=False)

Bases: nova.openstack.common.cfg.Opt

Int opt values are converted to integers using the int() builtin.

class ListOpt(name, dest=None, short=None, default=None, metavar=None, help=None, secret=False)

Bases: nova.openstack.common.cfg.Opt

List opt values are simple string values separated by commas. The opt value is a list containing these strings.

class MultiConfigParser

Bases: object

get(section, name)
read(config_files)
class MultiStrOpt(name, dest=None, short=None, default=None, metavar=None, help=None, secret=False)

Bases: nova.openstack.common.cfg.Opt

Multistr opt values are string opts which may be specified multiple times. The opt value is a list containing all the string values specified.

multi = True
exception NoSuchGroupError(group_name)

Bases: nova.openstack.common.cfg.Error

Raised if a group which doesn’t exist is referenced.

exception NoSuchOptError(opt_name, group=None)

Bases: nova.openstack.common.cfg.Error, exceptions.AttributeError

Raised if an opt which doesn’t exist is referenced.

class Opt(name, dest=None, short=None, default=None, metavar=None, help=None, secret=False)

Bases: object

Base class for all configuration options.

An Opt object has no public methods, but has a number of public string properties:

name:
the name of the option, which may include hyphens
dest:
the (hyphen-less) ConfigOpts property which contains the option value
short:
a single character CLI option name
default:
the default value of the option
metavar:
the name shown as the argument to a CLI option in –help output
help:
an string explaining how the options value is used
multi = False
class OptGroup(name, title=None, help=None)

Bases: object

Represents a group of opts.

CLI opts in the group are automatically prefixed with the group name.

Each group corresponds to a section in config files.

An OptGroup object has no public methods, but has a number of public string properties:

name:
the name of the group
title:
the group title as displayed in –help
help:
the group description as displayed in –help
exception ParseError(msg, lineno, line, filename)

Bases: nova.openstack.common.iniparser.ParseError

class StrOpt(name, dest=None, short=None, default=None, metavar=None, help=None, secret=False)

Bases: nova.openstack.common.cfg.Opt

String opts do not have their values transformed and are returned as str objects.

exception TemplateSubstitutionError(msg=None)

Bases: nova.openstack.common.cfg.Error

Raised if an error occurs substituting a variable in an opt value.

find_config_files(project=None, prog=None)

Return a list of default configuration files.

Parameters:
  • project – an optional project name
  • prog – the program name, defaulting to the basename of sys.argv[0]

We default to two config files: [${project}.conf, ${prog}.conf]

And we look for those config files in the following directories:

~/.${project}/
~/
/etc/${project}/
/etc/

We return an absolute path for (at most) one of each the default config files, for the topmost directory it exists in.

For example, if project=foo, prog=bar and /etc/foo/foo.conf, /etc/bar.conf and ~/.foo/bar.conf all exist, then we return [‘/etc/foo/foo.conf’, ‘~/.foo/bar.conf’]

If no project name is supplied, we only look for ${prog.conf}.