Replace custom lazy loading by stevedore

Now we have own implementation for loading
extensions. stevedore avoids creating yet
another extension mechanism by building on
top of setuptools entry points.

Change-Id: I7fcb981b58b20fb49d10eea3d477f962e4bf3565
This commit is contained in:
Oleksii Chuprykov 2014-10-01 17:53:38 +03:00
parent cf62047215
commit d40185f248
3 changed files with 21 additions and 44 deletions

View File

@ -477,41 +477,6 @@ def setup_remote_pydev_debug(host, port):
LOG.exception(error_msg)
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.GlanceException(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 validate_key_cert(key_file, cert_file):
try:
error_key_name = "private key"

View File

@ -20,21 +20,30 @@
"""Database setup and migration commands."""
import os
import threading
from oslo.config import cfg
from oslo import db
from stevedore import driver
from glance.common import utils
from glance.db.sqlalchemy import api as db_api
_IMPL = None
_LOCK = threading.Lock()
db.options.set_defaults(cfg.CONF)
IMPL = utils.LazyPluggable(
'backend',
config_group='database',
sqlalchemy='oslo.db.sqlalchemy.migration')
def get_backend():
global _IMPL
if _IMPL is None:
with _LOCK:
if _IMPL is None:
_IMPL = driver.DriverManager(
"glance.database.migration_backend",
cfg.CONF.database.backend).driver
return _IMPL
INIT_VERSION = 0
@ -47,9 +56,10 @@ MIGRATE_REPO_PATH = os.path.join(
def db_sync(version=None, init_version=0, engine=None):
"""Migrate the database to `version` or the most recent version."""
if engine is None:
engine = db_api.get_engine()
return IMPL.db_sync(engine=engine,
abs_path=MIGRATE_REPO_PATH,
version=version,
init_version=init_version)
return get_backend().db_sync(engine=engine,
abs_path=MIGRATE_REPO_PATH,
version=version,
init_version=init_version)

View File

@ -43,6 +43,8 @@ oslo.config.opts =
glance.scrubber = glance.opts:list_scrubber_opts
glance.cache= glance.opts:list_cache_opts
glance.manage = glance.opts:list_manage_opts
glance.database.migration_backend =
sqlalchemy = oslo.db.sqlalchemy.migration
[build_sphinx]
all_files = 1