OpenStack configuration parsing library to use oslo.config

OpenStack project aims to separate out the oslo basic functions of the system in reusable, oslo.config is one of the widely-used library, the main purpose of this work is to parse OpenStack in the command line (CLI) or configuration file (.conf) the configuration information.

  In the context of this article, there are several concepts:

  Profiles:  

    OpenStack used to configure each service ini style configuration file, usually ending with .conf;

  Configuration items (options):   

    Left profile values ​​given in the command line or the configuration information, such as: enabled_apis = ec2, osapi_keystone, osapi_compute the "enabled_apis";

  Configuration value of the item:    

    The right profile or configuration information values ​​given in the command line, such as: enabled_apis = ec2, osapi_keystone, osapi_compute the "ec2, osapi_keystone, osapi_compute";

  Configuration group (option groups):   

    A set of configuration items in the configuration file by [...] is represented as [rabbit] my.conf file field indicates the next start of a rabbit named group configuration;

  Other modules:  

    Module specific operations according to the value of the need to achieve a run-time configuration item;

  Configuration Mode (option schemas) items:

    Before parsing the configuration file, get the value of configuration item, other configuration items declared modules they need. Configuration file is usually for a full service, so that other modules may not use all the configuration items in the configuration file, so you must tell the system which themselves depend on the configuration items, this process is the setting mode configuration items. CI statement includes the name of the configuration file, set the default value (once the configuration file is not the configuration item and another configuration module in turn is dependent on the item, the default values ​​stated herein) and the like of the configuration item;

  Reference (reference):    

    Other module parses the configuration file to obtain the value of the configuration item, can be arranged such specific values ​​used in the following implementation;

  Registration (register):    

    Other modules before referring to the value of configuration items, you must register mode configuration item that they were going to reference. In other words, the configuration file of configuration items other modules do not always declare their model, configuration items declared its pattern does not necessarily registered, of course, if you do not register, even if the declared mode, can not be quoted.

 

  The following give a high-level process to explain how to use the library, parsing OpenStack in the configuration file has the following main steps:

  . Step1 properly configure each service master configuration file (* .conf file), the step of each service (such as: keystone) completion.

  . Step2 claims mode configuration item that will be used to used in the module configuration information, including the name of the configuration item, data type, default values, and descriptions;

  Step3. create an object, the object class is created to act as a configuration manager, the value of the configuration item after storage as a container object.

  step3 step4 created object calls the appropriate registration methods (such as: register_opt ())., declared in the register step2 mode configuration items. This process does not resolve the configuration file, just open up the appropriate fields for the object created in step3.

  step5. step3 directly call the object created, the incoming information on the configuration file paths. In this case the profile will be resolved, if the profile is not specified, all default mode step2. Step4 resolution process will extract the value registered in the configuration items, and these items can be configured as a direct reference to the object's properties step3 created.

  

  A complete example is as follows:

  We use my.conf to store all the configuration information to indicate a use config.py my.conf depend on other modules in the configuration mode information.

  My.conf first set file, in the context of oslo.config, [the DEFAULT] field can not be omitted. 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#-*-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

  Then write a script file config.py, the function of the script is very simple, the script uses to print the value of configuration items when executed directly.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

#-*-coding:utf-8-*-

# config.py

# Author: D. Wang

 

from oslo.config import cfg

# 声明配置项模式

# 单个配置项模式

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 in CONF.enabled_apis:

        print ("DEFAULT.enabled_apis: " + i)

     

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

  执行config.py,结果如下:

1

2

3

4

5

6

7

8

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

  下面的config_test.py不指定配置文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

# config_test.py

 

from config import CONF

 

if __name__ =="__main__":

#   CONF(default_config_files=['my.conf'])

    CONF()

    for in CONF.enabled_apis:

        print ("DEFAULT.enabled_apis: " + i)

     

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

  执行config_test.py比较结果差别:

1

2

3

4

5

6

7

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

  可以发现,执行config.py时,成功读取了配置项的值,而执行config_test.py时,由于没有指定要解析的配置文件,所以使用的都是设置模式时指定的默认值。

  OpenStack的oslo.config项目提供了一种开放的配置项解析工具,可以在其上实现自己需要的命令行和配置文件解析工具,也可以直接应用到自己的项目中,本文对于oslo.config项目的使用方法就介绍到这里。

Guess you like

Origin blog.csdn.net/weixin_41738417/article/details/93410802