2018-05-08 20:43:34 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2019-04-16 05:45:10 +00:00
|
|
|
set -x
|
|
|
|
|
2019-09-22 14:37:39 +00:00
|
|
|
function createPostgresConfig() {
|
2019-12-05 20:27:46 +00:00
|
|
|
cp /etc/postgresql/12/main/postgresql.custom.conf.tmpl /etc/postgresql/12/main/conf.d/postgresql.custom.conf
|
|
|
|
sudo -u postgres echo "autovacuum = $AUTOVACUUM" >> /etc/postgresql/12/main/conf.d/postgresql.custom.conf
|
|
|
|
cat /etc/postgresql/12/main/conf.d/postgresql.custom.conf
|
2019-04-16 05:45:10 +00:00
|
|
|
}
|
|
|
|
|
2019-09-22 14:37:39 +00:00
|
|
|
function setPostgresPassword() {
|
|
|
|
sudo -u postgres psql -c "ALTER USER renderer PASSWORD '${PGPASSWORD:-renderer}'"
|
|
|
|
}
|
|
|
|
|
2018-05-08 20:43:34 +00:00
|
|
|
if [ "$#" -ne 1 ]; then
|
|
|
|
echo "usage: <import|run>"
|
|
|
|
echo "commands:"
|
|
|
|
echo " import: Set up the database and import /data.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"
|
2019-06-13 13:39:04 +00:00
|
|
|
echo " UPDATES: consecutive updates (enabled/disabled)"
|
2018-05-08 20:43:34 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$1" = "import" ]; then
|
2020-02-19 20:04:24 +00:00
|
|
|
# Ensure that database directory is in right state
|
|
|
|
chown postgres:postgres -R /var/lib/postgresql
|
|
|
|
if [ ! -f /var/lib/postgresql/12/main/PG_VERSION ]; then
|
|
|
|
sudo -u postgres /usr/lib/postgresql/12/bin/pg_ctl -D /var/lib/postgresql/12/main/ initdb -o "--locale C.UTF-8"
|
|
|
|
fi
|
|
|
|
|
2018-05-08 20:43:34 +00:00
|
|
|
# Initialize PostgreSQL
|
2019-09-22 14:37:39 +00:00
|
|
|
createPostgresConfig
|
2018-05-08 20:43:34 +00:00
|
|
|
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 "ALTER TABLE geometry_columns OWNER TO renderer;"
|
|
|
|
sudo -u postgres psql -d gis -c "ALTER TABLE spatial_ref_sys OWNER TO renderer;"
|
2019-09-22 14:37:39 +00:00
|
|
|
setPostgresPassword
|
2018-05-08 20:43:34 +00:00
|
|
|
|
|
|
|
# Download Luxembourg as sample if no data is provided
|
2020-02-20 19:29:14 +00:00
|
|
|
if [ ! -f /data.osm.pbf ] && [ -z "$DOWNLOAD_PBF" ]; then
|
2018-05-08 20:43:34 +00:00
|
|
|
echo "WARNING: No import file at /data.osm.pbf, so importing Luxembourg as example..."
|
2020-04-07 00:03:26 +00:00
|
|
|
DOWNLOAD_PBF="https://download.geofabrik.de/europe/luxembourg-latest.osm.pbf"
|
|
|
|
DOWNLOAD_POLY="https://download.geofabrik.de/europe/luxembourg.poly"
|
2020-02-20 07:46:46 +00:00
|
|
|
fi
|
2020-02-20 19:29:14 +00:00
|
|
|
|
|
|
|
if [ -n "$DOWNLOAD_PBF" ]; then
|
|
|
|
echo "INFO: Download PBF file: $DOWNLOAD_PBF"
|
2021-09-03 10:46:37 +00:00
|
|
|
wget $WGET_ARGS "$DOWNLOAD_PBF" -O /data.osm.pbf
|
2020-02-20 19:29:14 +00:00
|
|
|
if [ -n "$DOWNLOAD_POLY" ]; then
|
|
|
|
echo "INFO: Download PBF-POLY file: $DOWNLOAD_POLY"
|
2021-09-03 10:46:37 +00:00
|
|
|
wget $WGET_ARGS "$DOWNLOAD_POLY" -O /data.poly
|
2020-02-20 07:46:46 +00:00
|
|
|
fi
|
2019-06-13 13:39:04 +00:00
|
|
|
fi
|
|
|
|
|
2019-11-07 20:12:29 +00:00
|
|
|
if [ "$UPDATES" = "enabled" ]; then
|
|
|
|
# determine and set osmosis_replication_timestamp (for consecutive updates)
|
|
|
|
osmium fileinfo /data.osm.pbf > /var/lib/mod_tile/data.osm.pbf.info
|
|
|
|
osmium fileinfo /data.osm.pbf | grep 'osmosis_replication_timestamp=' | cut -b35-44 > /var/lib/mod_tile/replication_timestamp.txt
|
|
|
|
REPLICATION_TIMESTAMP=$(cat /var/lib/mod_tile/replication_timestamp.txt)
|
2019-06-13 13:39:04 +00:00
|
|
|
|
2019-11-07 20:12:29 +00:00
|
|
|
# initial setup of osmosis workspace (for consecutive updates)
|
|
|
|
sudo -u renderer openstreetmap-tiles-update-expire $REPLICATION_TIMESTAMP
|
|
|
|
fi
|
2019-06-13 13:39:04 +00:00
|
|
|
|
|
|
|
# copy polygon file if available
|
|
|
|
if [ -f /data.poly ]; then
|
|
|
|
sudo -u renderer cp /data.poly /var/lib/mod_tile/data.poly
|
2018-05-08 20:43:34 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Import data
|
2020-04-26 17:00:59 +00:00
|
|
|
sudo -u renderer osm2pgsql -d gis --create --slim -G --hstore --tag-transform-script /home/renderer/src/openstreetmap-carto/openstreetmap-carto.lua --number-processes ${THREADS:-4} -S /home/renderer/src/openstreetmap-carto/openstreetmap-carto.style /data.osm.pbf ${OSM2PGSQL_EXTRA_ARGS}
|
2019-06-14 21:23:42 +00:00
|
|
|
|
|
|
|
# Create indexes
|
2021-07-19 20:05:18 +00:00
|
|
|
sudo -u postgres psql -d gis -f /home/renderer/src/openstreetmap-carto/indexes.sql
|
|
|
|
|
2021-05-13 12:41:01 +00:00
|
|
|
#Import external data
|
|
|
|
sudo chown -R renderer: /home/renderer/src
|
|
|
|
sudo -u renderer python3 /home/renderer/src/openstreetmap-carto/scripts/get-external-data.py -c /home/renderer/src/openstreetmap-carto/external-data.yml -D /home/renderer/src/openstreetmap-carto/data
|
2019-06-14 21:23:42 +00:00
|
|
|
|
2019-10-12 10:21:02 +00:00
|
|
|
# Register that data has changed for mod_tile caching purposes
|
|
|
|
touch /var/lib/mod_tile/planet-import-complete
|
|
|
|
|
2019-03-14 09:42:58 +00:00
|
|
|
service postgresql stop
|
2018-05-08 20:43:34 +00:00
|
|
|
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$1" = "run" ]; then
|
2019-06-18 10:26:15 +00:00
|
|
|
# Clean /tmp
|
|
|
|
rm -rf /tmp/*
|
2019-08-20 17:40:24 +00:00
|
|
|
|
2019-06-18 10:55:51 +00:00
|
|
|
# Fix postgres data privileges
|
|
|
|
chown postgres:postgres /var/lib/postgresql -R
|
2019-06-18 10:26:15 +00:00
|
|
|
|
2019-08-31 11:44:23 +00:00
|
|
|
# Configure Apache CORS
|
2020-02-12 19:16:43 +00:00
|
|
|
if [ "$ALLOW_CORS" == "enabled" ] || [ "$ALLOW_CORS" == "1" ]; then
|
2019-08-31 11:44:23 +00:00
|
|
|
echo "export APACHE_ARGUMENTS='-D ALLOW_CORS'" >> /etc/apache2/envvars
|
|
|
|
fi
|
|
|
|
|
2018-05-08 20:43:34 +00:00
|
|
|
# Initialize PostgreSQL and Apache
|
2019-09-22 14:37:39 +00:00
|
|
|
createPostgresConfig
|
2018-05-08 20:43:34 +00:00
|
|
|
service postgresql start
|
|
|
|
service apache2 restart
|
2019-09-22 14:37:39 +00:00
|
|
|
setPostgresPassword
|
2018-05-08 20:43:34 +00:00
|
|
|
|
|
|
|
# Configure renderd threads
|
|
|
|
sed -i -E "s/num_threads=[0-9]+/num_threads=${THREADS:-4}/g" /usr/local/etc/renderd.conf
|
|
|
|
|
2019-06-13 13:39:04 +00:00
|
|
|
# start cron job to trigger consecutive updates
|
2020-02-12 19:16:43 +00:00
|
|
|
if [ "$UPDATES" = "enabled" ] || [ "$UPDATES" = "1" ]; then
|
2019-06-13 13:39:04 +00:00
|
|
|
/etc/init.d/cron start
|
|
|
|
fi
|
|
|
|
|
2019-10-12 10:35:16 +00:00
|
|
|
# Run while handling docker stop's SIGTERM
|
|
|
|
stop_handler() {
|
|
|
|
kill -TERM "$child"
|
|
|
|
}
|
|
|
|
trap stop_handler SIGTERM
|
|
|
|
|
|
|
|
sudo -u renderer renderd -f -c /usr/local/etc/renderd.conf &
|
|
|
|
child=$!
|
|
|
|
wait "$child"
|
|
|
|
|
2019-03-14 09:42:58 +00:00
|
|
|
service postgresql stop
|
2018-05-08 20:43:34 +00:00
|
|
|
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "invalid command"
|
2019-03-14 09:42:58 +00:00
|
|
|
exit 1
|