# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: Copyright contributors to the OpenScanHub project.

"""
Instance-specific settings.
"""

import os

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    ('Siteshwar Vashisht', 'svashisht@redhat.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'openscanhub',
        'USER': 'openscanhub',
{% if env == 'staging' %}
        'PASSWORD': '{{ openscanhub_db_password_stg }}',
        'HOST': 'db01.stg.iad2.fedoraproject.org',
{% else %}
        'PASSWORD': '{{ openscanhub_db_password }}',
        'HOST': 'db01.iad2.fedoraproject.org',
{% endif %}
        'PORT': '5432',
    },
}

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)-7s %(asctime)s %(pathname)-50s:%(lineno)d %(funcName)s   %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'formatter': 'verbose',
            'filename': '/var/log/osh/hub/hub.log',
            'maxBytes': 10 * (1024 ** 2),  # 10 MB
            'backupCount': 14,
        },
    },
    'loggers': {
        'osh': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
        # 'django.db.backends': {
        #     'handlers': ['file'],
        #     'propagate': False,
        #     'level': 'INFO',
        # },
        'kobo': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        }
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Europe/Prague'

# TODO: Change it for Fedora staging and production deployments.
KRB_AUTH_PRINCIPAL = 'HTTP/covscan.lab.eng.brq2.redhat.com@REDHAT.COM'
KRB_AUTH_KEYTAB = '/etc/httpd/conf/httpd.keytab'

# This should work with both staging and production.
# https://docs.fedoraproject.org/en-US/infra/sysadmin_guide/bastion-hosts-info/
EMAIL_HOST = "bastion.fedoraproject.org"
EMAIL_PORT = 25

###############################################################################
# OpenScanHub SPECIFIC
###############################################################################

# Absolute path to task logs and other files
FILES_PATH = '/var/lib/osh/hub'

# Files for kobo tasks with predefined structure
TASK_DIR = os.path.join(FILES_PATH, 'tasks')

# Root directory for uploaded files
UPLOAD_DIR = os.path.join(FILES_PATH, 'upload')

BZ_URL = "https://bugzilla.stage.redhat.com"
JIRA_URL = "https://issues.stage.redhat.com"

# TODO: Change or remove this URL?
ET_URL = 'https://errata-web-01.host.stage.eng.bos.redhat.com'

# TODO: Change or remove these URLs?
UMB_BROKER_URLS = [
    'amqps://umb-broker01.stage.api.redhat.com:5671',
    'amqps://umb-broker02.stage.api.redhat.com:5671',
    'amqps://umb-broker03.stage.api.redhat.com:5671',
    'amqps://umb-broker04.stage.api.redhat.com:5671',
    'amqps://umb-broker05.stage.api.redhat.com:5671',
    'amqps://umb-broker06.stage.api.redhat.com:5671']

# TODO: Change or remove this setting?
# https://github.com/openscanhub/fedora-infra/issues/3
UMB_CLIENT_CERT = '/etc/osh/hub/msg-client-osh.pem'
UMB_TOPIC_PREFIX = 'topic://VirtualTopic.eng.OpenScanHub.scan'

# E-mail address used in the From: field of notifications generated by OpenScanHub
NOTIFICATION_EMAIL_ADDRESS = "openscanhub@lists.fedoraproject.org"

# E-mail address where notifications about failed/cancelled tasks are sent
DEVEL_EMAIL_ADDRESS = "no-reply@example.org"

# Default e-mail domain name for sending notifications to the user.
# It is used when `email` field in the user model is not set.
DEFAULT_EMAIL_DOMAIN = "fedoraproject.org"

URL_PREFIX = ""
STATIC_URL = URL_PREFIX + '/static/'

# If this setting is enabled, a worker is only used to perform a single task.
ENABLE_SINGLE_USE_WORKERS = True

{% if env == 'staging' %}
# This number should be same as the `max` field in the `/etc/resallocserver/pools.yaml`
# on the resalloc server. Otherwise, we may end up with too less or too many tickets
# being opened.
MAX_SINGLE_USE_WORKERS = 128

# TODO: This url is used to dynamically generate worker configuration files.
SINGLE_USE_WORKER_OSH_HUB_URL = "https://openscanhub.stg.fedoraproject.org/xmlrpc"
{% else %}
# This number should be same as the `max` field in the `/etc/resallocserver/pools.yaml`
# on the resalloc server. Otherwise, we may end up with too less or too many tickets
# being opened.
MAX_SINGLE_USE_WORKERS = 32

# TODO: This url is used to dynamically generate worker configuration files.
SINGLE_USE_WORKER_OSH_HUB_URL = "https://openscanhub.fedoraproject.org/xmlrpc"
{% endif %}

# This user is used to ssh to newly created worker.
SINGLE_USE_WORKER_SSH_USER = "ec2-user"

# TODO: What should we use here if we want to deploy across multiple clouds?
# May be, related public key should be copied when a new worker is set up.
SINGLE_USE_WORKER_SSH_PRIVATE_KEY = "/etc/osh/worker-manager/id_rsa"

NOTIFICATION_EMAIL_FOOTER = """You can reply to this e-mail to initiate a discussion on \
                            https://lists.fedoraproject.org/archives/list/openscanhub@lists.fedoraproject.org/"""

ALLOWED_HOSTS = ['openscanhub{{ env_suffix }}.fedoraproject.org']
