Skip to content

Commit 7e13aa9

Browse files
Merge pull request #298 from mapswipe/dev
Stuctural changes to the configuration and data path handling.
2 parents a1addf7 + dfe55bc commit 7e13aa9

File tree

15 files changed

+195
-155
lines changed

15 files changed

+195
-155
lines changed

README.md

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,48 @@ Please refer to the documentation for more information: https://mapswipe-workers
1515

1616
## Ressources
1717

18-
- GitHub repository of the MapSwipe Back-End: https://github.com/mapswipe/python-mapswipe-workers
19-
- GitHub repository of the MapSwipe Website: https://github.com/mapswipe/mapswipe.github.io
20-
- GitHub repository of the MapSwipe App https://github.com/mapswipe/mapswipe
21-
- Website of MapSwipe: https://mapswipe.org
22-
- OSM-Wiki page of MapSwipe: https://wiki.openstreetmap.org/wiki/MapSwipe
18+
- MapSwipe Back-End: https://github.com/mapswipe/python-mapswipe-workers
19+
- MapSwipe App https://github.com/mapswipe/mapswipe
20+
- MapSwipe Website: https://mapswipe.org
21+
- MapSwipe OSM-Wiki: https://wiki.openstreetmap.org/wiki/MapSwipe
22+
23+
24+
## Contributing Guidelines
25+
26+
### Feature Branch
27+
28+
To contribute to the MapSwipe back-end please create dedicated feature branches based on the `dev` branch. After the changes create a Pull Request of the `feature` branch into the `dev` branch on GitHub:
29+
30+
```bash
31+
git checkout dev
32+
git checkout -b featureA
33+
# Hack away ...
34+
git commit -am 'Describe changes.'
35+
git push -u origin featureA
36+
# Create a Pull Request from feature branch into the dev branch on GitHub.
37+
```
38+
39+
> Note: If a bug in production (master branch) needs fixing before a new versions of MapSwipe Workers gets released (merging dev into master branch), a hotfix branch should be created. In the hotfix branch the bug should be fixed and then merged with the master branch (and also dev).
40+
41+
42+
### Style Guide
43+
44+
This project uses [black](https://github.com/psf/black) and [flake8](https://gitlab.com/pycqa/flake8) to achieve a unified style.
45+
46+
Use [pre-commit](https://pre-commit.com/) to run `black` and `flake8` prior to any git commit. `pre-commit`, `black` and `flake8` should already be installed in your virtual environment since they are listed in `requirements.txt`. To setup pre-commit simply run:
47+
48+
```
49+
pre-commit install
50+
```
51+
52+
From now on `black` and `flake8` should run automatically whenever `git commit` is executed.
2353

2454

2555
## License
2656

2757
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details
2858

59+
2960
## Authors
3061

3162
* **Benjamin Herfort** - HeiGIT - [Hagellach37](https://github.com/Hagellach37)
@@ -34,6 +65,7 @@ This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENS
3465

3566
See also the list of [contributors](contributors.md) who participated in this project.
3667

68+
3769
## Acknowledgements
3870

3971
* Humanitarian organizations can't help people if they can't find them.

docker-compose.yaml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ services:
3030
build:
3131
context: api/
3232
volumes:
33-
- ./api-data:/usr/share/nginx/html/api/:ro
33+
- ./mapswipe-data/api:/usr/share/nginx/html/api/:ro
3434
restart: unless-stopped
3535
expose:
3636
- "80"
@@ -55,15 +55,7 @@ services:
5555
- postgres
5656
command: mapswipe_workers --verbose run --schedule
5757
volumes:
58-
- ./api-data:/var/lib/mapswipe_workers/api-data/:rw
59-
- ./api-data/agg_results:/var/lib/mapswipe_workers/api-data/agg_results:rw
60-
- ./api-data/groups:/var/lib/mapswipe_workers/api-data/groups:rw
61-
- ./api-data/history:/var/lib/mapswipe_workers/api-data/history:rw
62-
- ./api-data/projects:/var/lib/mapswipe_workers/api-data/projects:rw
63-
- ./api-data/results:/var/lib/mapswipe_workers/api-data/results:rw
64-
- ./api-data/tasks:/var/lib/mapswipe_workers/api-data/tasks:rw
65-
- ./api-data/yes_maybe:/var/lib/mapswipe_workers/api-data/yes_maybe:rw
66-
- ./api-data/hot_tm:/var/lib/mapswipe_workers/api-data/hot_tm:rw
58+
- ./mapswipe-data:/root/.local/share/mapswipe_workers
6759
restart: "no"
6860
networks:
6961
- mapswipe_workers

docs/source/contributing.md

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,22 @@
11
# Contributing
22

3-
4-
## Clone from GitHub
5-
6-
```bash
7-
git clone https://github.com/mapswipe/python-mapswipe-workers.git
8-
cd python-mapswipe-workers
9-
git checkout dev
10-
```
11-
12-
13-
## Install MapSwipe Workers
14-
15-
Create a Python virtual environment and activate it. Install MapSwipe Workers using pip:
16-
17-
```bash
18-
python -m venv venv
19-
source venv/bin/activate
20-
pip install --editable .
21-
```
3+
This document describes how to setup Python MapSwipe Workers locally without using the provided Dockerfile for development purposes.
224

235

246
## Feature Branch
257

26-
To contribute to the MapSwipe back-end please create dedicated feature branches from dev:
8+
To contribute to the MapSwipe back-end please create dedicated feature branches based on the `dev` branch:
279

2810
```bash
2911
git checkout dev
3012
git checkout -b featureA
31-
git commit -am 'add new project type'
13+
# Hack away ...
14+
git commit -am 'Describe changes.'
3215
git push -u origin featureA
33-
git request-pull origin/dev featureA
16+
# Create a Pull Request from feature branch into the dev branch on GitHub.
3417
```
3518

36-
> Note: If a bug in production (master branch) needs fixing before a new versions of MapSwipe Workers gets released (merging dev into master branch), a hotfix branch should be created. In the hotfix branch the bug should be fixed and then merged back with master and also dev.
19+
> Note: If a bug in production (master branch) needs fixing before a new versions of MapSwipe Workers gets released (merging dev into master branch), a hotfix branch should be created. In the hotfix branch the bug should be fixed and then merged with the master branch (and also dev).
3720
3821

3922
## Style Guide

docs/source/dev_setup.md

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,69 @@
11
# Development Setup
22

3-
In this document some tips and workflows for development are loosely collected. Those are independend of the production setup and Docker. A working Firebase Project (including Firebase Functions and Database Rules) is presupposed.
3+
In this document some tips and workflows for development are loosely collected. Those are independend of the production setup using Docker. A working Firebase Project (including Firebase Functions and Database Rules) is presupposed.
44

55

6-
1. Install GDAL (python-gdal)
7-
2. Setup virtual environment with system-site-packages enabled (To get access to GDAL)
8-
- `python3 -m venv --system-site-packages venv`
9-
3. Activate virtual environment
10-
- `source venv/bin/activate`
11-
4. Install mapswipe_workers
12-
- `pip install -e .`
13-
5. Configure MapSwipe Workers as described in the section 'MapSwipe Workers Setup' in the [Setup](setup.md) chapter
14-
- Make sure configuration and data paths are created and permissions are set as described in the next section [Configuration and data path](#Configurationa_and_data_path).
15-
- Move the configuration (`config/configuration.json`) and the Service Account Key (`config/serviceAccountKey.json`) to `/usr/share/config/mapswipe_workers/`
16-
6. Setup a postgres instance
17-
- Use the Docker image of MapSwipe
18-
- `cd postgres/`
19-
- `docker build -t mapswipe_postgres .`
20-
- `docker run -d -p 5432:5432 --name mapswipe_postgres -e POSTGRES_DB=mapswipe -e POSTGRES_USER=mapswipe_workers -e POSTGRES_PASSWORD=your_password mapswipe_postgres`
21-
- Or set up your own using the `initdb.sql` file in the `postgres/` folder
22-
7. Run Mapswipe Workers using the command: `mapswipe_wokers`
23-
- eg. `mapswipe_wokers --help`
6+
## Installation
247

8+
### Requirements
259

26-
## Configuration and data path
10+
MapSwipe Workers requires GDAL/OGR (`gdal-bin`) and GDAL for Python (`libgdal-dev`, `python-gdal`) to be installed.
2711

28-
Mapsipe Workers needs access to three directories:
2912

30-
- CONFIG_PATH (`/usr/share/config/mapswipe_workers/`)
31-
- DATA_PATH (`/var/lib/mapswipe_workers/`)
32-
- LOG_PATH (`/var/log/mapswipe_workers/`)
13+
### Clone from GitHub
14+
15+
```bash
16+
git clone https://github.com/mapswipe/python-mapswipe-workers.git
17+
cd python-mapswipe-workers
18+
git checkout dev
19+
```
20+
21+
22+
### Configuration
23+
24+
MapSwipe Workers looks for configuration in `~/.config/mapswipe_workers`. (XDG Base Directory Specification is respected). It expects three files:
25+
26+
- `configuration.json`
27+
- `serviceAccountKey.json`
28+
- `logging.cfg`
29+
30+
Please refer to the [configuration](configuration.md) and [setup](setup.md) documentation for further details.
31+
32+
In addition the data directory for MapSwipe Workers needs to be created:
33+
`mkdir --parents .local/share/mapswipe_workers`
3334

34-
Make sure those are existing and accessible:
3535

36-
- Use `mkdir DATA_PATH` to create the directory.
37-
- Use `chown -R $USER:$USER PATH` (eg. `chown -R $USER:$USER /user/share/config/mapswipe_workers/`) to give write permission to current user.
36+
### Install MapSwipe Workers Python Package
37+
38+
1. Create a Python virtual environment with `system-site-packages` option enabled to get access to GDAL/OGR Python packages
39+
2. Activate the vitrual environment.
40+
3. Install MapSwipe Workers using pip.
41+
4. Run it.
42+
43+
```bash
44+
python -m venv --system-site-packages venv
45+
source venv/bin/activate
46+
pip install --editable .
47+
mapswipe_workers --help
48+
```
49+
50+
51+
## Postgres
52+
53+
Setup a local Postgres instance for MapSwipe Workers using the provided Dockerfile.
54+
55+
```bash
56+
cd postgres/`
57+
docker build -t mapswipe_postgres .
58+
docker run -d -p 5432:5432 --name mapswipe_postgres -e POSTGRES_DB=mapswipe -e POSTGRES_USER=mapswipe_workers -e POSTGRES_PASSWORD=your_password mapswipe_postgres
59+
```
3860

39-
Alternatively you can change the PATH variables in `definitions.py` to your desired path. Except of the path for logs. This is defined in `logging.cfg`.
61+
Or set up your own using the `initdb.sql` file in the `postgres/` folder
4062

4163

4264
## Logging
4365

44-
Mapswipe workers logs are generated using the Python logging module of the standard library (See [Official docs](https://docs.python.org/3/library/logging.html) or this [Tutorial](https://realpython.com/python-logging/#the-logging-module). The configuration file of the logging module is located at `mapswipe_workers/logging.cfg`. With this configuration a logger object is generated in the `definitions` module (`mapswipe_workers.definitions.py`) and is imported in other modules to write logs.
66+
Mapswipe workers logs are generated using the Python logging module of the standard library (See [Official docs](https://docs.python.org/3/library/logging.html) or this [Tutorial](https://realpython.com/python-logging/#the-logging-module). The configuration file of the logging module is located at `~/.config/mapswipe_workers/logging.cfg`. With this configuration a logger object is generated in the `definitions` module (`mapswipe_workers/definitions.py`) and is imported by other modules for writing logs.
4567

4668
```python
4769
from mapswipe_workers.definitions import logger
@@ -50,12 +72,12 @@ logger.waring('warning')
5072
5173
# Include stack trace in the log
5274
try:
53-
print(something)
75+
do_something()
5476
except Exception:
55-
logger.exception('something')
77+
logger.exception('Additional information.')
5678
```
5779

58-
Default logging level is Warning. To change the logging level edit the configuration (`mapswipe_workers/logging.cfg`).
80+
Default logging level is Info. To change the logging level edit the configuration. Logs are written to STDOUT and `~/.local/share/mapswipe_workers/mapswipe_workers.log`.
5981

6082
Per default logging of third-party packages is disabled. To change this edit the definition module (`mapswipe_workers/defintions.md`). Set the `disable_existing_loggers` parameter of the `logging.config.fileConfig()` function to False.
6183

docs/source/installation.md

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -251,28 +251,16 @@ certbot certonly \
251251
--non-interactive
252252
```
253253

254+
> Note: Certbot systemd timer for renewal of certificate will not work for standalone certificates because the service (docker nginx) which occupies port 80 has to be stopped before renewal.
254255
255-
Enable and start Certbot systemd timer for renewal of certificates:
256+
For certificate renewal a cronjob is used:
256257

257258
```bash
258-
systemctl enable certbot.timer
259-
systemctl start certbot.timer
260-
# To check if certbot.timer is enabled run:
261-
systemctl list-units --type timer | grep certbot
262-
```
263-
264-
Add renewal post hook to reload nginx after certificate renwal:
265-
266-
- `mkdir -p /etc/letsencrypt/renewal-hooks/deploy`
267-
- `vim /etc/letsencrypt/renewal-hooks/deploy/nginx`
259+
SHELL=/bin/sh
260+
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
268261

262+
0 */12 * * * certbot -q renew --pre-hook "docker stop nginx" --post-hook "docker start nginx"
269263
```
270-
#!/usr/bin/env bash
271-
272-
docker container restart nginx
273-
```
274-
275-
- `chmod +x /etc/letsencrypt/renewal-hooks/deploy/nginx`
276264

277265

278266
### Nginx

mapswipe_workers/Dockerfile

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,45 @@
22
# Image includes python3.6, gdal-python, gdal-bin
33
FROM osgeo/gdal:ubuntu-small-latest
44

5+
# Set C.UTF-8 locale as default (Needed by the Click library)
56
ENV LC_ALL=C.UTF-8
67
ENV LANG=C.UTF-8
78

89
# Install pip
910
RUN apt-get update
1011
RUN apt-get --yes install python3-pip
1112

12-
# Create directories for config, logs and data
13-
ARG config_dir=/usr/share/config/mapswipe_workers/
14-
ARG repo_dir=/usr/local/mapswipe_workers/
15-
ARG data_dir=/var/lib/mapswipe_workers/
16-
ARG logs_dir=/var/log/mapswipe_workers/
17-
18-
RUN mkdir -p $config_dir
19-
RUN mkdir -p $repo_dir
20-
RUN mkdir -p $logs_dir
21-
RUN mkdir -p $data_dir
22-
RUN mkdir -p $data_dir"api-data"
23-
RUN mkdir -p $data_dir"api-data/agg_results/"
24-
RUN mkdir -p $data_dir"api-data/groups/"
25-
RUN mkdir -p $data_dir"api-data/history/"
26-
RUN mkdir -p $data_dir"api-data/projects/"
27-
RUN mkdir -p $data_dir"api-data/results/"
28-
RUN mkdir -p $data_dir"api-data/tasks/"
29-
RUN mkdir -p $data_dir"api-data/yes_maybe/"
30-
RUN mkdir -p $data_dir"api-data/hot_tm/"
13+
####
14+
# Due to unresolved permission error of shared volume
15+
# fall back to root user.
16+
#
17+
## Create user mapswipe
18+
# RUN groupadd --system mapswipe
19+
# RUN useradd --create-home --no-log-init --system --gid mapswipe mapswipe
20+
## Change user to mapswipe
21+
# USER mapswipe
22+
# WORKDIR /home/mapswipe
23+
####
24+
25+
WORKDIR /root
26+
27+
# Create directories for config, data and logs
28+
RUN mkdir --parents .config/mapswipe_workers
29+
RUN mkdir --parents .local/share/mapswipe_workers
3130

3231
# Copy mapswipe workers repo from local repo
33-
WORKDIR $repo_dir
3432
COPY mapswipe_workers/ mapswipe_workers/
3533
COPY sample_data/ sample_data/
3634
COPY tests/ tests/
3735
COPY requirements.txt .
3836
COPY setup.py .
39-
COPY config $config_dir
37+
COPY config/ .config/mapswipe_workers/
4038

4139
# Update setuptools and install mapswipe-workers with dependencies (requirements.txt)
4240
RUN pip3 install --upgrade setuptools
4341
RUN pip3 install .
4442

43+
# Add platform-specific user binary directory for Python applications to $PATH
44+
# ENV PATH="$PATH:/home/mapswipe/.local/bin"
45+
4546
# Don't use a CMD here, this will be defined in docker-compose.yaml

mapswipe_workers/config/logging.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ args=(sys.stdout,)
2727
class=handlers.TimedRotatingFileHandler
2828
level=INFO
2929
formatter=mapswipeFormatter
30-
args=('/var/log/mapswipe_workers/mapswipe_workers.log','D', 1, 14,)
30+
args=('%(logfilename)s','D', 1, 14,)
3131

3232
[formatter_mapswipeFormatter]
3333
format=%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(message)s

0 commit comments

Comments
 (0)