Add database init + sync commands

This commit is contained in:
Kiall Mac Innes 2012-10-26 19:36:10 +01:00
parent d533ec92b7
commit 61d0f9a029
4 changed files with 62 additions and 0 deletions

0
moniker/cli/__init__.py Normal file
View File

55
moniker/cli/database.py Normal file
View File

@ -0,0 +1,55 @@
# Copyright 2012 Managed I.T.
#
# Author: Kiall Mac Innes <kiall@managedit.ie>
#
# 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.
import os
from moniker.openstack.common import log as logging
from moniker.openstack.common import cfg
from migrate.exceptions import DatabaseAlreadyControlledError
from migrate.versioning import api as versioning_api
from cliff.command import Command
from moniker.database import sqlalchemy # Import for sql_connection cfg def.
LOG = logging.getLogger(__name__)
URL = cfg.CONF.sql_connection
REPOSITORY = os.path.abspath(os.path.join(os.path.dirname(__file__), '..',
'database', 'sqlalchemy',
'migrate_repo'))
class InitCommand(Command):
"Init database"
def take_action(self, parsed_args):
try:
LOG.info('Attempting to initialize database')
versioning_api.version_control(url=URL, repository=REPOSITORY)
LOG.info('Database initialize sucessfully')
except DatabaseAlreadyControlledError:
LOG.error('Database already initialized')
class SyncCommand(Command):
"Sync database"
def take_action(self, parsed_args):
# TODO: Support specifying version
try:
LOG.info('Attempting to synchronize database')
versioning_api.upgrade(url=URL, repository=REPOSITORY,
version=None)
LOG.info('Database synchronized sucessfully')
except DatabaseAlreadyControlledError:
LOG.error('Database synchronize failed')

View File

@ -46,5 +46,11 @@ setup(
'bin/moniker-api',
'bin/moniker-central',
],
entry_points={
'moniker.cli': [
'database init = moniker.cli.database:InitCommand',
'database sync = moniker.cli.database:SyncCommand',
],
},
cmdclass=common_setup.get_cmdclass(),
)

View File

@ -5,6 +5,7 @@ jsonschema>=0.6
ipaddr
PasteDeploy
sqlalchemy-migrate>=0.7.2
-e git://github.com/managedit/python-monikerclient.git#egg=monikerclient
# From OpenStack Common
routes==1.12.3