2018-05-08 20:43:34 +00:00
#!/bin/bash
2021-12-11 14:39:36 +00:00
set -euo pipefail
2019-09-22 14:37:39 +00:00
function createPostgresConfig( ) {
2023-03-18 20:45:25 +00:00
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
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 } ' "
}
2023-06-26 07:02:43 +00:00
function compileStyle( ) {
cd /data/style/
2023-07-03 15:57:06 +00:00
git clone --single-branch --branch master https://github.com/nobohan/OpenArdenneMap.git --depth 1
2023-06-26 07:02:43 +00:00
# mv symbols folder
2023-07-03 15:57:06 +00:00
# mkdir /data/style/symbols/
# mv /data/style/cyclosm-cartocss-style/symbols/* /data/style/symbols/
2023-06-26 07:02:43 +00:00
# change dbname
2023-07-03 16:34:24 +00:00
sed -i 's/"dbname": "osmpg_db",/"dbname": "gis",/g' ./OpenArdenneMap/osm2pgsql/project.mml
2023-07-03 15:57:06 +00:00
# # remove some deprecated fonts
# sed -i 's/, "unifont Medium", "Unifont Upper Medium"//g' ./cyclosm-cartocss-style/fonts.mss
# sed -i 's/"Noto Sans Tibetan Regular",//g' ./cyclosm-cartocss-style/fonts.mss
# sed -i 's/"Noto Sans Tibetan Bold",//g' ./cyclosm-cartocss-style/fonts.mss
# sed -i 's/Noto Sans Syriac Eastern Regular/Noto Sans Syriac Regular/g' ./cyclosm-cartocss-style/fonts.mss
2023-07-03 16:34:24 +00:00
# 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.shp,g' ./OpenArdenneMap/osm2pgsql/project.mml
2023-06-26 07:02:43 +00:00
# carto build
2023-07-03 15:57:06 +00:00
carto OpenArdenneMap/osm2pgsql/project.mml > mapnik.xml
2023-06-26 07:02:43 +00:00
}
2018-05-08 20:43:34 +00:00
if [ " $# " -ne 1 ] ; then
echo "usage: <import|run>"
echo "commands:"
2022-04-09 13:00:29 +00:00
echo " import: Set up the database and import /data/region.osm.pbf"
2018-05-08 20:43:34 +00:00
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)"
2022-03-31 11:44:56 +00:00
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"
2018-05-08 20:43:34 +00:00
exit 1
fi
2021-09-03 10:51:45 +00:00
set -x
2023-06-22 09:08:13 +00:00
if [ " $1 " = = "terminal" ] ; then
2023-07-03 15:57:06 +00:00
compileStyle
2023-06-22 09:08:13 +00:00
bash
fi
2022-04-09 13:00:29 +00:00
if [ " $1 " = = "import" ] ; then
2023-06-25 08:49:01 +00:00
2023-06-26 07:02:43 +00:00
compileStyle
2023-06-25 08:49:01 +00:00
2020-02-19 20:04:24 +00:00
# Ensure that database directory is in right state
2022-05-01 22:43:17 +00:00
mkdir -p /data/database/postgres/
chown renderer: /data/database/
2022-04-09 13:00:29 +00:00
chown -R postgres: /var/lib/postgresql /data/database/postgres/
if [ ! -f /data/database/postgres/PG_VERSION ] ; then
2023-03-18 20:45:25 +00:00
sudo -u postgres /usr/lib/postgresql/$PG_VERSION /bin/pg_ctl -D /data/database/postgres/ initdb -o "--locale C.UTF-8"
2020-02-19 20:04:24 +00:00
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
2022-04-09 13:00:29 +00:00
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..."
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
2021-12-11 14:39:36 +00:00
if [ -n " ${ DOWNLOAD_PBF :- } " ] ; then
2020-02-20 19:29:14 +00:00
echo " INFO: Download PBF file: $DOWNLOAD_PBF "
2022-04-09 13:00:29 +00:00
wget ${ WGET_ARGS :- } " $DOWNLOAD_PBF " -O /data/region.osm.pbf
2022-07-05 12:23:15 +00:00
if [ -n " ${ DOWNLOAD_POLY :- } " ] ; then
2020-02-20 19:29:14 +00:00
echo " INFO: Download PBF-POLY file: $DOWNLOAD_POLY "
2022-04-09 13:00:29 +00:00
wget ${ WGET_ARGS :- } " $DOWNLOAD_POLY " -O /data/region.poly
2020-02-20 07:46:46 +00:00
fi
2019-06-13 13:39:04 +00:00
fi
2022-04-09 13:00:29 +00:00
if [ " ${ UPDATES :- } " = = "enabled" ] || [ " ${ UPDATES :- } " = = "1" ] ; then
2019-11-07 20:12:29 +00:00
# determine and set osmosis_replication_timestamp (for consecutive updates)
2022-07-15 12:19:46 +00:00
REPLICATION_TIMESTAMP = ` osmium fileinfo -g header.option.osmosis_replication_timestamp /data/region.osm.pbf`
2019-06-13 13:39:04 +00:00
2019-11-07 20:12:29 +00:00
# initial setup of osmosis workspace (for consecutive updates)
2022-07-22 13:18:24 +00:00
sudo -E -u renderer openstreetmap-tiles-update-expire.sh $REPLICATION_TIMESTAMP
2019-11-07 20:12:29 +00:00
fi
2019-06-13 13:39:04 +00:00
# copy polygon file if available
2022-04-09 13:00:29 +00:00
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 "
2018-05-08 20:43:34 +00:00
fi
# Import data
2023-07-04 08:19:43 +00:00
sudo -u renderer osm2pgsql -d gis --create --slim -G -S /data/style/OpenArdenneMap/osm2pgsql/OpenArdenneMap.style --extra-attributes \
2022-04-09 13:00:29 +00:00
/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
2019-06-14 21:23:42 +00:00
# Create indexes
2022-04-09 13:00:29 +00:00
if [ -f /data/style/${ NAME_SQL :- indexes .sql } ] ; then
sudo -u postgres psql -d gis -f /data/style/${ NAME_SQL :- indexes .sql }
fi
2021-07-19 20:05:18 +00:00
2019-10-12 10:21:02 +00:00
# Register that data has changed for mod_tile caching purposes
2022-04-09 13:00:29 +00:00
sudo -u renderer touch /data/database/planet-import-complete
2019-10-12 10:21:02 +00:00
2019-03-14 09:42:58 +00:00
service postgresql stop
2018-05-08 20:43:34 +00:00
exit 0
fi
2022-04-09 13:00:29 +00:00
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
2023-06-26 07:02:43 +00:00
compileStyle
2023-06-25 15:40:32 +00:00
2022-04-09 13:00:29 +00:00
# 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
2019-06-18 10:55:51 +00:00
# Fix postgres data privileges
2022-04-09 13:00:29 +00:00
chown -R postgres: /var/lib/postgresql/ /data/database/postgres/
2019-06-18 10:26:15 +00:00
2019-08-31 11:44:23 +00:00
# Configure Apache CORS
2021-12-11 14:39:36 +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
2023-06-25 15:40:32 +00:00
#sed -i -E "s/num_threads=[0-9]+/num_threads=${THREADS:-4}/g" /etc/renderd.conf
2018-05-08 20:43:34 +00:00
2019-06-13 13:39:04 +00:00
# start cron job to trigger consecutive updates
2022-04-09 13:00:29 +00:00
if [ " ${ UPDATES :- } " = = "enabled" ] || [ " ${ UPDATES :- } " = = "1" ] ; then
/etc/init.d/cron start
2022-07-05 12:23:15 +00:00
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 &
2019-06-13 13:39:04 +00:00
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
2022-07-05 12:23:15 +00:00
sudo -u renderer renderd -f -c /etc/renderd.conf &
2019-10-12 10:35:16 +00:00
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
2023-06-22 14:52:25 +00:00
if [ " $1 " = = "generate_tiles" ] ; then
2023-06-29 06:59:37 +00:00
compileStyle
2023-06-22 14:52:25 +00:00
service postgresql start
service apache2 restart
sudo -u renderer renderd -c /etc/renderd.conf
2023-07-03 15:57:06 +00:00
cat /data/tiles/oam/tiles.list | render_list --map oam
2023-06-29 06:59:37 +00:00
bash
2023-06-22 14:52:25 +00:00
fi
2018-05-08 20:43:34 +00:00
echo "invalid command"
2019-03-14 09:42:58 +00:00
exit 1