Skip to content

Commit

Permalink
feat: Migrate to poetry (#2214)
Browse files Browse the repository at this point in the history
Co-authored-by: Kim Gustyr <khvn26@gmail.com>
  • Loading branch information
tushar5526 and khvn26 authored Aug 7, 2023
1 parent 7a15101 commit 0754071
Show file tree
Hide file tree
Showing 11 changed files with 4,060 additions and 689 deletions.
29 changes: 10 additions & 19 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,21 @@ ENV ENV=prod
ENV STATIC_ASSET_CDN_URL=/static/
RUN cd frontend && npm run bundledjango


# Step 2 - Build Python virtualenv
FROM python:3.11 as build-python
WORKDIR /app

RUN apt-get update && apt-get install -y gcc build-essential libpq-dev musl-dev python3-dev

# Set up venv
RUN python -m venv /opt/venv
# Make sure we use the virtualenv:
ENV PATH="/opt/venv/bin:$PATH"

COPY api/requirements.txt .
COPY api/pyproject.toml api/poetry.lock api/Makefile ./
ARG POETRY_VIRTUALENVS_CREATE=false
RUN make install-poetry
ENV PATH="$PATH:/root/.local/bin"

# Make sure we are running latest pip and setuptools to avoid potential security warnings
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools

# Install our python dependencies
RUN make generate-requirements-file opts="main"
RUN pip install -r requirements.txt


# Step 3 - Build Django Application
FROM python:3.11-slim as application

WORKDIR /app
COPY api /app/

# Install SAML dependency if required
ARG SAML_INSTALLED="0"
Expand All @@ -47,8 +35,11 @@ ARG TARGETARCH
RUN if [ "${TARGETARCH}" != "amd64" ]; then apt-get update && apt-get install -y libpq-dev && rm -rf /var/lib/apt/lists/*; fi;

# Copy the python venv from step 2
COPY --from=build-python /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
COPY --from=build-python /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
# Copy the bin folder as well to copy the executables created in package installation
COPY --from=build-python /usr/local/bin /usr/local/bin

COPY api /app/

# Compile static Django assets
RUN python /app/manage.py collectstatic --no-input
Expand Down
28 changes: 11 additions & 17 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@

# Step 1 - Build Python virtualenv
FROM python:3.11 as build-python
WORKDIR /app

RUN apt-get update && apt-get install -y gcc build-essential libpq-dev musl-dev python3-dev

# Set up venv
RUN python -m venv /opt/venv
# Make sure we use the virtualenv:
ENV PATH="/opt/venv/bin:$PATH"
COPY pyproject.toml poetry.lock Makefile ./

COPY requirements.txt .
ARG POETRY_VIRTUALENVS_CREATE=false
RUN make install-poetry
ENV PATH="$PATH:/root/.local/bin"

# Make sure we are running latest pip and setuptools to avoid potential security warnings
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools

# Install our python dependencies
RUN make generate-requirements-file opts="main"
RUN pip install -r requirements.txt


# Step 2 - Build Django Application
FROM python:3.11-slim as application

WORKDIR /app
COPY . .

# Install SAML dependency if required
ARG SAML_INSTALLED="0"
Expand All @@ -34,8 +25,11 @@ ARG TARGETARCH
RUN if [ "${TARGETARCH}" != "amd64" ]; then apt-get update && apt-get install -y libpq-dev && rm -rf /var/lib/apt/lists/*; fi;

# Copy the python venv from step 2
COPY --from=build-python /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
COPY --from=build-python /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
# Copy the bin folder as well to copy the executables created in package installation
COPY --from=build-python /usr/local/bin /usr/local/bin

COPY . .

# Compile static Django assets
RUN python manage.py collectstatic --no-input
Expand Down
7 changes: 6 additions & 1 deletion api/Dockerfile.redhat-ubi
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ USER root
RUN yum -y update-minimal --security --sec-severity=Important --sec-severity=Critical
USER 1001

ADD --chown=1001:0 requirements.txt .
ARG POETRY_VIRTUALENVS_CREATE=false
ADD --chown=1001:0 pyproject.toml poetry.lock Makefile .
RUN make generate-requirements-file opts="main"
RUN pip install -r requirements.txt
ENV PATH="$PATH:/root/.local/bin"
RUN make install-packages opts="--no-root --only main"

ADD --chown=1001:0 docker/ bin/
ADD --chown=1001:0 . src/

Expand Down
34 changes: 22 additions & 12 deletions api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,41 @@ COMPOSE_PROJECT_NAME ?= flagsmith

DOTENV_OVERRIDE_FILE ?= .env

POETRY_VERSION ?= 1.5.0

-include .env-local
-include $(DOTENV_OVERRIDE_FILE)

.PHONY: install-pip
install-pip:
python -m pip install --upgrade pip

.PHONY: install-poetry
install-poetry:
curl -sSL https://install.python-poetry.org | python3 - --version ${POETRY_VERSION}

.PHONY: install-packages
install-packages:
pip install -r requirements-all.txt
poetry install $(opts)

.PHONY: install
install: install-pip install-packages
install: install-pip install-poetry install-packages

.PHONY: generate-requirements-file
generate-requirements-file:
poetry export -f requirements.txt --output requirements.txt --only $(opts)

.PHONY: lint-black
lint-black:
black --check .
poetry run black --check .

.PHONY: lint-isort
lint-isort:
isort --check-only --diff .
poetry run isort --check-only --diff .

.PHONY: lint-flake8
lint-flake8:
flake8
poetry run flake8

.PHONY: lint
lint: lint-black lint-isort lint-flake8
Expand All @@ -58,22 +68,22 @@ docker-build:

.PHONY: test
test:
pytest $(opts)
poetry run pytest $(opts)

.PHONY: django-make-migrations
django-make-migrations:
python manage.py waitfordb
python manage.py makemigrations $(opts)
poetry run python manage.py waitfordb
poetry run python manage.py makemigrations $(opts)

.PHONY: django-migrate
django-migrate:
python manage.py waitfordb
python manage.py migrate
poetry run python manage.py waitfordb
poetry run python manage.py migrate

.PHONY: django-collect-static
django-collect-static:
python manage.py collectstatic --noinput
poetry run python manage.py collectstatic --noinput

.PHONY: serve
serve:
gunicorn --bind 0.0.0.0:8000 app.wsgi --reload
poetry run gunicorn --bind 0.0.0.0:8000 app.wsgi --reload
Loading

3 comments on commit 0754071

@vercel
Copy link

@vercel vercel bot commented on 0754071 Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 0754071 Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./docs

docs-git-main-flagsmith.vercel.app
docs.bullet-train.io
docs-flagsmith.vercel.app
docs.flagsmith.com

@vercel
Copy link

@vercel vercel bot commented on 0754071 Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.