Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adopt poetry #1172

Merged
merged 10 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ jobs:
with:
fetch-depth: 0

- name: Install poetry
run: pipx install poetry
mattwang44 marked this conversation as resolved.
Show resolved Hide resolved

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'pip'
cache: 'poetry'

- name: Set up Python dependencies
run: |
pip install -U pip
pip install -r requirements/dev.txt
poetry install

- uses: actions/setup-node@v3
with:
Expand All @@ -51,16 +53,16 @@ jobs:

- name: Run Flake8 on src
run: |
flake8 src
poetry run flake8 src

- name: Run DB migration for test
run: |
python src/manage.py migrate --verbosity 1 --noinput
poetry run python src/manage.py migrate --verbosity 1 --noinput

- name: Run Test
run: |
cd src
pytest -n 2 --cov=. --cov-report=term-missing --cov-report=xml
poetry run pytest -n 2 --cov=. --cov-report=term-missing --cov-report=xml

- name: Run Codecov action
uses: codecov/codecov-action@v4
Expand Down
27 changes: 20 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ RUN yarn install --dev --frozen-lockfile \
# [Python Stage for Django web server]
FROM python:3.10.14-slim-bullseye as python_stage

ENV PYTHONUNBUFFERED 1
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1

ENV BASE_DIR /usr/local
ENV APP_DIR $BASE_DIR/app

Expand Down Expand Up @@ -47,17 +54,23 @@ RUN adduser --system --disabled-login docker \
&& mkdir -p "$BASE_DIR" "$APP_DIR" "$APP_DIR/src/assets" "$APP_DIR/src/media" \
&& chown -R docker:nogroup "$BASE_DIR" "$APP_DIR"

USER docker
# Install Poetry
josix marked this conversation as resolved.
Show resolved Hide resolved
RUN pip install --no-cache-dir pip==23.3.2 && \
pip install --no-cache-dir poetry==1.8.2

# Install Python dependencies (only main dependencies)
COPY --chown=docker:nogroup pyproject.toml poetry.lock ./
RUN poetry install --no-root && \
yes | poetry cache clear --all pypi

