initial postgres docker implementation
This commit is contained in:
parent
bb54ed1177
commit
d79880ff9d
|
@ -133,9 +133,13 @@ WSGI_APPLICATION = "dhub.wsgi.application"
|
||||||
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
|
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
|
||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
"default": {
|
'default': {
|
||||||
"ENGINE": "django.db.backends.sqlite3",
|
'ENGINE': os.getenv('DB_ENGINE', 'django.db.backends.postgresql'),
|
||||||
"NAME": BASE_DIR / "db/db.sqlite3",
|
'NAME': os.getenv('DB_NAME', 'devicehub'),
|
||||||
|
'USER': os.getenv('DB_USER', 'devicehub'),
|
||||||
|
'PASSWORD': os.getenv('DB_PASSWORD', 'devicehub'),
|
||||||
|
'HOST': os.getenv('DB_HOST', 'postgres'),
|
||||||
|
'PORT': os.getenv('DB_PORT', '5432'),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,25 @@ services:
|
||||||
- .:/opt/devicehub-django
|
- .:/opt/devicehub-django
|
||||||
ports:
|
ports:
|
||||||
- ${DEVICEHUB_PORT:-8000}:${DEVICEHUB_PORT:-8000}
|
- ${DEVICEHUB_PORT:-8000}:${DEVICEHUB_PORT:-8000}
|
||||||
|
depends_on:
|
||||||
|
- postgres
|
||||||
|
|
||||||
|
devicehub-postgres:
|
||||||
|
image: postgres:17
|
||||||
|
environment:
|
||||||
|
- DB_NAME=${DB_NAME:-devicehub}
|
||||||
|
- db_USER=${DB_USER:-devicehub}
|
||||||
|
- db_PASSWORD=${DB_PASSWORD:-devicehub}
|
||||||
|
volumes:
|
||||||
|
- pg_data:/var/lib/postgresql/data
|
||||||
|
ports:
|
||||||
|
- ${DB_PORT}:${DB_PORT}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
pg_data:
|
||||||
|
|
||||||
|
|
||||||
# TODO add database service for idhub, meanwhile sqlite
|
# TODO add database service for idhub, meanwhile sqlite
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,14 @@ set -u
|
||||||
# DEBUG
|
# DEBUG
|
||||||
set -x
|
set -x
|
||||||
|
|
||||||
|
wait_for_postgres() {
|
||||||
|
# thanks https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/
|
||||||
|
while ! nc -z $DB_HOST $DB_PORT ; do
|
||||||
|
sleep 0.5
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# TODO there is a conflict between two shared vars
|
# TODO there is a conflict between two shared vars
|
||||||
# 1. from the original docker compose devicehub-teal
|
# 1. from the original docker compose devicehub-teal
|
||||||
# 2. from the new docker compose that integrates all dpp services
|
# 2. from the new docker compose that integrates all dpp services
|
||||||
|
@ -33,6 +41,13 @@ gen_env_vars() {
|
||||||
INIT_PASSWD="${INIT_PASSWD:-1234}"
|
INIT_PASSWD="${INIT_PASSWD:-1234}"
|
||||||
ADMIN='True'
|
ADMIN='True'
|
||||||
PREDEFINED_TOKEN="${PREDEFINED_TOKEN:-}"
|
PREDEFINED_TOKEN="${PREDEFINED_TOKEN:-}"
|
||||||
|
|
||||||
|
DB_NAME=${DB_NAME:-devicehub}
|
||||||
|
DB_USER=${DB_USER:-ereuse}
|
||||||
|
DB_PASSWORD=${DB_PASSWORD:-ereuse}
|
||||||
|
DB_HOST=${DB_HOST:-localhost}
|
||||||
|
DB_PORT=${DB_PORT:-5432}
|
||||||
|
|
||||||
# specific dpp env vars
|
# specific dpp env vars
|
||||||
if [ "${DPP:-}" = 'true' ]; then
|
if [ "${DPP:-}" = 'true' ]; then
|
||||||
# fill env vars in this docker entrypoint
|
# fill env vars in this docker entrypoint
|
||||||
|
@ -210,10 +225,13 @@ deploy() {
|
||||||
echo "DOMAIN: ${DOMAIN}"
|
echo "DOMAIN: ${DOMAIN}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# detect if existing deployment (TODO only works with sqlite)
|
#IMPORTANT: run python manage.py dumpdata --natural-foreign --natural-primary > sqlite3dump.json
|
||||||
if [ -f "${program_dir}/db/db.sqlite3" ]; then
|
# detect if exists a sqlite3 dump
|
||||||
echo "INFO: detected EXISTING deployment"
|
if [ -f "${program_dir}/db/sqlite3dump.json" ]; then
|
||||||
|
echo "INFO: detected EXISTING sqlite3 deployment. Migrating to postgres"
|
||||||
./manage.py migrate
|
./manage.py migrate
|
||||||
|
./manage.py loaddata ${program_dir}/db/sqltie3dump.json
|
||||||
|
|
||||||
else
|
else
|
||||||
# move the migrate thing in docker entrypoint
|
# move the migrate thing in docker entrypoint
|
||||||
# inspired by https://medium.com/analytics-vidhya/django-with-docker-and-docker-compose-python-part-2-8415976470cc
|
# inspired by https://medium.com/analytics-vidhya/django-with-docker-and-docker-compose-python-part-2-8415976470cc
|
||||||
|
@ -248,6 +266,7 @@ main() {
|
||||||
program_dir='/opt/devicehub-django'
|
program_dir='/opt/devicehub-django'
|
||||||
cd "${program_dir}"
|
cd "${program_dir}"
|
||||||
gen_env_vars
|
gen_env_vars
|
||||||
|
wait_for_postgres
|
||||||
deploy
|
deploy
|
||||||
runserver
|
runserver
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue