From 7cda95b9b57427498f4ed34aca4358cc7ef72673 Mon Sep 17 00:00:00 2001 From: Stephan Porada Date: Tue, 28 Jul 2020 16:31:14 +0200 Subject: [PATCH] Fix DB issue --- .env.tpl | 9 +++++++++ .gitignore | 3 +-- app/bundesdata_app/settings.py | 28 +++++----------------------- app/secrets_example.json | 7 ------- docker-compose.yml | 15 +++++++++------ 5 files changed, 24 insertions(+), 38 deletions(-) create mode 100644 .env.tpl delete mode 100755 app/secrets_example.json diff --git a/.env.tpl b/.env.tpl new file mode 100644 index 0000000..1e5a1cc --- /dev/null +++ b/.env.tpl @@ -0,0 +1,9 @@ +### These are examples do not use in production! ### + +### Django settings ### +SECRET_KEY=secretkey + +### Postgress DB ### +POSTGRES_PASSWORD=password +POSTGRES_USER=bundesdata_app_user +POSTGRES_DB_NAME=bundesdata_db \ No newline at end of file diff --git a/.gitignore b/.gitignore index e8dc8a3..af8c003 100755 --- a/.gitignore +++ b/.gitignore @@ -15,5 +15,4 @@ static_volume !**/.gitkeep -**/secrets.json -secrets.json +.env \ No newline at end of file diff --git a/app/bundesdata_app/settings.py b/app/bundesdata_app/settings.py index 712c218..7e41aa3 100755 --- a/app/bundesdata_app/settings.py +++ b/app/bundesdata_app/settings.py @@ -10,30 +10,11 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.1/ref/settings/ """ -import json import os -from django.core.exceptions import ImproperlyConfigured - # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - -# Loads json files with secrets. Follow this example to create your own secrets -# file with SECRET_KEY and other information. -# Example https://stackoverflow.com/a/42077576 -with open(os.path.join(BASE_DIR, 'secrets.json')) as secrets_file: - secrets = json.load(secrets_file) - - -def get_secret(setting, secrets=secrets): - """Get secret setting or fail with ImproperlyConfigured""" - try: - return secrets[setting] - except KeyError: - raise ImproperlyConfigured("Set the {} setting".format(setting)) - - # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ @@ -41,7 +22,7 @@ def get_secret(setting, secrets=secrets): # This is just some random genearted key to test the App. If you want to set up # your own running public version of this app replace this key with an new one # that you will keep secret! -SECRET_KEY = get_secret("SECRET_KEY") +SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -106,12 +87,13 @@ WSGI_APPLICATION = 'bundesdata_app.wsgi.application' # Change NAME, USER and PASSWORD details before deploying your own public # version of this app. Or use your onw secrets.json file. Explanation found can # be foudn here: https://stackoverflow.com/a/42077576 + DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', - 'NAME': get_secret("DB_NAME"), - 'USER': get_secret("DB_USER"), - 'PASSWORD': get_secret("DB_PASSWORD"), + 'NAME': os.environ.get("POSTGRES_DB_NAME"), + 'USER': os.environ.get("POSTGRES_USER"), + 'PASSWORD': os.environ.get("POSTGRES_PASSWORD"), 'HOST': 'db', 'PORT': '5432', } diff --git a/app/secrets_example.json b/app/secrets_example.json deleted file mode 100755 index d6594aa..0000000 --- a/app/secrets_example.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "_comment": "DO not use these values in production! This is an example for development!", - "SECRET_KEY": "=7n(1!he%todz-)jo))$upf0(vor9v9ke5rn&fli%6l562!_=0", - "DB_PASSWORD": "v3ry53cr3t", - "DB_USER": "bundesdata_app_user", - "DB_NAME": "bundesdata_db" -} diff --git a/docker-compose.yml b/docker-compose.yml index e98c817..d4c8557 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,10 +9,18 @@ services: - ./static_volume:/usr/src/app/staticfiles expose: - 8000 + env_file: .env depends_on: - db command: bash -c /wait-for-it/wait-for-it.sh db:5432 --strict --timeout=0 db: + env_file: .env + environment: + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_DB_NAME: ${POSTGRES_DB_NAME} + expose: + - 5432 image: postgres:11.2 volumes: - ./postgres_data:/var/lib/postgresql/data/ @@ -23,9 +31,4 @@ services: ports: - 8000:80 depends_on: - - web - -volumes: - postgres_data: - static_volume: - input_volume: + - web \ No newline at end of file