Install the oslo.config module
pip install oslo.config
python oslo.config reads the configuration file
Create code directory
mkdir /python/cinder/
Copy a cinder configuration file
cp /etc/cinder/cinder.conf /python/cinder/
cat /python/cinder/cinder.conf
[keystone_authtoken]
memcached_servers = localhost:11211
signing_dir = /var/cache/cinder
cafile = /opt/stack/data/ca-bundle.pem
project_domain_name = Default
project_name = service
user_domain_name = Default
password = secret
username = cinder
auth_url = http://192.168.56.3/identity
auth_type = password
[DEFAULT]
cinder_internal_tenant_user_id = c2a6fd5dc41f42b3b85387c9a2e7a327
cinder_internal_tenant_project_id = 14dd8b649a624dc794a5047b8cbaa26a
graceful_shutdown_timeout = 5
glance_api_version = 2
glance_api_servers = http://192.168.56.3/image
osapi_volume_workers = 2
logging_exception_prefix = ERROR %(name)s %(instance)s
logging_default_format_string = %(color)s%(levelname)s %(name)s [-%(color)s] %(instance)s%(color)s%(message)s
logging_context_format_string = %(color)s%(levelname)s %(name)s [%(global_request_id)s %(request_id)s %(project_name)s %(user_name)s%(color)s] %(instance)s%(color)s%(message)s
logging_debug_format_suffix = {
{
(pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d}}
transport_url = rabbit://stackrabbit:secret@192.168.56.3:5672/
default_volume_type = lvmdriver-1
enabled_backends = lvmdriver-1
my_ip = 192.168.56.3
periodic_interval = 60
state_path = /opt/stack/data/cinder
osapi_volume_listen = 0.0.0.0
osapi_volume_extension = cinder.api.contrib.standard_extensions
rootwrap_config = /etc/cinder/rootwrap.conf
api_paste_config = /etc/cinder/api-paste.ini
iscsi_helper = lioadm
debug = True
auth_strategy = keystone
[database]
connection = mysql+pymysql://root:secret@192.168.56.3/cinder?charset=utf8
[oslo_concurrency]
lock_path = /opt/stack/data/cinder
[key_manager]
fixed_key = a410bdf9db41db63895265624958ac5e
backend = cinder.keymgr.conf_key_mgr.ConfKeyManager
[lvmdriver-1]
image_volume_cache_enabled = True
volume_clear = zero
lvm_type = auto
iscsi_helper = lioadm
volume_group = stack-volumes-lvmdriver-1
volume_driver = cinder.volume.drivers.lvm.LVMVolumeDriver
volume_backend_name = lvmdriver-1
[nova]
region_name = RegionOne
memcached_servers = localhost:11211
signing_dir = /var/cache/cinder
cafile = /opt/stack/data/ca-bundle.pem
project_domain_name = Default
project_name = service
user_domain_name = Default
password = secret
username = nova
auth_url = http://192.168.56.3/identity
auth_type = password
[coordination]
backend_url = etcd3+http://192.168.56.3:2379
python read configuration file
cat /python/cinder/cinder.py
#!/usr/bin/env python
#coding:utf8
#from __future__ import print_function
from oslo_config import cfg
#声明参数选项
opt_group=cfg.OptGroup('keystone_authtoken')
auth_opts=[
cfg.StrOpt('memcached_servers',
default = 'localhost:11211',
choices=("localhost:11211", "0.0.0.0:11211"),
help = ('localhost local','0.0.0.0 So listen')
),
cfg.StrOpt('signing_dir',
default='/var/cache/cinder',
choices=("/var/cache/cinder", "/var/cache/cinder"),
),
]
token_opts=[
cfg.StrOpt('project_domain_name'),
cfg.StrOpt('project_name'),
]
CINDER_OPTS=(auth_opts+
token_opts)
#注册参数选项
CONF=cfg.CONF
CONF.register_group(opt_group)
CONF.register_opts(CINDER_OPTS,group=opt_group)
if __name__ == "__main__":
#要读取哪个配置文件
CONF(default_config_files = [ 'cinder.conf'])
print('keystone_authtoken部分,memcached_servers参数值为%s'% (CONF.keystone_authtoken.memcached_servers))
print('keystone_authtoken部分,signing_dir参数值为%s'%(CONF.keystone_authtoken.signing_dir))
print('keystone_authtoken部分,project_domain_name参数值为%s'%(CONF.keystone_authtoken.project_domain_name))
print('keystone_authtoken部分,project_name参数值为%s'%(CONF.keystone_authtoken.project_name))
print('配置组为%s'%(opt_group))
print('配置选项为%s'%(CINDER_OPTS))
carried out
[root@localhost cinder]# python cinder.py
keystone_authtoken部分,memcached_servers参数值为localhost:11211
keystone_authtoken部分,signing_dir参数值为/var/cache/cinder
keystone_authtoken部分,project_domain_name参数值为Default
keystone_authtoken部分,project_name参数值为service
配置组为keystone_authtoken
配置选项为[<oslo_config.cfg.StrOpt object at 0x7f4f4f665590>, <oslo_config.cfg.StrOpt object at 0x7f4f4f66d990>, <oslo_config.cfg.StrOpt object at 0x7f4f4f66da10>, <oslo_config.cfg.StrOpt object at 0x7f4f4f66dbd0>]
python oslo.config parsing command line parameters
Create code directory
mkdir /python/cli/
Python parsing command line parameters
cat /python/cli/cli.py
#!/usr/bin/env python
#coding:utf8
# 声明参数选项
from oslo_config import cfg
import sys
cli_opts = [
cfg.StrOpt('ip_address',
default='88.88.88.88',
help='just ip address'),
]
# 注册命令行参数选项
cfg.CONF.register_cli_opts(cli_opts)
# 解析命令行参数选项
cfg.CONF(args=sys.argv[1:])
# 读取解析后的参数选项
print cfg.CONF.items()
for name, values in cfg.CONF.items():
print "%s : %s" % (name, values)
carried out
不传入参数值,用默认值
[root@localhost cli]# python cli.py
[('config_file', []), ('config_dir', []), ('ip_address', '88.88.88.88'), ('config_source', [])]
config_file : []
config_dir : []
ip_address : 88.88.88.88
config_source : []
传入参数,指定值
[root@localhost cli]# python cli.py --ip_address 10.0.0.0
[('config_file', []), ('config_dir', []), ('ip_address', '10.0.0.0'), ('config_source', [])]
config_file : []
config_dir : []
ip_address : 10.0.0.0
config_source : []
config_file and config_dir are two items that are automatically registered by default in the module. They are used to specify the storage path of the configuration file read by the module. Since they are registered, they can naturally also be passed in through command parameters.
python cli.py --config-file /etc/cinder/cinder.conf --ip_address 10.0.0.0
[('config_file', ['/etc/cinder/cinder.conf']), ('config_dir', []), ('ip_address', '10.0.0.0'), ('config_source', [])]
config_file : ['/etc/cinder/cinder.conf']
config_dir : []
ip_address : 10.0.0.0
config_source : []
[root@localhost cli]# python cli.py --config-file /etc/cinder/cinder.conf
[('config_file', ['/etc/cinder/cinder.conf']), ('config_dir', []), ('ip_address', '88.88.88.88'), ('config_source', [])]
config_file : ['/etc/cinder/cinder.conf']
config_dir : []
ip_address : 88.88.88.88
config_source : []
python oslo.config reads the configuration file
Edit configuration file
cat /python/cli/cli.conf
[DEFAULT]
app_name = cinder
debug = true
extern = message
[database]
ip = 192.168.229.121
port = 8776
python read configuration file
cat /python/cli/cli.py
#!/bin/python
# -*- coding: utf-8 -*-
from oslo_config import cfg
import sys
# 声明配置项
conf_opts = [
cfg.StrOpt('app_name',
default='unknown_app',
help='app name'),
cfg.BoolOpt('debug',
default=False,
help='open debug log')
]
# 注册配置项
cfg.CONF.register_opts(conf_opts)
# 解析配置文件
cfg.CONF(default_config_files=['/python/cli/cli.conf'])
# 读取解析后的配置
for name, values in cfg.CONF.items():
print "%s : %s" % (name, values)
carried out
python cli.py
debug : True
config_dir : []
config_file : ['/python/cli/cli.conf']
app_name : cinder
config_source : []