oslo: oslo_config组件解析 和代码中的应用

oslo: oslo_config组件解析

OpenStack中大量使用了一些第三方组件,为了使一些基础组件的接口的统一及更友好的使用,Openstack利用oslo的项目统一了第三方组件的接口。如最常使用的oslo项目:

  • oslo_config
  • oslo_log
  • oslo_messaging
  • stevedore

oslo_config
OpenStack中用于从配置文件及命令行解析配置参数的第三方库。在Openstack中配置文件相关的概念简要介绍如下:

  • 配置文件: INI风格的文件,key=value,形式,以.conf结尾;
  • 配置项(options): 配置文件或命令行中的[key=value]的左值,即key;
  • 配置项的值: [key=value]的右值,即value;
  • 配置组(group): 一组配置项,在配置文件中以[...]开头,如[DEFAULT];

oslo_config的使用:

  1. 声明所需要的配置项,声明配置项的名称、类型、默认值、描述等信息;
  2. 注册配置项
  3. 解析配置文件

oslo_config对配置文件中的值进行解析时,值的优先级从高到低如下:

  1. 命令行
  2. 环境变量
  3. --config-dir 配置文件,默认值:
    ~/.project/project.conf.d/,~/project.conf.d/,/etc/project/project.conf.d/,/etc/project.conf.d/
  4. --config-file指定的配置文件,默认值:
    ~/.project/project.conf,~/project.conf,/etc/project/project.conf,/etc/project.conf

如果一个配置项在多个地方都有定义,则取值的优先级如上,命令行的优先级最高。

#!/usr/bin/env python3.7
#-*-coding:utf-8-*-

import sys

from oslo_config import cfg

# 声明配置项
debug = cfg.BoolOpt("debug",short='d',default=False,help="Turn On Debug.")

# 声明配置项组
grp = cfg.OptGroup(name='API',title="API Opts")

# 声明一个单个配置项
# 此处声明了配置项的名称、默认值、描述信息
host = cfg.IPOpt("host",default="0.0.0.0",help="Listening IP")

# 声明多个配置项,Bool类型及Int类型
api_opts = [
        cfg.BoolOpt('ssl',
                   default=False,
                   help='Use SSL to Connect.'),
                
        cfg.IntOpt('bind_port',
                   default=4000,
                   help='Port number to listen.')
    ]

def add_opts():
        # 注册配置项
        cfg.CONF.register_opt(debug)

        # 注册配置组
        cfg.CONF.register_group(grp)

        # 注册grp配置组下的配置项
        cfg.CONF.register_opt(host,group=grp)
        cfg.CONF.register_opts(api_opts,group=grp)

def add_cli_opts():
        # 注册配置项
        cfg.CONF.register_cli_opt(debug)

add_opts()
add_cli_opts()

cfg.CONF(sys.argv[1:])

print(cfg.CONF.debug)
print(cfg.CONF.API.host)
print(cfg.CONF.API.bind_port)
print(cfg.CONF.API.ssl)

如上,基本步骤如下:

  1. 声明配置项及配置项组
    debug = cfg.BoolOpt("debug",short='d',default=False,help="Turn On Debug.")
    布尔类型,别称:'d',默认值为false。
  2. 注册配置项
    cfg.CONF.register_opt(host,group=grp)
    注册grp组下的host配置项,若未指明组,则为[DEFAULT]下,注册配置项前,需先注册组。
    其中register_cli_opt:注册命令行配置项
  3. 解析配置文件
    cfg.CONF(sys.argv[1:])
    命令行中指明配置文件。
$ python3.7 config.py --help
usage: config [-h] [--config-dir DIR] [--config-file PATH] [--debug]
              [--nodebug]

optional arguments:
  -h, --help          show this help message and exit
  --config-dir DIR    Path to a config directory to pull `*.conf` files from.
                      This file set is sorted, so as to provide a predictable
                      parse order if individual options are over-ridden. The
                      set is parsed after the file(s) specified via previous
                      --config-file, arguments hence over-ridden options in
                      the directory take precedence. This option must be set
                      from the command-line.
  --config-file PATH  Path to a config file to use. Multiple config files can
                      be specified, with values in later files taking
                      precedence. Defaults to None. This option must be set
                      from the command-line.
  --debug, -d         Turn On Debug.
  --nodebug           The inverse of --debug

api.conf:

[DEFAULT]
debug = true

[API]
host = 127.0.0.1
bind_port = 8080
ssl= False

运行示例:

$ python3.7 config.py --config-file api.conf --nodebug
False
127.0.0.1
8080
False

配置项支持的类型:

  • BoolOpt
  • FloatOpt
  • IntOpt
  • IPOpt
  • DictOpt
  • ListOpt
  • MultiStrOpt
  • PortOpt
  • StrOpt

项目中的实际应用

from nova.openstack.common import cfg 的内容

nova/openstack/common/cfg.py


from oslo_config.cfg import *

定义和注册配置文件信息

nova/virt/libvirt/driver.py

nova/virt/libvirt/driver.py

from nova.openstack.common import cfg


#定义配置文件 
lvm_opts = [
    cfg.StrOpt('volume_group',
               default='raidVG',
               help='Name for the VG that will contain exported volumes'),
    cfg.IntOpt('num_shell_tries',
               default=3,
               help='number of times to attempt to run flakey shell commands'),
    cfg.IntOpt('num_iscsi_scan_tries',
               default=3,
               help='number of times to rescan iSCSI target to find volume'),
]

#注册配置文件信息
FLAGS.register_opts(lvm_opts)

代码中调用配置信息

nova.virt.libvirt.driver.LibvirtDriver.active_vg_pvnum
 
def active_vg_pvnum(self, volume_group):
        if not self.check_vg_exist(FLAGS.volume_group):
            return 0

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值