forked from peter-evans/nominatim-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-entrypoint.sh
87 lines (63 loc) · 3.26 KB
/
docker-entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
if [ "$NOMINATIM_MODE" != "CREATE" ] && [ "$NOMINATIM_MODE" != "RESTORE" ]; then
# Default to CREATE
NOMINATIM_MODE="CREATE"
fi
# Defaults
NOMINATIM_DATA_PATH=${NOMINATIM_DATA_PATH:="/srv/nominatim/data"}
NOMINATIM_DATA_LABEL=${NOMINATIM_DATA_LABEL:="data"}
NOMINATIM_PBF_URL=${NOMINATIM_PBF_URL:="http://download.geofabrik.de/asia/maldives-latest.osm.pbf"}
NOMINATIM_POSTGRESQL_DATA_PATH=${NOMINATIM_POSTGRESQL_DATA_PATH:="/var/lib/postgresql/9.5/main"}
# Google Storage variables
NOMINATIM_SA_KEY_PATH=${NOMINATIM_SA_KEY_PATH:=""}
NOMINATIM_PROJECT_ID=${NOMINATIM_PROJECT_ID:=""}
NOMINATIM_GS_BUCKET=${NOMINATIM_GS_BUCKET:=""}
NOMINATIM_PG_THREADS=${NOMINATIM_PG_THREADS:=2}
if [ "$NOMINATIM_MODE" == "CREATE" ]; then
# Retrieve the PBF file
curl -k -L $NOMINATIM_PBF_URL --create-dirs -o $NOMINATIM_DATA_PATH/$NOMINATIM_DATA_LABEL.osm.pbf
# Allow user accounts read access to the data
chmod 755 $NOMINATIM_DATA_PATH
# Start PostgreSQL
service postgresql start
# Import data
sudo -u postgres psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='nominatim'" | grep -q 1 || sudo -u postgres createuser -s nominatim
sudo -u postgres psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='www-data'" | grep -q 1 || sudo -u postgres createuser -SDR www-data
sudo -u postgres psql postgres -c "DROP DATABASE IF EXISTS nominatim"
useradd -m -p password1234 nominatim
sudo -u nominatim /srv/nominatim/build/utils/setup.php --osm-file $NOMINATIM_DATA_PATH/$NOMINATIM_DATA_LABEL.osm.pbf --all --threads $NOMINATIM_PG_THREADS
if [ -n "$NOMINATIM_SA_KEY_PATH" ] && [ -n "$NOMINATIM_PROJECT_ID" ] && [ -n "$NOMINATIM_GS_BUCKET" ]; then
# Stop PostgreSQL
service postgresql stop
# Archive PostgreSQL data
tar cz $NOMINATIM_POSTGRESQL_DATA_PATH | split -b 1024MiB - $NOMINATIM_DATA_PATH/$NOMINATIM_DATA_LABEL.tgz_
# Activate the service account to access storage
gcloud auth activate-service-account --key-file $NOMINATIM_SA_KEY_PATH
# Set the Google Cloud project ID
gcloud config set project $NOMINATIM_PROJECT_ID
# Copy the archive to storage
gsutil -m cp $NOMINATIM_DATA_PATH/*.tgz* $NOMINATIM_GS_BUCKET/$NOMINATIM_DATA_LABEL
# Start PostgreSQL
service postgresql start
fi
else
if [ -n "$NOMINATIM_SA_KEY_PATH" ] && [ -n "$NOMINATIM_PROJECT_ID" ] && [ -n "$NOMINATIM_GS_BUCKET" ]; then
# Activate the service account to access storage
gcloud auth activate-service-account --key-file $NOMINATIM_SA_KEY_PATH
# Set the Google Cloud project ID
gcloud config set project $NOMINATIM_PROJECT_ID
# Copy the archive from storage
mkdir -p $NOMINATIM_DATA_PATH
gsutil -m cp $NOMINATIM_GS_BUCKET/$NOMINATIM_DATA_LABEL/*.tgz* $NOMINATIM_DATA_PATH
# Remove any files present in the target directory
rm -rf ${NOMINATIM_POSTGRESQL_DATA_PATH:?}/*
# Extract the archive
cat $NOMINATIM_DATA_PATH/$NOMINATIM_DATA_LABEL.tgz_* | tar xz -C $NOMINATIM_POSTGRESQL_DATA_PATH --strip-components=5
# Start PostgreSQL
service postgresql start
fi
fi
# Tail Apache logs
tail -f /var/log/apache2/* &
# Run Apache in the foreground
/usr/sbin/apache2ctl -D FOREGROUND