oslo.config
oslo.config 是用OpenStack 项目中使用频繁比较高的一个类库,主要作用是用解析OpenStack 中命令行(CLI)、配置文件(.conf)的配置信息。
- 项目源码:https://git.openstack.org/cgit/openstack/oslo.config
- 项目文档:https://docs.openstack.org/oslo.config/latest
- Bugs: https://bugs.launchpad.net/oslo.config
例如;
keystone.conf (/etc/keystone/keystone.conf
) 配置文件中的catalog
配置信息:
[catalog]
#
# From keystone
#
# Absolute path to the file used for the templated catalog backend. This option
# is only used if the `[catalog] driver` is set to `templated`. (string value)
#template_file = default_catalog.templates
# Entry point for the catalog driver in the `keystone.catalog` namespace.
# Keystone provides a `sql` option (which supports basic CRUD operations
# through SQL), a `templated` option (which loads the catalog from a templated
# catalog file on disk), and a `endpoint_filter.sql` option (which supports
# arbitrary service catalogs per project). (string value)
#driver = sql
# Toggle for catalog caching. This has no effect unless global caching is
# enabled. In a typical deployment, there is no reason to disable this.
# (boolean value)
#caching = true
# Time to cache catalog data (in seconds). This has no effect unless global and
# catalog caching are both enabled. Catalog data (services, endpoints, etc.)
# typically does not change frequently, and so a longer duration than the
# global default may be desirable. (integer value)
#cache_time = <None>
# Maximum number of entities that will be returned in a catalog collection.
# There is typically no reason to set this, as it would be unusual for a
# deployment to have enough services or endpoints to exceed a reasonable limit.
# (integer value)
#list_limit = <None>
使用oslo.config进行解析:
文件位置: keystone/keystone/conf/catalog.py
from oslo_config import cfg
from keystone.conf import utils
# 对应 keystone.conf 配置文件中[catalog]中的template_file
template_file = cfg.StrOpt(
'template_file',
default='default_catalog.templates',
help=utils.fmt("""
Absolute path to the file used for the templated catalog backend. This option
is only used if the `[catalog] driver` is set to `templated`.
"""))
# 对应 keystone.conf 配置文件中[catalog]中的driver
driver = cfg.StrOpt(
'driver',
default='sql',
help=utils.fmt("""
Entry point for the catalog driver in the `keystone.catalog` namespace.
Keystone provides a `sql` option (which supports basic CRUD operations through
SQL), a `templated` option (which loads the catalog from a templated catalog
file on disk), and a `endpoint_filter.sql` option (which supports arbitrary
service catalogs per project).
"""))
# 对应 keystone.conf 配置文件中[catalog]中的caching
aching = cfg.BoolOpt(
'caching',
default=True,
help=utils.fmt("""
Toggle for catalog caching. This has no effect unless global caching is
enabled. In a typical deployment, there is no reason to disable this.
"""))
# 对应 keystone.conf 配置文件中[catalog]中的cache_time
cache_time = cfg.IntOpt(
'cache_time',
help=utils.fmt("""
Time to cache catalog data (in seconds). This has no effect unless global and
catalog caching are both enabled. Catalog data (services, endpoints, etc.)
typically does not change frequently, and so a longer duration than the global
default may be desirable.
"""))
# 对应 keystone.conf 配置文件中[catalog]中的list_limit
list_limit = cfg.IntOpt(
'list_limit',
help=utils.fmt("""
Maximum number of entities that will be returned in a catalog collection. There
is typically no reason to set this, as it would be unusual for a deployment to
have enough services or endpoints to exceed a reasonable limit.
"""))
GROUP_NAME = __name__.split('.')[-1]
ALL_OPTS = [
template_file,
driver,
aching,
cache_time,
list_limit,
]
def register_opts(conf):
conf.register_opts(ALL_OPTS, group=GROUP_NAME)
def list_opts():
return {GROUP_NAME: ALL_OPTS}
oslo.config 安装
pip install oslo.config
oslo.config 使用
简单使用:
#
# cfg 是 oslo_config 核心模块
# types:String、Boolean、Integer、Float、Port、List、Dict、IPAddress、Hostname、HostAddress、URI。
#
from oslo_config import cfg
from oslo_config import types
PortType = types.Integer(1, 65535)
common_opts = [
cfg.StrOpt('bind_host', default='0.0.0.0', help='IP address to listen on.'),
cfg.Opt('bind_port', type=PortType, default=9292, help='Port number to listen on.')
]
与之对应的 .conf
:
$ cat 01.conf
# IP address to listen on.
bind_host = '0.0.0.0'
# Port number to listen on.
bind_port = 9292
- cfg.StrOpt
driver = cfg.StrOpt(
'driver',
default='sql',
help=utils.fmt("""
Entry point for the assignment backend driver (where role assignments are
stored) in the `keystone.assignment` namespace. Only a SQL driver is supplied
by keystone itself. Unless you are writing proprietary drivers for keystone,
you do not need to set this option.
"""))
- cfg.ListOpt
prohibited_implied_role = cfg.ListOpt(
'prohibited_implied_role',
default=['admin'],
help=utils.fmt("""
A list of role names which are prohibited from being an implied role.
"""))
- cfg.URIOpt
public_endpoint = cfg.URIOpt(
'public_endpoint',
help=utils.fmt("""
The base public endpoint URL for Keystone that is advertised to clients (NOTE:
this does NOT affect how Keystone listens for connections). Defaults to the
base host URL of the request. For example, if keystone receives a request to
`http://server:5000/v3/users`, then this will option will be automatically
treated as `http://server:5000`. You should only need to set option if either
the value of the base URL contains a path that keystone does not automatically
infer (`/prefix/v3`), or if the endpoint should be found on a different host.
"""))
- cfg.IntOpt
max_project_tree_depth = cfg.IntOpt(
'max_project_tree_depth',
default=5,
help=utils.fmt("""
Maximum depth of the project hierarchy, excluding the project acting as a
domain at the top of the hierarchy. WARNING: Setting it to a large value may
adversely impact performance.
"""))
- cfg.BoolOpt
strict_password_check = cfg.BoolOpt(
'strict_password_check',
default=False,
help=utils.fmt("""
If set to true, strict password length checking is performed for password
manipulation. If a password exceeds the maximum length, the operation will fail
with an HTTP 403 Forbidden error. If set to false, passwords are automatically
truncated to the maximum length.
"""))
注册
from oslo_config import cfg
from oslo_config import types
conf = cfg.CONF
PortType = types.Integer(1, 65535)
common_opts = [
cfg.StrOpt('bind_host', default='0.0.0.0', help='IP address to listen on.'),
cfg.Opt('bind_port', type=PortType, default=9292, help='Port number to listen on.')
]
# 使用 register_opts
conf.register_opts(common_opts, group='default')
注册CLI
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)