天天看點

openstack oslo.config配置解析子產品

什麼是oslo.config

    oslo.config是openstack解析指令行(CLI)或者配置檔案(.conf)中配置資訊的庫.官網位址點選打開連結     安裝: $ sudo pip install oslo.config

cfg子產品

    每一個配置項都是一個Opt類或其子類例如:

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.')
]
           

    選項類型

        選項可以有任意的類型,為了友善,在oslo.config.cfg中預先定義了選項類型的子類如下表     

Type Option
oslo_config.types.String
  • oslo_config.cfg.StrOpt
  • oslo_config.cfg.SubComt
oslo_config.types.Boolean oslo_config.cfg.BoolOpt
oslo_config.types.Integer
  • oslo_config.cfg.IntOpt
  • oslo_config.cfg.PortOpt
oslo_config.types.Float oslo_config.cfg.FloatOpt
oslo_config.types.List oslo_config.cfg.ListOpt
oslo_config.types.Dict oslo_config.cfg.DictOpt
oslo_config.types.IPAddress oslo_config.cfg.IPOpt
oslo_config.types.Hostname      oslo_config.cfg.HostnameOp

        cfg子產品也支援具有多個值的選項,使用oslo_config.cfg.MultiOpt類來定義.功能感覺和 oslo_config.types.List類似.     

    注冊選項

        選項在使用前應該先注冊,完善一下第一個例子:

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.')
]


def add_common_opts(conf):   # 注冊選項
        conf.register_opts(common_opts)


def get_bind_host(conf):   # 使用選項
        return conf.bind_host


def get_bind_port(conf):
        return conf.bind_port

# 建立配置類
cf = cfg.CONF
# 開始注冊
add_common_opts(cf)
print(get_bind_host(cf))
           

        通過指令行配置

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)
cf = cfg.CONF
add_common_opts(cf)
           

      運作時加-h選項可看到定義的東西已經加上 usage: config.py [-h] [--config-dir DIR] [--config-file PATH] [--debug]

                 [--nodebug] [--noverbose] [--verbose] [--version]

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.

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

  --debug, -d         Print debugging output.

  --nodebug           The inverse of --debug

  --noverbose         The inverse of --verbose

  --verbose, -v       Print more verbose output.

  --version           show program's version number and exit                  注冊選項組

#定義選項組
rabbit_group = cfg.OptGroup(name='rabbit',
                            title='RabbitMQ options')
#定義兩個選項
rabbit_host_opt = cfg.StrOpt('host',
                             default='localhost',
                             help='IP/hostname to listen on'),
rabbit_port_opt = cfg.IntOpt('port',
                             default=5672,
                             help='Port number to listen on')
#注冊
def register_rabbit_opts(conf):
    conf.register_group(rabbit_group)
    # options can be registered under a group in either of these ways:
    #使用選項組的變量名注冊
    conf.register_opt(rabbit_host_opt, group=rabbit_group)
    #使用選項組名注冊
    conf.register_opt(rabbit_port_opt, group='rabbit')
           

        通過配置檔案注冊         配置檔案config.conf

[DEFAULT]
bind_host = 127.0.0.1
bind_port = 8000
           

        config.py

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.')
]


def add_common_opts(conf):   # 注冊選項
        conf.register_opts(common_opts)


def get_bind_host(conf):   # 使用選項
        return conf.bind_host


def get_bind_port(conf):
        return conf.bind_port

# 建立配置類
cf = cfg.CONF
# 開始注冊
add_common_opts(cf)
cf(default_config_files=['config.conf'])
print(get_bind_host(cf))
print(get_bind_port(cf))
           

       運作結果: 127.0.0.1

8000

        成功的讀取了配置檔案的資訊,若沒有指定配置檔案,則讀取預設的配置資訊.