#!/bin/bash set -euo pipefail function createPostgresConfig() { cp /etc/postgresql/$PG_VERSION/main/postgresql.custom.conf.tmpl /etc/postgresql/$PG_VERSION/main/conf.d/postgresql.custom.conf sudo -u postgres echo "autovacuum = $AUTOVACUUM" >> /etc/postgresql/$PG_VERSION/main/conf.d/postgresql.custom.conf cat /etc/postgresql/$PG_VERSION/main/conf.d/postgresql.custom.conf } function setPostgresPassword() { sudo -u postgres psql -c "ALTER USER renderer PASSWORD '${PGPASSWORD:-renderer}'" } function compileStyle() { cd /data/style/ git clone --single-branch --branch osm-tile-server https://github.com/nobohan/OpenArdenneMap.git --depth 1 # mv symbols folder mkdir /data/img/ mv /data/style/OpenArdenneMap/img/* /data/img/ # copy fonts cp /data/style/OpenArdenneMap/fonts/* /usr/share/fonts/ # change dbname sed -i 's/"dbname": "osmpg_db",/"dbname": "gis",/g' ./OpenArdenneMap/osm2pgsql/project.mml # change hillshade sed -i 's,/mnt/tera/ChampsLibres/Projets/OSM/CyclOSM/hillshade/data/dem_be_10x10_hillshade_semi_transparent_3857.tif,/data/hillshade.tif,g' ./OpenArdenneMap/osm2pgsql/project.mml # change contours sed -i 's,../contour/beautiful_contour_belgium.shp,/data/shp/contours_3857.shp,g' ./OpenArdenneMap/osm2pgsql/project.mml # carto build carto OpenArdenneMap/osm2pgsql/project.mml > mapnik.xml } if [ "$#" -ne 1 ]; then echo "usage: " echo "commands:" echo " import: Set up the database and import /data/region.osm.pbf" echo " run: Runs Apache and renderd to serve tiles at /tile/{z}/{x}/{y}.png" echo "environment variables:" echo " THREADS: defines number of threads used for importing / tile rendering" echo " UPDATES: consecutive updates (enabled/disabled)" echo " NAME_LUA: name of .lua script to run as part of the style" echo " NAME_STYLE: name of the .style to use" echo " NAME_MML: name of the .mml file to render to mapnik.xml" echo " NAME_SQL: name of the .sql file to use" exit 1 fi set -x if [ "$1" == "terminal" ]; then bash fi if [ "$1" == "import" ]; then compileStyle # Ensure that database directory is in right state mkdir -p /data/database/postgres/ chown renderer: /data/database/ chown -R postgres: /var/lib/postgresql /data/database/postgres/ if [ ! -f /data/database/postgres/PG_VERSION ]; then sudo -u postgres /usr/lib/postgresql/$PG_VERSION/bin/pg_ctl -D /data/database/postgres/ initdb -o "--locale C.UTF-8" fi # Initialize PostgreSQL createPostgresConfig service postgresql start sudo -u postgres createuser renderer sudo -u postgres createdb -E UTF8 -O renderer gis sudo -u postgres psql -d gis -c "CREATE EXTENSION postgis;" sudo -u postgres psql -d gis -c "CREATE EXTENSION hstore;" sudo -u postgres psql -d gis -c "CREATE EXTENSION postgis_sfcgal;" sudo -u postgres psql -d gis -c "ALTER TABLE geometry_columns OWNER TO renderer;" sudo -u postgres psql -d gis -c "ALTER TABLE spatial_ref_sys OWNER TO renderer;" setPostgresPassword # Download Luxembourg as sample if no data is provided if [ ! -f /data/region.osm.pbf ] && [ -z "${DOWNLOAD_PBF:-}" ]; then echo "WARNING: No import file at /data/region.osm.pbf, so importing Luxembourg as example..." DOWNLOAD_PBF="https://download.geofabrik.de/europe/luxembourg-latest.osm.pbf" DOWNLOAD_POLY="https://download.geofabrik.de/europe/luxembourg.poly" fi if [ -n "${DOWNLOAD_PBF:-}" ]; then echo "INFO: Download PBF file: $DOWNLOAD_PBF" wget ${WGET_ARGS:-} "$DOWNLOAD_PBF" -O /data/region.osm.pbf if [ -n "${DOWNLOAD_POLY:-}" ]; then echo "INFO: Download PBF-POLY file: $DOWNLOAD_POLY" wget ${WGET_ARGS:-} "$DOWNLOAD_POLY" -O /data/region.poly fi fi if [ "${UPDATES:-}" == "enabled" ] || [ "${UPDATES:-}" == "1" ]; then # determine and set osmosis_replication_timestamp (for consecutive updates) REPLICATION_TIMESTAMP=`osmium fileinfo -g header.option.osmosis_replication_timestamp /data/region.osm.pbf` # initial setup of osmosis workspace (for consecutive updates) sudo -E -u renderer openstreetmap-tiles-update-expire.sh $REPLICATION_TIMESTAMP fi # copy polygon file if available if [ -f /data/region.poly ]; then cp /data/region.poly /data/database/region.poly chown renderer: /data/database/region.poly fi # flat-nodes if [ "${FLAT_NODES:-}" == "enabled" ] || [ "${FLAT_NODES:-}" == "1" ]; then OSM2PGSQL_EXTRA_ARGS="${OSM2PGSQL_EXTRA_ARGS:-} --flat-nodes /data/database/flat_nodes.bin" fi # Import data sudo -u renderer osm2pgsql -d gis --create --slim -G -S /data/style/OpenArdenneMap/osm2pgsql/OpenArdenneMap.style --extra-attributes \ /data/region.osm.pbf \ ${OSM2PGSQL_EXTRA_ARGS:-} \ ; # old flat-nodes dir if [ -f /nodes/flat_nodes.bin ] && ! [ -f /data/database/flat_nodes.bin ]; then mv /nodes/flat_nodes.bin /data/database/flat_nodes.bin chown renderer: /data/database/flat_nodes.bin fi # Create indexes if [ -f /data/style/${NAME_SQL:-indexes.sql} ]; then sudo -u postgres psql -d gis -f /data/style/${NAME_SQL:-indexes.sql} fi sudo -u postgres psql -d gis -f /data/style/OpenArdenneMap/create_views.sql sudo -u postgres psql -d gis -f /data/alter_view_owner.sql # Register that data has changed for mod_tile caching purposes sudo -u renderer touch /data/database/planet-import-complete service postgresql stop exit 0 fi if [ "$1" == "run" ]; then # Clean /tmp rm -rf /tmp/* compileStyle # migrate old files if [ -f /data/database/PG_VERSION ] && ! [ -d /data/database/postgres/ ]; then mkdir /data/database/postgres/ mv /data/database/* /data/database/postgres/ fi if [ -f /nodes/flat_nodes.bin ] && ! [ -f /data/database/flat_nodes.bin ]; then mv /nodes/flat_nodes.bin /data/database/flat_nodes.bin fi if [ -f /data/tiles/data.poly ] && ! [ -f /data/database/region.poly ]; then mv /data/tiles/data.poly /data/database/region.poly fi # sync planet-import-complete file if [ -f /data/tiles/planet-import-complete ] && ! [ -f /data/database/planet-import-complete ]; then cp /data/tiles/planet-import-complete /data/database/planet-import-complete fi if ! [ -f /data/tiles/planet-import-complete ] && [ -f /data/database/planet-import-complete ]; then cp /data/database/planet-import-complete /data/tiles/planet-import-complete fi # Fix postgres data privileges chown -R postgres: /var/lib/postgresql/ /data/database/postgres/ # Configure Apache CORS if [ "${ALLOW_CORS:-}" == "enabled" ] || [ "${ALLOW_CORS:-}" == "1" ]; then echo "export APACHE_ARGUMENTS='-D ALLOW_CORS'" >> /etc/apache2/envvars fi # Initialize PostgreSQL and Apache createPostgresConfig service postgresql start service apache2 restart setPostgresPassword # Configure renderd threads #sed -i -E "s/num_threads=[0-9]+/num_threads=${THREADS:-4}/g" /etc/renderd.conf # start cron job to trigger consecutive updates if [ "${UPDATES:-}" == "enabled" ] || [ "${UPDATES:-}" == "1" ]; then /etc/init.d/cron start sudo -u renderer touch /var/log/tiles/run.log; tail -f /var/log/tiles/run.log >> /proc/1/fd/1 & sudo -u renderer touch /var/log/tiles/osmosis.log; tail -f /var/log/tiles/osmosis.log >> /proc/1/fd/1 & sudo -u renderer touch /var/log/tiles/expiry.log; tail -f /var/log/tiles/expiry.log >> /proc/1/fd/1 & sudo -u renderer touch /var/log/tiles/osm2pgsql.log; tail -f /var/log/tiles/osm2pgsql.log >> /proc/1/fd/1 & fi # Run while handling docker stop's SIGTERM stop_handler() { kill -TERM "$child" } trap stop_handler SIGTERM sudo -u renderer renderd -f -c /etc/renderd.conf & child=$! wait "$child" service postgresql stop exit 0 fi if [ "$1" == "generate_tiles" ]; then compileStyle service postgresql start service apache2 restart sudo -u renderer renderd -c /etc/renderd.conf cat /data/tiles/oam/tiles.list | render_list --map oam bash fi echo "invalid command" exit 1