From 74112dd7cfbfad89c1c3e65193610fe7291a2e12 Mon Sep 17 00:00:00 2001 From: Prudhvi Rao Shedimbi Date: Tue, 29 Nov 2016 23:49:57 +0000 Subject: [PATCH] Implemented db config module Implemented db config module Partially Implements: blueprint centralise-config-opts. Also moved basedir_def, bindir_def and state_path_def to watcher.conf.paths Change-Id: I73d201f6a23bbdb1c6189434b11314a66620e85c --- watcher/common/paths.py | 15 ----------- watcher/conf/__init__.py | 2 ++ watcher/conf/_opts.py | 4 +-- watcher/conf/db.py | 44 +++++++++++++++++++++++++++++++++ watcher/conf/paths.py | 15 +++++++++++ watcher/db/sqlalchemy/models.py | 20 +++------------ 6 files changed, 67 insertions(+), 33 deletions(-) create mode 100644 watcher/conf/db.py diff --git a/watcher/common/paths.py b/watcher/common/paths.py index b36187bd8..ff0529129 100644 --- a/watcher/common/paths.py +++ b/watcher/common/paths.py @@ -22,21 +22,6 @@ from watcher import conf CONF = conf.CONF -def basedir_def(*args): - """Return an uninterpolated path relative to $pybasedir.""" - return os.path.join('$pybasedir', *args) - - -def bindir_def(*args): - """Return an uninterpolated path relative to $bindir.""" - return os.path.join('$bindir', *args) - - -def state_path_def(*args): - """Return an uninterpolated path relative to $state_path.""" - return os.path.join('$state_path', *args) - - def basedir_rel(*args): """Return a path relative to $pybasedir.""" return os.path.join(CONF.pybasedir, *args) diff --git a/watcher/conf/__init__.py b/watcher/conf/__init__.py index 3535d9516..940c982d7 100644 --- a/watcher/conf/__init__.py +++ b/watcher/conf/__init__.py @@ -20,6 +20,7 @@ from oslo_config import cfg from watcher.conf import api +from watcher.conf import db from watcher.conf import exception from watcher.conf import paths from watcher.conf import service @@ -32,3 +33,4 @@ api.register_opts(CONF) utils.register_opts(CONF) paths.register_opts(CONF) exception.register_opts(CONF) +db.register_opts(CONF) diff --git a/watcher/conf/_opts.py b/watcher/conf/_opts.py index 4765daf36..22328d261 100644 --- a/watcher/conf/_opts.py +++ b/watcher/conf/_opts.py @@ -21,10 +21,10 @@ from keystoneauth1 import loading as ka_loading from watcher.applier import manager as applier_manager from watcher.common import clients from watcher.conf import api as conf_api +from watcher.conf import db from watcher.conf import exception from watcher.conf import paths from watcher.conf import utils -from watcher.db.sqlalchemy import models from watcher.decision_engine.audit import continuous from watcher.decision_engine import manager as decision_engine_manager from watcher.decision_engine.planner import manager as planner_manager @@ -39,7 +39,7 @@ def list_opts(): paths.PATH_OPTS + utils.UTILS_OPTS)), ('api', conf_api.API_SERVICE_OPTS), - ('database', models.SQL_OPTS), + ('database', db.SQL_OPTS), ('watcher_decision_engine', (decision_engine_manager.WATCHER_DECISION_ENGINE_OPTS + continuous.WATCHER_CONTINUOUS_OPTS)), diff --git a/watcher/conf/db.py b/watcher/conf/db.py new file mode 100644 index 000000000..898968798 --- /dev/null +++ b/watcher/conf/db.py @@ -0,0 +1,44 @@ +# -*- encoding: utf-8 -*- +# Copyright (c) 2016 Intel Corp +# +# Authors: Prudhvi Rao Shedimbi +# +# 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 as oslo_db_options + +from watcher.conf import paths + +_DEFAULT_SQL_CONNECTION = 'sqlite:///{0}'.format( + paths.state_path_def('watcher.sqlite')) + +database = cfg.OptGroup(name='database', + title='Configuration Options for database') + +SQL_OPTS = [ + cfg.StrOpt('mysql_engine', + default='InnoDB', + help='MySQL engine to use.') +] + + +def register_opts(conf): + oslo_db_options.set_defaults(conf, connection=_DEFAULT_SQL_CONNECTION) + conf.register_group(database) + conf.register_opts(SQL_OPTS, group=database) + + +def list_opts(): + return [('database', SQL_OPTS)] diff --git a/watcher/conf/paths.py b/watcher/conf/paths.py index 14a0fa1ba..a499614e8 100644 --- a/watcher/conf/paths.py +++ b/watcher/conf/paths.py @@ -34,6 +34,21 @@ PATH_OPTS = [ ] +def basedir_def(*args): + """Return an uninterpolated path relative to $pybasedir.""" + return os.path.join('$pybasedir', *args) + + +def bindir_def(*args): + """Return an uninterpolated path relative to $bindir.""" + return os.path.join('$bindir', *args) + + +def state_path_def(*args): + """Return an uninterpolated path relative to $state_path.""" + return os.path.join('$state_path', *args) + + def register_opts(conf): conf.register_opts(PATH_OPTS) diff --git a/watcher/db/sqlalchemy/models.py b/watcher/db/sqlalchemy/models.py index 0ac8919d8..59a8c08a1 100644 --- a/watcher/db/sqlalchemy/models.py +++ b/watcher/db/sqlalchemy/models.py @@ -16,8 +16,6 @@ SQLAlchemy models for watcher service """ -from oslo_config import cfg -from oslo_db import options as db_options from oslo_db.sqlalchemy import models from oslo_serialization import jsonutils import six.moves.urllib.parse as urlparse @@ -33,25 +31,15 @@ from sqlalchemy import Text from sqlalchemy.types import TypeDecorator, TEXT from sqlalchemy import UniqueConstraint -from watcher.common import paths +from watcher import conf -SQL_OPTS = [ - cfg.StrOpt('mysql_engine', - default='InnoDB', - help='MySQL engine to use.') -] - -_DEFAULT_SQL_CONNECTION = 'sqlite:///{0}'.format( - paths.state_path_def('watcher.sqlite')) - -cfg.CONF.register_opts(SQL_OPTS, 'database') -db_options.set_defaults(cfg.CONF, _DEFAULT_SQL_CONNECTION, 'watcher.sqlite') +CONF = 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