From d86b5735cde2f91d2bfd12afa5b8a82b039cc56f Mon Sep 17 00:00:00 2001 From: Hieu LE Date: Fri, 19 Aug 2016 08:24:22 +0700 Subject: [PATCH] Centralize config option: database section Centralize config option of database section. Replace oslo_conf cfg to magnum.conf. Change-Id: Id12bbf3ad8d3342450cd64cf23761a60d49ee46a Implements: blueprint centralize-config-magnum --- magnum/conf/__init__.py | 4 ++-- magnum/conf/database.py | 40 +++++++++++++++++++++++++++++++ magnum/db/__init__.py | 31 ------------------------ magnum/db/migration.py | 7 +++--- magnum/db/sqlalchemy/api.py | 4 ++-- magnum/db/sqlalchemy/migration.py | 6 +++-- magnum/db/sqlalchemy/models.py | 8 ++++--- magnum/opts.py | 1 - magnum/tests/unit/db/base.py | 4 ++-- 9 files changed, 59 insertions(+), 46 deletions(-) create mode 100644 magnum/conf/database.py diff --git a/magnum/conf/__init__.py b/magnum/conf/__init__.py index 7eaedf5c31..0303abcdff 100644 --- a/magnum/conf/__init__.py +++ b/magnum/conf/__init__.py @@ -23,7 +23,7 @@ from magnum.conf import cluster from magnum.conf import cluster_heat from magnum.conf import cluster_templates from magnum.conf import conductor -# from magnum.conf import database +from magnum.conf import database # from magnum.conf import docker from magnum.conf import glance from magnum.conf import heat @@ -44,7 +44,7 @@ cluster_heat.register_opts(CONF) # certificates.register_opts(CONF) cinder.register_opts(CONF) conductor.register_opts(CONF) -# database.register_opts(CONF) +database.register_opts(CONF) # docker.register_opts(CONF) glance.register_opts(CONF) heat.register_opts(CONF) diff --git a/magnum/conf/database.py b/magnum/conf/database.py new file mode 100644 index 0000000000..e16fa387b1 --- /dev/null +++ b/magnum/conf/database.py @@ -0,0 +1,40 @@ +# 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. + +from oslo_config import cfg +from oslo_db import options + +from magnum.conf import paths + + +_DEFAULT_SQL_CONNECTION = 'sqlite:///' + paths.state_path_def('magnum.sqlite') + +database_group = cfg.OptGroup(name='database', + title='Options for Magnum Database') + +sql_opts = [ + cfg.StrOpt('mysql_engine', + default='InnoDB', + help='MySQL engine to use.') +] + + +def register_opts(conf): + conf.register_group(database_group) + conf.register_opts(sql_opts, group=database_group) + options.set_defaults(conf, _DEFAULT_SQL_CONNECTION, 'magnum.sqlite') + + +def list_opts(): + return { + database_group: sql_opts + } diff --git a/magnum/db/__init__.py b/magnum/db/__init__.py index 1e5b8a1370..e69de29bb2 100644 --- a/magnum/db/__init__.py +++ b/magnum/db/__init__.py @@ -1,31 +0,0 @@ -# Copyright 2015 NEC Corporation. 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. - -from oslo_config import cfg -from oslo_db import options - -from magnum.conf import paths - - -sql_opts = [ - cfg.StrOpt('mysql_engine', - default='InnoDB', - help='MySQL engine to use.') -] - -_DEFAULT_SQL_CONNECTION = 'sqlite:///' + paths.state_path_def('magnum.sqlite') - - -cfg.CONF.register_opts(sql_opts, 'database') -options.set_defaults(cfg.CONF, _DEFAULT_SQL_CONNECTION, 'magnum.sqlite') diff --git a/magnum/db/migration.py b/magnum/db/migration.py index 2606860a2e..2a93f1c5ec 100644 --- a/magnum/db/migration.py +++ b/magnum/db/migration.py @@ -16,18 +16,19 @@ """Database setup and migration commands.""" -from oslo_config import cfg from stevedore import driver +import magnum.conf + +CONF = magnum.conf.CONF _IMPL = None def get_backend(): global _IMPL if not _IMPL: - cfg.CONF.import_opt('backend', 'oslo_db.options', group='database') _IMPL = driver.DriverManager("magnum.database.migration_backend", - cfg.CONF.database.backend).driver + CONF.database.backend).driver return _IMPL diff --git a/magnum/db/sqlalchemy/api.py b/magnum/db/sqlalchemy/api.py index 488474545f..4404758bd3 100644 --- a/magnum/db/sqlalchemy/api.py +++ b/magnum/db/sqlalchemy/api.py @@ -14,7 +14,6 @@ """SQLAlchemy storage backend.""" -from oslo_config import cfg from oslo_db import exception as db_exc from oslo_db.sqlalchemy import session as db_session from oslo_db.sqlalchemy import utils as db_utils @@ -25,11 +24,12 @@ from sqlalchemy.orm.exc import MultipleResultsFound from sqlalchemy.orm.exc import NoResultFound from magnum.common import exception +import magnum.conf from magnum.db import api from magnum.db.sqlalchemy import models from magnum.i18n import _ -CONF = cfg.CONF +CONF = magnum.conf.CONF _FACADE = None diff --git a/magnum/db/sqlalchemy/migration.py b/magnum/db/sqlalchemy/migration.py index f2c4ca66a8..cfbe6e8977 100644 --- a/magnum/db/sqlalchemy/migration.py +++ b/magnum/db/sqlalchemy/migration.py @@ -16,9 +16,11 @@ import os -from oslo_config import cfg from oslo_db.sqlalchemy.migration_cli import manager +import magnum.conf + +CONF = magnum.conf.CONF _MANAGER = None @@ -31,7 +33,7 @@ def get_manager(): os.path.join(os.path.dirname(__file__), 'alembic')) migration_config = {'alembic_ini_path': alembic_path, 'alembic_repo_path': migrate_path, - 'db_url': cfg.CONF.database.connection} + 'db_url': CONF.database.connection} _MANAGER = manager.MigrationManager(migration_config) return _MANAGER diff --git a/magnum/db/sqlalchemy/models.py b/magnum/db/sqlalchemy/models.py index 621fa0ab5d..e80956e9dc 100644 --- a/magnum/db/sqlalchemy/models.py +++ b/magnum/db/sqlalchemy/models.py @@ -18,7 +18,6 @@ SQLAlchemy models for container service import json -from oslo_config import cfg from oslo_db.sqlalchemy import models import six.moves.urllib.parse as urlparse from sqlalchemy import Boolean @@ -31,13 +30,16 @@ from sqlalchemy import String from sqlalchemy import Text from sqlalchemy.types import TypeDecorator, TEXT +import magnum.conf from magnum.i18n import _LE +CONF = magnum.conf.CONF + def table_args(): - engine_name = urlparse.urlparse(cfg.CONF.database.connection).scheme + engine_name = urlparse.urlparse(CONF.database.connection).scheme if engine_name == 'mysql': - return {'mysql_engine': cfg.CONF.database.mysql_engine, + return {'mysql_engine': CONF.database.mysql_engine, 'mysql_charset': "utf8"} return None diff --git a/magnum/opts.py b/magnum/opts.py index 15e48038bf..95ad52514c 100644 --- a/magnum/opts.py +++ b/magnum/opts.py @@ -33,7 +33,6 @@ def list_opts(): magnum.common.rpc_service.periodic_opts, magnum.common.service.service_opts, )), - ('database', magnum.db.sql_opts), ('docker', magnum.common.docker_utils.docker_opts), ('trust', magnum.common.keystone.trust_opts), ('x509', magnum.common.x509.config.x509_opts), diff --git a/magnum/tests/unit/db/base.py b/magnum/tests/unit/db/base.py index cffbb38fc1..711d30caeb 100644 --- a/magnum/tests/unit/db/base.py +++ b/magnum/tests/unit/db/base.py @@ -16,8 +16,8 @@ """Magnum DB test base class.""" import fixtures -from oslo_config import cfg +import magnum.conf from magnum.db import api as dbapi from magnum.db.sqlalchemy import api as sqla_api from magnum.db.sqlalchemy import migration @@ -25,7 +25,7 @@ from magnum.db.sqlalchemy import models from magnum.tests import base -CONF = cfg.CONF +CONF = magnum.conf.CONF _DB_CACHE = None