From 4b9ee514f8cd50add0eded2ecc2572c4d19f0663 Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Wed, 2 Feb 2022 11:52:23 +0100 Subject: [PATCH] Add `VBV` prefix to env variables --- .envs/.local/.postgres | 10 ++++----- README.md | 4 ++-- compose/production/django/entrypoint | 16 +++++++------- .../production/postgres/maintenance/backup | 18 +++++++-------- .../production/postgres/maintenance/restore | 22 +++++++++---------- config/settings/base.py | 6 ++--- config/settings/local.py | 2 +- config/settings/production.py | 16 +++++++------- config/settings/test.py | 2 +- example.env | 3 +-- setup.cfg | 2 +- 11 files changed, 50 insertions(+), 51 deletions(-) diff --git a/.envs/.local/.postgres b/.envs/.local/.postgres index a2040311..f8887ab7 100644 --- a/.envs/.local/.postgres +++ b/.envs/.local/.postgres @@ -1,7 +1,7 @@ # PostgreSQL # ------------------------------------------------------------------------------ -POSTGRES_HOST=postgres -POSTGRES_PORT=5432 -POSTGRES_DB=vbv_lernwelt -POSTGRES_USER=MRsLOrFLFqmAnAxxWMsHXfUSqWHThtGQ -POSTGRES_PASSWORD=hNqfCdG6bwCLcnfboDtNM1L2Hiwp8GuKp1DJ6t2rcKl15Vls2QbByoIZ6IQlciKM +VBV_POSTGRES_HOST=postgres +VBV_POSTGRES_PORT=5432 +VBV_POSTGRES_DB=vbv_lernwelt +VBV_POSTGRES_USER=MRsLOrFLFqmAnAxxWMsHXfUSqWHThtGQ +VBV_POSTGRES_PASSWORD=hNqfCdG6bwCLcnfboDtNM1L2Hiwp8GuKp1DJ6t2rcKl15Vls2QbByoIZ6IQlciKM diff --git a/README.md b/README.md index 82a56260..eadebd29 100644 --- a/README.md +++ b/README.md @@ -16,10 +16,10 @@ createuser vbv_lernwelt Set the environment variable accordingly ```bash -export DATABASE_URL='postgres://vbv_lernwelt@localhost:5432/vbv_lernwelt' +export VBV_DATABASE_URL='postgres://vbv_lernwelt@localhost:5432/vbv_lernwelt' ``` -Set `DJANGO_READ_DOT_ENV_FILE=True` to make the config read the `example.env` file (with direnv!?). +Set `VBV_DJANGO_READ_DOT_ENV_FILE=True` to make the config read the `example.env` file (with direnv!?). Apply migrations and run async server diff --git a/compose/production/django/entrypoint b/compose/production/django/entrypoint index 2c5bec8f..aa78749d 100644 --- a/compose/production/django/entrypoint +++ b/compose/production/django/entrypoint @@ -7,11 +7,11 @@ set -o nounset -if [ -z "${POSTGRES_USER}" ]; then +if [ -z "${VBV_POSTGRES_USER}" ]; then base_postgres_image_default_user='postgres' - export POSTGRES_USER="${base_postgres_image_default_user}" + export VBV_POSTGRES_USER="${base_postgres_image_default_user}" fi -export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" +export VBV_DATABASE_URL="postgres://${VBV_POSTGRES_USER}:${VBV_POSTGRES_PASSWORD}@${VBV_POSTGRES_HOST}:${VBV_POSTGRES_PORT}/${VBV_POSTGRES_DB}" postgres_ready() { python << END @@ -21,11 +21,11 @@ import psycopg2 try: psycopg2.connect( - dbname="${POSTGRES_DB}", - user="${POSTGRES_USER}", - password="${POSTGRES_PASSWORD}", - host="${POSTGRES_HOST}", - port="${POSTGRES_PORT}", + dbname="${VBV_POSTGRES_DB}", + user="${VBV_POSTGRES_USER}", + password="${VBV_POSTGRES_PASSWORD}", + host="${VBV_POSTGRES_HOST}", + port="${VBV_POSTGRES_PORT}", ) except psycopg2.OperationalError: sys.exit(-1) diff --git a/compose/production/postgres/maintenance/backup b/compose/production/postgres/maintenance/backup index ee0c9d63..e41feb57 100644 --- a/compose/production/postgres/maintenance/backup +++ b/compose/production/postgres/maintenance/backup @@ -17,22 +17,22 @@ source "${working_dir}/_sourced/constants.sh" source "${working_dir}/_sourced/messages.sh" -message_welcome "Backing up the '${POSTGRES_DB}' database..." +message_welcome "Backing up the '${VBV_POSTGRES_DB}' database..." -if [[ "${POSTGRES_USER}" == "postgres" ]]; then - message_error "Backing up as 'postgres' user is not supported. Assign 'POSTGRES_USER' env with another one and try again." +if [[ "${VBV_POSTGRES_USER}" == "postgres" ]]; then + message_error "Backing up as 'postgres' user is not supported. Assign 'VBV_POSTGRES_USER' env with another one and try again." exit 1 fi -export PGHOST="${POSTGRES_HOST}" -export PGPORT="${POSTGRES_PORT}" -export PGUSER="${POSTGRES_USER}" -export PGPASSWORD="${POSTGRES_PASSWORD}" -export PGDATABASE="${POSTGRES_DB}" +export PGHOST="${VBV_POSTGRES_HOST}" +export PGPORT="${VBV_POSTGRES_PORT}" +export PGUSER="${VBV_POSTGRES_USER}" +export PGPASSWORD="${VBV_POSTGRES_PASSWORD}" +export PGDATABASE="${VBV_POSTGRES_DB}" backup_filename="${BACKUP_FILE_PREFIX}_$(date +'%Y_%m_%dT%H_%M_%S').sql.gz" pg_dump | gzip > "${BACKUP_DIR_PATH}/${backup_filename}" -message_success "'${POSTGRES_DB}' database backup '${backup_filename}' has been created and placed in '${BACKUP_DIR_PATH}'." +message_success "'${VBV_POSTGRES_DB}' database backup '${backup_filename}' has been created and placed in '${BACKUP_DIR_PATH}'." diff --git a/compose/production/postgres/maintenance/restore b/compose/production/postgres/maintenance/restore index 9661ca7f..0a1c0046 100644 --- a/compose/production/postgres/maintenance/restore +++ b/compose/production/postgres/maintenance/restore @@ -30,26 +30,26 @@ if [[ ! -f "${backup_filename}" ]]; then exit 1 fi -message_welcome "Restoring the '${POSTGRES_DB}' database from the '${backup_filename}' backup..." +message_welcome "Restoring the '${VBV_POSTGRES_DB}' database from the '${backup_filename}' backup..." -if [[ "${POSTGRES_USER}" == "postgres" ]]; then - message_error "Restoring as 'postgres' user is not supported. Assign 'POSTGRES_USER' env with another one and try again." +if [[ "${VBV_POSTGRES_USER}" == "postgres" ]]; then + message_error "Restoring as 'postgres' user is not supported. Assign 'VBV_POSTGRES_USER' env with another one and try again." exit 1 fi -export PGHOST="${POSTGRES_HOST}" -export PGPORT="${POSTGRES_PORT}" -export PGUSER="${POSTGRES_USER}" -export PGPASSWORD="${POSTGRES_PASSWORD}" -export PGDATABASE="${POSTGRES_DB}" +export PGHOST="${VBV_POSTGRES_HOST}" +export PGPORT="${VBV_POSTGRES_PORT}" +export PGUSER="${VBV_POSTGRES_USER}" +export PGPASSWORD="${VBV_POSTGRES_PASSWORD}" +export PGDATABASE="${VBV_POSTGRES_DB}" message_info "Dropping the database..." dropdb "${PGDATABASE}" message_info "Creating a new database..." -createdb --owner="${POSTGRES_USER}" +createdb --owner="${VBV_POSTGRES_USER}" message_info "Applying the backup to the new database..." -gunzip -c "${backup_filename}" | psql "${POSTGRES_DB}" +gunzip -c "${backup_filename}" | psql "${VBV_POSTGRES_DB}" -message_success "The '${POSTGRES_DB}' database has been restored from the '${backup_filename}' backup." +message_success "The '${VBV_POSTGRES_DB}' database has been restored from the '${backup_filename}' backup." diff --git a/config/settings/base.py b/config/settings/base.py index f7fec8fa..61794286 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -10,7 +10,7 @@ ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent APPS_DIR = ROOT_DIR / "vbv_lernwelt" env = environ.Env() -READ_DOT_ENV_FILE = env.bool("DJANGO_READ_DOT_ENV_FILE", default=False) +READ_DOT_ENV_FILE = env.bool("VBV_DJANGO_READ_DOT_ENV_FILE", default=False) if READ_DOT_ENV_FILE: # OS environment variables take precedence over variables from .env env.read_env(str(ROOT_DIR / "example.env")) @@ -40,7 +40,7 @@ LOCALE_PATHS = [str(ROOT_DIR / "locale")] # DATABASES # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#databases -DATABASES = {"default": env.db("DATABASE_URL")} +DATABASES = {"default": env.db("VBV_DATABASE_URL")} DATABASES["default"]["ATOMIC_REQUESTS"] = True # https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-DEFAULT_AUTO_FIELD DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" @@ -258,7 +258,7 @@ LOGGING = { # django-allauth # ------------------------------------------------------------------------------ -ACCOUNT_ALLOW_REGISTRATION = env.bool("DJANGO_ACCOUNT_ALLOW_REGISTRATION", True) +ACCOUNT_ALLOW_REGISTRATION = env.bool("VBV_DJANGO_ACCOUNT_ALLOW_REGISTRATION", True) # https://django-allauth.readthedocs.io/en/latest/configuration.html ACCOUNT_AUTHENTICATION_METHOD = "username" # https://django-allauth.readthedocs.io/en/latest/configuration.html diff --git a/config/settings/local.py b/config/settings/local.py index 1700cfec..03a0d209 100644 --- a/config/settings/local.py +++ b/config/settings/local.py @@ -7,7 +7,7 @@ from .base import env DEBUG = True # https://docs.djangoproject.com/en/dev/ref/settings/#secret-key SECRET_KEY = env( - "DJANGO_SECRET_KEY", + "VBV_DJANGO_SECRET_KEY", default="J9FiYN31FuY7lHrmx9Mpai3GGpTVCxakEclOfCLretDe7bTf2DtTsgazJ0aIMtbq", ) # https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts diff --git a/config/settings/production.py b/config/settings/production.py index fc2d1b42..896269ec 100644 --- a/config/settings/production.py +++ b/config/settings/production.py @@ -11,13 +11,13 @@ from .base import env # GENERAL # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#secret-key -SECRET_KEY = env("DJANGO_SECRET_KEY") +SECRET_KEY = env("VBV_DJANGO_SECRET_KEY") # https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts -ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default=["vbv-lernwelt.iterativ.ch"]) +ALLOWED_HOSTS = env.list("VBV_DJANGO_ALLOWED_HOSTS", default=["vbv-lernwelt.iterativ.ch"]) # DATABASES # ------------------------------------------------------------------------------ -DATABASES["default"] = env.db("DATABASE_URL") # noqa F405 +DATABASES["default"] = env.db("VBV_DATABASE_URL") # noqa F405 DATABASES["default"]["ATOMIC_REQUESTS"] = True # noqa F405 DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60) # noqa F405 @@ -26,7 +26,7 @@ DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60) # no CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", - "LOCATION": env("REDIS_URL"), + "LOCATION": env("VBV_REDIS_URL"), "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", # Mimicing memcache behavior. @@ -41,7 +41,7 @@ CACHES = { # https://docs.djangoproject.com/en/dev/ref/settings/#secure-proxy-ssl-header SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") # https://docs.djangoproject.com/en/dev/ref/settings/#secure-ssl-redirect -SECURE_SSL_REDIRECT = env.bool("DJANGO_SECURE_SSL_REDIRECT", default=True) +SECURE_SSL_REDIRECT = env.bool("VBV_DJANGO_SECURE_SSL_REDIRECT", default=True) # https://docs.djangoproject.com/en/dev/ref/settings/#session-cookie-secure SESSION_COOKIE_SECURE = True # https://docs.djangoproject.com/en/dev/ref/settings/#csrf-cookie-secure @@ -75,7 +75,7 @@ DEFAULT_FROM_EMAIL = env( default="VBV Lernwelt ", ) # https://docs.djangoproject.com/en/dev/ref/settings/#server-email -SERVER_EMAIL = env("DJANGO_SERVER_EMAIL", default=DEFAULT_FROM_EMAIL) +SERVER_EMAIL = env("VBV_DJANGO_SERVER_EMAIL", default=DEFAULT_FROM_EMAIL) # https://docs.djangoproject.com/en/dev/ref/settings/#email-subject-prefix EMAIL_SUBJECT_PREFIX = env( "DJANGO_EMAIL_SUBJECT_PREFIX", @@ -85,7 +85,7 @@ EMAIL_SUBJECT_PREFIX = env( # ADMIN # ------------------------------------------------------------------------------ # Django Admin URL regex. -ADMIN_URL = env("DJANGO_ADMIN_URL") +ADMIN_URL = env("VBV_DJANGO_ADMIN_URL") # Anymail # ------------------------------------------------------------------------------ @@ -139,7 +139,7 @@ LOGGING = { # Sentry # ------------------------------------------------------------------------------ -SENTRY_DSN = env("SENTRY_DSN") +SENTRY_DSN = env("VBV_SENTRY_DSN") SENTRY_LOG_LEVEL = env.int("DJANGO_SENTRY_LOG_LEVEL", logging.INFO) sentry_logging = LoggingIntegration( diff --git a/config/settings/test.py b/config/settings/test.py index 0b7926bc..b26e1a05 100644 --- a/config/settings/test.py +++ b/config/settings/test.py @@ -9,7 +9,7 @@ from .base import env # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#secret-key SECRET_KEY = env( - "DJANGO_SECRET_KEY", + "VBV_DJANGO_SECRET_KEY", default="1NpUCSvAKLpDZL9e3tqDaUe8Kk2xAuF1tXosFjBanc4lFCgNcfBp02MD3UjB72ZS", ) # https://docs.djangoproject.com/en/dev/ref/settings/#test-runner diff --git a/example.env b/example.env index 159363a6..4260645e 100644 --- a/example.env +++ b/example.env @@ -1,3 +1,2 @@ -export DATABASE_URL='postgres://vbv_lernwelt@localhost:5432/vbv_lernwelt' -export CELERY_BROKER_URL='redis://localhost:6379/0' +export VBV_DATABASE_URL='postgres://vbv_lernwelt@localhost:5432/vbv_lernwelt' export USE_DOCKER=false diff --git a/setup.cfg b/setup.cfg index 8b4fbe86..89bb03c8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,7 +27,7 @@ warn_unused_configs = True plugins = mypy_django_plugin.main, mypy_drf_plugin.main [mypy.plugins.django-stubs] -django_settings_module = config.settings.test +DJANGO_SETTINGS_MODULE = config.settings.test [mypy-*.migrations.*] # Django migrations should not produce any errors: