diff --git a/swh/deposit/config.py b/swh/deposit/config.py --- a/swh/deposit/config.py +++ b/swh/deposit/config.py @@ -47,8 +47,8 @@ def setup_django_for(platform): - """Setup function for command line tools (swh.deposit.create_user, - swh.deposit.scheduler.cli) to initialize the needed db access. + """Setup function for command line tools (swh.deposit.create_user) to + initialize the needed db access. Note: Do not import any django related module prior to this function diff --git a/swh/deposit/settings/production.py b/swh/deposit/settings/production.py --- a/swh/deposit/settings/production.py +++ b/swh/deposit/settings/production.py @@ -1,8 +1,11 @@ -# Copyright (C) 2017 The Software Heritage developers +# Copyright (C) 2017-2019 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +import os +# import logging + from .common import * # noqa from .common import ALLOWED_HOSTS from swh.core import config @@ -19,14 +22,12 @@ # https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-DATABASES # https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/#databases -DEFAULT_PATH = 'deposit/private' - -private_conf = config.load_named_config(DEFAULT_PATH) - -if not private_conf: - raise ValueError('Cannot run in production, missing private data file.') - -SECRET_KEY = private_conf.get('secret_key', 'change me') +# No checks here, everything here should be fine (cf. swh.deposit.wsgi) +# which does the checks on the production configuration needed +config_filename = os.environ.get('SWH_CONFIG_FILENAME') +configuration = config.load_named_config(config_filename) +private_conf = configuration['private'] +SECRET_KEY = private_conf['secret_key'] # https://docs.djangoproject.com/en/1.10/ref/settings/#logging LOGGING = { diff --git a/swh/deposit/wsgi.py b/swh/deposit/wsgi.py --- a/swh/deposit/wsgi.py +++ b/swh/deposit/wsgi.py @@ -1,4 +1,4 @@ -# Copyright (C) 2017 The Software Heritage developers +# Copyright (C) 2017-2019 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -13,12 +13,33 @@ """ import os -import sys from django.core.wsgi import get_wsgi_application -sys.path.append('/etc/softwareheritage') -os.environ.setdefault("DJANGO_SETTINGS_MODULE", - "swh.deposit.settings.production") +from swh.core import config -application = get_wsgi_application() + +DEFAULT_CONFIG_PATH = '/etc/softwareheritage/deposit.yml' + + +def make_app_from_configfile(config_file=DEFAULT_CONFIG_PATH): + """Build a django wsgi application. + + """ + config_file = os.environ.get('SWH_CONFIG_FILENAME', config_file) + conf = config.load_named_config(config_file) + if not conf: + raise ValueError( + 'Cannot run in production, configuration %s does not exist.' % ( + config_file, )) + + for key in ('scheduler', 'private'): + if not conf.get(key): + raise ValueError( + "invalid configuration; missing %s config entry." % key) + + app = get_wsgi_application() + return app + + +application = make_app_from_configfile()