Skip to content

Instantly share code, notes, and snippets.

@chmouel
Last active July 4, 2017 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chmouel/05fb715f96344161268c to your computer and use it in GitHub Desktop.
Save chmouel/05fb715f96344161268c to your computer and use it in GitHub Desktop.
Oslo cfg backed by etcd example
### The script which access oslo.cfg and print a value
chmouel@TheBatman:GIT/openstack/oslo.config$ cat ~/tmp/c.py [etcd]
import sys
from oslo.config import cfg
opt_group = cfg.OptGroup(name='simple', title='A Simple Example')
simple_opts = [
cfg.StrOpt('hello', default="thedefaultvalue",
help=('hello my beloved default')),
cfg.BoolOpt('enable', default=True,
help=('True enables, False disables'))
]
if __name__ == '__main__':
conf = cfg.ConfigOpts()
conf(args=sys.argv[1:], project='blah',
default_config_files=[])
conf.register_group(opt_group)
conf.register_opts(simple_opts, opt_group)
print conf.simple.hello
### Let's try launching it and asking to connect to the localhost etcd server,
### since we don't have anything in that etcd server it would get the default
### value from oslo config specified in the code
chmouel@TheBatman:GIT/openstack/oslo.config$ python ~/tmp/c.py --etcd-server localhost [etcd]
thedefaultvalue
### Now let's override it in etcd manually with etcdctl this is using etcdctl
### the base path is obviously going to be changed in the future
chmouel@TheBatman:GIT/openstack/oslo.config$ etcdctl mk /org/chmouel/simple/hello "Overridedbyetcd" [etcd]
Overridedbyetcd
### Let's relaunch the script again and see if it got the value we overrided in etcd
chmouel@TheBatman:GIT/openstack/oslo.config$ python ~/tmp/c.py --etcd-server localhost [etcd]
Overridedbyetcd
### Kaboom!
@TommyLike
Copy link

Hey Chmouel Boudjnah, curious about your code, I reckon the '--etcd-server' option is not supported by oslo_config. How can you make it work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment