Replace custom lazy loading by stevedore
Change-Id: I0b09e8822af6e8c3ce7299d31eb53f30a50262bc
This commit is contained in:
parent
adba8b3ca3
commit
72d6cf7fc4
@ -145,41 +145,6 @@ def random_alnum(size=32):
|
||||
return ''.join(random.choice(characters) for _ in range(size))
|
||||
|
||||
|
||||
class LazyPluggable(object):
|
||||
"""A pluggable backend loaded lazily based on some value."""
|
||||
|
||||
def __init__(self, pivot, config_group=None, **backends):
|
||||
self.__backends = backends
|
||||
self.__pivot = pivot
|
||||
self.__backend = None
|
||||
self.__config_group = config_group
|
||||
|
||||
def __get_backend(self):
|
||||
if not self.__backend:
|
||||
if self.__config_group is None:
|
||||
backend_name = CONF[self.__pivot]
|
||||
else:
|
||||
backend_name = CONF[self.__config_group][self.__pivot]
|
||||
if backend_name not in self.__backends:
|
||||
msg = _('Invalid backend: %s') % backend_name
|
||||
raise exception.IronicException(msg)
|
||||
|
||||
backend = self.__backends[backend_name]
|
||||
if isinstance(backend, tuple):
|
||||
name = backend[0]
|
||||
fromlist = backend[1]
|
||||
else:
|
||||
name = backend
|
||||
fromlist = backend
|
||||
|
||||
self.__backend = __import__(name, None, None, fromlist)
|
||||
return self.__backend
|
||||
|
||||
def __getattr__(self, key):
|
||||
backend = self.__get_backend()
|
||||
return getattr(backend, key)
|
||||
|
||||
|
||||
def delete_if_exists(pathname):
|
||||
"""delete a file, but ignore file not found error."""
|
||||
|
||||
|
@ -16,34 +16,41 @@
|
||||
|
||||
"""Database setup and migration commands."""
|
||||
|
||||
from ironic.common import utils
|
||||
from oslo.config import cfg
|
||||
from stevedore import driver
|
||||
|
||||
IMPL = utils.LazyPluggable(
|
||||
pivot='backend',
|
||||
config_group='database',
|
||||
sqlalchemy='ironic.db.sqlalchemy.migration')
|
||||
_IMPL = None
|
||||
|
||||
|
||||
def get_backend():
|
||||
global _IMPL
|
||||
if not _IMPL:
|
||||
cfg.CONF.import_opt('backend', 'oslo.db.options', group='database')
|
||||
_IMPL = driver.DriverManager("ironic.database.migration_backend",
|
||||
cfg.CONF.database.backend).driver
|
||||
return _IMPL
|
||||
|
||||
|
||||
def upgrade(version=None):
|
||||
"""Migrate the database to `version` or the most recent version."""
|
||||
return IMPL.upgrade(version)
|
||||
return get_backend().upgrade(version)
|
||||
|
||||
|
||||
def downgrade(version=None):
|
||||
return IMPL.downgrade(version)
|
||||
return get_backend().downgrade(version)
|
||||
|
||||
|
||||
def version():
|
||||
return IMPL.version()
|
||||
return get_backend().version()
|
||||
|
||||
|
||||
def stamp(version):
|
||||
return IMPL.stamp(version)
|
||||
return get_backend().stamp(version)
|
||||
|
||||
|
||||
def revision(message, autogenerate):
|
||||
return IMPL.revision(message, autogenerate)
|
||||
return get_backend().revision(message, autogenerate)
|
||||
|
||||
|
||||
def create_schema():
|
||||
return IMPL.create_schema()
|
||||
return get_backend().create_schema()
|
||||
|
Loading…
Reference in New Issue
Block a user