# Only copy and install requirements to improve caching between builds
# Install Python dependencies
COPY --chown=docker:nogroup ./requirements $APP_DIR/requirements
RUN pip3 install --user -r "$APP_DIR/requirements/production.txt" \
&& rm -rf $HOME/.cache/pip/*
# Add poetry bin directory to PATH
ENV PATH="${WORKDIR}/.venv/bin:$PATH"

# Finally, copy all the project files along with source files
COPY --chown=docker:nogroup ./ $APP_DIR

USER docker

WORKDIR $APP_DIR/src
VOLUME $APP_DIR/src/media
EXPOSE 8000
Expand Down
25 changes: 17 additions & 8 deletions dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ FROM python:3.10.14-slim-bullseye as python_stage

WORKDIR /app

ENV PYTHONUNBUFFERED 1
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1

# Infrastructure tools
# gettext is used for django to compile .po to .mo files.
Expand All @@ -30,14 +36,17 @@ RUN apt-get install -y \
libxml2-dev \
libxslt-dev

# Only copy and install requirements to improve caching between builds
# Install Poetry
josix marked this conversation as resolved.
Show resolved Hide resolved
RUN pip install --no-cache-dir pip==23.3.2 && \
pip install --no-cache-dir poetry==1.8.2

# Install Python dependencies
COPY ./requirements ./requirements
RUN pip3 install -r ./requirements/dev.txt
COPY pyproject.toml poetry.lock ./
RUN poetry install --no-root && \
yes | poetry cache clear --all pypi

# Add poetry bin directory to PATH
ENV PATH="${WORKDIR}/.venv/bin:$PATH"

COPY --from=node_stage /node_modules ./node_modules
COPY --from=node_stage /usr/local/bin/node /usr/local/bin/node

# for entry point
COPY ./docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
4 changes: 2 additions & 2 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ services:
- DJANGO_SUPERUSER_USERNAME=admin
- DJANGO_SUPERUSER_PASSWORD=1234
- [email protected]
entrypoint: /docker-entrypoint.sh
command: python /app/src/manage.py runserver 0.0.0.0:8000
josix marked this conversation as resolved.
Show resolved Hide resolved
working_dir: /app/src
command: tail -f /dev/null
8 changes: 0 additions & 8 deletions docker-entrypoint.sh

This file was deleted.

24 changes: 23 additions & 1 deletion document/deploy_docker_dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,30 @@
DATABASE_URL=postgres://postgres:secretpostgres@db:5432/pycontw2016
```

2. Simply run the following command to install all dependencies, activate a containerized Postgres server, and run the Django server (<code>ctrl+c</code> to stop).
2. Simply run the following command to install all dependencies, activate a containerized Postgres server, and enter into a poetry shell inside the application container (<code>ctrl+c</code> to quit).

```
./enter_dev_env.sh
```

3. In the shell, you can run any commands as if you are in a local development environment. Here are some common Django commands:

```sh
# make migrations
python manage.py makemigrations

# apply migrations
python manage.py migrate

# create a superuser
python manage.py createsuperuser

# pull out strings for translations
python manage.py makemessages -l en_US -l zh_Hant

# compile translations
python manage.py compilemessages

# run the dev server (you can access the site at http://localhost:8000/)
python manage.py runserver 0.0.0.0:8000
```
14 changes: 4 additions & 10 deletions document/deploy_local_env_dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,15 @@ Create and start the database for development:

docker-compose -f docker-compose-db.yml up

#### Python - Built-in `venv`
#### Python - Poetry

Create your virtual environment:

python3 -m venv venv
poetry env use 3.10

And enable it:

. venv/bin/activate

#### Python - [virtualenvwrapper](https://virtualenvwrapper.readthedocs.org)

You need to specify your python path when creating the virtual environment:

mkvirtualenv --python=$(which python3) pycontw2016
poetry shell

#### Node.js - [nvm](https://github.com/creationix/nvm)

Expand All @@ -45,7 +39,7 @@ Switch to version specified in `.nvmrc`:

Use pip to install Python depedencies:

pip install -r requirements.txt
poetry install

Use Yarn to install Node dependencies:

Expand Down
5 changes: 2 additions & 3 deletions enter_dev_env.sh
Copy link
Collaborator

Choose a reason for hiding this comment

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

just being picky: for some users using Mac with chip M1 as me, L12 will have to revise to
"export DOCKER_DEFAULT_PLATFORM=linux/arm64/v8"
manually. Maybe note this at quotes inside or readme?

Copy link
Member Author

Choose a reason for hiding this comment

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

i use M1 and I've never needed to change these before. I believe it's another issue that we need to dig in. Let's open another ticket/issue for this, shall we?

Copy link
Member Author

@mattwang44 mattwang44 Apr 12, 2024

Choose a reason for hiding this comment

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

P.S. I followed this instruction to install x86 versions of homebrew and have my terminal opened with rosetta by default, and I also followed this instruction to properly setup terminal profiles in VSCode. I believe these settings have saved me from a bunch of system issues on M1.

Copy link
Collaborator

Choose a reason for hiding this comment

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

good idea. I will move the further discussion at #1175

Copy link
Member Author

Choose a reason for hiding this comment

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

Thx!

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/bash -xe
CONTAINER="${USER}_pycontw_vm"
COMPOSE_FILE="./docker-compose-dev.yml"
START_SHELL="sh"

# test if the container is running
HASH=`docker ps -q -f name=$CONTAINER`
Expand All @@ -16,14 +15,14 @@ fi
if [ -n "$HASH" ];then
echo "found existing running container $CONTAINER, proceeding to exec another shell"
docker-compose -f $COMPOSE_FILE restart
docker exec -it $HASH $START_SHELL
docker exec -w /app/src -it $HASH bash -c "SHELL=bash poetry shell"
josix marked this conversation as resolved.
Show resolved Hide resolved
elif [ -n "$HASH_STOPPED" ];then
echo "found existing stopped container $CONTAINER, starting"
docker-compose -f $COMPOSE_FILE restart
docker start --attach -i $HASH_STOPPED
else
echo "existing container not found, creating a new one, named $CONTAINER"
docker-compose -f $COMPOSE_FILE pull
docker-compose -f $COMPOSE_FILE run -p 8000:8000 --name=$CONTAINER pycontw
docker-compose -f $COMPOSE_FILE run -p 8000:8000 --name=$CONTAINER pycontw bash -c "SHELL=bash poetry shell"
fi
echo "see you, use 'docker rm $CONTAINER' to kill the dev container or 'docker-compose -f $COMPOSE_FILE down' to kill both the postgres and the dev container if you want a fresh env next time"
Loading
Loading