Skip to content

Instantly share code, notes, and snippets.

@hub-cap
Last active December 11, 2015 12:08
Show Gist options
  • Save hub-cap/4598250 to your computer and use it in GitHub Desktop.
Save hub-cap/4598250 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright (c) 2012 Rackspace Hosting
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Configuration support for all drivers.
This module allows support for setting configurations either from default
or from a particular FLAGS group, to be able to set multiple configurations
for a given set of values.
For instance, two lvm configurations can be set by naming them in groups as
[lvm1]
volume_group=lvm-group-1
...
[lvm2]
volume_group=lvm-group-2
...
And the configuration group name will be passed in so that all calls to
configuration.volume_group within that instance will be mapped to the proper
named group.
This class also ensures the implementation's configuration is grafted into the
option group. This is due to the way cfg works. All cfg options must be defined
and registered.
"""
from cinder import flags
from cinder.openstack.common import log as logging
FLAGS = flags.FLAGS
LOG = logging.getLogger(__name__)
class Configuration(object):
def __init__(self, implementation, config_group=None):
"""This takes care of grafting the implementation's config
values into the config group"""
self.implementation = implementation
self.config_group = config_group
# set the local conf so that __call__'s know what to use
if self.config_group:
self.ensure_config_values()
self.local_conf = FLAGS._get(self.config_group)
else:
self.local_conf = FLAGS
def ensure_config_values(self):
"""Hackity magic to put a given impls *_opts into the group"""
opts = [x for x in dir(self.implementation) if x.endswith("_opts")]
for optgrp in opts:
FLAGS.register_opts(getattr(self.implementation, optgrp),
group=self.config_group)
def __getattr__(self, value):
return getattr(self.local_conf, value)
### Gives u the ability to do this
class Foo(object):
foo_opts = [cfg.StrOpt('volume_group',
default='cinder-volumes',
help='Name for the VG that will contain exported volumes'),
]
from cinder.volume import configuration
c = configuration.Configuration(Foo, 'lvm1')
c.volume_group
^ ^ This is calling FLAGS.lvm1.volume_group
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment