Docker postgres service and docker-compose merge support #46

Open
rskthomas wants to merge 18 commits from feature/devicehub-postgres_docker into main
9 changed files with 169 additions and 38 deletions

View file

@ -32,6 +32,19 @@ DEVICEHUB_ALLOWED_HOSTS=${DEVICEHUB_DOMAIN},${DEVICEHUB_DOMAIN}:${DEVICEHUB_PORT
#EVIDENCES_DIR=/path/to/TODO
#DEMO_IDHUB_DOMAIN='idhub.example.org'
#https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
TIME_ZONE='Europe/Madrid'
###
# DEVICEHUB-POSTGRES
###
DB_NAME="devicehub"
DB_USER="ereuse"
DB_PASSWORD="ereuse"
DB_HOST="devicehub-postgres"
DB_PORT=5432
####
# IDHUB
####
@ -71,3 +84,11 @@ IDHUB_ENABLE_EMAIL=false
IDHUB_ENABLE_2FACTOR_AUTH=false
IDHUB_ENABLE_DOMAIN_CHECKER=false
IDHUB_PREDEFINED_TOKEN='27f944ce-3d58-4f48-b068-e4aa95f97c95'
# IDHUB- Postgres
###
IDHUB_DB_NAME="idhub"
IDHUB_DB_USER="ereuse"
IDHUB_DB_PASSWORD="ereuse"
IDHUB_DB_HOST="idhub-postgres"
IDHUB_DB_PORT=5432

1
.gitignore vendored
View file

@ -3,5 +3,4 @@ __pycache__/
.env
# the following could be autogenerated by devicehub
db.sqlite3
example/snapshots/snapshot_workbench-script_verifiable-credential.json

View file

@ -133,9 +133,13 @@ WSGI_APPLICATION = "dhub.wsgi.application"
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db/db.sqlite3",
'default': {
'ENGINE': os.getenv('DB_ENGINE', 'django.db.backends.postgresql'),
'NAME': os.getenv('DB_NAME', 'devicehub'),
'USER': os.getenv('DB_USER', 'ereuse'),
'PASSWORD': os.getenv('DB_PASSWORD', 'ereuse'),
'HOST': os.getenv('DB_HOST', 'devicehub-postgres'),
'PORT': os.getenv('DB_PORT', '5432'),
}
}

View file

@ -0,0 +1,21 @@
# usage: docker compose -f docker-compose.yml -f docker-compose.override.prod.yml up
# https://docs.docker.com/compose/how-tos/multiple-compose-files/merge/
services:
devicehub-django:
image: farga.pangea.org/ereuse/devicehub-django/latest
volumes:
- devicehub_data:/opt/devicehub-django
environment:
- DEBUG=false
idhub:
image: farga.pangea.org/ereuse/idhub:latest
volumes:
- idhub_data:/opt/idhub
environment:
- DEBUG=false
volumes:
devicehub_data:
idhub_data:

View file

@ -0,0 +1,30 @@
#Default override compose file for dev options
services:
devicehub-django:
build:
context: .
dockerfile: docker/devicehub-django.Dockerfile
volumes:
- .:/opt/devicehub-django # bind mount for local development
environment:
- DEBUG=true
devicehub-postgres:
ports:
- 5432:5432
idhub:
image: farga.pangea.org/ereuse/idhub:latest
build:
context: /path/to/idhub/directory
dockerfile: /path/to/idhub/dockerfile
volumes:
- /path/to/idhub/directory:/opt/idhub
environment:
- DEMO=true
- DEBUG=true
idhub-postgres:
ports:
- 5433:5432

View file

