diff --git a/Taskfile.backend.yml b/Taskfile.backend.yml index 2075f63..df0642c 100644 --- a/Taskfile.backend.yml +++ b/Taskfile.backend.yml @@ -1,82 +1,56 @@ version: '3' env: - BE_BASE_PATH: "/{{ .TASKFILE_DIR }}/backend" - VENV_PATH: "{{ .BE_BASE_PATH }}/.venv" + VENV_PATH: "{{ .TASKFILE_DIR }}/.venv" VENV_BIN: "{{ .VENV_PATH }}/bin" - VENV_ACTIVATE: "{{ .VENV_BIN }}/activate" - DOTENV: "{{ .BE_BASE_PATH }}/.env" APP_CONTAINER_NAME: "rotini_app" + DB_CONTAINER_NAME: "rotini_db" tasks: bootstrap: internal: true - cmds: - - . script/bootstrap + cmd: . script/bootstrap sources: - - "{{ .BE_BASE_PATH }}/pyproject.toml" + - ./pyproject.toml generates: - "{{ .VENV_PATH }}/*" - dir: backend lint: - desc: "Lints /backend using black + pylint." + desc: "Checks /backend for linting and formatting problems." deps: [bootstrap] - cmds: - - "{{ .VENV_BIN }}/black . --check" - - "{{ .VENV_BIN }}/pylint ./rotini" - dir: backend + cmd: . script/format.sh dotenv: - ../backend-test.env lintfix: - desc: "Lints and fixes /backend using black + pylint." + desc: "Resolves linting and formatting problems in /backend." deps: [bootstrap] - cmds: - - "{{ .VENV_BIN }}/black ." - - "{{ .VENV_BIN }}/pylint ./rotini" - dir: backend + cmd: . script/format.sh + env: + FIX: 1 dotenv: - ../backend-test.env test: desc: "Run the test suites." deps: [bootstrap] cmd: . script/test - dir: backend dotenv: - ../backend-test.env - start: - desc: "Starts the backend application." - deps: [docker-build] - cmd: docker run -d -p 8000:8000 --name {{ .APP_CONTAINER_NAME }} {{ .CLI_ARGS }} --add-host docker.host.internal:host-gateway --env-file ../../backend.env rotini:dev - dir: backend/rotini - stop: - desc: "Stops the backend application." - cmd: docker rm -f {{ .APP_CONTAINER_NAME }} - logs: - desc: "Shortcut to Docker container logs" - cmd: docker logs {{ .APP_CONTAINER_NAME }} -f - start-db: - desc: "Provisions a local Postgres database." - dotenv: - - "{{ .DOTENV }}" - cmd: . script/provision-db - dir: backend - migrate: - desc: "Applies migrations. Usage: be:migrate -- " - deps: [bootstrap] - env: - PYTHONPATH: '..' - ROTINI_MIGRATE: 1 - dotenv: - - "{{ .DOTENV }}" - cmd: "{{ .VENV_BIN }}/python migrate.py {{ .CLI_ARGS }}" - dir: backend/rotini/migrations lock-deps: desc: "Locks production and development dependencies" deps: [bootstrap] cmd: . script/requirements-lock - dir: backend - docker-build: + docker:start: + desc: "Starts the backend application." + deps: [build] + cmd: . script/start.sh + dotenv: + - ../backend.env + docker:stop: + desc: "Stops the backend application." + cmd: docker rm -f {{ .APP_CONTAINER_NAME }} {{ .DB_CONTAINER_NAME }} + docker:logs: + desc: "Shortcut to Docker container logs" + cmd: docker logs {{ .APP_CONTAINER_NAME }} -f + docker:build: desc: "Builds a docker image from /backend" - cmd: docker build --build-arg PYTHON_VERSION=$(cat .python-version) -t rotini:dev . - dir: backend + cmd: . script/build.sh diff --git a/Taskfile.yml b/Taskfile.yml index f2f1775..ebdd8c6 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -3,5 +3,7 @@ version: '3' # See environment-specific taskfiles for commands about # that environment. includes: - be: ./Taskfile.backend.yml + be: + taskfile: ./Taskfile.backend.yml + dir: backend fe: ./Taskfile.frontend.yml diff --git a/backend-test.env b/backend-test.env index 157ab3b..3eccfae 100644 --- a/backend-test.env +++ b/backend-test.env @@ -1,2 +1,7 @@ DJANGO_SECRET_KEY="notakey" JWT_SIGNING_SECRET="notasecret" +DB_HOST="rotini_db" +DB_USER="postgres" +DB_PASSWORD="postgres" +DB_PORT=5432 +DB_NAME="postgres" diff --git a/backend/rotini/base/settings.py b/backend/rotini/base/settings.py index 1793cce..027514b 100644 --- a/backend/rotini/base/settings.py +++ b/backend/rotini/base/settings.py @@ -64,11 +64,11 @@ WSGI_APPLICATION = "base.wsgi.application" DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql_psycopg2", - "NAME": "postgres", - "USER": "postgres", - "PASSWORD": "test", - "HOST": "docker.host.internal", - "PORT": "5432", + "NAME": os.environ["DB_NAME"], + "USER": os.environ["DB_USER"], + "PASSWORD": os.environ["DB_PASSWORD"], + "HOST": os.environ["DB_HOST"], + "PORT": os.environ["DB_PORT"], } } diff --git a/backend/script/build.sh b/backend/script/build.sh new file mode 100644 index 0000000..25bfed7 --- /dev/null +++ b/backend/script/build.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +docker build -t rotini:dev . diff --git a/backend/script/format.sh b/backend/script/format.sh new file mode 100644 index 0000000..66f1673 --- /dev/null +++ b/backend/script/format.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +if [ "$FIX" != "1" ]; then + $VENV_BIN/black . --check + $VENV_BIN/pylint ./rotini + return +fi + +$VENV_BIN/black . +$VENV_BIN/pylint ./rotini diff --git a/backend/script/start.sh b/backend/script/start.sh new file mode 100644 index 0000000..54de080 --- /dev/null +++ b/backend/script/start.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +docker run \ + --name $DB_CONTAINER_NAME \ + -e POSTGRES_PASSWORD=$DATABASE_PASSWORD \ + -e POSTGRES_USER=$DATABASE_USER \ + -e POSTGRES_DB=$DATABASE_NAME \ + -v $DATABASE_STORAGE_PATH:/var/lib/postgresql/data \ + -p 5432:5432 \ + -d \ + postgres:15.4 + +until [ -n "$(docker exec $DB_CONTAINER_NAME pg_isready | grep accepting)" ]; do + echo "Waiting for DB to come alive..." + sleep 0.1; +done; + +docker run \ + --detach \ + --publish 8000:8000 \ + --name $APP_CONTAINER_NAME \ + --env-file ../backend.env \ + rotini:dev