基于OpenStack Ocata版本源码分析--oslo.config

本文介绍了OpenStack项目中的oslo.config库,主要用于解析命令行CLI和配置文件(.conf)的配置信息。文章详细讲解了oslo.config的安装、使用方法,并通过keystone.conf配置文件的例子展示其解析过程,同时还提到了配置选项的注册和CLI的注册。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

oslo.config

oslo.config 是用OpenStack 项目中使用频繁比较高的一个类库,主要作用是用解析OpenStack 中命令行(CLI)、配置文件(.conf)的配置信息。

例如;

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:StringBooleanInteger、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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值