From 03f027f1001761a54372a7cf9f52e702d32ff675 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Mon, 8 Apr 2024 16:29:46 -0300 Subject: [PATCH] Additional variables to docker compose --- docker-compose.yml | 13 +++++++--- entrypoint.sh | 65 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 60 insertions(+), 18 deletions(-) mode change 100644 => 100755 entrypoint.sh diff --git a/docker-compose.yml b/docker-compose.yml index 6b443f5..31c818e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,8 +6,8 @@ services: volumes: - postgres_data:/var/lib/postgresql/data environment: - - POSTGRES_DB=routerfleet - - POSTGRES_USER=routerfleet + - POSTGRES_DB=${POSTGRES_DB} + - POSTGRES_USER=${POSTGRES_USER} - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} routerfleet: @@ -18,10 +18,17 @@ services: environment: - SERVER_ADDRESS=${SERVER_ADDRESS} - DEBUG_MODE=${DEBUG_MODE} + - DATABASE_ENGINE=${DATABASE_ENGINE} + - POSTGRES_HOST=${POSTGRES_HOST} + - POSTGRES_PORT=${POSTGRES_PORT} + - POSTGRES_DB=${POSTGRES_DB} + - POSTGRES_USER=${POSTGRES_USER} - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} - TIMEZONE=${TIMEZONE} + - COMPOSE_TYPE=with-postgres volumes: + - sqlite_volume:/var/lib/routerfleet_sqlite/ - media_root:/var/lib/routerfleet/ - static_volume:/app_static_files/ command: /bin/bash /app/init.sh @@ -54,7 +61,6 @@ services: context: ./containers/nginx dockerfile: Dockerfile-nginx volumes: - - ./containers/nginx/virtualhost.conf:/etc/nginx/conf.d/routerfleet.conf - static_volume:/static - https_cert:/certificate ports: @@ -68,3 +74,4 @@ volumes: https_cert: media_root: postgres_data: + sqlite_volume: diff --git a/entrypoint.sh b/entrypoint.sh old mode 100644 new mode 100755 index c288e35..4d9fced --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,4 +1,5 @@ #!/bin/bash +PRODUCTION_SETTINGS_FILE="/app/routerfleet/production_settings.py" set -e @@ -7,8 +8,51 @@ if [ -z "$SERVER_ADDRESS" ]; then exit 1 fi -if [ -z "$POSTGRES_PASSWORD" ]; then - echo "POSTGRES_PASSWORD environment variable is not set. Exiting." +if [[ "${DATABASE_ENGINE,,}" == "sqlite" ]]; then + if [[ "$COMPOSE_TYPE" != "no-postgres" ]]; then + echo "ERROR: Please use 'docker-compose-no-postgres.yml' when using sqlite as DATABASE_ENGINE. Exiting." + exit 1 + fi + DATABASES_CONFIG="DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': '/var/lib/routerfleet_sqlite/routerfleet-db.sqlite3', + } + }" +elif [[ "${DATABASE_ENGINE,,}" == "postgres" ]]; then + if [ -n "$POSTGRES_HOST" ]; then + if [[ "$COMPOSE_TYPE" != "no-postgres" ]]; then + echo "ERROR: When using a remote PostgreSQL server, please use 'docker-compose-no-postgres.yml'. Exiting." + exit 1 + fi + if [ -z "$POSTGRES_PORT" ]; then + POSTGRES_PORT="5432" + fi + else + if [[ "$COMPOSE_TYPE" != "with-postgres" ]]; then + echo "ERROR: Local postgres is selected. Please use 'docker-compose.yml'. Exiting." + exit 1 + fi + POSTGRES_HOST="routerfleet-postgres" + POSTGRES_PORT="5432" + fi + + if [ -z "$POSTGRES_DB" ] || [ -z "$POSTGRES_USER" ] || [ -z "$POSTGRES_PASSWORD" ]; then + echo "POSTGRES_DB, POSTGRES_USER, or POSTGRES_PASSWORD environment variable is not set. Exiting." + exit 1 + fi + DATABASES_CONFIG="DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': '$POSTGRES_DB', + 'USER': '$POSTGRES_USER', + 'PASSWORD': '$POSTGRES_PASSWORD', + 'HOST': '$POSTGRES_HOST', + 'PORT': '$POSTGRES_PORT', + } + }" +else + echo "Unsupported DATABASE_ENGINE. Exiting." exit 1 fi @@ -17,27 +61,18 @@ if [[ "${DEBUG_MODE,,}" == "true" ]]; then DEBUG_VALUE="True" fi -cat > /app/routerfleet/production_settings.py < $PRODUCTION_SETTINGS_FILE <> /app/routerfleet/production_settings.py + echo "TIME_ZONE = '$TIMEZONE'" >> $PRODUCTION_SETTINGS_FILE fi sed -i "/^ path('admin\/', admin.site.urls),/s/^ / # /" /app/routerfleet/urls.py -exec "$@" \ No newline at end of file +exec "$@"