Tuesday, May 17, 2016

OpenStack Oslo Config Tutorial

osloconfig

installation

git clone https://github.com/openstack/oslo.config -b stable/kilo
cd oslo.config
python setup install

Now you can start to use oslo-config as your python config lib.

The following show two example
* Simple Oslo Config Example (Basic)
* Ceilometer Oslo Config Example (Advanced)

We introduce the Advanced case is for providing a simple approach to OpenStack Ceilometer.
The sources is in my github

https://github.com/jonahwu/lab/tree/master/osloconfig

Simple Oslo Config Example (Basic)

Here is the structure of example directory.

root@ubuntu:~/osloconfig# ls -l
total 32
-rw-r--r-- 1 root root  304 May 16 22:03 app.conf
-rwxr-xr-x 1 root root 1263 May 16 22:09 app.py
-rw-r--r-- 1 root root  578 May 16 22:05 con.py
-rw-r--r-- 1 root root    0 May 16 22:09 __init__.py

app.py

#! /usr/bin/env python
from oslo.config import cfg
import sys

opt_simple_group = cfg.OptGroup(name='simple',
                         title='A Simple Example')

opt_moredata_group = cfg.OptGroup(name='moredata',
                         title='A More Complex Example')
simple_opts = [
    cfg.BoolOpt('enable', default=False,
                help=('True enables, False disables'))
]

moredata_opts = [
    cfg.StrOpt('message', default='No data',
               help=('A message')),
    cfg.ListOpt('usernames', default=None,
                help=('A list of usernames')),
    cfg.DictOpt('jobtitles', default=None,
                help=('A dictionary of usernames and job titles')),
    cfg.IntOpt('payday', default=30,
                help=('Default payday monthly date')),
    cfg.FloatOpt('pi', default=0.0,
                help=('The value of Pi'))
]

CONF = cfg.CONF

CONF.register_group(opt_simple_group)
CONF.register_opts(simple_opts, opt_simple_group)

CONF.register_group(opt_moredata_group)
CONF.register_opts(moredata_opts, opt_moredata_group)


if __name__ == "__main__":
    CONF(sys.argv[1:], project='app')
    print CONF.simple.enable
    print CONF.moredata.usernames

app.conf

[simple]
enable = True

[moredata]

# StrOpt
message = Hello World

# ListOpt
usernames = ['Magic', 'Jessica', 'Peter']

# DictOpt
usermetadata = {'Magic': 'Manager', 'Jessica': 'CEO', 'Peter': 'Security Guard'}

# IntOpt
payday = 20

# FloatOpt
pi = 3.14

execute

Here we follow the example of OpenStack Ceilometer, that is to get config file through exection command. It's more clear than config file through file, since we can easily to get the config information.

python ./app.py  --config-file /root/osloconfig/app.conf
or
./app.py  --config-file /root/osloconfig/app.conf

IF you ignore --config-file, the script cannot find the app.conf configuation file.

Ceilometer Oslo Config Example (Advanced)

This work is for an approach to OpenStack Ceilometer and how to use the oslo-config in different file and different CLASS.

The structure of the directory.

root@ubuntu:~/osloconfig# ls -l
total 32
-rw-r--r-- 1 root root  304 May 16 22:03 app.conf
-rwxr-xr-x 1 root root 1263 May 16 22:09 app.py
-rw-r--r-- 1 root root  578 May 16 22:05 con.py
-rw-r--r-- 1 root root    0 May 16 22:09 __init__.py
-rw-r--r-- 1 root root  186 May 16 22:07 service.py

We add service.py as CONF(sys.argv[1:], project='app'), hence app.py becomes

if __name__ == "__main__":
    #CONF(sys.argv[1:], project='app')
    service.prepare_service()
    print CONF.simple.enable
    print CONF.moredata.usernames
    con.alm()

of course you have to import service

import service

where service.py

from oslo_config import cfg
import sys

def prepare_service(argv=None):
    if argv is None:
        argv = sys.argv
    cfg.CONF(argv[1:], project='app', validate_default_values=True)

The principle is the same, but we abstract to another file same as OpenStack Ceilometer.

How to use in another file or another class

Where Con.alm() we insert here is another exmaple that reading configuration in other class.

from oslo_config import cfg

ALARM_API_OPTS = [
    cfg.BoolOpt('record_history',
                default=False,
                help='Record alarm change events.'
                ),
    cfg.StrOpt('alarm_sec',
                default=0,
                help='Record alarm change events.'
                ),
]
CONF = cfg.CONF
CONF.register_opts(ALARM_API_OPTS, group='alarm')


class alm(object):
    def __init__(self):
        user_record_history = CONF.alarm.record_history
        print 'user record %s'%user_record_history
        print CONF.alarm.alarm_sec

Again we need to define CONF=cfg.CONF, but we don't need to claim the config file location. And we then need to register option and read configuration shown in above example.

app.conf

[simple]
enable = True

[moredata]

# StrOpt
message = Hello World

# ListOpt
usernames = ['Magic', 'Jessica', 'Peter']

# DictOpt
usermetadata = {'Magic': 'Manager', 'Jessica': 'CEO', 'Peter': 'Security Guard'}

# IntOpt
payday = 20

# FloatOpt
pi = 3.14

[alarm]
record_history=True
alarm_sec=20

No comments:

Post a Comment