Add `VBV` prefix to env variables

This commit is contained in:
Daniel Egger 2022-02-02 11:52:23 +01:00
parent 4a22e38a05
commit 4b9ee514f8
11 changed files with 50 additions and 51 deletions

View File

@ -1,7 +1,7 @@
# PostgreSQL # PostgreSQL
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
POSTGRES_HOST=postgres VBV_POSTGRES_HOST=postgres
POSTGRES_PORT=5432 VBV_POSTGRES_PORT=5432
POSTGRES_DB=vbv_lernwelt VBV_POSTGRES_DB=vbv_lernwelt
POSTGRES_USER=MRsLOrFLFqmAnAxxWMsHXfUSqWHThtGQ VBV_POSTGRES_USER=MRsLOrFLFqmAnAxxWMsHXfUSqWHThtGQ
POSTGRES_PASSWORD=hNqfCdG6bwCLcnfboDtNM1L2Hiwp8GuKp1DJ6t2rcKl15Vls2QbByoIZ6IQlciKM VBV_POSTGRES_PASSWORD=hNqfCdG6bwCLcnfboDtNM1L2Hiwp8GuKp1DJ6t2rcKl15Vls2QbByoIZ6IQlciKM

View File

@ -16,10 +16,10 @@ createuser vbv_lernwelt
Set the environment variable accordingly Set the environment variable accordingly
```bash ```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 Apply migrations and run async server

View File

