Skip to content

Commit bdee3bd

Browse files
authored
Web container fixes and backups templates (#301)
* Add osmcha container * Add chartpress config for osmcha * Update start point in osmcha * Add osmcha database helm template * Update osmcha db config * Add helm template for osmcha-web * Rename files * Update osmcha db helm template * Update osmcha-web deployment template * Add osm server env var for osmcha * Update OSMCha Dockerfile and start script * Remove pip uninstall command * Update env vars for osmcha web * Update env var * Open port 81 for cgimap * Fix service - web config * Add cgimap endpoint * Update to web-cgimap endpoint * Update cgimap endpoint * Build osmcha to ohm config + add env vars * Fix git clone url * Use npm install instead of yarn * Install yarnpkg and use it instead of npm * Try to fix error on yarn command * Fix yarn instalation for osmcha * Add extra osmcha env vars in deployent * Fix the path for static files - osmcha * Build osmcha app * Remove cgimap from web config * Add multiple backup database jobs * Add supervisor + nginx * Update docker container for backup db * Update Script to backup and restore db * Update path for bucket and env vars for backups db * Change port for osmcha web * Add env vars for backups * Update bakup db template * Add function to clean up backup older than x days * Update target port for osmcha * Update env vars for backup db * Update log and config for backups * Add node selector for backups config * Raising shared memory limit for web api * Install wget for db-bakups container * Store results of restoring db * Fix name for backup * Set file name * osm-seed/values.yaml * Add sharedMemorySize for db * Remove comment * Add liveness using bash
1 parent 4b6efd6 commit bdee3bd

27 files changed

+1068
-182
lines changed

chartpress.yaml

+46-42
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
charts:
2-
- name: osm-seed
3-
imagePrefix: developmentseed/osmseed-
4-
repo:
5-
git: developmentseed/osm-seed-chart
6-
published: https://devseed.com/osm-seed-chart
2+
- name: osm-seed
3+
imagePrefix: developmentseed/osmseed-
4+
repo:
5+
git: developmentseed/osm-seed-chart
6+
published: https://devseed.com/osm-seed-chart
77

8-
images:
9-
web:
10-
valuesPath: web.image
11-
db:
12-
valuesPath: db.image
13-
backup-restore:
14-
valuesPath: dbBackupRestore.image
15-
planet-dump:
16-
valuesPath: planetDump.image
17-
full-history:
18-
valuesPath: fullHistory.image
19-
replication-job:
20-
valuesPath: replicationJob.image
21-
populate-apidb:
22-
valuesPath: populateApidb.image
23-
osm-processor:
24-
valuesPath: osmProcessor.image
25-
tiler-db:
26-
valuesPath: tilerDb.image
27-
tiler-imposm:
28-
valuesPath: tilerImposm.image
29-
tiler-server:
30-
valuesPath: tilerServer.image
31-
tasking-manager-api:
32-
valuesPath: tmApi.image
33-
# tiler-visor:
34-
# valuesPath: tilerVisor.image
35-
nominatim:
36-
valuesPath: nominatimApi.image
37-
overpass-api:
38-
valuesPath: overpassApi.image
39-
taginfo:
40-
valuesPath: taginfo.image
41-
osm-simple-metrics:
42-
valuesPath: osmSimpleMetrics.image
43-
changeset-replication-job:
44-
valuesPath: changesetReplicationJob.image
8+
images:
9+
web:
10+
valuesPath: web.image
11+
db:
12+
valuesPath: db.image
13+
backup-restore:
14+
valuesPath: dbBackupRestore.image
15+
planet-dump:
16+
valuesPath: planetDump.image
17+
full-history:
18+
valuesPath: fullHistory.image
19+
replication-job:
20+
valuesPath: replicationJob.image
21+
populate-apidb:
22+
valuesPath: populateApidb.image
23+
osm-processor:
24+
valuesPath: osmProcessor.image
25+
tiler-db:
26+
valuesPath: tilerDb.image
27+
tiler-imposm:
28+
valuesPath: tilerImposm.image
29+
tiler-server:
30+
valuesPath: tilerServer.image
31+
tasking-manager-api:
32+
valuesPath: tmApi.image
33+
# tiler-visor:
34+
# valuesPath: tilerVisor.image
35+
nominatim:
36+
valuesPath: nominatimApi.image
37+
overpass-api:
38+
valuesPath: overpassApi.image
39+
taginfo:
40+
valuesPath: taginfo.image
41+
osm-simple-metrics:
42+
valuesPath: osmSimpleMetrics.image
43+
changeset-replication-job:
44+
valuesPath: changesetReplicationJob.image
45+
osmcha-web:
46+
valuesPath: osmchaWeb.image
47+
osmcha-db:
48+
valuesPath: osmchaDb.image

compose/db-backup-restore.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ services:
99
context: ../images/backup-restore
1010
dockerfile: Dockerfile
1111
volumes:
12-
- ../data/db-backup-restore-data:/mnt/data
12+
- ../data/db-backup-restore-data:/mnt/data
1313
command: >
1414
/bin/bash -c "
1515
echo 'Set cron job for backing up DB, every 4 minutes';
1616
while :; do echo 'Creating DB backup...'; /start.sh; sleep 4m; done;
1717
"
1818
env_file:
19-
- ../envs/.env.db
20-
- ../envs/.env.db-utils
21-
- ../envs/.env.cloudprovider
19+
- ../envs/.env.db
20+
- ../envs/.env.osmcha
21+
- ../envs/.env.db-utils
22+
- ../envs/.env.cloudprovider

compose/osmcha.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: '3'
2+
services:
3+
osmcha-db:
4+
platform: linux/amd64
5+
image: osmseed-osmcha-db:v1
6+
build:
7+
context: ../images/osmcha-db
8+
dockerfile: Dockerfile
9+
ports:
10+
- '5432:5432'
11+
volumes:
12+
- ../data/osmcha-db-data:/var/lib/postgresql/data
13+
env_file:
14+
- ../envs/.env.osmcha
15+
osmcha-web:
16+
platform: linux/amd64
17+
image: osmseed-osmcha-web:v1
18+
build:
19+
context: ../images/osmcha-web
20+
dockerfile: Dockerfile
21+
ports:
22+
- '8000:8000'
23+
env_file:
24+
- ../envs/.env.osmcha

envs/.env.osmcha.example

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
POSTGRES_DB=osmcha
2+
POSTGRES_USER=postgres
3+
POSTGRES_PASSWORD=1234
4+
PGHOST=osmcha-db
5+
DJANGO_DEBUG=true
6+
DJANGO_SECRET_KEY=abc
7+
OAUTH_OSM_KEY=xya
8+
OAUTH_OSM_SECRET=xya
9+
DJANGO_ANON_USER_THROTTLE_RATE=30/min
10+
DJANGO_COMMON_USER_THROTTLE_RATE=180/min
11+
DJANGO_NON_STAFF_USER_THROTTLE_RATE=3/min
12+
OSMCHA_URL=public-url
13+
OAUTH_REDIRECT_URI=$OSMCHA_URL/oauth-landing.html
14+
DJANGO_ENABLE_CHANGESET_COMMENTS=False
15+
DJANGO_OSM_COMMENTS_API_KEY=""
16+
DJANGO_ROOT=/app
17+
DJANGO_SETTINGS_MODULE=config.settings.local
18+
C_FORCE_ROOT=True
19+
REACT_APP_OSM_URL=https://www.openhistoricalmap.org
20+
REACT_APP_OSM_API=https://www.openhistoricalmap.org/api/0.6
21+
REACT_APP_OVERPASS_BASE=https://overpass-api.openhistoricalmap.org/api/interpreter
22+
REACT_APP_NOMINATIM_URL=https://www.openhistoricalmap.org/geocoder/search_osm_nominatim
23+
REACT_APP_PRODUCTION_API_URL=$OSMCHA_URL/api/v1
24+
REACT_APP_MAPBOX_ACCESS_TOKEN=''

images/backup-restore/Dockerfile

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
FROM python:3.9.9
2-
RUN apt-get update
3-
RUN apt-get install -y \
4-
curl \
5-
postgresql-client
1+
FROM python:3.9.9-slim
62

7-
# Install AWS CLI
8-
RUN pip install awscli
3+
# Install Postgres client, GCP CLI, and Azure CLI
4+
RUN apt-get update \
5+
&& apt-get install -y curl apt-transport-https lsb-release gnupg \
6+
&& echo "deb http://apt.postgresql.org/pub/repos/apt/ bullseye-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
7+
&& curl -L https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
8+
&& curl -sL https://aka.ms/InstallAzureCLIDeb | bash \
9+
&& apt-get update \
10+
&& apt-get install -y postgresql-client-14 wget \
11+
&& apt-get clean \
12+
&& rm -rf /var/lib/apt/lists/*
913

1014
# Install GCP CLI
11-
RUN curl -sSL https://sdk.cloud.google.com | bash
12-
RUN ln -f -s /root/google-cloud-sdk/bin/gsutil /usr/bin/gsutil
15+
RUN curl -sSL https://sdk.cloud.google.com | bash \
16+
&& ln -f -s /root/google-cloud-sdk/bin/gsutil /usr/bin/gsutil
17+
RUN rm -rf /root/google-cloud-sdk/.install/.backup
1318

14-
# Install Azure CLI
15-
RUN curl -sL https://aka.ms/InstallAzureCLIDeb | bash
19+
# Install AWS CLI
20+
RUN pip install awscli
1621

17-
VOLUME /mnt/data
1822
COPY ./start.sh /
19-
CMD /start.sh
23+
CMD ["/start.sh"]

images/backup-restore/start.sh

+99-53
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,111 @@
11
#!/usr/bin/env bash
2+
set -e
23
export PGPASSWORD=$POSTGRES_PASSWORD
3-
export VOLUME_DIR=/mnt/data
4+
# Upload files
5+
cloudStorageOps() {
6+
local LOCAL_STATE_FILE=state.txt
7+
local filepath=$1
8+
local cloudpath=$2
49

5-
date=$(date '+%y%m%d_%H%M')
6-
local_backupFile=$VOLUME_DIR/osmseed-db-${date}.sql.gz
7-
cloud_backupFile=database/osmseed-db-${date}.sql.gz
8-
stateFile=$VOLUME_DIR/state.txt
9-
restoreFile=$VOLUME_DIR/backup.sql.gz
10-
11-
echo "Start...$DB_ACTION action"
12-
# Backing up DataBase
13-
if [ "$DB_ACTION" == "backup" ]; then
14-
# Backup database and make maximum compression at the slowest speed
15-
pg_dump -h $POSTGRES_HOST -U $POSTGRES_USER $POSTGRES_DB | gzip -9 >$local_backupFile
16-
17-
# AWS
18-
if [ "$CLOUDPROVIDER" == "aws" ]; then
19-
echo "$AWS_S3_BUCKET/$cloud_backupFile" > $stateFile
20-
# Upload db backup file
21-
aws s3 cp $local_backupFile $AWS_S3_BUCKET/$cloud_backupFile
22-
# Upload state.txt file
23-
aws s3 cp $stateFile $AWS_S3_BUCKET/database/state.txt
24-
fi
25-
26-
# GCP
27-
if [ "$CLOUDPROVIDER" == "gcp" ]; then
28-
echo "$GCP_STORAGE_BUCKET/$cloud_backupFile" > $stateFile
29-
# Upload db backup file
30-
gsutil cp $local_backupFile $GCP_STORAGE_BUCKET/$cloud_backupFile
31-
# Upload state.txt file
32-
gsutil cp $stateFile $GCP_STORAGE_BUCKET/database/state.txt
33-
fi
34-
35-
# Azure
36-
if [ "$CLOUDPROVIDER" == "azure" ]; then
37-
# Save the path file
38-
echo "blob://$AZURE_STORAGE_ACCOUNT/$AZURE_CONTAINER_NAME/$cloud_backupFile" > $stateFile
39-
# Upload db backup file
10+
case "${CLOUDPROVIDER}" in
11+
aws)
12+
aws s3 cp ${filepath} s3://${AWS_S3_BUCKET}/${cloudpath}
13+
echo s3://${AWS_S3_BUCKET}/${cloudpath} >${LOCAL_STATE_FILE}
14+
aws s3 cp ${LOCAL_STATE_FILE} s3://${AWS_S3_BUCKET}/${BACKUP_CLOUD_FOLDER}/state.txt
15+
;;
16+
gcp)
17+
gsutil cp ${filepath} gs://${GCP_STORAGE_BUCKET}/${cloudpath}
18+
echo "gs://${GCP_STORAGE_BUCKET}/${CLOUD_BACKUP_FILE}" >${LOCAL_STATE_FILE}
19+
gsutil cp ${LOCAL_STATE_FILE} gs://${GCP_STORAGE_BUCKET}/${BACKUP_CLOUD_FOLDER}/state.txt
20+
;;
21+
azure)
4022
az storage blob upload \
41-
--container-name $AZURE_CONTAINER_NAME \
42-
--file $local_backupFile \
43-
--name $cloud_backupFile \
23+
--container-name ${AZURE_CONTAINER_NAME} \
24+
--file ${filepath} \
25+
--name ${cloudpath} \
4426
--output table
45-
# Upload state.txt file
27+
echo "blob://${AZURE_STORAGE_ACCOUNT}/${AZURE_CONTAINER_NAME}/${CLOUD_BACKUP_FILE}" >${LOCAL_STATE_FILE}
4628
az storage blob upload \
47-
--container-name $AZURE_CONTAINER_NAME \
48-
--file $stateFile \
49-
--name database/state.txt \
29+
--container-name ${AZURE_CONTAINER_NAME} \
30+
--file ${LOCAL_STATE_FILE} \
31+
--name ${BACKUP_CLOUD_FOLDER}/state.txt \
5032
--output table
33+
;;
34+
esac
35+
}
36+
37+
backupDB() {
38+
local LOCAL_BACKUP_FILE=${BACKUP_CLOUD_FILE}.sql.gz
39+
local CLOUD_BACKUP_FILE="${BACKUP_CLOUD_FOLDER}/${BACKUP_CLOUD_FILE}.sql.gz"
40+
if [ "$SET_DATE_AT_NAME" == "true" ]; then
41+
local CURRENT_DATE=$(date '+%Y%m%d-%H%M')
42+
LOCAL_BACKUP_FILE="${BACKUP_CLOUD_FILE}-${CURRENT_DATE}.sql.gz"
43+
CLOUD_BACKUP_FILE="${BACKUP_CLOUD_FOLDER}/${BACKUP_CLOUD_FILE}-${CURRENT_DATE}.sql.gz"
5144
fi
52-
fi
5345

54-
# Restoring DataBase
55-
if [ "$DB_ACTION" == "restore" ]; then
56-
# AWS
57-
flag=true
46+
# Backup database with max compression
47+
echo "Backing up DB ${POSTGRES_DB} into ${LOCAL_BACKUP_FILE}"
48+
pg_dump -h ${POSTGRES_HOST} -U ${POSTGRES_USER} ${POSTGRES_DB} | gzip -9 >${LOCAL_BACKUP_FILE}
49+
50+
# Handle cloud storage based on the provider
51+
cloudStorageOps "${LOCAL_BACKUP_FILE}" "${CLOUD_BACKUP_FILE}"
52+
}
53+
54+
restoreDB() {
55+
local CURRENT_DATE=$(date '+%Y%m%d-%H%M')
56+
local RESTORE_FILE="backup.sql.gz"
57+
local LOG_RESULT_FILE="restore_results-${CURRENT_DATE}.log"
58+
local flag=true
59+
5860
while "$flag" = true; do
59-
pg_isready -h $POSTGRES_HOST -p 5432 >/dev/null 2>&2 || continue
60-
flag=false
61-
wget -O $restoreFile $RESTORE_URL_FILE
62-
gunzip <$restoreFile | psql -h $POSTGRES_HOST -U $POSTGRES_USER -d $POSTGRES_DB
63-
echo " Import data to $POSTGRES_DB has finished ..."
61+
pg_isready -h ${POSTGRES_HOST} -p 5432 >/dev/null 2>&2 || continue
62+
flag=false
63+
wget -O ${RESTORE_FILE} ${RESTORE_URL_FILE}
64+
echo "Restoring ${RESTORE_URL_FILE} in ${POSTGRES_DB}"
65+
gunzip -c <${RESTORE_FILE} | psql -h ${POSTGRES_HOST} -U ${POSTGRES_USER} -d ${POSTGRES_DB} | tee ${LOG_RESULT_FILE}
66+
aws s3 cp ${LOG_RESULT_FILE} s3://${AWS_S3_BUCKET}/${LOG_RESULT_FILE}
67+
echo "Import data to ${POSTGRES_DB} has finished ..."
68+
done
69+
}
70+
71+
delete_old_s3_files() {
72+
# Use RETENTION_DAYS from environment variable or default to 30 days
73+
if [ -z "${RETENTION_DAYS}" ]; then
74+
DAYS_AGO=30
75+
else
76+
DAYS_AGO="${RETENTION_DAYS}"
77+
fi
78+
79+
echo "Files older than $DAYS_AGO days will be deleted."
80+
echo "Processing s3://${AWS_S3_BUCKET}/${BACKUP_CLOUD_FOLDER}/"
81+
TARGET_DATE=$(date -d "${DAYS_AGO} days ago" +%Y-%m-%d)
82+
aws s3 ls "s3://${AWS_S3_BUCKET}/${BACKUP_CLOUD_FOLDER}/" --recursive | while read -r line; do
83+
FILE_DATE=$(echo "$line" | awk '{print $1}')
84+
FILE_PATH=$(echo "$line" | awk '{print $4}')
85+
if [[ "$FILE_DATE" < "$TARGET_DATE" && ! -z "$FILE_PATH" ]]; then
86+
echo "Deleting ${FILE_PATH} which was modified on ${FILE_DATE}"
87+
aws s3 rm "s3://${AWS_S3_BUCKET}/${FILE_PATH}"
88+
fi
6489
done
90+
}
91+
92+
# Main logic
93+
case "${DB_ACTION}" in
94+
backup)
95+
backupDB
96+
;;
97+
restore)
98+
restoreDB
99+
;;
100+
*)
101+
echo "Unknown action: ${DB_ACTION}"
102+
exit 1
103+
;;
104+
esac
105+
106+
# Check for the CLEAN_BACKUPS var
107+
if [ "$CLEANUP_BACKUPS" == "true" ]; then
108+
delete_old_s3_files
109+
else
110+
echo "CLEANUP_BACKUPS is not set to true. Skipping deletion."
65111
fi

images/osmcha-db/Dockerfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM postgres:14
2+
# Install PostGIS
3+
RUN apt-get update && \
4+
apt-get install -y --no-install-recommends \
5+
postgis \
6+
postgresql-14-postgis-3 && \
7+
rm -rf /var/lib/apt/lists/*
8+
# Initialize PostGIS
9+
COPY init-postgis.sql /docker-entrypoint-initdb.d/

images/osmcha-db/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Postgres 14 for OpenStreetMap Changeset Analyzer (OSMCha)
2+
3+
## Build
4+
```sh
5+
docker compose -f <PathTo>/compose/osmcha.yml build
6+
```
7+
8+
## Run
9+
10+
```sh
11+
docker compose -f <PathTo>/compose/osmcha.yml up
12+
```

images/osmcha-db/init-postgis.sql

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE EXTENSION IF NOT EXISTS postgis;

0 commit comments

Comments
 (0)