当前位置:   article > 正文

#Sora#OpenStack基础库oslo.config试用总结

sora试用

170448_VpT9_987833.jpeg


什么是oslo.config?

oslo.config是OpenStack用于解析配置文件和命令行参数的工具,大概是封装了argparse和configparse,令配置应用更方便。

官方文档:http://docs.openstack.org/developer/oslo.config

目前打算在项目sora中引入该库,简化程序配置方式


测试:

准备:安装oslo.config及组织相关目录

  1. pip install oslo.config
  2. mkdir oslo
  3. cd oslo
  4. touch app.conf
  5. touch registry.py

目录树:

172321_u6FL_987833.png


相关代码:

  1. #app.conf
  2. [DEFAULT]
  3.                              #defautl这个section有的时候并不需要,但是如果注册的opts没有指定一个group,则会属于default组,建议写上
  4.                     
  5. [schedule]
  6. scheduler = simple
  7. enable = True
  8. [service]
  9. service_name = sora_compute
  10. service_id = 1213
  11. endpoint_ip = 10.20.10.70


  1. #registry.py
  2. from oslo.config import cfg
  3. schedule_opt_group = cfg.OptGroup(name='schedule',
  4.         title='Scheduler Configuration')                   #注册一个组,name与app.conf中的schedule section对应
  5.         
  6. scheduler_opts = [                                         #注册属于schedule_opt_group的配置选项,这里使用了StrOpt与BoolOpt
  7.         cfg.StrOpt('scheduler',
  8.                    default='memory',
  9.                    help='The type of scheduler'),
  10.         cfg.BoolOpt('enable',
  11.                    default='True',
  12.                    help='Enable the scheduler'),
  13. ]
  14. service_opt_group = cfg.OptGroup(name='service',           #注册service组
  15.         title='The service which use the scheduler')
  16.         
  17. service_opts = [                                           #注册属于service_opts组的配置选项,用了StrOpt、IntOpt和IPOpt
  18.         cfg.StrOpt('service',
  19.                    default='sora_compute',
  20.                    help='The default user service'),
  21.         cfg.IntOpt('service_id',
  22.                    help='The service id'),
  23.         cfg.IPOpt('endpoint_ip',
  24.                    help='The service endpoint ip'),
  25. ]
  26. MYCONF = cfg.CONF                                            #注册一个对象,作为容器
  27. MYCONF.register_group(schedule_opt_group)                    #注册组
  28. MYCONF.register_group(service_opt_group)
  29. MYCONF.register_opts(scheduler_opts,group=schedule_opt_group)        #注册以字典格式的配置选项,用group指定所属组
  30. MYCONF.register_opts(service_opts,group=service_opt_group)
  31. MYCONF(default_config_files=['/root/oslo/app.conf'])                 #注册配置文件,如果没有这个,将会使用default中的数据,配置文件可以为多个,用字典指定。注意绝对路径


使用oslo.config获取配置:

  1. >>> from oslo.config import cfg
  2. >>> import registry
  3. >>> print cfg.CONF.schedule.scheduler                  
  4. simple
  5. >>> print cfg.CONF.schedule.enable
  6. True
  7. >>> print cfg.CONF.service.service_id
  8. 1213
  9. >>> print cfg.CONF.service.endpoint_ip
  10. 10.20.10.70


可能的疑问:为什么导入了registry却不直接用它?

回答:这的确是一种奇怪的方式,但它可以工作。我们可以用一种更直观的方式使用registry:

  1. #例子来源于OpenStack源码
  2. from oslo.config import cfg
  3. cfg.CONF.import_opt('host','service')           #指定从service.py(一个配置注册文件,类似上面的registry)导入host这个配置选项
  4. hostname = cfg.CONF.host


关于oslo.config所支持的配置选项有:

  1. StrOpt:字符串类型

  2. BoolOpt:布尔型

  3. IntOpt:整数型

  4. FloatOpt:浮点数型

  5. ListOpt:字符串列表类型

  6. DictOpt:字典类型,要求字典中的值为字符串类型

  7. MultiStrOpt:可以分多次配置的字符串列表

  8. IPOpt:Ip地址类型

  9. OptGroup:组类型


在新的oslo.config中,还支持进行类型检查,不过目前大多数的OpenStack项目都用上文的方式配置,故不详细说明。


测试过程中的坑:

在注册配置选项时,如果像:

  1. enabled_apis_opt = cfg.ListOpt('enabled_apis',
  2.                                    default=['ec2''osapi_compute'],
  3.                                    help='List of APIs to enable by default.')

这样的单条配置选项,注册时用CONF.register_opt()

而多条配置选项时,需要用CONF.register_opts()。否则会报错

看到没?就差一个's',坑爹啊!

另外,在注册组中的配置项前,必须先注册组。


参考:

http://docs.openstack.org/developer/oslo.config

*http://www.lihuai.net/program/python/1698.html

http://www.sdnlab.com/5484.html

http://www.360doc.com/content/13/0814/22/3038654_307194414.shtml

http://blog.csdn.net/alvine008/article/details/24364243






转载于:https://my.oschina.net/hochikong/blog/478029

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/214669
推荐阅读
相关标签
  

闽ICP备14008679号