From 8ddbd7f62e8a00f06e73e8ebb166221dec5d263e Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Mon, 3 Jun 2024 14:52:24 +0100 Subject: Revised Dockerfile to use slim and prod config for postgres --- conf/settings/prod.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 conf/settings/prod.py (limited to 'conf/settings/prod.py') diff --git a/conf/settings/prod.py b/conf/settings/prod.py new file mode 100644 index 0000000..2633982 --- /dev/null +++ b/conf/settings/prod.py @@ -0,0 +1,25 @@ +from .base import * + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +# Read database credentials from mounted Secret volume +secret_volume_path = '/etc/secret-volume' +db_host = open(os.path.join(secret_volume_path, 'host')).read().strip() +db_port = open(os.path.join(secret_volume_path, 'port')).read().strip() +db_name = open(os.path.join(secret_volume_path, 'dbname')).read().strip() +db_user = open(os.path.join(secret_volume_path, 'user')).read().strip() +db_password = open(os.path.join(secret_volume_path, 'password')).read().strip() + +# Configure Django database settings +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': db_name, + 'USER': db_user, + 'PASSWORD': db_password, + 'HOST': db_host, + 'PORT': db_port, + } +} + -- cgit v1.2.3 From 19fb1fe5756cd11eb00704162405d6a8399eb9af Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Mon, 3 Jun 2024 15:03:50 +0100 Subject: Defensive settings to handle local build --- conf/settings/prod.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'conf/settings/prod.py') diff --git a/conf/settings/prod.py b/conf/settings/prod.py index 2633982..f44152a 100644 --- a/conf/settings/prod.py +++ b/conf/settings/prod.py @@ -1,15 +1,25 @@ from .base import * +import os # Database # https://docs.djangoproject.com/en/4.0/ref/settings/#databases -# Read database credentials from mounted Secret volume -secret_volume_path = '/etc/secret-volume' -db_host = open(os.path.join(secret_volume_path, 'host')).read().strip() -db_port = open(os.path.join(secret_volume_path, 'port')).read().strip() -db_name = open(os.path.join(secret_volume_path, 'dbname')).read().strip() -db_user = open(os.path.join(secret_volume_path, 'user')).read().strip() -db_password = open(os.path.join(secret_volume_path, 'password')).read().strip() +# Check if running in OpenShift cluster +if os.path.exists('/etc/secret-volume'): + # Read database credentials from mounted Secret volume + secret_volume_path = '/etc/secret-volume' + db_host = open(os.path.join(secret_volume_path, 'host')).read().strip() + db_port = open(os.path.join(secret_volume_path, 'port')).read().strip() + db_name = open(os.path.join(secret_volume_path, 'database')).read().strip() + db_user = open(os.path.join(secret_volume_path, 'user')).read().strip() + db_password = open(os.path.join(secret_volume_path, 'password')).read().strip() +else: + # Use environment variables for local development + db_host = os.environ.get('DB_HOST', 'localhost') + db_port = os.environ.get('DB_PORT', '5432') + db_name = os.environ.get('DB_NAME', 'your_local_db_name') + db_user = os.environ.get('DB_USER', 'your_local_db_user') + db_password = os.environ.get('DB_PASSWORD', 'your_local_db_password') # Configure Django database settings DATABASES = { -- cgit v1.2.3