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

build: update backend dependency management to pdm + dockerfile #5184

Merged
merged 24 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
027532a
build: update backend dependency management to pdm + dockerfile
spwoodcock Jun 15, 2022
bac87f8
build: update legacy dockerfiles to work without requirements.txt
spwoodcock Jun 16, 2022
0df5bff
refactor: update references to requirements.txt
spwoodcock Jun 16, 2022
e94c4c0
docs: update references to requirements.txt
spwoodcock Jun 16, 2022
15f5f2a
docs: use pdm for dep install during pr review
spwoodcock Jun 16, 2022
9c13552
update additional dockerfiles to reflect pdm dep management
spwoodcock Jun 16, 2022
5b186e3
docs: update docs for dependency install, ensure python installed
spwoodcock Jun 21, 2022
d281fde
build: add flask-mail dependency, update max python version to pinned
spwoodcock Jul 21, 2022
ff3ff3f
docs: update docs to reflect recent breaking changes to pdm (venv def…
spwoodcock Jul 21, 2022
4ea8910
fix: update dockerfiles to reflect breaking pdm changes (venv default)
spwoodcock Jul 21, 2022
c676f98
build: update requirements in pyproject to latest develop
spwoodcock Apr 21, 2023
dc6d91c
build: update python version requires to 3.10
spwoodcock Apr 21, 2023
fc154e1
build: update pyproj 2.6.1 --> 3.5.0 (required for proj 9.x)
spwoodcock Apr 21, 2023
b3adcee
build: overhaul backend dockerfile, pdm export deps
spwoodcock Apr 21, 2023
2b02d5c
build: downgrade python 3.10 --> 3.9 until all dependencies available
spwoodcock Apr 21, 2023
9bb300f
build: fix permissions in backend dockerfile code dir
spwoodcock Apr 21, 2023
3c27a77
fix: update default LOG_DIR to appuser home dir (permissions)
spwoodcock Apr 21, 2023
6cf1057
build: change to appuser in backend dockerfile after pyc compile
spwoodcock Apr 21, 2023
3578a38
ci: circleci first pdm export requirements.txt before install
spwoodcock Apr 21, 2023
2545528
docs: add details about pdm export in backend dev setup
spwoodcock Apr 21, 2023
fd7e0cd
ci: add pdm to system during circleci dep install
spwoodcock Apr 21, 2023
f3f5502
docs: update docs for review-pr about backend dep install
spwoodcock Apr 21, 2023
2c84444
fix: comment in dockerfile causing tzlocal locale not set
spwoodcock Apr 27, 2023
3420d04
build: update schematics 2.1.0 --> 2.1.1 (last version)
spwoodcock May 10, 2023
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
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ jobs:
yarn
cd ${CIRCLE_WORKING_DIRECTORY}
# Install Python dependencies
pip install --upgrade pip
pip install --upgrade pip pdm
pdm export --prod --without-hashes > requirements.txt
pip install -r requirements.txt
- run:
name: Run backend code checks
Expand Down
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.*
*.yml
**/*.pyc
**/__pypackages__
**/node_modules
**/npm-debug.log
logs/
Expand Down
6 changes: 3 additions & 3 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- tests/**/*
- migrations/**/*
- ./manage.py
- ./requirements.txt
- ./pyproject.toml
"Component: Infrastructure":
- .circleci/*
- .github/**/*
Expand All @@ -15,10 +15,10 @@
- docker-*.yml
"dependencies":
- frontend/yarn.lock
- ./requirements.txt
- ./pyproject.toml
"javascript":
- frontend/yarn.lock
"python":
- ./requirements.txt
- ./pyproject.toml
"Type: Translations":
- frontend/src/locales/*
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ logs/
#python
venv/
*pyc
.pdm.toml
__pypackages__/

# Editor generated files #
.idea/
Expand Down
88 changes: 68 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
FROM quay.io/hotosm/base-python-image as base
LABEL version=0.1
LABEL maintainer="HOT Sysadmin <sysadmin@hotosm.org>"
LABEL description="Builds backend docker image"
ARG ALPINE_IMG_TAG=3.17
ARG PYTHON_IMG_TAG=3.9

WORKDIR /usr/src/app
FROM docker.io/python:${PYTHON_IMG_TAG}-alpine${ALPINE_IMG_TAG} as base
eternaltyro marked this conversation as resolved.
Show resolved Hide resolved
ARG APP_VERSION=0.1.0
ARG DOCKERFILE_VERSION=0.4.0
ARG ALPINE_IMG_TAG
ARG PYTHON_IMG_TAG
ARG MAINTAINER=sysadmin@hotosm.org
LABEL org.hotosm.tasks.app-version="${APP_VERSION}" \
eternaltyro marked this conversation as resolved.
Show resolved Hide resolved
org.hotosm.tasks.alpine-img-tag="${ALPINE_IMG_TAG}" \
org.hotosm.tasks.python-img-tag="${PYTHON_IMG_TAG}" \
org.hotosm.tasks.dockerfile-version="${DOCKERFILE_VERSION}" \
org.hotosm.tasks.maintainer="${MAINTAINER}" \
org.hotosm.tasks.api-port="5000"
# Fix timezone (do not change - see issue #3638)
ENV TZ UTC



FROM base as extract-deps
WORKDIR /opt/python
COPY pyproject.toml pdm.lock README.md /opt/python/
RUN pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir pdm==2.5.3
RUN pdm export --prod --without-hashes > requirements.txt

FROM base as builder


FROM base as build
WORKDIR /opt/python
# Setup backend build-time dependencies
RUN apk update && \
apk add \
Expand All @@ -20,29 +42,55 @@ RUN apk update && \
proj-util \
proj-dev \
make
# Setup backend Python dependencies
COPY --from=extract-deps \
/opt/python/requirements.txt /opt/python/
RUN pip install --user --no-warn-script-location \
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm assuming --no-warn-script-location because we are installing from /opt/python/requirements.txt which is not in PYPATH

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 decided on that flag so long ago now I have forgotten why 😄 Presumably it's to suppress the warning, but I can't seem to reproduce a warning on the python:3.9 image, so perhaps it's been updated since.

--no-cache-dir -r /opt/python/requirements.txt

COPY requirements.txt .

RUN pip install \
--prefix=/install \
--no-cache-dir \
--no-warn-script-location \
-r requirements.txt

FROM base as runtime
ARG PYTHON_IMG_TAG
WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONFAULTHANDLER=1 \
PATH="/home/appuser/.local/bin:$PATH" \
PYTHON_LIB="/home/appuser/.local/lib/python$PYTHON_IMG_TAG/site-packages" \
eternaltyro marked this conversation as resolved.
Show resolved Hide resolved
SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt \
REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
# Setup backend runtime dependencies
FROM base

RUN apk update && \
apk add \
apk add --no-cache \
postgresql-libs geos proj-util

COPY --from=builder /install /usr/local
COPY --from=build \
/root/.local \
/home/appuser/.local
eternaltyro marked this conversation as resolved.
Show resolved Hide resolved
COPY backend backend/
COPY migrations migrations/
COPY scripts/world scripts/world/
COPY scripts/database scripts/database/
COPY manage.py .
# Add non-root user, permissions, init log dir
RUN adduser -D -u 900 -h /home/appuser -s /bin/false appuser \
&& chown -R appuser:appuser /usr/src /home/appuser



FROM runtime as debug
RUN pip install --no-warn-script-location \
--no-cache-dir debugpy==1.6.7
USER appuser
CMD ["python", "-m", "debugpy", "--wait-for-client", "--listen", "0.0.0.0:5678", \
"-m", "gunicorn", "-c", "python:backend.gunicorn", "manage:application", \
"--reload", "--log-level", "error"]



ENV TZ UTC # Fix timezone (do not change - see issue #3638)
EXPOSE 5000
CMD ["gunicorn", "-c", "python:backend.gunicorn", "manage:application"]
FROM runtime as prod
# Pre-compile packages to .pyc (init speed gains)
RUN python -c "import compileall; compileall.compile_path(maxlevels=10, quiet=1)"
USER appuser
CMD ["gunicorn", "-c", "python:backend.gunicorn", "manage:application", \
"--workers", "1", "--log-level", "error"]
2 changes: 1 addition & 1 deletion backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class EnvironmentConfig:

# Logging settings
LOG_LEVEL = os.getenv("TM_LOG_LEVEL", logging.DEBUG)
LOG_DIR = os.getenv("TM_LOG_DIR", "logs")
LOG_DIR = os.getenv("TM_LOG_DIR", "/home/appuser/logs")

# Mapper Level values represent number of OSM changesets
MAPPER_LEVEL_INTERMEDIATE = int(os.getenv("TM_MAPPER_LEVEL_INTERMEDIATE", 250))
Expand Down
2 changes: 1 addition & 1 deletion docs-old/contributing-translation.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Everybody is welcome to support translations through the Transifex website.
## Developers

For developers, Transifex offers a [CLI client](https://docs.transifex.com/client/introduction/) and the Tasking
Manager offers commands to interact with it. The client is already included in `requirements.txt` so you should have the Transifex commands installed once you have set up your backend side code.
Manager offers commands to interact with it. The client is already included in the Python requirements so you should have the Transifex commands installed once you have set up your backend side code.

The Tasking Manager is using Angular Translate to display the translated strings. It works with key/value pairs in .json format, which is also the format used to store the translations in Transifex.

Expand Down
21 changes: 18 additions & 3 deletions docs-old/review-pr.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ and ask the person to check on the error.

1. Test behaviour and edge cases
Install the PR on your local setup, make sure you run
a. backend dependency installation: `pip install -r requirements.txt`
a. backend dependency installation:
* Linux/Mac (Option 1: pep582):
* First ensure the Python version in `pyproject.toml:requires-python` is installed on your system.
* ```pip install --upgrade pdm```
* ```pdm config --global python.use_venv False```
* ```pdm --pep582 >> ~/.bash_profile```
* ```source ~/.bash_profile```
* ```pdm install```
* Linux/Mac (Option 2: pip (system/venv)):
* ```pip install --upgrade pdm```
* ```pdm export --without-hashes > requirements.txt```
* ```pip install requirements.txt```
b. introduced database migrations: `python manage.py db upgrade`
c. frontend dependency installation: `cd frontend && yarn && cd ..`
d. rebuild the frontend: `cd frontend && yarn build && cd ..`
Expand Down Expand Up @@ -100,7 +111,11 @@ git pull https://github.com/author/tasking-manager.git branchname

Make sure that python dependencies are up to date:

`pip install -r requirements.txt`
* Option 1:
* ```pdm install```
* Option 2:
* ```pdm export --without-hashes > requirements.txt```
* ```pip install requirements.txt```

### Frontend

Expand All @@ -127,7 +142,7 @@ If you get an error, you may have an upgraded database from a prior PR. Try down

I have found it better to run tests on a separate database from the live version, but the choice is up to you. Again, it is probably wise to back up your database first if you choose to run it on your main database.

`venv/bin/ ./tests --with-xunit --xunit-file unitresults.xml --with-coverage --cover-erase --cover-package=./backend`
`python3 -m unittest discover ./tests --with-xunit --xunit-file unitresults.xml --with-coverage --cover-erase --cover-package=./backend`

### Check changes

Expand Down
20 changes: 13 additions & 7 deletions docs-old/setup-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,18 @@ There are two ways to configure Tasking Manager. You can set some environment va

#### Build

* Create a Python Virtual Environment, using Python 3.6+:
* ```python3 -m venv ./venv```
* Activate your virtual environment and install dependencies:
* Linux/Mac:
* ```. ./venv/bin/activate```
* ```pip install -r requirements.txt```
* Install project dependencies:
* Linux/Mac (Option 1: pep582):
* First ensure the Python version in `pyproject.toml:requires-python` is installed on your system.
* ```pip install --upgrade pdm```
* ```pdm config --global python.use_venv False```
* ```pdm --pep582 >> ~/.bash_profile```
* ```source ~/.bash_profile```
* ```pdm install```
* Linux/Mac (Option 2: pip (system/venv)):
* ```pip install --upgrade pdm```
* ```pdm export --without-hashes > requirements.txt```
* ```pip install requirements.txt```

#### Tests

Expand Down Expand Up @@ -149,7 +155,7 @@ In order to authenticate on the API, you need to have an Authorization Token.
1. Run the command line `manage.py` with the `gen_token` option and `-u <OSM_User_ID_number>`. The command line can be run in any shell session as long as you are in the tasking-manager directory.

```
venv/bin/python manage.py gen_token -u 99999999
python manage.py gen_token -u 99999999
```

This will generate a line that looks like this:
Expand Down
Loading