Removes glance.common.db.sqlalchemy and moves registration of models and create_engine into glance.registry.db.api.
This commit is contained in:
parent
8cd4bc1899
commit
7acd7cd92b
@ -1,16 +0,0 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2010-2011 OpenStack LLC.
|
||||
# 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.
|
@ -1,45 +0,0 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2010 United States Government as represented by the
|
||||
# Administrator of the National Aeronautics and Space Administration.
|
||||
# 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.
|
||||
|
||||
"""
|
||||
Session Handling for SQLAlchemy backend
|
||||
"""
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
_ENGINE = None
|
||||
_MAKER = None
|
||||
|
||||
|
||||
def get_engine(echo=False):
|
||||
global _ENGINE
|
||||
if not _ENGINE:
|
||||
_ENGINE = create_engine('sqlite://', echo=echo)
|
||||
return _ENGINE
|
||||
|
||||
|
||||
def get_session(autocommit=True, expire_on_commit=False):
|
||||
"""Helper method to grab session"""
|
||||
global _MAKER
|
||||
if not _MAKER:
|
||||
engine = get_engine()
|
||||
_MAKER = sessionmaker(bind=engine,
|
||||
autocommit=autocommit,
|
||||
expire_on_commit=expire_on_commit)
|
||||
return _MAKER()
|
@ -16,12 +16,3 @@
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
"""
|
||||
DB abstraction for Nova and Glance
|
||||
"""
|
||||
|
||||
from glance.registry.db import models
|
||||
|
||||
|
||||
models.register_models()
|
||||
|
@ -21,13 +21,19 @@
|
||||
Defines interface for DB access
|
||||
"""
|
||||
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import joinedload
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from glance.common import exception
|
||||
from glance.common import utils
|
||||
from glance.common.db.sqlalchemy.session import get_session
|
||||
from glance.registry.db import models
|
||||
|
||||
_ENGINE = None
|
||||
_MAKER = None
|
||||
BASE = declarative_base()
|
||||
|
||||
# attributes common to all models
|
||||
BASE_MODEL_ATTRS = set(['id', 'created_at', 'updated_at', 'deleted_at',
|
||||
@ -36,7 +42,44 @@ BASE_MODEL_ATTRS = set(['id', 'created_at', 'updated_at', 'deleted_at',
|
||||
IMAGE_ATTRS = BASE_MODEL_ATTRS | set(['name', 'type', 'status', 'size',
|
||||
'is_public', 'location'])
|
||||
|
||||
###################
|
||||
|
||||
def configure_db(options):
|
||||
"""
|
||||
Establish the database, create an engine if needed, and
|
||||
register the models.
|
||||
|
||||
:param options: Mapping of configuration options
|
||||
"""
|
||||
global _ENGINE
|
||||
if not _ENGINE:
|
||||
_ENGINE = create_engine(options['sql_connection'],
|
||||
echo=options['verbose'])
|
||||
register_models()
|
||||
|
||||
|
||||
def get_session(autocommit=True, expire_on_commit=False):
|
||||
"""Helper method to grab session"""
|
||||
global _MAKER, _ENGINE
|
||||
if not _MAKER:
|
||||
assert _ENGINE
|
||||
_MAKER = sessionmaker(bind=_ENGINE,
|
||||
autocommit=autocommit,
|
||||
expire_on_commit=expire_on_commit)
|
||||
return _MAKER()
|
||||
|
||||
|
||||
def register_models():
|
||||
"""Register Models and create properties"""
|
||||
global _ENGINE
|
||||
assert _ENGINE
|
||||
BASE.metadata.create_all(_ENGINE)
|
||||
|
||||
|
||||
def unregister_models():
|
||||
"""Unregister Models, useful clearing out data before testing"""
|
||||
global _ENGINE
|
||||
assert _ENGINE
|
||||
BASE.metadata.drop_all(engine)
|
||||
|
||||
|
||||
def image_create(context, values):
|
||||
|
@ -29,7 +29,7 @@ from sqlalchemy import ForeignKey, DateTime, Boolean, Text
|
||||
from sqlalchemy import UniqueConstraint
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
from glance.common.db.sqlalchemy.session import get_session, get_engine
|
||||
import glance.registry.db.api
|
||||
from glance.common import exception
|
||||
|
||||
BASE = declarative_base()
|
||||
@ -49,7 +49,7 @@ class ModelBase(object):
|
||||
|
||||
def save(self, session=None):
|
||||
"""Save this object"""
|
||||
session = session or get_session()
|
||||
session = session or glance.registry.db.api.get_session()
|
||||
session.add(self)
|
||||
session.flush()
|
||||
|
||||
@ -125,15 +125,3 @@ class ImageProperty(BASE, ModelBase):
|
||||
|
||||
key = Column(String(255), index=True)
|
||||
value = Column(Text)
|
||||
|
||||
|
||||
def register_models():
|
||||
"""Register Models and create properties"""
|
||||
engine = get_engine()
|
||||
BASE.metadata.create_all(engine)
|
||||
|
||||
|
||||
def unregister_models():
|
||||
"""Unregister Models, useful clearing out data before testing"""
|
||||
engine = get_engine()
|
||||
BASE.metadata.drop_all(engine)
|
||||
|
@ -32,6 +32,7 @@ class Controller(wsgi.Controller):
|
||||
|
||||
def __init__(self, options):
|
||||
self.options = options
|
||||
db_api.configure_db(options)
|
||||
|
||||
def index(self, req):
|
||||
"""Return basic information for all public, non-deleted images
|
||||
|
@ -239,7 +239,8 @@ def stub_out_registry_and_store_server(stubs):
|
||||
self.req.body = body
|
||||
|
||||
def getresponse(self):
|
||||
res = self.req.get_response(rserver.API({'sql_connection': 'sqlite://'}))
|
||||
res = self.req.get_response(rserver.API({'sql_connection': 'sqlite://',
|
||||
'verbose': True}))
|
||||
|
||||
# httplib.Response has a read() method...fake it out
|
||||
def fake_reader():
|
||||
@ -284,7 +285,8 @@ def stub_out_registry_and_store_server(stubs):
|
||||
self.req.body = body
|
||||
|
||||
def getresponse(self):
|
||||
res = self.req.get_response(server.API({'registry_host': '0.0.0.0',
|
||||
res = self.req.get_response(server.API({'verbose': True,
|
||||
'registry_host': '0.0.0.0',
|
||||
'registry_port': '9191',
|
||||
'default_store': 'file',
|
||||
'filesystem_store_datadir': FAKE_FILESYSTEM_ROOTDIR}))
|
||||
|
Loading…
x
Reference in New Issue
Block a user