From cd254bf7a776dc73ee04d5cb9db8b20d63d9c1fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Schneider?= Date: Mon, 9 Mar 2015 17:33:52 +0100 Subject: [PATCH] Migrate on Mandaye >= 0.11 --- manager.py | 56 ++++++ mandaye_vincennes/__init__.py | 5 + mandaye_vincennes/config.py | 185 ------------------ mandaye_vincennes/default-config.ini | 9 + mandaye_vincennes/mappers/biblio_vincennes.py | 4 +- mandaye_vincennes/mappers/duonet_vincennes.py | 4 +- .../mappers/famille_vincennes.py | 4 +- mandaye_vincennes/wsgi.py | 10 +- mandaye_vincennes_manager | 66 ------- requirements.txt | 2 +- mandaye_vincennes_server => server.py | 14 +- setup.py | 4 +- 12 files changed, 96 insertions(+), 267 deletions(-) create mode 100755 manager.py delete mode 100644 mandaye_vincennes/config.py delete mode 100755 mandaye_vincennes_manager rename mandaye_vincennes_server => server.py (63%) diff --git a/manager.py b/manager.py new file mode 100755 index 0000000..36b8b56 --- /dev/null +++ b/manager.py @@ -0,0 +1,56 @@ +#! /usr/bin/python +# -*- coding: utf-8 -*- + +""" Script to administrate mandaye server +""" + +import os + +from optparse import OptionParser +from mandaye_vincennes import default_config + +def get_cmd_options(): + usage = "usage: %prog --config=/path/to/config.ini --createdb|--upgradedb" + parser = OptionParser(usage=usage) + parser.add_option("--config", + dest="config", + type="string", + help="Path of the configuration file" + ) + parser.add_option("--createdb", + dest="createdb", + default=False, + action="store_true", + help="Create Mandaye database" + ) + parser.add_option("--upgradedb", + dest="upgradedb", + default=False, + action="store_true", + help="Upgrade Mandaye database" + ) + (options, args) = parser.parse_args() + return options + +def main(): + options = get_cmd_options() + + config_files = [default_config] + if options.config: + config_files.append(options.config) + os.environ['MANDAYE_CONFIG_FILES'] = ' '.join(config_files) + + from mandaye import config + from mandaye.log import logger + if options.createdb or options.upgradedb: + logger.info("Creating or upgrading database...") + from alembic.config import Config + from alembic import command + alembic_cfg = Config(config.alembic_cfg) + alembic_cfg.set_main_option("script_location", config.alembic_script_path) + command.upgrade(alembic_cfg, "head") + logger.info("Database upgraded") + +if __name__ == "__main__": + main() + diff --git a/mandaye_vincennes/__init__.py b/mandaye_vincennes/__init__.py index 96738fd..9f76262 100644 --- a/mandaye_vincennes/__init__.py +++ b/mandaye_vincennes/__init__.py @@ -1 +1,6 @@ __version__="0.3.4" + +import os + +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) +default_config = os.path.join(BASE_DIR, 'default-config.ini') diff --git a/mandaye_vincennes/config.py b/mandaye_vincennes/config.py deleted file mode 100644 index 614b23c..0000000 --- a/mandaye_vincennes/config.py +++ /dev/null @@ -1,185 +0,0 @@ -import logging -import os - -from ConfigParser import SafeConfigParser -from mandaye.exceptions import ImproperlyConfigured - -# get configuration files from : -# 1. default-settings.ini from source code -# 2. os.environ.get('SETTINGS_INI') if it exists -# else /etc/mandaye-cam/config.ini -# and then /etc/mandaye-cam/local-config.ini -BASE_DIR = os.path.dirname(os.path.abspath(__file__)) -SETTINGS_INI = (os.path.join(BASE_DIR, 'default-config.ini'),) -if os.environ.get('SETTINGS_INI'): - SETTINGS_INI += (os.environ.get('SETTINGS_INI'),) -else: - ETC_DIR = os.path.join('/', 'etc', 'mandaye-vincennes') - SETTINGS_INI += ( - os.path.join(ETC_DIR, 'config.ini'), - os.path.join(BASE_DIR, 'local-config.ini') - ) - -config = SafeConfigParser() -config.read(SETTINGS_INI) - -## SQL Backend config -# Database configuration -# http://docs.sqlalchemy.org/en/rel_0_7/core/engines.html -# rfc 1738 https://tools.ietf.org/html/rfc1738 -# dialect+driver://username:password@host:port/database -db_url = config.get('database', 'url') - -## LDAP Backend config -ldap_url = config.get('ldap', 'url') -ldap_bind_dn = config.get('ldap', 'bind_dn') -ldap_bind_password = config.get('ldap', 'bind_password') -ldap_base_dn = config.get('ldap', 'base_dn') - -debug = config.getboolean('debug', 'debug') - -# Log configuration -LOGGING = { - 'version': 1, - 'disable_existing_loggers': True, - - 'formatters': { - 'console': { - 'format': '%(asctime)s %(levelname)s %(message)s', - 'datefmt': '%H:%M:%S', - }, - 'file': { - 'format': '%(asctime)s %(levelname)s %(uuid)s %(message)s', - 'datefmt': '%Y-%m-%d %H:%M:%S' - } - }, - 'handlers': { - 'console': { - 'level': 'DEBUG', - 'class': 'logging.StreamHandler', - 'formatter': 'console' - }, - 'syslog': { - 'level': 'DEBUG', - 'class': 'entrouvert.logging.handlers.SysLogHandler', - 'formatter': 'file', - 'address': '/dev/log' - }, - }, - 'loggers': { - '': { - 'handlers': ['console'], - 'level': 'INFO', - 'propagate': False, - }, - 'mandaye': { - 'handlers': ['console', 'syslog'], - 'level': 'INFO', - 'propagate': False, - }, - 'cam': { - 'handlers': ['console', 'syslog'], - 'level': 'INFO', - 'propagate': False, - }, - }, - } - -if config.getboolean('debug', 'log_debug'): - LOGGING['loggers']['']['level'] = 'DEBUG' - LOGGING['loggers']['mandaye']['level'] = 'DEBUG' - LOGGING['loggers']['cam']['level'] = 'DEBUG' - -## PATH -# Configuration directory -config_root = config.get('dirs', 'config_root') -# Templates directories -templates_directories = [] -if config.get('dirs', 'templates_directories'): - templates_directories = config.get('dirs', 'templates_directories').split(' ') -templates_directories.append(os.path.join(BASE_DIR, 'templates')) -# Static url -static_url = config.get('dirs', 'static_url') -# Static folder -static_root = config.get('dirs', 'static_root') -# Data dir -data_dir = config.get('dirs', 'data_dir') - -# template vars -template_vars = {} -if config.has_section('template_vars'): - for option in config.options('template_vars'): - template_vars[option] = config.get('template_vars', option) - -# Supported authentification -authentifications = { - 'saml2': 'mandaye.auth.saml2.SAML2Auth', - 'saml2_espace_famille': 'mandaye_vincennes.auth.espacefamille.EspaceFamilleAuth' - } - -# sp mappers -mappers = { - 'biblio': 'mandaye_vincennes.mappers.biblio_vincennes', - 'duonet': 'mandaye_vincennes.mappers.duonet_vincennes', - 'espace_famille': 'mandaye_vincennes.mappers.famille_vincennes', - } - -# Raven Sentry configuration -raven_dsn = config.get('debug', 'sentry_dsn') - -# Email notification configuration -email_notification = config.getboolean('email', 'notification') -email_prefix = config.get('email', 'prefix') -smtp_host = config.get('email', 'smtp_host') -smtp_port = config.getint('email', 'smtp_port') -email_from = config.get('email', 'from') -email_to = config.get('email', 'to').split() - -# Use long traceback with xtraceback -use_long_trace = config.getboolean('debug', 'use_long_trace') -# Ask Mandaye to auto decompress a response message -# Decompress response only if you load a filter -auto_decompress = config.getboolean('mandaye', 'auto_decompress') -# Ask mandaye to add a toolbar with Mandaye's links -mandaye_toolbar = config.getboolean('mandaye', 'toolbar') -mandaye_offline_toolbar = config.getboolean('mandaye', 'offline_toolbar') -# Authentic 2 auto connection -a2_auto_connection = config.getboolean('mandaye', 'a2_auto_connection') - -# Choose storage (sql or ldap) -if config.get('mandaye', 'storage_backend') == 'sql': - storage_backend = "mandaye.backends.sql" -elif config.get('mandaye', 'storage_backend') == 'ldap': - storage_backend = "mandaye.backends.ldap_back" -else: - ImproperlyConfigured('Storage backend must be sql or ldap') - - -# Encrypt service provider passwords with a secret -# You should install pycypto to use this feature -encrypt_sp_password = config.getboolean('mandaye', 'encrypt_sp_password') -# Must be a 15, 24, or 32 bytes long -encrypt_secret = config.get('mandaye', 'encrypt_secret') - -session_type = config.get('session', 'type') -if session_type not in ('file', 'dbm', 'memory', 'memcached'): - raise ImproperlyConfigured('Sesssion type %r not supported' % session_type) -if session_type == 'memcached': - session_type = 'ext:memcached' - -# Beaker session configuration -session_opts = { - 'session.type': session_type, - 'session.url': config.get('session', 'url'), - 'session.cookie_expires': config.getboolean('session', 'cookie_expires'), - 'session.timeout': config.getint('session', 'timeout'), - 'session.data_dir': config.get('session', 'data_dir') -} - -# Import local config -try: - from cam.local_config import * -except ImportError, e: - if not 'local_config' in e.args[0]: - raise ImproperlyConfigured('Error while importing "local_config.py"') - diff --git a/mandaye_vincennes/default-config.ini b/mandaye_vincennes/default-config.ini index e8f9919..e8b973b 100644 --- a/mandaye_vincennes/default-config.ini +++ b/mandaye_vincennes/default-config.ini @@ -13,6 +13,7 @@ bind_password: AdminPassword base_dn: ou=mandaye,dc=acompany,dc=org [dirs] +module_name: mandaye_vincennes config_root: %(base_dir)s/conf.d data_dir: %(base_dir)s/data static_root: %(base_dir)s/mandaye_vincennes/static @@ -40,6 +41,14 @@ encrypt_sp_password: false ; must be a 16, 24, or 32 bytes long encrypt_secret: +[mappers] +biblio: mandaye_vincennes.mappers.biblio_vincennes +duonet: mandaye_vincennes.mappers.duonet_vincennes +espace_famille: mandaye_vincennes.mappers.famille_vincennes + +[authentifications] +saml2_espace_famille: mandaye_vincennes.auth.espacefamille.EspaceFamilleAuth + [template_vars] wcs_url: https://demarches.vincennes.fr site_url: https://www.vincennes.fr diff --git a/mandaye_vincennes/mappers/biblio_vincennes.py b/mandaye_vincennes/mappers/biblio_vincennes.py index bf0e023..df7d31c 100644 --- a/mandaye_vincennes/mappers/biblio_vincennes.py +++ b/mandaye_vincennes/mappers/biblio_vincennes.py @@ -21,6 +21,8 @@ urls = { 'disassociate_url': '/mandaye/disassociate', } +replay_condition = lambda env, response: response.code==302 + mapping = [ { 'path': r'/(?!/*mandaye)', @@ -50,7 +52,6 @@ mapping = [ 'method': 'GET', 'response': { 'auth': 'login', - 'values': {'condition': 'response.code==302'}, } }, { @@ -112,7 +113,6 @@ mapping = [ 'method': 'POST', 'response': { 'auth': 'associate_submit', - 'values': {'condition': "response.code==302"} } }, { diff --git a/mandaye_vincennes/mappers/duonet_vincennes.py b/mandaye_vincennes/mappers/duonet_vincennes.py index bc1ea21..9e56644 100644 --- a/mandaye_vincennes/mappers/duonet_vincennes.py +++ b/mandaye_vincennes/mappers/duonet_vincennes.py @@ -21,6 +21,8 @@ urls = { 'disassociate_url': '/mandaye/disassociate', } +replay_condition = lambda env, response: response.code==302 + mapping = [ { 'path': r'/', @@ -41,7 +43,6 @@ mapping = [ 'method': 'GET', 'response': { 'auth':'login', - 'values': {'condition': 'response.code==302'}, } }, { @@ -63,7 +64,6 @@ mapping = [ 'method': 'POST', 'response': { 'auth': 'associate_submit', - 'values': {'condition': 'response.code==302'} } }, { diff --git a/mandaye_vincennes/mappers/famille_vincennes.py b/mandaye_vincennes/mappers/famille_vincennes.py index 10fcbe1..d018208 100644 --- a/mandaye_vincennes/mappers/famille_vincennes.py +++ b/mandaye_vincennes/mappers/famille_vincennes.py @@ -22,6 +22,8 @@ urls = { 'disassociate_url': '/mandaye/disassociate', } +replay_condition = lambda env, response: response.code==302 + mapping = [ { 'path': r'/$', @@ -45,7 +47,6 @@ mapping = [ 'method': 'GET', 'response': { 'auth': 'login', - 'values': {'condition': 'response.code==302'}, }, }, { @@ -68,7 +69,6 @@ mapping = [ 'method': 'POST', 'response': { 'auth': 'associate_submit', - 'values': {'condition': 'response.code==302'} }, }, { diff --git a/mandaye_vincennes/wsgi.py b/mandaye_vincennes/wsgi.py index 19935ad..2b0c515 100644 --- a/mandaye_vincennes/wsgi.py +++ b/mandaye_vincennes/wsgi.py @@ -1,14 +1,18 @@ import os +from mandaye_vincennes import default_config -os.environ.setdefault("MANDAYE_CONFIG_MODULE", "mandaye_vincennes.config") +if os.environ.has_key('MANDAYE_CONFIG_FILES'): + os.environ['MANDAYE_CONFIG_FILES'] = default_config + ' ' + \ + os.environ['MANDAYE_CONFIG_FILES'] +else: + os.environ['MANDAYE_CONFIG_FILES'] = default_config from beaker.middleware import SessionMiddleware from whitenoise import WhiteNoise -from mandaye_vincennes import config - import mandaye +from mandaye import config from mandaye.server import MandayeApp # production diff --git a/mandaye_vincennes_manager b/mandaye_vincennes_manager deleted file mode 100755 index 2ed657c..0000000 --- a/mandaye_vincennes_manager +++ /dev/null @@ -1,66 +0,0 @@ -#! /usr/bin/python -# -*- coding: utf-8 -*- - -""" Script to administrate mandaye server -""" - -import os -os.environ['MANDAYE_CONFIG_MODULE'] = 'mandaye_vincennes.config' - -from optparse import OptionParser - -from mandaye import config -from mandaye.backends.default import Association -from mandaye.log import logger - -def get_cmd_options(): - usage = "usage: %prog --createdb|--upgradedb|--sql2ldap=SQL_SCHEMA" - parser = OptionParser(usage=usage) - parser.add_option("--createdb", - dest="createdb", - default=False, - action="store_true", - help="Create SQL Mandaye database" - ) - parser.add_option("--upgradedb", - dest="upgradedb", - default=False, - action="store_true", - help="Upgrade SQL Mandaye database" - ) - parser.add_option("--sql2ldap", - dest="sql_schema", - default="", - type="string", - help="SQL_SCHEMA: sqlalchemy schema (ex postgresql://mandaye@/mandayeold)") - (options, args) = parser.parse_args() - return options - -def main(): - options = get_cmd_options() - if options.createdb or options.upgradedb: - logger.info("Creating or upgrading database...") - from alembic.config import Config - from alembic import command - from mandaye import global_config - alembic_cfg = Config(global_config.alembic_cfg) - alembic_cfg.set_main_option("script_location", global_config.alembic_script_path) - command.upgrade(alembic_cfg, "head") - logger.info("Database upgraded") - if options.sql_schema: - logger.info("Migrate from %r to %r", options.sql_schema, config.ldap_url) - from sqlalchemy import create_engine - from sqlalchemy.orm import sessionmaker, scoped_session - from mandaye.models import SPUser - sql_session = scoped_session(sessionmaker( - bind=create_engine(options.sql_schema))) - for sp_user in sql_session.query(SPUser).all(): - Association.update_or_create(sp_user.service_provider.name, - sp_user.login, sp_user.post_values, - sp_user.idp_user.unique_id, sp_user.idp_user.idp_id, - creation_date=sp_user.creation_date, - last_connection_date=sp_user.last_connection) - -if __name__ == "__main__": - main() - diff --git a/requirements.txt b/requirements.txt index e1af84c..d606c92 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ gunicorn>=0.17 pycrypto>=2.6 -mandaye>=0.7 +mandaye>=0.11 BeautifulSoup>=3.1 diff --git a/mandaye_vincennes_server b/server.py similarity index 63% rename from mandaye_vincennes_server rename to server.py index 31c9105..adfb1cb 100755 --- a/mandaye_vincennes_server +++ b/server.py @@ -5,11 +5,8 @@ """ import os -os.environ['MANDAYE_CONFIG_MODULE'] = 'mandaye_vincennes.config' - import sys -from mandaye.log import logger from gunicorn.app.wsgiapp import WSGIApplication class MandayeWSGIApplication(WSGIApplication): @@ -22,7 +19,16 @@ def main(): """ The ``gunicorn`` command line runner for launcing Gunicorn with generic WSGI applications. """ - logger.info('mandaye_vincennes reverse-proxy start') + config_file = None + config_arg_pos = None + for i, arg in enumerate(sys.argv[1:]): + if arg.startswith('--config='): + config_file = arg.split('=')[1] + config_arg_pos = i + if config_file: + os.environ['MANDAYE_CONFIG_FILES'] = config_file + if config_arg_pos is not None: + del sys.argv[config_arg_pos + 1] MandayeWSGIApplication("%(prog)s [OPTIONS]").run() if __name__ == "__main__": diff --git a/setup.py b/setup.py index 20ff947..25455ff 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ from sys import version install_requires=[ 'gunicorn>=0.17', - 'mandaye>=0.8', + 'mandaye>=0.11', 'pycrypto>=2.6', 'BeautifulSoup>=3.1' ] @@ -41,7 +41,7 @@ setup(name="mandaye-vincennes", author_email="info@entrouvert.org", maintainer="Jerome Schneider", maintainer_email="jschneider@entrouvert.com", - scripts=['mandaye_vincennes_manager', 'mandaye_vincennes_server'], + scripts=['manager.py', 'server.py'], packages=find_packages(), include_package_data=True, install_requires=install_requires