How to use the OpenStack configuration analysis library oslo.config


First need to install the module

pip install oslo.config

1 Basic concepts

OpenStack's oslo project aims to isolate the reusable basic functions in the system. oslo.config is one of the widely used libraries. The main purpose of this work is to parse the command line (CLI) or configuration files (.conf) in OpenStack. Configuration information in.

  • Configuration file :
    ini-style configuration file used to configure each service of OpenStack, usually ending with .conf;

  • Configuration items (options) :   
    the left value of the configuration information given in the configuration file or command line, such as: enabled_apis = "enabled_apis" in ec2, osapi_keystone, osapi_compute;

  • The value of the configuration item :
    the right value of the configuration information given in the configuration file or command line, such as: enabled_apis = ec2, osapi_keystone, "ec2, osapi_keystone, osapi_compute" in osapi_compute;

  • Option groups :   
    A group of configuration items, which are represented by [...] in the configuration file. For example, the [rabbit] field in the my.conf file indicates that a configuration group named rabbit will start next; the concept of configuration groups Is the sections in the configuration file

In the configuration file below, [rabbit] is a section, which is a configuration group

#-*-coding:utf-8-*-
# my.conf
 
[DEFAULT]
#[DEFAULT]不可省略
enabled_apis = ec2, osapi_keystone, osapi_compute
bind_host = 196.168.1.111
bind_port = 9999
 
[rabbit]
host = 127.0.0.1
port = 12345
use_ssl=true
user_id = guest
password = guest
  • Configuration item mode (option schemas):
    Simply put, it is to declare what configuration items you need. Before parsing the configuration file and obtaining the value of the configuration item, declare the configuration items you need. The configuration items in the configuration file are for the entire service. A module may only use a part of the configuration items, that is to say, you need to tell the system which configuration items you need. This process is the mode of setting the configuration items. The content of the declaration includes the name of the configuration item, the default value (the default value is used if there is no configuration file or the configuration item is not in the configuration file), and help information;

2 A complete case

Let's first experience how oslo.config is used through a simple example. Don't worry about not understanding it. Detailed comments will be given in the code.

  • my.conf
#coding=utf8
# my.conf

[DEFAULT]
#[DEFAULT]不可省略
enabled_apis = ec2, osapi_keystone, osapi_compute
bind_host = 196.168.1.111
bind_port = 9999

#[rabbit]是一个section,也就是一个配置组
[rabbit]
host = 127.0.0.1
port = 12345
use_ssl=true
user_id = guest
password = guest
  • config.py
# coding=utf8
# config.py

from oslo_config import cfg

# 声明配置项模式
# 单个配置项模式
# 配置项类型为list,配置项为enabled_apis,default为默认值,配置文件不存在或者拿不到配置项的值时,使用默认值,help为帮助信息
enabled_apis_opt = cfg.ListOpt('enabled_apis',
                               default=['ec2', 'osapi_compute'],
                               help='List of APIs to enable by default.')
# 多个配置项组成一个模式
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.')
]
# 配置组
rabbit_group = cfg.OptGroup(
    name='rabbit',
    title='RabbitMQ options'
)
# 配置组中的模式,通常以配置组的名称为前缀(非必须)
rabbit_ssl_opt = cfg.BoolOpt('use_ssl',
                             default=False,
                             help='use ssl for connection')
# 配置组中的多配置项模式
rabbit_Opts = [
    cfg.StrOpt('host',
               default='localhost',
               help='IP/hostname to listen on.'),
    cfg.IntOpt('port',
               default=5672,
               help='Port number to listen on.')
]

# 创建对象CONF,用来充当容器
CONF = cfg.CONF
# 注册单个配置项模式
CONF.register_opt(enabled_apis_opt)

# 注册含有多个配置项的模式
CONF.register_opts(common_opts)

# 配置组必须在其组件被注册前注册!
CONF.register_group(rabbit_group)

# 注册配置组中含有多个配置项的模式,必须指明配置组
CONF.register_opts(rabbit_Opts, rabbit_group)

# 注册配置组中的单配置项模式,指明配置组
CONF.register_opt(rabbit_ssl_opt, rabbit_group)

# 接下来打印使用配置项的值
if __name__ == "__main__":
    CONF(default_config_files=['my.conf'])

for i in CONF.enabled_apis:
    print("DEFAULT.enabled_apis: " + i)

# bind_port,rabbit.use_ssl,rabbit.port不是字符串类型,所以转换为字符串再和字符串相加
print("DEFAULT.bind_host: " + CONF.bind_host)
print("DEFAULT.bind_port: " + str(CONF.bind_port))
print("rabbit.use_ssl: " + str(CONF.rabbit.use_ssl))
print("rabbit.host: " + CONF.rabbit.host)
print("rabbit.port: " + str(CONF.rabbit.port))

The execution results are as follows:

$ python config.py 
DEFAULT.enabled_apis: ec2
DEFAULT.enabled_apis: osapi_keystone
DEFAULT.enabled_apis: osapi_compute
DEFAULT.bind_host: 196.168.1.111
DEFAULT.bind_port: 9999
rabbit.use_ssl: True
rabbit.host: 127.0.0.1
rabbit.port: 12345

You can see that the values ​​of the configuration items printed are all the values ​​in the configuration file my.conf. Comment the 59th and 60th lines in the file config.py and execute it again. The result is as follows:

$ python config.py 
DEFAULT.enabled_apis: ec2
DEFAULT.enabled_apis: osapi_compute
DEFAULT.bind_host: 0.0.0.0
DEFAULT.bind_port: 9292
rabbit.use_ssl: False
rabbit.host: localhost
rabbit.port: 5672

It can be seen that when the parsing file is not specified, the configuration item cannot find the value of the configuration item in the configuration file, and the printed value is the default value provided when the configuration item is declared above.

3 Use summary

After reading the case of the above configuration items, let's go back and talk about how to use the oslo.config library. The analysis of configuration files in OpenStack mainly involves the following steps:

  • step1. Correctly configure the main configuration file (*.conf file) of each service. This step is completed in each service (such as keystone). Corresponding to the above example, is to write the configuration file my.conf
  • step2. Declare the mode of the configuration items to be used in the module that will use the configuration information, including the name, data type, default value and description of the configuration item; in the corresponding example, declare the configuration items enabled_apis_opt, common_opts, etc.
  • step3. Create an object, and the class that created the object will act as a configuration manager. This object will be used as a container to store the value of configuration items. Corresponding to line 42 in the example, CONF = cfg.CONF
  • step4. Call the corresponding registration method in the object created in step3 (eg register_opt(), register_opts(), register_group) to register the configuration item mode declared in step2. This process will not parse the configuration file, just open up the corresponding fields for the object created in step 3. Corresponding to the 44th, 47th, 50th, 53, 56th lines in the example
  • step5. Directly call the object created in step3, and pass in information such as the configuration file path. At this time, the configuration file will be parsed. If the configuration file is not specified, all the default values ​​in step2 mode will be used. Corresponding to line 60 in the example, the configuration file is parsed here, and the configuration items in the configuration file are taken. Then these configuration items can be used as attributes of the object created in step 3, such as CONF.bind_host. The configuration items are used like CONF attributes.

Reference:
https://www.cnblogs.com/Security-Darren/p/3854797.html

Guess you like

Origin blog.csdn.net/happyjacob/article/details/110732139