Skip to content

Commit

Permalink
Revamp backend (#72)
Browse files Browse the repository at this point in the history
* Replace Django with Flask and initial custom exceptions for CLI

* revise task definitions, file watchers, format isort

* add types

* revise build tasks

* FE cleanup

* Restore GA

* fix tests
  • Loading branch information
kvdomingo authored Sep 11, 2022
1 parent cbd5922 commit be01397
Show file tree
Hide file tree
Showing 66 changed files with 6,302 additions and 7,284 deletions.
58 changes: 27 additions & 31 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,55 +1,51 @@
FROM python:3.9.7-bullseye as base
FROM python:3.10-bullseye as base

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV POETRY_VERSION 1.1.12

COPY requirements.txt /tmp/requirements.txt

RUN python -m pip install -U pip setuptools

RUN pip install --no-cache-dir -r /tmp/requirements.txt
RUN pip install poetry==$POETRY_VERSION

FROM base as dev

COPY requirements.dev.txt /tmp/requirements.dev.txt
COPY requirements.txt /tmp/requirements.txt
WORKDIR /primerdriver

RUN pip install --no-cache-dir -r /tmp/requirements.dev.txt
COPY pyproject.toml poetry.lock ./

WORKDIR /primerdriver
RUN poetry install

ENTRYPOINT python manage.py migrate && \
SHORT_SHA=$(git show --format="%h" --no-patch) gunicorn primerx.wsgi \
-b 0.0.0.0:5000 \
--workers 2 \
--threads 4 \
--log-file - \
--capture-output \
--reload
ENTRYPOINT ["poetry", "run", "flask", "run", "-h", "0.0.0.0", "-p", "5000", "--reload"]

FROM node:16-alpine as build
FROM node:16-alpine as web_build

WORKDIR /web

COPY ./web/app/ ./
COPY ./web/app/public ./public
COPY ./web/app/src ./src
COPY ./web/app/package.json ./web/app/yarn.lock ./

RUN yarn install --prod

RUN yarn build
RUN yarn install && yarn build

FROM base as prod

ARG SHORT_SHA=$SHORT_SHA
ENV SHORT_SHA $SHORT_SHA

WORKDIR /tmp

COPY pyproject.toml poetry.lock ./

RUN poetry export -f requirements.txt | pip install --no-cache-dir -r /dev/stdin

WORKDIR /primerdriver

COPY ./primerdriver/ ./primerdriver/
COPY ./primerx/ ./primerx/
COPY ./sdm/ ./sdm/
COPY ./manage.py ./manage.py
COPY --from=build /web/build ./web/app/
COPY ./*.py ./
COPY ./*.sh ./
COPY --from=web_build /web/build ./web/app/

ARG SHORT_SHA=$SHORT_SHA

ENV SHORT_SHA $SHORT_SHA
EXPOSE $PORT

ENTRYPOINT python manage.py collectstatic --noinput && \
python manage.py migrate && \
gunicorn primerx.wsgi -b 0.0.0.0:$PORT --workers 1 --threads 2 --log-file -
ENTRYPOINT [ "gunicorn", "-b", "0.0.0.0:$PORT", "-c", "./gunicorn.conf.py" ]
18 changes: 0 additions & 18 deletions Dockerfile.build

This file was deleted.

36 changes: 24 additions & 12 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,54 @@
version: '3'

env:
NAME: primerdriver-dev
SHORT_SHA:
sh: git show --format="%h" --no-patch

tasks:
default:
cmds:
- docker compose up -d --build --remove-orphans
- docker compose -p $NAME up -d --build --remove-orphans {{.CLI_ARGS}}
- task logs

logs:
cmds:
- docker compose logs --follow
- docker compose -p $NAME logs --follow {{.CLI_ARGS}}

make-builder:
test:
cmds:
- docker build -t kvdomingo/primerdriver-makelinux:latest -f Dockerfile.build .
- poetry run python -m unittest {{.CLI_ARGS}}

publish-builder:
make-builder:
cmds:
- docker push kvdomingo/primerdriver-makelinux:latest
- docker build -t kvdomingo/primerdriver-makelinux:latest -f build.Dockerfile .
- docker push kvdomingo/primerdriver-makelinux:latest

build-linux:
cmds:
- docker run -v "$(pwd)":/primerdriver --rm --name primerdriver-makelinux kvdomingo/primerdriver-makelinux:latest

build:
cmds:
- pyinstaller --clean -F --name primerdriver setup.py
- poetry run pyinstaller --clean -F --name primerdriver setup.py

build-all:
cmds:
- task build
- task build-linux

shutdown:
cmds:
- docker compose stop
- docker compose -p $NAME stop {{.CLI_ARGS}}

restart:
cmds:
- docker compose restart
- docker compose -p $NAME restart {{.CLI_ARGS}}

restart-proxy:
reload-proxy:
cmds:
- docker compose restart proxy
- docker compose -p $NAME exec -- proxy nginx -s reload

clean:
cmds:
- docker compose down -v --remove-orphans
- docker compose -p $NAME down -v --remove-orphans
20 changes: 20 additions & 0 deletions build.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM python:3.10-bullseye

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV POETRY_VERSION 1.1.13

RUN apt-get update && apt-get install upx-ucl libgfortran-10-dev libquadmath0 -y

RUN pip install "poetry==$POETRY_VERSION"

RUN sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b /bin

WORKDIR /primerdriver

COPY pyproject.toml poetry.lock ./

RUN poetry config virtualenvs.create false && poetry install --no-interaction --no-ansi


ENTRYPOINT [ "/bin/task", "build" ]
2 changes: 1 addition & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ steps:
id: Deploy
entrypoint: gcloud

timeout: 900s
timeout: 1500s

images:
- '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
37 changes: 25 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
version: "3.8"

networks:
gateway:
external: true

services:
apiserver:
backend:
build:
context: .
target: dev
Expand All @@ -15,20 +19,29 @@ services:
context: ./web/app
target: dev
image: kvdomingo/primerdriver-app:latest
env_file: ./web/app/.env
environment:
- HOST=0.0.0.0
- PORT=3000
- NODE_ENV=development
- CHOKIDAR_USEPOLLING=true
- TSC_WATCHFILE=UseFsEventsWithFallbackDynamicPolling
- WDS_SOCKET_PORT=0
volumes:
- ./web:/web

proxy:
image: nginx:latest
ports:
- 8000:8000
command:
- nginx
- "-g"
- daemon off;
image: bitnami/nginx:1.21.6
environment:
- NGINX_HTTP_PORT_NUMBER=8000
volumes:
- ./proxy/nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- apiserver
- ./proxy/nginx.conf:/opt/bitnami/nginx/conf/server_blocks/primerdriver.conf:ro
labels:
- "traefik.enable=true"
- "traefik.http.routers.primerdriver.rule=Host(`primerdriver.localhost`)"
- "traefik.http.routers.primerdriver.entrypoints=http"
- "traefik.docker.network=gateway"
- "traefik.http.services.primerdriver.loadbalancer.server.port=8000"
restart: unless-stopped
networks:
- default
- gateway
10 changes: 10 additions & 0 deletions gunicorn.conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
wsgi_app = "primerx.wsgi"

worker_class = "gthread"
workers = 1
threads = 2

errorlog = "-"
accesslog = "-"
capture_output = True
loglevel = "error"
Loading

0 comments on commit be01397

Please sign in to comment.