@ -1,49 +1,68 @@
services:
devicehub-django:
init: true
image: farga.pangea.org/ereuse/devicehub-django/latest
build:
context: .
dockerfile: docker/devicehub-django.Dockerfile
environment:
- DEBUG=${DEBUG:-false}
# General
- DOMAIN=${DEVICEHUB_DOMAIN:-localhost}
- PORT=${DEVICEHUB_PORT:-8000}
- ALLOWED_HOSTS=${DEVICEHUB_ALLOWED_HOSTS:-$DEVICEHUB_DOMAIN}
# Demo settings
- DEMO=${DEMO:-false}
- DEMO_IDHUB_DOMAIN=${DEMO_IDHUB_DOMAIN:-}
- DEMO_IDHUB_PREDEFINED_TOKEN=${IDHUB_PREDEFINED_TOKEN:-}
# Auth & Security
- PREDEFINED_TOKEN=${PREDEFINED_TOKEN:-}
- DPP=${DPP:-false}
# TODO manage volumes dev vs prod
volumes:
- .:/opt/devicehub-django
# DB vars
- DB_HOST=${DB_HOST:-devicehub-postgres}
- DB_PORT=${DB_PORT:-5432}
- DB_NAME=${DB_NAME}
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
ports:
- ${DEVICEHUB_PORT:-8000}:${DEVICEHUB_PORT:-8000}
depends_on:
devicehub-postgres:
condition: service_healthy
restart: true
# TODO add database service for idhub, meanwhile sqlite
devicehub-postgres:
image: postgres:17
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
volumes:
- pg_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_NAME}"]
start_period: 1s
interval: 1s
timeout: 10s
retries: 10
idhub:
# https://docs.docker.com/compose/how-tos/profiles/
profiles: [idhub]
init: true
image: farga.pangea.org/ereuse/idhub/latest
environment:
# General
- DOMAIN=${IDHUB_DOMAIN:-localhost}
- ALLOWED_HOSTS=${IDHUB_ALLOWED_HOSTS:-$IDHUB_DOMAIN}
- DEBUG=true
- DEMO=${DEMO:-false}
- DEMO=${IDHUB_DEMO:-}
# Admin & User
- INITIAL_ADMIN_EMAIL=${IDHUB_ADMIN_EMAIL}
- INITIAL_ADMIN_PASSWORD=${IDHUB_ADMIN_PASSWD}
- CREATE_TEST_USERS=true
- ENABLE_EMAIL=${IDHUB_ENABLE_EMAIL:-true}
- ENABLE_2FACTOR_AUTH=${IDHUB_ENABLE_2FACTOR_AUTH:-true}
# Email Configuration
- ENABLE_EMAIL=${IDHUB_ENABLE_EMAIL}
- ENABLE_DOMAIN_CHECKER=${IDHUB_ENABLE_DOMAIN_CHECKER:-true}
- PREDEFINED_TOKEN=${IDHUB_PREDEFINED_TOKEN:-}
- SECRET_KEY=${IDHUB_SECRET_KEY:-publicsecretisnotsecureVtmKBfxpVV47PpBCF2Nzz2H6qnbd}
- STATIC_ROOT=${IDHUB_STATIC_ROOT:-/static/}
- MEDIA_ROOT=${IDHUB_MEDIA_ROOT:-/media/}
- PORT=${IDHUB_PORT:-9001}
- DEFAULT_FROM_EMAIL=${IDHUB_DEFAULT_FROM_EMAIL}
- EMAIL_HOST=${IDHUB_EMAIL_HOST}
- EMAIL_HOST_USER=${IDHUB_EMAIL_HOST_USER}
@ -51,9 +70,50 @@ services:
- EMAIL_PORT=${IDHUB_EMAIL_PORT}
- EMAIL_USE_TLS=${IDHUB_EMAIL_USE_TLS}
- EMAIL_BACKEND=${IDHUB_EMAIL_BACKEND}
- SUPPORTED_CREDENTIALS=['Snapshot']
# Auth & Security
- SECRET_KEY=${IDHUB_SECRET_KEY:-publicsecretisnotsecureVtmKBfxpVV47PpBCF2Nzz2H6qnbd}
- PREDEFINED_TOKEN=${IDHUB_PREDEFINED_TOKEN:-}
- ENABLE_2FACTOR_AUTH=${IDHUB_ENABLE_2FACTOR_AUTH:-true}
# App
- SYNC_ORG_DEV=${IDHUB_SYNC_ORG_DEV}
- STATIC_ROOT=${IDHUB_STATIC_ROOT:-/static/}
- MEDIA_ROOT=${IDHUB_MEDIA_ROOT:-/media/}
- PORT=${IDHUB_PORT:-9001}
- SUPPORTED_CREDENTIALS=${IDHUB_SUPPORTED_CREDENTIALS:-}
# DB vars
- DB_PORT=${IDHUB_DB_PORT:-5432}
- DB_HOST=${IDHUB_DB_HOST:-devicehub-postgres}
- DB_NAME=${IDHUB_DB_NAME}
- DB_USER=${IDHUB_DB_USER}
- DB_PASSWORD=${IDHUB_DB_PASSWORD}
ports:
- 9001:9001
depends_on:
idhub-postgres:
condition: service_healthy
restart: true
# TODO add database service for idhub, meanwhile sqlite
idhub-postgres:
profiles: [idhub]
image: postgres:17
environment:
- POSTGRES_DB=${IDHUB_DB_NAME}
- POSTGRES_USER=${IDHUB_DB_USER}
- POSTGRES_PASSWORD=${IDHUB_DB_PASSWORD}
volumes:
- idhub_pg_data:/var/lib/postgresql/data
# https://docs.docker.com/compose/how-tos/startup-order/
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${IDHUB_DB_USER} -d ${IDHUB_DB_NAME}"]
start_period: 1s
interval: 1s
timeout: 10s
retries: 10
volumes:
pg_data:
idhub_pg_data:

