首页 > 技术文章 > openstack配置模块

littlebugfish 2014-10-31 21:13 原文

一、简介
  openstack中负责处理配置的模块是oslo.config,它可以处理配置项和配置文件。通常的配置处理都是如下形式:

from oslo.config import cfg
CONF=cfg.CONF

  然后用CONF对象注册配置项或者获取配置项。CONF 实际上是一个全局ConfigOpts对象。

 

二、添加配置项

from oslo.config import cfg
from oslo.config import types

#扩展配置项类型,进行具体定制
PortType = types.Integer(1, 65535)

#定义配置项,cfg模块中预定义了一些配置项类型
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.')
]

#将配置项注册到全局ConfigOpts对象中
CONF=cfg.CONF
CONF.register_opts(common_opts)

#注册配置组
conductor_group = cfg.OptGroup(name='conductor',
                               title='Conductor Options')                               
CONF.register_group(conductor_group)

#将配置项注册到配置组    
CONF.register_opts(common_opts, conductor_group)    

 

三、读取配置文件

from oslo.config import cfg
cfg.CONF(xxx,...)

  在xxx中用["--config-file","xxx"]指定配置文件,配置文件格式是标准的ini文件格式,其实CONF这个ConfigOpts对象有一个__call__函数,并且其有两个缺省的命令行参数,即–-config-file和-–config-dir。

 

四、使用配置项

from oslo.config import cfg
CONF=cfg.CONF
CONF.xxx

  可以直接引用xxx配置项的内容,如果是某个组的配置项就先.xxx引用该组,然后.xxx引用该配置项


参考文档:

http://www.choudan.net/2013/11/27/OpenStack-Oslo.config-%E5%AD%A6%E4%B9%A0(%E4%B8%80).html 
http://docs.openstack.org/developer/oslo.config/
 
 
 

推荐阅读