From aa3dc3dba4d5a450666359356fc854df5fc6e678 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Fri, 30 Sep 2022 12:10:50 -0400 Subject: [PATCH] docker composed local dev env --- compose/local/fastapi/Dockerfile | 2 +- compose/local/fastapi/celery/worker/start | 6 +++++ docker-compose.yml | 2 +- project/users/__init__.py | 4 +-- tasks.py | 33 ++++++++++++++++++----- 5 files changed, 37 insertions(+), 10 deletions(-) diff --git a/compose/local/fastapi/Dockerfile b/compose/local/fastapi/Dockerfile index f917e1e..f8e0886 100644 --- a/compose/local/fastapi/Dockerfile +++ b/compose/local/fastapi/Dockerfile @@ -24,7 +24,7 @@ RUN chmod +x /entrypoint COPY ./compose/local/fastapi/start /start-web RUN sed -i 's/\r$//g' /start-web -RUN chmod +x /start +RUN chmod +x /start-web COPY ./compose/local/fastapi/celery/worker/start /start-celeryworker RUN sed -i 's/\r$//g' /start-celeryworker diff --git a/compose/local/fastapi/celery/worker/start b/compose/local/fastapi/celery/worker/start index e69de29..9dad2ed 100644 --- a/compose/local/fastapi/celery/worker/start +++ b/compose/local/fastapi/celery/worker/start @@ -0,0 +1,6 @@ +#!/bin/bash + +set -o errexit +set -o nounset + +celery -A main.celery worker --loglevel=info diff --git a/docker-compose.yml b/docker-compose.yml index 24ea839..de67f15 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,7 @@ services: dockerfile: ./compose/local/fastapi/Dockerfile image: fastapi_celery_example_web # '/start' is the shell script used to run the service - command: /start + command: /start-web # this volume is used to map the files and folders on the host to the container # so if we change code on the host, code in the docker container will also be changed volumes: diff --git a/project/users/__init__.py b/project/users/__init__.py index 4c574b2..c5369c0 100644 --- a/project/users/__init__.py +++ b/project/users/__init__.py @@ -1,9 +1,9 @@ from fastapi import APIRouter -from project.celery_utils import create_celery - users_router = APIRouter( prefix="/users", ) from . import models, tasks # noqa + +from project.celery_utils import create_celery diff --git a/tasks.py b/tasks.py index 10057bd..36394b9 100644 --- a/tasks.py +++ b/tasks.py @@ -3,13 +3,21 @@ A set of `invoke` helper commands used for this project. """ from invoke import task +# pylint: disable=unused-argument + @task -def start_app(c): - """Starts the app.""" +def start_dev(c): + """Starts the app in a docker-compose developer environment.""" print("Starting the developer environment...") - start_redis(c) - c.run("uvicorn main:app --reload", pty=True) + c.run("docker-compose -f docker-compose.yml up -d") + + +@task +def stop_dev(c): + """Stops the developer environment.""" + print("Stoping local development environment.") + c.run("docker-compose -f docker-compose.yml down") @task @@ -52,8 +60,8 @@ def apply_migration(c, revision="head"): c.run(f"alembic upgrade {revision}") -@task(help={"username": "Username of the application", "email": "User's email"}) -def add_user(username="", email=""): +@task(help={"username": "A username", "email": "The user's email"}) +def add_user(c, username="", email=""): "Adds a dummy user to the application Database" if not username or email: raise ValueError("You must provide a username and email") @@ -67,3 +75,16 @@ def add_user(username="", email=""): session.add(user) session.commit() session.close() + + +@task( + help={ + "requirements": "Path to a pip requirements file.", + "dev_requirements": "Path to a pip dev requirements file.", + } +) +def sync_env( + c, requirements="requirements.txt", dev_requirements="dev-requirements.txt" +): + """Uses pip-sync to sync the local virtualenv.""" + c.run(f"pip-sync {requirements} {dev_requirements}")