@ -7,11 +7,11 @@ set -o nounset
if [ -z "${POSTGRES_USER}" ]; then if [ -z "${VBV_POSTGRES_USER}" ]; then
base_postgres_image_default_user='postgres' base_postgres_image_default_user='postgres'
export POSTGRES_USER="${base_postgres_image_default_user}" export VBV_POSTGRES_USER="${base_postgres_image_default_user}"
fi 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() { postgres_ready() {
python << END python << END
@ -21,11 +21,11 @@ import psycopg2
try: try:
psycopg2.connect( psycopg2.connect(
dbname="${POSTGRES_DB}", dbname="${VBV_POSTGRES_DB}",
user="${POSTGRES_USER}", user="${VBV_POSTGRES_USER}",
password="${POSTGRES_PASSWORD}", password="${VBV_POSTGRES_PASSWORD}",
host="${POSTGRES_HOST}", host="${VBV_POSTGRES_HOST}",
port="${POSTGRES_PORT}", port="${VBV_POSTGRES_PORT}",
) )
except psycopg2.OperationalError: except psycopg2.OperationalError:
sys.exit(-1) sys.exit(-1)

View File

@ -17,22 +17,22 @@ source "${working_dir}/_sourced/constants.sh"
source "${working_dir}/_sourced/messages.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 if [[ "${VBV_POSTGRES_USER}" == "postgres" ]]; then
message_error "Backing up as 'postgres' user is not supported. Assign 'POSTGRES_USER' env with another one and try again." message_error "Backing up as 'postgres' user is not supported. Assign 'VBV_POSTGRES_USER' env with another one and try again."
exit 1 exit 1
fi fi
export PGHOST="${POSTGRES_HOST}" export PGHOST="${VBV_POSTGRES_HOST}"
export PGPORT="${POSTGRES_PORT}" export PGPORT="${VBV_POSTGRES_PORT}"
export PGUSER="${POSTGRES_USER}" export PGUSER="${VBV_POSTGRES_USER}"
export PGPASSWORD="${POSTGRES_PASSWORD}" export PGPASSWORD="${VBV_POSTGRES_PASSWORD}"
export PGDATABASE="${POSTGRES_DB}" export PGDATABASE="${VBV_POSTGRES_DB}"
backup_filename="${BACKUP_FILE_PREFIX}_$(date +'%Y_%m_%dT%H_%M_%S').sql.gz" backup_filename="${BACKUP_FILE_PREFIX}_$(date +'%Y_%m_%dT%H_%M_%S').sql.gz"
pg_dump | gzip > "${BACKUP_DIR_PATH}/${backup_filename}" 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}'."

View File

@ -30,26 +30,26 @@ if [[ ! -f "${backup_filename}" ]]; then
exit 1 exit 1
fi 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 if [[ "${VBV_POSTGRES_USER}" == "postgres" ]]; then
message_error "Restoring as 'postgres' user is not supported. Assign 'POSTGRES_USER' env with another one and try again." message_error "Restoring as 'postgres' user is not supported. Assign 'VBV_POSTGRES_USER' env with another one and try again."
exit 1 exit 1
fi fi
export PGHOST="${POSTGRES_HOST}" export PGHOST="${VBV_POSTGRES_HOST}"
export PGPORT="${POSTGRES_PORT}" export PGPORT="${VBV_POSTGRES_PORT}"
export PGUSER="${POSTGRES_USER}" export PGUSER="${VBV_POSTGRES_USER}"
export PGPASSWORD="${POSTGRES_PASSWORD}" export PGPASSWORD="${VBV_POSTGRES_PASSWORD}"
export PGDATABASE="${POSTGRES_DB}" export PGDATABASE="${VBV_POSTGRES_DB}"
message_info "Dropping the database..." message_info "Dropping the database..."
dropdb "${PGDATABASE}" dropdb "${PGDATABASE}"
message_info "Creating a new database..." message_info "Creating a new database..."
createdb --owner="${POSTGRES_USER}" createdb --owner="${VBV_POSTGRES_USER}"
message_info "Applying the backup to the new database..." 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."

View File

@ -10,7 +10,7 @@ ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
APPS_DIR = ROOT_DIR / "vbv_lernwelt" APPS_DIR = ROOT_DIR / "vbv_lernwelt"
env = environ.Env() 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: if READ_DOT_ENV_FILE:
# OS environment variables take precedence over variables from .env # OS environment variables take precedence over variables from .env
env.read_env(str(ROOT_DIR / "example.env")) env.read_env(str(ROOT_DIR / "example.env"))
@ -40,7 +40,7 @@ LOCALE_PATHS = [str(ROOT_DIR / "locale")]
# DATABASES # DATABASES
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#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 DATABASES["default"]["ATOMIC_REQUESTS"] = True
# https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-DEFAULT_AUTO_FIELD # https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-DEFAULT_AUTO_FIELD
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
@ -258,7 +258,7 @@ LOGGING = {
# django-allauth # 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 # https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_AUTHENTICATION_METHOD = "username" ACCOUNT_AUTHENTICATION_METHOD = "username"
# https://django-allauth.readthedocs.io/en/latest/configuration.html # https://django-allauth.readthedocs.io/en/latest/configuration.html

View File

@ -7,7 +7,7 @@ from .base import env
DEBUG = True DEBUG = True
# https://docs.djangoproject.com/en/dev/ref/settings/#secret-key # https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
SECRET_KEY = env( SECRET_KEY = env(
"DJANGO_SECRET_KEY", "VBV_DJANGO_SECRET_KEY",
default="J9FiYN31FuY7lHrmx9Mpai3GGpTVCxakEclOfCLretDe7bTf2DtTsgazJ0aIMtbq", default="J9FiYN31FuY7lHrmx9Mpai3GGpTVCxakEclOfCLretDe7bTf2DtTsgazJ0aIMtbq",
) )
# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts # https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts

View File

@ -11,13 +11,13 @@ from .base import env
# GENERAL # GENERAL
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#secret-key # 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 # 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
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
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"]["ATOMIC_REQUESTS"] = True # noqa F405
DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60) # 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 = { CACHES = {
"default": { "default": {
"BACKEND": "django_redis.cache.RedisCache", "BACKEND": "django_redis.cache.RedisCache",
"LOCATION": env("REDIS_URL"), "LOCATION": env("VBV_REDIS_URL"),
"OPTIONS": { "OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient", "CLIENT_CLASS": "django_redis.client.DefaultClient",
# Mimicing memcache behavior. # Mimicing memcache behavior.
@ -41,7 +41,7 @@ CACHES = {
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-proxy-ssl-header # https://docs.djangoproject.com/en/dev/ref/settings/#secure-proxy-ssl-header
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-ssl-redirect # 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 # https://docs.djangoproject.com/en/dev/ref/settings/#session-cookie-secure
SESSION_COOKIE_SECURE = True SESSION_COOKIE_SECURE = True
# https://docs.djangoproject.com/en/dev/ref/settings/#csrf-cookie-secure # https://docs.djangoproject.com/en/dev/ref/settings/#csrf-cookie-secure
@ -75,7 +75,7 @@ DEFAULT_FROM_EMAIL = env(
default="VBV Lernwelt <noreply@vbv-lernwelt.iterativ.ch>", default="VBV Lernwelt <noreply@vbv-lernwelt.iterativ.ch>",
) )
# https://docs.djangoproject.com/en/dev/ref/settings/#server-email # 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 # https://docs.djangoproject.com/en/dev/ref/settings/#email-subject-prefix
EMAIL_SUBJECT_PREFIX = env( EMAIL_SUBJECT_PREFIX = env(
"DJANGO_EMAIL_SUBJECT_PREFIX", "DJANGO_EMAIL_SUBJECT_PREFIX",
@ -85,7 +85,7 @@ EMAIL_SUBJECT_PREFIX = env(
# ADMIN # ADMIN
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Django Admin URL regex. # Django Admin URL regex.
ADMIN_URL = env("DJANGO_ADMIN_URL") ADMIN_URL = env("VBV_DJANGO_ADMIN_URL")
# Anymail # Anymail
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -139,7 +139,7 @@ LOGGING = {
# Sentry # Sentry
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
SENTRY_DSN = env("SENTRY_DSN") SENTRY_DSN = env("VBV_SENTRY_DSN")
SENTRY_LOG_LEVEL = env.int("DJANGO_SENTRY_LOG_LEVEL", logging.INFO) SENTRY_LOG_LEVEL = env.int("DJANGO_SENTRY_LOG_LEVEL", logging.INFO)
sentry_logging = LoggingIntegration( sentry_logging = LoggingIntegration(

View File

@ -9,7 +9,7 @@ from .base import env
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#secret-key # https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
SECRET_KEY = env( SECRET_KEY = env(
"DJANGO_SECRET_KEY", "VBV_DJANGO_SECRET_KEY",
default="1NpUCSvAKLpDZL9e3tqDaUe8Kk2xAuF1tXosFjBanc4lFCgNcfBp02MD3UjB72ZS", default="1NpUCSvAKLpDZL9e3tqDaUe8Kk2xAuF1tXosFjBanc4lFCgNcfBp02MD3UjB72ZS",
) )
# https://docs.djangoproject.com/en/dev/ref/settings/#test-runner # https://docs.djangoproject.com/en/dev/ref/settings/#test-runner

View File

@ -1,3 +1,2 @@
export DATABASE_URL='postgres://vbv_lernwelt@localhost:5432/vbv_lernwelt' export VBV_DATABASE_URL='postgres://vbv_lernwelt@localhost:5432/vbv_lernwelt'
export CELERY_BROKER_URL='redis://localhost:6379/0'
export USE_DOCKER=false export USE_DOCKER=false

View File

@ -27,7 +27,7 @@ warn_unused_configs = True
plugins = mypy_django_plugin.main, mypy_drf_plugin.main plugins = mypy_django_plugin.main, mypy_drf_plugin.main
[mypy.plugins.django-stubs] [mypy.plugins.django-stubs]
django_settings_module = config.settings.test DJANGO_SETTINGS_MODULE = config.settings.test
[mypy-*.migrations.*] [mypy-*.migrations.*]
# Django migrations should not produce any errors: # Django migrations should not produce any errors: