From 147699e4176853cff5777c5d9c49b093d328ece3 Mon Sep 17 00:00:00 2001 From: Vineeth Voruganti <13438633+VVoruganti@users.noreply.github.com> Date: Tue, 22 Oct 2024 01:47:01 -0400 Subject: [PATCH] fix(deployment) Docker and fly settings --- .github/workflows/fly.yml | 6 ++--- Dockerfile | 56 ++++++++++++++++++++++----------------- api/main.py | 7 +++-- fly.toml | 25 ++++++++++------- 4 files changed, 55 insertions(+), 39 deletions(-) diff --git a/.github/workflows/fly.yml b/.github/workflows/fly.yml index f289c96..6364ede 100644 --- a/.github/workflows/fly.yml +++ b/.github/workflows/fly.yml @@ -10,7 +10,7 @@ jobs: name: Deploy Production App runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: superfly/flyctl-actions/setup-flyctl@master - run: flyctl deploy -c fly.toml --app bloom-bot --remote-only env: @@ -20,8 +20,8 @@ jobs: name: Deploy Staging App runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: superfly/flyctl-actions/setup-flyctl@master - - run: flyctl deploy -c fly.toml --app tutor-gpt --remote-only + - run: flyctl deploy -c fly.toml --app bloom-staging --remote-only env: FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} diff --git a/Dockerfile b/Dockerfile index c8bd4fd..84e3278 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,40 +1,46 @@ # https://pythonspeed.com/articles/base-image-python-docker-images/ # https://testdriven.io/blog/docker-best-practices/ -FROM python:3.10-slim-bullseye +FROM python:3.11-slim-bullseye -RUN apt-get update && apt-get install -y build-essential +COPY --from=ghcr.io/astral-sh/uv:0.4.9 /uv /bin/uv +# Set Working directory WORKDIR /app -# https://stackoverflow.com/questions/53835198/integrating-python-poetry-with-docker -ENV PYTHONFAULTHANDLER=1 \ - PYTHONUNBUFFERED=1 \ - PYTHONHASHSEED=random \ - PIP_NO_CACHE_DIR=off \ - PIP_DISABLE_PIP_VERSION_CHECK=on \ - PIP_DEFAULT_TIMEOUT=100 \ - POETRY_VERSION=1.4.1 +RUN addgroup --system app && adduser --system --group app +RUN chown -R app:app /app +USER app + +# Enable bytecode compilation +ENV UV_COMPILE_BYTECODE=1 + +# Copy from the cache instead of linking since it's a mounted volume +ENV UV_LINK_MODE=copy -RUN pip install "poetry==$POETRY_VERSION" +# Install the project's dependencies using the lockfile and settings +RUN --mount=type=cache,target=/root/.cache/uv \ + --mount=type=bind,source=uv.lock,target=uv.lock \ + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ + --mount=type=bind,source=api/,target=api/ \ + --mount=type=bind,source=agent/,target=agent/ \ + --mount=type=bind,source=bot/,target=bot/ \ + uv sync --frozen --no-dev # Copy only requirements to cache them in docker layer -WORKDIR /app -COPY poetry.lock pyproject.toml /app/ +COPY --chown=app:app uv.lock /app/ +COPY --chown=app:app pyproject.toml /app/ -# Project initialization: -RUN poetry config virtualenvs.create false \ - && poetry install --no-root --no-interaction --no-ansi --without dev +# Place executables in the environment at the front of the path +ENV PATH="/app/.venv/bin:$PATH" -WORKDIR /app +EXPOSE 8000 -RUN addgroup --system app && adduser --system --group app -USER app +COPY --chown=app:app agent/ /app/agent/ +COPY --chown=app:app bot/ /app/bot/ +COPY --chown=app:app api/ /app/api/ -COPY agent/ agent/ -COPY common/ common/ -COPY bot/ bot/ -COPY api/ api/ +# RUN --mount=type=cache,target=/root/.cache/uv \ +RUN uv pip install -r /app/api/pyproject.toml # https://stackoverflow.com/questions/29663459/python-app-does-not-print-anything-when-running-detached-in-docker -CMD ["python", "-m", "uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000"] -# CMD ["python", "-u", "-m", "bot.app"] +CMD ["fastapi", "run", "--host", "0.0.0.0", "api/main.py"] diff --git a/api/main.py b/api/main.py index 75e2eb8..5225777 100644 --- a/api/main.py +++ b/api/main.py @@ -6,7 +6,7 @@ from dotenv import load_dotenv from api.routers import conversation, chat, messages -load_dotenv() +load_dotenv(override=True) SENTRY_DSN = os.getenv("SENTRY_DN") @@ -20,9 +20,12 @@ app = FastAPI() +URL = os.getenv("URL", "http://localhost:3000") +print("URL:", URL) + app.add_middleware( CORSMiddleware, - allow_origin_regex=os.environ["URL"], + allow_origin_regex=URL, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], diff --git a/fly.toml b/fly.toml index f349850..8ec48cb 100644 --- a/fly.toml +++ b/fly.toml @@ -1,26 +1,33 @@ -# fly.toml app configuration file generated for vineeth-test-py on 2023-09-09T17:18:22-04:00 +# fly.toml app configuration file generated for bloom-staging on 2024-10-22T01:01:35-04:00 # # See https://fly.io/docs/reference/configuration/ for information about how to use this file. # -primary_region = "bos" -kill_signal = "SIGINT" -kill_timeout = "5s" +primary_region = 'bos' +kill_signal = 'SIGINT' +kill_timeout = '5s' [experimental] auto_rollback = true +[build] + [processes] -discord = "python -u -m bot.app" -api = "python -m uvicorn api.main:app --host 0.0.0.0 --port 8000" + api = 'fastapi run api/main.py --host 0.0.0.0 --port 8000' [http_service] internal_port = 8000 force_https = true - auto_stop_machines = true + auto_stop_machines = 'stop' auto_start_machines = true min_machines_running = 1 - processes = ["api"] + processes = ['api'] + [http_service.concurrency] - type = "requests" + type = 'requests' hard_limit = 250 soft_limit = 200 + +[[vm]] + memory = '1gb' + cpu_kind = 'shared' + cpus = 1