summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYogeshwar Srikrishnan <yoga80@yahoo.com>2011-05-17 15:33:34 -0500
committerYogeshwar Srikrishnan <yoga80@yahoo.com>2011-05-17 15:33:34 -0500
commitff389cbd08d9dcaeb4d311e801ac19f2384c0607 (patch)
tree2c03922fd4406c76eb872c4b27d00797e6cee7ec
parent565223a519c5f9a24973cacd7a1728e1130dbf73 (diff)
Changes to move the db settings to conf file.
-rw-r--r--README.md4
-rw-r--r--etc/keystone.conf10
-rw-r--r--keystone/db/sqlalchemy/__init__.py24
-rw-r--r--keystone/db/sqlalchemy/api.py58
-rw-r--r--keystone/db/sqlalchemy/models.py28
-rw-r--r--keystone/db/sqlalchemy/session.py64
-rwxr-xr-xkeystone/server.py4
7 files changed, 81 insertions, 111 deletions
diff --git a/README.md b/README.md
index 344a462a..4d3141ed 100644
--- a/README.md
+++ b/README.md
@@ -120,11 +120,11 @@ After starting keystone a keystone.db sqlite database should be created in the k
Add test data to the database:
- $ sqlite3 keystone/keystone.db < test/test_setup.sql
+ $ sqlite3 bin/keystone.sqlite < test/test_setup.sql
To clean the test database
- $ sqlite3 keystone/keystone.db < test/kill.sql
+ $ sqlite3 bin/keystone.sqlite < test/kill.sql
To run client demo (with all auth middleware running locally on sample service):
diff --git a/etc/keystone.conf b/etc/keystone.conf
index 198cab3f..8de7ac79 100644
--- a/etc/keystone.conf
+++ b/etc/keystone.conf
@@ -25,3 +25,13 @@ bind_port = 8080
#log_file = /var/log/keystone.log
log_file = keystone.log
+# SQLAlchemy connection string for the reference implementation
+# registry server. Any valid SQLAlchemy connection string is fine.
+# See: http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/connections.html#sqlalchemy.create_engine
+sql_connection = sqlite:///keystone.sqlite
+
+# Period in seconds after which SQLAlchemy should reestablish its connection
+# to the database.
+#
+sql_idle_timeout = 30
+
diff --git a/keystone/db/sqlalchemy/__init__.py b/keystone/db/sqlalchemy/__init__.py
index cffaa881..e69de29b 100644
--- a/keystone/db/sqlalchemy/__init__.py
+++ b/keystone/db/sqlalchemy/__init__.py
@@ -1,24 +0,0 @@
-# vim: tabstop=4 shiftwidth=4 softtabstop=4
-# Copyright (c) 2010-2011 OpenStack, LLC.
-#
-# 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.
-# Not Yet PEP8 standardized
-
-
-from models import register_models
-from session import get_session, get_engine
-
-session = get_session()
-engine = get_engine()
-register_models(session, engine)
diff --git a/keystone/db/sqlalchemy/api.py b/keystone/db/sqlalchemy/api.py
index 78c05ae4..edbeb309 100644
--- a/keystone/db/sqlalchemy/api.py
+++ b/keystone/db/sqlalchemy/api.py
@@ -15,12 +15,68 @@
# limitations under the License.
# Not Yet PEP8 standardized
+import logging
-from session import get_session
from sqlalchemy.orm import joinedload, aliased
+from sqlalchemy import create_engine
+from sqlalchemy.orm import sessionmaker
+from keystone.common import config
import models
+_ENGINE = None
+_MAKER = None
+BASE = models.Base
+
+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:
+ debug = config.get_option(
+ options, 'debug', type='bool', default=False)
+ verbose = config.get_option(
+ options, 'verbose', type='bool', default=False)
+ timeout = config.get_option(
+ options, 'sql_idle_timeout', type='int', default=3600)
+ _ENGINE = create_engine(options['sql_connection'],
+ pool_recycle=timeout)
+ logger = logging.getLogger('sqlalchemy.engine')
+ if debug:
+ logger.setLevel(logging.DEBUG)
+ elif verbose:
+ logger.setLevel(logging.INFO)
+ 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)
+
#
# Tenant API operations
#
diff --git a/keystone/db/sqlalchemy/models.py b/keystone/db/sqlalchemy/models.py
index f8050377..bdcd5015 100644
--- a/keystone/db/sqlalchemy/models.py
+++ b/keystone/db/sqlalchemy/models.py
@@ -20,9 +20,7 @@ from sqlalchemy import DateTime
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, object_mapper
-
-from session import get_session
-
+import api as db_api
Base = declarative_base()
@@ -32,7 +30,7 @@ class KeystoneBase(object):
def save(self, session=None):
"""Save this object."""
if not session:
- session = get_session()
+ session = db_api.get_session()
session.add(self)
try:
session.flush()
@@ -109,15 +107,6 @@ class Tenant(Base, KeystoneBase):
enabled = Column(Integer)
groups = relationship('Group', backref='tenants')
-
-class Group(Base, KeystoneBase):
- __tablename__ = 'groups'
-
- id = Column(String(255), primary_key=True, unique=True)
- desc = Column(String(255))
- tenant_id = Column(String(255), ForeignKey('tenants.id'))
-
-
class Token(Base, KeystoneBase):
__tablename__ = 'token'
@@ -126,10 +115,11 @@ class Token(Base, KeystoneBase):
tenant_id = Column(String(255))
expires = Column(DateTime)
+class Group(Base, KeystoneBase):
+ __tablename__ = 'groups'
+
+ id = Column(String(255), primary_key=True, unique=True)
+ desc = Column(String(255))
+ tenant_id = Column(String(255), ForeignKey('tenants.id'))
+
-def register_models(session, engine):
- models = (User, Tenant, Group, Token, UserGroupAssociation,
- UserTenantAssociation)
- for model in models:
- model.metadata.create_all(engine)
- session.flush()
diff --git a/keystone/db/sqlalchemy/session.py b/keystone/db/sqlalchemy/session.py
deleted file mode 100644
index 4514ef7a..00000000
--- a/keystone/db/sqlalchemy/session.py
+++ /dev/null
@@ -1,64 +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
-"""
-import logging
-import os
-
-from sqlalchemy import create_engine
-from sqlalchemy import pool
-from sqlalchemy.orm import sessionmaker
-
-
-_ENGINE = None
-_MAKER = None
-
-#TODO(India Team) Change the connection string to generic connection string
-
-def get_connection_string():
- path = os.path.realpath(__file__)
- dbpath = os.path.normpath(os.path.join(path,
- os.pardir, # sqlalchemy
- os.pardir, # db
- os.pardir)) # keystone
- connection_string = "sqlite:///%s/keystone.db" % dbpath
- logging.debug('SQL ALchemy connection string: %s', connection_string)
- return connection_string
-
-
-def get_session(autocommit=True, expire_on_commit=False):
- """Helper method to grab session"""
- global _ENGINE
- global _MAKER
- if not _MAKER:
- if not _ENGINE:
- kwargs = {'pool_recycle': 30, 'echo': False}
- kwargs['poolclass'] = pool.NullPool # for SQLite3
- _ENGINE = create_engine(get_connection_string(), **kwargs)
- _MAKER = (sessionmaker(bind=_ENGINE,
- autocommit=autocommit,
- expire_on_commit=expire_on_commit))
- session = _MAKER()
- return session
-
-
-def get_engine():
- if not _ENGINE:
- raise
- return _ENGINE
diff --git a/keystone/server.py b/keystone/server.py
index 5c78f7ae..d7e46cdf 100755
--- a/keystone/server.py
+++ b/keystone/server.py
@@ -58,6 +58,7 @@ if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'keystone', '__init__.py')):
from queryext import exthandler
from keystone.common import wsgi
+from keystone.db.sqlalchemy import api as db_api
import keystone.logic.service as serv
import keystone.logic.types.tenant as tenants
import keystone.logic.types.auth as auth
@@ -578,7 +579,8 @@ class KeystoneAPI(wsgi.Router):
def __init__(self, options):
self.options = options
mapper = routes.Mapper()
-
+
+ db_api.configure_db(options)
# Token Operations
auth_controller = AuthController(options)
mapper.connect("/v1.0/token", controller=auth_controller,