View file

@ -5,7 +5,6 @@ RUN apt update && \
apt-get install -y \
python3-xapian \
git \
sqlite3 \
curl \
jq \
time \

View file

@ -33,6 +33,7 @@ gen_env_vars() {
INIT_PASSWD="${INIT_PASSWD:-1234}"
ADMIN='True'
PREDEFINED_TOKEN="${PREDEFINED_TOKEN:-}"
# specific dpp env vars
if [ "${DPP:-}" = 'true' ]; then
# fill env vars in this docker entrypoint
@ -168,6 +169,7 @@ config_phase() {
init_flagfile="${program_dir}/already_configured"
if [ ! -f "${init_flagfile}" ]; then
echo "INFO: detected NEW deployment"
# non DL user (only for the inventory)
./manage.py add_institution "${INIT_ORG}"
# TODO: one error on add_user, and you don't add user anymore
@ -190,6 +192,8 @@ config_phase() {
# remain next command as the last operation for this if conditional
touch "${init_flagfile}"
else
echo "INFO: detected PREVIOUS deployment"
fi
}
@ -210,17 +214,10 @@ deploy() {
echo "DOMAIN: ${DOMAIN}"
fi
# detect if existing deployment (TODO only works with sqlite)
if [ -f "${program_dir}/db/db.sqlite3" ]; then
echo "INFO: detected EXISTING deployment"
./manage.py migrate
else
# move the migrate thing in docker entrypoint
# inspired by https://medium.com/analytics-vidhya/django-with-docker-and-docker-compose-python-part-2-8415976470cc
echo "INFO detected NEW deployment"
./manage.py migrate
config_phase
fi
# move the migrate thing in docker entrypoint
# inspired by https://medium.com/analytics-vidhya/django-with-docker-and-docker-compose-python-part-2-8415976470cc
./manage.py migrate
config_phase
}
runserver() {

View file

@ -14,4 +14,4 @@ json-repair==0.30.0
setuptools==65.5.1
requests==2.32.3
wheel==0.45.1
psycopg2-binary==2.9.10