diff --git a/swh/deposit/config.py b/swh/deposit/config.py index 2e6b35e1..1800d67c 100644 --- a/swh/deposit/config.py +++ b/swh/deposit/config.py @@ -1,101 +1,101 @@ # Copyright (C) 2017-2018 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 swh.core.config import SWHConfig from swh.scheduler import get_scheduler # IRIs (Internationalized Resource identifier) sword 2.0 specified EDIT_SE_IRI = 'edit_se_iri' EM_IRI = 'em_iri' CONT_FILE_IRI = 'cont_file_iri' SD_IRI = 'servicedocument' COL_IRI = 'upload' STATE_IRI = 'state_iri' PRIVATE_GET_RAW_CONTENT = 'private-download' PRIVATE_CHECK_DEPOSIT = 'check-deposit' PRIVATE_PUT_DEPOSIT = 'private-update' PRIVATE_GET_DEPOSIT_METADATA = 'private-read' PRIVATE_LIST_DEPOSITS = 'private-deposit-list' ARCHIVE_KEY = 'archive' METADATA_KEY = 'metadata' RAW_METADATA_KEY = 'raw-metadata' ARCHIVE_TYPE = 'archive' METADATA_TYPE = 'metadata' AUTHORIZED_PLATFORMS = ['development', 'production', 'testing'] DEPOSIT_STATUS_REJECTED = 'rejected' DEPOSIT_STATUS_PARTIAL = 'partial' DEPOSIT_STATUS_DEPOSITED = 'deposited' DEPOSIT_STATUS_VERIFIED = 'verified' DEPOSIT_STATUS_LOAD_SUCCESS = 'done' DEPOSIT_STATUS_LOAD_FAILURE = 'failed' # Revision author for deposit SWH_PERSON = { 'name': 'Software Heritage', 'fullname': 'Software Heritage', 'email': 'robot@softwareheritage.org' } 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 call. Otherwise, this will raise an django.core.exceptions.ImproperlyConfigured error message. Args: platform (str): the platform the scheduling is running Raises: ValueError in case of wrong platform inputs. """ if platform not in AUTHORIZED_PLATFORMS: raise ValueError('Platform should be one of %s' % AUTHORIZED_PLATFORMS) os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'swh.deposit.settings.%s' % platform) import django django.setup() class SWHDefaultConfig(SWHConfig): """Mixin intended to enrich views with SWH configuration. """ CONFIG_BASE_FILENAME = 'deposit/server' DEFAULT_CONFIG = { 'max_upload_size': ('int', 209715200), 'checks': ('bool', True), 'scheduler': ('dict', { 'cls': 'remote', 'args': { 'url': 'http://localhost:5008/' } }) } ADDITIONAL_CONFIG = {} def __init__(self, **config): super().__init__() self.config = self.parse_config_file( additional_configs=[self.ADDITIONAL_CONFIG]) self.config.update(config) self.log = logging.getLogger('swh.deposit') if self.config['checks']: self.scheduler = get_scheduler(**self.config['scheduler']) diff --git a/swh/deposit/settings/production.py b/swh/deposit/settings/production.py index e8a99bee..aebaaf9c 100644 --- a/swh/deposit/settings/production.py +++ b/swh/deposit/settings/production.py @@ -1,91 +1,108 @@ -# 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 ALLOWED_HOSTS += ['deposit.softwareheritage.org'] # Setup support for proxy headers USE_X_FORWARDED_HOST = True SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') DEBUG = False # Database # https://docs.djangoproject.com/en/1.10/ref/settings/#databases # 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') +# Retrieve the deposit's configuration file +# and check the required setup is ok +# If not raise an error explaining the errors +config_file = os.environ.get('SWH_CONFIG_FILENAME') +if not os.path.exists(config_file): + raise ValueError('Production: configuration file %s does not exist!' % ( + config_file, )) + +conf = config.load_named_config(config_file) +if not conf: + raise ValueError( + 'Production: configuration %s does not exist.' % ( + config_file, )) + +for key in ('scheduler', 'private'): + if not conf.get(key): + raise ValueError( + "Production: invalid configuration; missing %s config entry." % ( + key, )) + +private_conf = conf['private'] +SECRET_KEY = private_conf['secret_key'] # https://docs.djangoproject.com/en/1.10/ref/settings/#logging LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s", # noqa 'datefmt': "%d/%b/%Y %H:%M:%S" }, }, 'handlers': { 'console': { 'level': 'INFO', 'class': 'logging.StreamHandler', 'formatter': 'standard' }, }, 'loggers': { 'django': { 'handlers': ['console'], 'level': 'INFO', 'propagate': True, }, }, } # database db_conf = private_conf.get('db', {'name': 'unset'}) db = { 'ENGINE': 'django.db.backends.postgresql', 'NAME': db_conf['name'], } db_user = db_conf.get('user') if db_user: db['USER'] = db_user db_pass = db_conf.get('password') if db_pass: db['PASSWORD'] = db_pass db_host = db_conf.get('host') if db_host: db['HOST'] = db_host db_port = db_conf.get('port') if db_port: db['PORT'] = db_port # https://docs.djangoproject.com/en/1.10/ref/settings/#databases DATABASES = { 'default': db, } # Upload user directory # https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-MEDIA_ROOT MEDIA_ROOT = private_conf.get('media_root') diff --git a/swh/deposit/wsgi.py b/swh/deposit/wsgi.py index a9dbb418..d39bdfca 100644 --- a/swh/deposit/wsgi.py +++ b/swh/deposit/wsgi.py @@ -1,24 +1,17 @@ -# 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 """ WSGI config for swh.deposit project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/ """ -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") - application = get_wsgi_application()