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 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 two CLI options defined by default, –config-file and –config-dir:

class ConfigOpts(object):

    def __call__(self, ...):

        opts = [
            MultiStrOpt('config-file',
                    ...),
            StrOpt('config-dir',
                   ...),
        ]

        self.register_cli_opts(opts)

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.OptGroup(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 it no group attributes are required other than the group name, the group need not be explicitly registered e.g.

def register_rabbit_opts(conf):
# The group will automatically be created, equivalent calling:: # conf.register_group(OptGroup(name=’rabbit’)) 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 required so that an error is raised if the user does not supply a value for the option.

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), ...

]

This module also contains a global instance of the CommonConfigOpts class in order to support a common usage pattern in OpenStack:

from openstack.common import cfg

opts = [
cfg.StrOpt(‘bind_host’ default=‘0.0.0.0’), cfg.IntOpt(‘bind_port’, default=9292),

]

CONF = cfg.CONF CONF.register_opts(opts)

def start(server, app):
server.start(app, CONF.bind_port, CONF.bind_host)
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, required=False, deprecated_name=None)

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

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 0xa7617ac>, <nova.openstack.common.cfg.BoolOpt object at 0xa7617cc>]
logging_cli_opts = [<nova.openstack.common.cfg.StrOpt object at 0xa76180c>, <nova.openstack.common.cfg.StrOpt object at 0xa7618ec>, <nova.openstack.common.cfg.StrOpt object at 0xa76198c>, <nova.openstack.common.cfg.StrOpt object at 0xa7619ac>, <nova.openstack.common.cfg.StrOpt object at 0xa7619ec>, <nova.openstack.common.cfg.BoolOpt object at 0xa761a6c>, <nova.openstack.common.cfg.StrOpt object at 0xa761acc>]
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

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.clear(*args, **kwargs)

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

ConfigOpts.clear_default(*args, **kwargs)

Clear an override an opt’s default value.

Clear a previously set override of the default value of given option.

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

NoSuchOptError, NoSuchGroupError

ConfigOpts.clear_override(*args, **kwargs)

Clear an override an opt value.

Clear a previously set override of the command line, config file and default values of a given option.

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

NoSuchOptError, NoSuchGroupError

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.find_file(name)

Locate a file located alongside the config files.

Search for a file with the supplied basename in the directories which we have already loaded config files from and other known configuration directories.

The directory, if any, supplied by the config_dir option is searched first. Then the config_file option is iterated over and each of the base directories of the config_files values are searched. Failing both of these, the standard directories searched by the module level find_config_files() function is used. The first matching file is returned.

Parameters:basename – the filename, e.g. ‘policy.json’
Returns:the path to a matching file, or None
ConfigOpts.import_opt(name, module_str, group=None)

Import an option definition from a module.

Import a module and check that a given option is registered.

This is intended for use with global configuration objects like cfg.CONF where modules commonly register options with CONF at module load time. If one module requires an option defined by another module it can use this method to explicitly declare the dependency.

Parameters:
  • name – the name/dest of the opt
  • module_str – the name of a module to import
  • group – an option OptGroup object or group name
Raises :

NoSuchOptError, NoSuchGroupError

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(*args, **kwargs)

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(*args, **kwargs)

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(*args, **kwargs)

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(*args, **kwargs)

Register multiple option schemas at once.

ConfigOpts.reset()

Clear the object state and unset overrides and defaults.

ConfigOpts.set_default(*args, **kwargs)

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(*args, **kwargs)

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

ConfigOpts.unregister_opt(*args, **kwargs)

Unregister an option.

Parameters:
  • opt – an Opt object
  • group – an optional OptGroup object or group name
Raises :

ArgsAlreadyParsedError, NoSuchGroupError

ConfigOpts.unregister_opts(*args, **kwargs)

Unregister multiple CLI option schemas at once.

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, required=False, deprecated_name=None)

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, required=False, deprecated_name=None)

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, required=False, deprecated_name=None)

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, names, multi=False)
read(config_files)
class MultiStrOpt(name, dest=None, short=None, default=None, metavar=None, help=None, secret=False, required=False, deprecated_name=None)

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, required=False, deprecated_name=None)

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

exception RequiredOptError(opt_name, group=None)

Bases: nova.openstack.common.cfg.Error

Raised if an option is required but no value is supplied by the user.

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

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, extension='.conf')

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]
  • extension – the type of the config file

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}.

Previous topic

The nova.objectstore.s3server Module

Next topic

The nova.openstack.common.context Module

This Page