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..5b9f41b --- /dev/null +++ b/bin/init.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -eux + +# run database migrations + +python manage.py migrate + +# collect static files + +python manage.py collectstatic --no-input 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 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