From 17afc0e0fd92dc8585dd6e60bc00d7f7ecdc9ddd Mon Sep 17 00:00:00 2001 From: Ruslan Kamaldinov Date: Mon, 31 Mar 2014 15:38:38 +0400 Subject: [PATCH] Added explicit db-sync command This patch changes the way we initialize the DB with tables: * there is new command - 'murano-manage db_sync' * DB is no longer automatically populated Change-Id: I1ae06448ddf4a832740e9a2bfccbfd725cb6c3f7 --- contrib/devstack/lib/murano | 3 +- muranoapi/cmd/manage.py | 65 +++++++++++++++++++++++++++++++++++++ muranoapi/db/session.py | 48 ++++++++++----------------- setup.cfg | 1 + 4 files changed, 84 insertions(+), 33 deletions(-) create mode 100644 muranoapi/cmd/manage.py diff --git a/contrib/devstack/lib/murano b/contrib/devstack/lib/murano index 754475b9..1546ed03 100644 --- a/contrib/devstack/lib/murano +++ b/contrib/devstack/lib/murano @@ -133,8 +133,7 @@ function init_murano() { # (re)create Murano database recreate_database murano utf8 - # TODO(ruhe): run DB migration script. it doesn't exist yet!!! - # $MURANO_BIN_DIR/murano-db-manage --config-file $MURANO_CONF_FILE upgrade head + $MURANO_BIN_DIR/murano-manage --config-file $MURANO_CONF_FILE db_sync } diff --git a/muranoapi/cmd/manage.py b/muranoapi/cmd/manage.py new file mode 100644 index 00000000..2d9af5e8 --- /dev/null +++ b/muranoapi/cmd/manage.py @@ -0,0 +1,65 @@ +# Copyright (c) 2014 Mirantis, Inc. +# +# 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. + +""" + CLI interface for murano management. +""" + +import sys + +from oslo.config import cfg + +import muranoapi +from muranoapi.db import session as db_session +from muranoapi.openstack.common import log + + +CONF = cfg.CONF + + +def do_db_sync(): + """ + Place a database under migration control and upgrade, + creating first if necessary. + """ + db_session.db_sync() + + +def add_command_parsers(subparsers): + parser = subparsers.add_parser('db_sync') + parser.set_defaults(func=do_db_sync) + parser.add_argument('version', nargs='?') + parser.add_argument('current_version', nargs='?') + +command_opt = cfg.SubCommandOpt('command', + title='Commands', + help='Show available commands.', + handler=add_command_parsers) + + +def main(): + CONF.register_cli_opt(command_opt) + try: + default_config_files = cfg.find_config_files('murano-api', 'murano') + CONF(sys.argv[1:], project='murano-api', prog='murano-manage', + version=muranoapi.__version__, + default_config_files=default_config_files) + log.setup("murano-api") + except RuntimeError as e: + sys.exit("ERROR: %s" % e) + + try: + CONF.command.func() + except Exception as e: + sys.exit("ERROR: %s" % e) diff --git a/muranoapi/db/session.py b/muranoapi/db/session.py index 8541266a..a5051f33 100644 --- a/muranoapi/db/session.py +++ b/muranoapi/db/session.py @@ -1,10 +1,4 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright (c) 2011 X.commerce, a business unit of eBay Inc. -# Copyright 2010 United States Government as represented by the -# Administrator of the National Aeronautics and Space Administration. -# Copyright 2011 Piston Cloud Computing, Inc. -# All Rights Reserved. +# Copyright (c) 2014 Mirantis, Inc. # # 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 @@ -17,6 +11,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. + """Session management functions.""" import os @@ -27,34 +22,10 @@ from migrate.versioning import api as versioning_api from muranoapi.common import config from muranoapi.db import migrate_repo from muranoapi.openstack.common.db.sqlalchemy import session as db_session -from muranoapi.openstack.common.gettextutils import _ # noqa from muranoapi.openstack.common import log as logging LOG = logging.getLogger(__name__) CONF = config.CONF - - -def get_session(autocommit=True, expire_on_commit=False): - s = _create_facade_lazily().get_session(autocommit=autocommit, - expire_on_commit=expire_on_commit) - if s: - if CONF.database.auto_create: - LOG.info(_('auto-creating DB')) - _auto_create_db() - else: - LOG.info(_('not auto-creating DB')) - return s - - -def _auto_create_db(): - repo_path = os.path.abspath(os.path.dirname(migrate_repo.__file__)) - try: - versioning_api.upgrade(CONF.database.connection, repo_path) - except versioning_exceptions.DatabaseNotControlledError: - versioning_api.version_control(CONF.database.connection, repo_path) - versioning_api.upgrade(CONF.database.connection, repo_path) - - _FACADE = None @@ -67,3 +38,18 @@ def _create_facade_lazily(): **dict(CONF.database.iteritems()) ) return _FACADE + + +def get_session(autocommit=True, expire_on_commit=False): + s = _create_facade_lazily().get_session(autocommit=autocommit, + expire_on_commit=expire_on_commit) + return s + + +def db_sync(): + repo_path = os.path.abspath(os.path.dirname(migrate_repo.__file__)) + try: + versioning_api.upgrade(CONF.database.connection, repo_path) + except versioning_exceptions.DatabaseNotControlledError: + versioning_api.version_control(CONF.database.connection, repo_path) + versioning_api.upgrade(CONF.database.connection, repo_path) diff --git a/setup.cfg b/setup.cfg index fe57f9e0..9fcc8a81 100644 --- a/setup.cfg +++ b/setup.cfg @@ -44,6 +44,7 @@ setup-hooks = console_scripts = murano-api = muranoapi.cmd.api:main murano-engine = muranoapi.cmd.engine:main + murano-manage = muranoapi.cmd.manage:main [build_sphinx] all_files = 1