Skip to content

Commit

Permalink
build: switch from mamba to Poetry
Browse files Browse the repository at this point in the history
use psycopg2-binary for dev and psycopg2 for prod
  • Loading branch information
matthiasschaub committed Aug 21, 2024
1 parent 7f18d5b commit 4ec4069
Show file tree
Hide file tree
Showing 7 changed files with 473 additions and 343 deletions.
52 changes: 25 additions & 27 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,43 @@ FROM node:16-slim AS bundler
COPY package.json package.json
COPY esbuild.js esbuild.js
COPY client-src/ client-src/

RUN npm install
RUN mkdir -p /sketch_map_tool/static/bundles
RUN npm run build


FROM condaforge/mambaforge:23.3.1-0
# HTTP request timeout. Default is 30 seconds.
ENV POETRY_REQUESTS_TIMEOUT=60
FROM ubuntu:24.04

RUN apt-get update \
&& apt-get install -y --no-upgrade \
libgl1 \
RUN apt update \
&& apt install -y --no-upgrade \
python3 \
python3-pip \
python3-gdal \
git \
libgdal-dev \
libpq-dev \
libzbar0 \
libfreetype6-dev \
&& rm -rf /var/lib/apt/lists/*
# within docker container: run without root privileges
RUN useradd -md /home/smt smt
WORKDIR /opt/smt
RUN chown smt:smt . -R
USER smt:smt

COPY --chown=smt:smt environment.yml environment.yml
RUN mamba env create --file environment.yml
ENV POETRY_NO_INTERACTION=1 \
POETRY_REQUESTS_TIMEOUT=600

# make RUN commands use the new environment:
SHELL ["mamba", "run", "--no-capture-output", "--name", "smt", "/bin/bash", "-c"]
COPY pyproject.toml poetry.lock .

COPY --chown=smt:smt pyproject.toml pyproject.toml
COPY --chown=smt:smt poetry.lock poetry.lock
RUN which python
RUN python -m poetry install --no-ansi --no-interaction --no-root
RUN python3 -m pip install --break-system-packages poetry setuptools \
&& python3 -m poetry install --only main --no-root --no-directory

COPY --chown=smt:smt sketch_map_tool sketch_map_tool
COPY --chown=smt:smt data/ data/
COPY --chown=smt:smt config/ config/
RUN python -m poetry install --no-ansi --no-interaction
COPY sketch_map_tool sketch_map_tool
COPY data data
COPY config config

# Compile translations
RUN python -m poetry run pybabel compile -d sketch_map_tool/translations
RUN python3 -m poetry install --only main --no-root --no-directory \
&& python3 -m poetry run python -m pip install \
gdal[numpy]=="$(gdal-config --version).*" \
psycopg \
&& python3 -m poetry run pybabel compile -d sketch_map_tool/translations

# get JS dependencies
COPY --from=bundler --chown=smt:smt /sketch_map_tool/static/bundles sketch_map_tool/static/bundles

# use entry-points defined in docker-compose file
15 changes: 3 additions & 12 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ services:
- "127.0.0.1:8081:8080"
entrypoint:
[
"mamba",
"poetry",
"run",
"--no-capture-output",
"--name",
"smt",
"waitress-serve",
"sketch_map_tool.routes:app",
]
Expand All @@ -40,11 +37,8 @@ services:
memory: 8G
entrypoint:
[
"mamba",
"poetry",
"run",
"--no-capture-output",
"--name",
"smt",
"celery",
"--app",
"sketch_map_tool.tasks",
Expand Down Expand Up @@ -72,11 +66,8 @@ services:
- celery
entrypoint:
[
"mamba",
"poetry",
"run",
"--no-capture-output",
"--name",
"smt",
"celery",
"--app",
"sketch_map_tool.tasks",
Expand Down
72 changes: 40 additions & 32 deletions docs/development-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,71 @@ For contributing to this project please also read the [Contribution Guideline](/
## Prerequisites (Requirements)

- Python: `>=3.11`
- [Mamba](https://github.com/conda-forge/miniforge#install): `>=1.4`
- Poetry
- Node: `>=14`
- NPM
- git
- GDAL
- freetype *(dependency of reportlab for creating PDFs)*
- zbar *(dependency of pyzbar for reading QR-codes)*

This project uses [Mamba](https://github.com/conda-forge/miniforge#install) for environment and dependencies management.
Please make sure it is installed on your system: [Installation Guide](https://github.com/conda-forge/miniforge#install).
Instead of Mamba, Conda can also be used.
This project uses [Poetry]() and [NPM]() for environment and dependencies management.

> Actually, Mamba and Poetry together are used to manage environment and dependencies.
> But only Mamba is required to be present on the system.
> Poetry will be installed by Mamba.
> Mamba installs pre-built binaries for dependencies like GDAL.
> Poetry installs the rest of the Python dependencies.
```bash
# Mac OS X:
# Make sure to have Python (and pip) and Node (and npm) installed
brew install \
pipx \
git \
gdal \
freetype \
zbar
pipx install poetry

# Debian/Ubuntu
sudo apt install \
python3 \
python3-pip \
python3-gdal \
pipx \
node \
npm \
git \
libgdal-dev \
libfreetype6-dev \
libzbar0
pipx install poetry
```

## Installation

### Python Package

> Note: Editors like Visual Studio Code or PyCharm (IDEA) will try to automatically setup Sketch Map Tool.
> They will fail if they try to create a virtual environment managed by Poetry.
> If this happens remove the environment (`poetry env remove 3.11`).
> Then execute steps below. Please see also the section on [Setup in an IDE](#Setup-in-an-IDE).
> Note: For setup on a Apple Mac with Mx chips please have a look at
> [this section](#Setup-on-an-Apple-Mac-with-M2-chip) first.
```bash
# clone repository
git clone https://github.com/GIScience/sketch-map-tool.git
cd sketch-map-tool

# setup environment and install package
mamba env create --file environment.yml

mamba activate smt
poetry install # poetry installs directly into activated mamba environment

# install git commit hooks
poetry install
poetry shell
pip install gdal[numpy]=="$(gdal-config --version).*"
pre-commit install

# fetch and run backend (postgres) and broker (redis) using docker
docker run --name smt-postgres -d -p 5432:5432 -e POSTGRES_PASSWORD=smt -e POSTGRES_USER=smt postgres:15
docker run --name smt-redis -d -p 6379:6379 redis:7

# compile languages:
pybabel compile -d sketch_map_tool/translations

# install local versions of esbuild, eslint and stylelint to build and check JS and CSS
npm install
npm run build # build/bundle JS and CSS
npm run build
```

Note: When dependencies changed the environment can be updated by running:
### Postgres (Database and Result Store) and Redis (Message Broker)

```bash
mamba activate smt
mamba env update --file environment.yml
poetry install
# fetch and run backend (postgres) and broker (redis) using docker
docker run --name smt-postgres -d -p 5432:5432 -e POSTGRES_PASSWORD=smt -e POSTGRES_USER=smt postgres:15
docker run --name smt-redis -d -p 6379:6379 redis:7
```

## Configuration
Expand Down
14 changes: 0 additions & 14 deletions environment.yml

This file was deleted.

Loading

0 comments on commit 4ec4069

Please sign in to comment.