From 33b14b1ec0cd58f66e197a6d1a5a2284e969d163 Mon Sep 17 00:00:00 2001 From: Tanshu Date: Sun, 31 Jan 2016 13:58:15 +0530 Subject: [PATCH] Feature: Take some of the parameters from the Environment so that it can work in a containerized environment --- brewman/__init__.py | 17 +++++++++++---- container.ini | 50 +++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 4 +++- setup.py | 7 ++++--- 4 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 container.ini diff --git a/brewman/__init__.py b/brewman/__init__.py index 40e16a02..efd9d39b 100644 --- a/brewman/__init__.py +++ b/brewman/__init__.py @@ -1,6 +1,8 @@ import datetime +import os from pyramid.config import Configurator from sqlalchemy import engine_from_config +from sqlalchemy.engine.url import make_url from pyramid.authentication import AuthTktAuthenticationPolicy from pyramid.authorization import ACLAuthorizationPolicy @@ -10,16 +12,23 @@ from brewman.models import initialize_sql from brewman.renderers import json_renderer, CSVRenderer from brewman.security import groupfinder -current_table = 1 - - def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ + if 'sqlalchemy.url' not in settings: + DB_NAME = os.environ['DB_NAME'] + DB_USER = os.environ['DB_USER'] + DB_PASS = os.environ['DB_PASS'] + DB_URI = 'postgresql://{0}:{1}@postgres:5432/{2}'.format( + DB_USER, DB_PASS, DB_NAME + ) + + settings['sqlalchemy.url']= DB_URI engine = engine_from_config(settings, 'sqlalchemy.') initialize_sql(engine) - session_factory = SignedCookieSessionFactory('secret') + SECRET_KEY = os.environ.get('SECRET_KEY', settings.get('secret_key', '')) + session_factory = SignedCookieSessionFactory(SECRET_KEY) authentication_policy = AuthTktAuthenticationPolicy('brewman', timeout=900, reissue_time=90, callback=groupfinder) authorization_policy = ACLAuthorizationPolicy() diff --git a/container.ini b/container.ini new file mode 100644 index 00000000..3dba1d55 --- /dev/null +++ b/container.ini @@ -0,0 +1,50 @@ +[app:main] +use = egg:brewman + +pyramid.reload_templates = false +pyramid.debug_authorization = false +pyramid.debug_notfound = false +pyramid.debug_routematch = false +pyramid.default_locale_name = en +secret_key = secret + +[server:main] +use = egg:waitress#main +host = 0.0.0.0 +port = 80 + +# Begin logging configuration + +[loggers] +keys = root, brewman, sqlalchemy.engine.base + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARNING +handlers = console + +[logger_brewman] +level = WARNING +handlers = +qualname = brewman + +[logger_sqlalchemy.engine.base] +level = DEBUG +handlers = +qualname = sqlalchemy.engine.base + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s + +# End logging configuration diff --git a/requirements.txt b/requirements.txt index ce7f81b7..6fce6af9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,7 @@ -pyramid +pyramid<1.6 waitress transaction zope.sqlalchemy SQLAlchemy +psycopg2 + diff --git a/setup.py b/setup.py index 1b25967c..ac2ed98b 100644 --- a/setup.py +++ b/setup.py @@ -11,9 +11,10 @@ with open(os.path.join(here, 'CHANGES.txt')) as f: requires = [ 'pyramid', 'waitress', - 'transaction', - 'zope.sqlalchemy', - 'SQLAlchemy', + 'transaction', + 'zope.sqlalchemy', + 'SQLAlchemy', + 'psycopg2', ] setup(name='brewman',