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
This commit is contained in:
Ruslan Kamaldinov 2014-03-31 15:38:38 +04:00 committed by Gerrit Code Review
parent d591d98b5e
commit 17afc0e0fd
4 changed files with 84 additions and 33 deletions

View File

@ -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
}

65
muranoapi/cmd/manage.py Normal file
View File

@ -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)

View File

@ -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)

View File

@ -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