From 044b37fac04216e9535767ecac6ae279018945d2 Mon Sep 17 00:00:00 2001 From: Luis Alvergue Date: Mon, 9 Dec 2024 20:36:38 +0000 Subject: [PATCH 1/2] feat: initialize Django and create/reset database - add env variables to reset db and for super user info - reset the db, run migrations, and create superuser when the devcontainer starts --- .devcontainer/devcontainer.json | 1 + .env.sample | 6 ++++++ bin/init.sh | 6 ++++++ bin/reset_db.sh | 24 ++++++++++++++++++++++++ compose.yml | 1 + 5 files changed, 38 insertions(+) create mode 100755 bin/init.sh create mode 100755 bin/reset_db.sh diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index ac7f3c6..915ec56 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -4,6 +4,7 @@ "service": "dev", "forwardPorts": ["docs:8000", "kibana:5601"], "workspaceFolder": "/home/caltrans/src", + "postStartCommand": ["/bin/bash", "bin/reset_db.sh"], "postAttachCommand": ["/bin/bash", ".devcontainer/postAttach.sh"], "customizations": { "vscode": { diff --git a/.env.sample b/.env.sample index ca0603f..6bdc79f 100644 --- a/.env.sample +++ b/.env.sample @@ -1,4 +1,10 @@ +# Django superuser account for backend admin access +DJANGO_SUPERUSER_USERNAME=pems-admin +DJANGO_SUPERUSER_EMAIL=pems-admin@compiler.la +DJANGO_SUPERUSER_PASSWORD=superuser12345! + # Django storage +DJANGO_DB_RESET=true DJANGO_STORAGE_DIR=. DJANGO_DB_FILE=django.db diff --git a/bin/init.sh b/bin/init.sh new file mode 100755 index 0000000..6c4feca --- /dev/null +++ b/bin/init.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -eux + +# run database migrations + +python manage.py migrate diff --git a/bin/reset_db.sh b/bin/reset_db.sh new file mode 100755 index 0000000..43f168e --- /dev/null +++ b/bin/reset_db.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +set -ex + +# whether to reset database file, defaults to true +DB_RESET="${DJANGO_DB_RESET:-true}" + +if [[ $DB_RESET = true ]]; then + # construct the path to the database file from environment or default + DB_DIR="${DJANGO_STORAGE_DIR:-.}" + DB_FILE="${DJANGO_DB_FILE:-django.db}" + DB_PATH="${DB_DIR}/${DB_FILE}" + + rm -f "${DB_PATH}" + + # run database migrations and other initialization + bin/init.sh + + # create a superuser account for backend admin access + # set username, email, and password using environment variables + # DJANGO_SUPERUSER_USERNAME, DJANGO_SUPERUSER_EMAIL, and DJANGO_SUPERUSER_PASSWORD + python manage.py createsuperuser --no-input +else + echo "DB_RESET is false, skipping" +fi diff --git a/compose.yml b/compose.yml index eb8c5ab..da61384 100644 --- a/compose.yml +++ b/compose.yml @@ -6,6 +6,7 @@ services: context: . dockerfile: .devcontainer/Dockerfile image: caltrans/pems:main + env_file: .env volumes: - ./:/home/caltrans/src From 15f66cf99a698af51ba7e04ead9f6cf222a3f470 Mon Sep 17 00:00:00 2001 From: Luis Alvergue Date: Mon, 9 Dec 2024 21:16:48 +0000 Subject: [PATCH 2/2] feat: collect static files --- bin/init.sh | 4 ++++ pems/settings.py | 2 ++ 2 files changed, 6 insertions(+) diff --git a/bin/init.sh b/bin/init.sh index 6c4feca..5b9f41b 100755 --- a/bin/init.sh +++ b/bin/init.sh @@ -4,3 +4,7 @@ set -eux # run database migrations python manage.py migrate + +# collect static files + +python manage.py collectstatic --no-input diff --git a/pems/settings.py b/pems/settings.py index 59b94c9..d034edf 100644 --- a/pems/settings.py +++ b/pems/settings.py @@ -112,6 +112,8 @@ def _filter_empty(ls): STATIC_URL = "static/" +STATIC_ROOT = os.path.join(BASE_DIR, "static") + # Default primary key field type # https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field