-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-entrypoint.sh
executable file
·401 lines (371 loc) · 18.9 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/bin/sh
set -e
source /opt/variables.sh
##### START WRITE SYSTEM CONF FILES #####
function write_fpmpool_conf() {
if [ ! -d $PHPFPM_CONF_FILEDIR ]; then
mkdir -p $PHPFPM_CONF_FILEDIR
fi
echo "[global]" > $PHPFPM_CONF_FILE
echo "error_log = ${PHPFPM_STDERR_FILE}" >> $PHPFPM_CONF_FILE
echo "[www]" >> $PHPFPM_CONF_FILE
echo "user = ${PHPFPM_USER}" >> $PHPFPM_CONF_FILE
echo "group = ${PHPFPM_GROUP}" >> $PHPFPM_CONF_FILE
echo "listen = ${PHPFPM_LISTEN}" >> $PHPFPM_CONF_FILE
echo "pm.status_path = ${PHPFPM_STATUS_PATH}" >> $PHPFPM_CONF_FILE
echo "pm = ${PHPFPM_PROCESS_MANAGER}" >> $PHPFPM_CONF_FILE
echo "pm.max_children = ${PHPFPM_MAX_CHILDREN}" >> $PHPFPM_CONF_FILE
if [[ "x${PHPFPM_PROCESS_MANAGER}" == "xondemand" ]]; then
echo "pm.process_idle_timeout = ${PHPFPM_PROCESS_IDLE_TIMEOUT};" >> $PHPFPM_CONF_FILE
fi
echo "pm.max_requests = ${PHPFPM_MAX_REQUESTS}" >> $PHPFPM_CONF_FILE
echo "clear_env = ${PHPFPM_CLEAR_ENV}" >> $PHPFPM_CONF_FILE
echo "catch_workers_output = ${PHPFPM_CATCH_WORKERS_OUTPUT}" >> $PHPFPM_CONF_FILE
echo "decorate_workers_output = ${PHPFPM_DECORATE_WORKERS_OUTPUT}" >> $PHPFPM_CONF_FILE
echo "ping.path = ${PHPFPM_PING_PATH}" >> $PHPFPM_CONF_FILE
}
function update_nginx_conf() {
sed -i 's/NGINX_WORKER_PROCESSES/'"${NGINX_WORKER_PROCESSES}"'/' $NGINX_CONF_FILE
sed -i 's/NGINX_WORKER_CONNECTIONS/'"${NGINX_WORKER_CONNECTIONS}"'/' $NGINX_CONF_FILE
sed -i 's/NGINX_STDERR_FILE/'"${NGINX_STDERR_FILE//\//\\/}"'/' $NGINX_CONF_FILE
sed -i 's/NGINX_STDOUT_FILE/'"${NGINX_STDOUT_FILE//\//\\/}"'/' $NGINX_CONF_FILE
sed -i 's/NGINX_KEEPALIVE_TIMEOUT/'"${NGINX_KEEPALIVE_TIMEOUT}"'/' $NGINX_CONF_FILE
if [ "x${NGINX_HTTPS_ENABLE}" == "xonly" ]; then
cat $CONF_DIR/nginx-redirect.conf > $NGINX_DEFAULT_CONF_FILE
cat $CONF_DIR/nginx-https.conf >> $NGINX_DEFAULT_CONF_FILE
elif [ "x${NGINX_HTTPS_ENABLE}" == "xtrue" ]; then
cat $CONF_DIR/nginx-http.conf > $NGINX_DEFAULT_CONF_FILE
cat $CONF_DIR/nginx-https.conf >> $NGINX_DEFAULT_CONF_FILE
else
cat $CONF_DIR/nginx-http.conf > $NGINX_DEFAULT_CONF_FILE
fi
sed -i 's/NGINX_HTTP_PORT/'"${NGINX_HTTP_PORT}"'/' $NGINX_DEFAULT_CONF_FILE
sed -i 's/PHPFPM_LISTEN/'"${PHPFPM_LISTEN//\//\\/}"'/' $NGINX_DEFAULT_CONF_FILE
sed -i 's/PHPFPM_PING_PATH/'"${PHPFPM_PING_PATH//\//\\/}"'/' $NGINX_DEFAULT_CONF_FILE
sed -i 's/PHPFPM_PING_STATUS_ALLOW/'"${PHPFPM_PING_STATUS_ALLOW//\//\\/}"'/' $NGINX_DEFAULT_CONF_FILE
sed -i 's/PHPFPM_STATUS_PATH/'"${PHPFPM_STATUS_PATH//\//\\/}"'/' $NGINX_DEFAULT_CONF_FILE
sed -i 's/NGINX_ROOT_DIR/'"${NGINX_ROOT_DIR//\//\\/}"'/' $NGINX_DEFAULT_CONF_FILE
sed -i 's/NGINX_SENDFILE/'"${NGINX_SENDFILE}"'/' $NGINX_DEFAULT_CONF_FILE
if [ "x${NGINX_HTTPS_ENABLE}" == "xtrue" ] || [ "x${NGINX_HTTPS_ENABLE}" == "xonly" ]; then
# Create self-signed SSL certs if they don't exist
if [ ! -f $SELF_SSL_DIR/nominatim.key ] || [ ! -f $SELF_SSL_DIR/nominatim.crt ]; then
openssl req -new -newkey rsa:2048 -days 3650 -nodes -x509 -subj "/C=CS/ST=ST/L=Nominatim/O=Nominatim/CN=nominatim" -keyout $SELF_SSL_DIR/nominatim.key -out $SELF_SSL_DIR/nominatim.crt
fi
# Update SSL settings
sed -i 's/NGINX_HTTPS_PORT/'"${NGINX_HTTPS_PORT}"'/' $NGINX_DEFAULT_CONF_FILE
sed -i 's/NGINX_SSL_CIPHERS/'"${NGINX_SSL_CIPHERS}"'/' $NGINX_DEFAULT_CONF_FILE
sed -i 's/NGINX_SSL_CRT_FILE/'"${NGINX_SSL_CRT_FILE//\//\\/}"'/' $NGINX_DEFAULT_CONF_FILE
sed -i 's/NGINX_SSL_KEY_FILE/'"${NGINX_SSL_KEY_FILE//\//\\/}"'/' $NGINX_DEFAULT_CONF_FILE
sed -i 's/NGINX_SSL_PROTOCOLS/'"${NGINX_SSL_PROTOCOLS}"'/' $NGINX_DEFAULT_CONF_FILE
fi
}
function write_supervisord_conf() {
if [ ! -d $SUPERVISORD_CONF_FILEDIR ]; then
mkdir -p $SUPERVISORD_CONF_FILEDIR
fi
echo "[supervisord]" > $SUPERVISORD_CONF_FILE
echo "nodaemon=true" >> $SUPERVISORD_CONF_FILE
echo "logfile=${SUPERVISORD_LOG_FILE}" >> $SUPERVISORD_CONF_FILE
echo "logfile_maxbytes=0" >> $SUPERVISORD_CONF_FILE
echo "pidfile=${SUPERVISORD_PID_FILE}" >> $SUPERVISORD_CONF_FILE
echo "user=${SUPERVISORD_USER}" >> $SUPERVISORD_CONF_FILE
echo "[program:php-fpm]" >> $SUPERVISORD_CONF_FILE
echo "command=${PHPFPM_COMMAND}" >> $SUPERVISORD_CONF_FILE
echo "stdout_logfile=${PHPFPM_STDOUT_FILE}" >> $SUPERVISORD_CONF_FILE
echo "stdout_logfile_maxbytes=0" >> $SUPERVISORD_CONF_FILE
echo "stderr_logfile=${PHPFPM_STDERR_FILE}" >> $SUPERVISORD_CONF_FILE
echo "stderr_logfile_maxbytes=0" >> $SUPERVISORD_CONF_FILE
echo "autorestart=false" >> $SUPERVISORD_CONF_FILE
echo "startretries=0" >> $SUPERVISORD_CONF_FILE
echo "[program:nginx]" >> $SUPERVISORD_CONF_FILE
echo "command=nginx -g 'daemon off;'" >> $SUPERVISORD_CONF_FILE
echo "stdout_logfile=${NGINX_STDOUT_FILE}" >> $SUPERVISORD_CONF_FILE
echo "stdout_logfile_maxbytes=0" >> $SUPERVISORD_CONF_FILE
echo "stderr_logfile=${NGINX_STDERR_FILE}" >> $SUPERVISORD_CONF_FILE
echo "stderr_logfile_maxbytes=0" >> $SUPERVISORD_CONF_FILE
echo "autorestart=false" >> $SUPERVISORD_CONF_FILE
echo "startretries=0" >> $SUPERVISORD_CONF_FILE
if [[ "x${NOMINATIM_POSTGRES_HOST}" == "x" ]]; then
echo "[program:postgres]" >> $SUPERVISORD_CONF_FILE
echo "command=/usr/bin/postgres" >> $SUPERVISORD_CONF_FILE
echo "user=postgres" >> $SUPERVISORD_CONF_FILE
echo "stdout_logfile=${POSTGRES_STDOUT_FILE}" >> $SUPERVISORD_CONF_FILE
echo "stdout_logfile_maxbytes=0" >> $SUPERVISORD_CONF_FILE
echo "stderr_logfile=${POSTGRES_STDERR_FILE}" >> $SUPERVISORD_CONF_FILE
echo "stderr_logfile_maxbytes=0" >> $SUPERVISORD_CONF_FILE
echo "autorestart=false" >> $SUPERVISORD_CONF_FILE
echo "startretries=0" >> $SUPERVISORD_CONF_FILE
fi
}
function config_postgres() {
if [ ! -f $POSTGRES_CONF_FILE ]; then
sudo -u postgres initdb -D $POSTGRES_DATA_DIR
echo "listen_addresses='*'" >> $POSTGRES_CONF_FILE
echo "include = '${POSTGRES_CONF_INCLUDE_FILE}'" >> $POSTGRES_CONF_FILE
write_postgres_include_conf
sudo -u postgres pg_ctl --silent start -D $POSTGRES_DATA_DIR
else
write_postgres_include_conf
fi
}
function write_postgres_include_conf() {
echo "autovacuum_work_mem = ${POSTGRES_AUTOVACUUM_WORK_MEM}" > $POSTGRES_CONF_INCLUDE_FILE
echo "checkpoint_completion_target = ${POSTGRES_CHECKPOINT_COMPLETION_TARGET}" >> $POSTGRES_CONF_INCLUDE_FILE
echo "checkpoint_timeout = ${POSTGRES_CHECKPOINT_TIMEOUT}" >> $POSTGRES_CONF_INCLUDE_FILE
echo "effective_cache_size = ${POSTGRES_EFFECTIVE_CACHE_SIZE}" >> $POSTGRES_CONF_INCLUDE_FILE
echo "external_pid_file = '${POSTGRES_PID_FILE}'" >> $POSTGRES_CONF_INCLUDE_FILE
echo "maintenance_work_mem = ${POSTGRES_MAINTENANCE_WORK_MEM}" >> $POSTGRES_CONF_INCLUDE_FILE
echo "max_wal_size = ${POSTGRES_MAX_WAL_SIZE}" >> $POSTGRES_CONF_INCLUDE_FILE
echo "shared_buffers = ${POSTGRES_SHARED_BUFFERS}" >> $POSTGRES_CONF_INCLUDE_FILE
echo "synchronous_commit = ${POSTGRES_SYNCHRONOUS_COMMIT}" >> $POSTGRES_CONF_INCLUDE_FILE
echo "work_mem = ${POSTGRES_WORK_MEM}" >> $POSTGRES_CONF_INCLUDE_FILE
}
##### END WRITE SYSTEM CONF FILES #####
##### START NOMINATIM CONFIG UPDATE #####
function manage_nominatim_creds() {
grep -c $NOMINATIM_SYSTEM_USER /etc/group | grep -q 1 || addgroup -g $NOMINATIM_SYSTEM_UID -S $NOMINATIM_SYSTEM_USER
grep -c $NOMINATIM_SYSTEM_USER /etc/passwd | grep -q 1 || adduser -S -D -u $NOMINATIM_SYSTEM_UID -G $NOMINATIM_SYSTEM_USER $NOMINATIM_SYSTEM_USER
# If postgres is local and is mounted volume users could have changed
if [[ "x${NOMINATIM_POSTGRES_HOST}" == "x" ]]; then
if [ ! -f "${POSTGRES_PID_FILE}" ]; then
sudo -u postgres pg_ctl --silent start -D $POSTGRES_DATA_DIR
fi
sudo -u postgres psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='${NOMINATIM_SYSTEM_USER}'" | grep -q 1 || sudo -u postgres createuser -s $NOMINATIM_SYSTEM_USER
sudo -u postgres psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='${NOMINATIM_DB_WEB_USER}'" | grep -q 1 || sudo -u postgres createuser -SDR $NOMINATIM_DB_WEB_USER
if [[ "x${NOMINATIM_SETUP_ENABLE}" == "xfalse" ]]; then
sudo -u postgres pg_ctl --silent stop -D $POSTGRES_DATA_DIR
fi
fi
}
function manage_nominatim_perms() {
chown -R $NOMINATIM_SYSTEM_USER:$NOMINATIM_SYSTEM_USER $NOMINATIM_HOME
}
function write_nominatim_local() {
echo "<?php" > $NOMINATIM_LOCAL_FILE
echo "@define('CONST_Postgresql_Version', '${NOMINATIM_POSTGRES_VERSION}');" >> $NOMINATIM_LOCAL_FILE
echo "@define('CONST_Postgis_Version', '${NOMINATIM_POSTGIS_VERSION}');" >> $NOMINATIM_LOCAL_FILE
echo "@define('CONST_Website_BaseURL', '${NOMINATIM_WEBSITE_BASEURL}');" >> $NOMINATIM_LOCAL_FILE
echo "@define('CONST_Database_Web_User', '${NOMINATIM_DB_WEB_USER}');" >> $NOMINATIM_LOCAL_FILE
echo "@define('CONST_Import_Style', CONST_BasePath.'/settings/${NOMINATIM_IMPORT_STYLE}.style');" >> $NOMINATIM_LOCAL_FILE
# if [[ "x${NOMINATIM_FLATNODE_ENABLE}" == "xtrue" ]]; then
# echo "@define('CONST_Osm2pgsql_Flatnode_File', '${NOMINATIM_FLATNODE_FILE}');" >> $NOMINATIM_LOCAL_FILE
# fi
# If postgres host is used
if [[ "x${NOMINATIM_POSTGRES_HOST}" != "x" ]]; then
if [[ "x${NOMINATIM_POSTGRES_PORT}" != "x" ]] && [[ "x${NOMINATIM_POSTGRES_USER}" != "x" ]] && [[ "x${NOMINATIM_POSTGRES_PASSWORD}" != "x" ]] && [[ "x${NOMINATIM_POSTGRES_DB}" != "x" ]]; then
echo "@define('CONST_Database_DSN', 'pgsql:host=${NOMINATIM_POSTGRES_HOST};port=${NOMINATIM_POSTGRES_PORT};user=${NOMINATIM_POSTGRES_USER};password=${NOMINATIM_POSTGRES_PASSWORD};dbname=${NOMINATIM_POSTGRES_DB}');" >> $NOMINATIM_LOCAL_FILE
else
echo "ERROR: NOMINATIM_POSTGRES_HOST is set but one of the following is empty: NOMINATIM_POSTGRES_PORT, NOMINATIM_POSTGRES_USER, NOMINATIM_POSTGRES_PASSWORD, NOMINATIM_POSTGRES_DB"
echo "Cannot continue...."
exit 1
fi
else
echo "@define('CONST_Database_DSN', 'pgsql:dbname=${NOMINATIM_POSTGRES_DB}');" >> $NOMINATIM_LOCAL_FILE
fi
}
##### END NOMINATIM CONFIG UPDATE #####
##### START MANAGE NOMINATIM DATA/PDF FILES #####
# Download optional data files for import
function check_nominatim_data_files() {
check_data_file $NOMINATIM_DATA_GB_POSTCODE_ENABLE $NOMINATIM_DATA_GB_POSTCODE_URL
check_data_file $NOMINATIM_DATA_US_POSTCODE_ENABLE $NOMINATIM_DATA_US_POSTCODE_URL
check_data_file $NOMINATIM_DATA_WIKIPEDIA_ARTICLE_ENABLE $NOMINATIM_DATA_WIKIPEDIA_ARTICLE_URL
check_data_file $NOMINATIM_DATA_WIKIPEDIA_REDIRECT_ENABLE $NOMINATIM_DATA_WIKIPEDIA_REDIRECT_URL
}
function check_data_file() {
DATA_FILE_ENABLE=$1
DATA_FILE_URL=$2
if [[ "x${DATA_FILE_ENABLE}" == "xtrue" ]]; then
DATA_FILE=${DATA_FILE_URL##*/}
if [ ! -f $NOMINATIM_DATA_DIR/$DATA_FILE ]; then
download_data_file $DATA_FILE_URL $DATA_FILE
fi
fi
}
# Download the data file
function download_data_file() {
DATA_FILE_URL=$1
DATA_FILE=$2
echo "Downloading ${DATA_FILE_URL} to ${NOMINATIM_DATA_DIR}"
sudo -u $NOMINATIM_SYSTEM_USER curl -k -# -L "${DATA_FILE_URL}" -o "${NOMINATIM_DATA_DIR}/${DATA_FILE}"
}
function check_nominatim_pbf_files() {
if [ ! -d $NOMINATIM_PBF_DIR ]; then
sudo -u $NOMINATIM_SYSTEM_USER mkdir -p $NOMINATIM_PBF_DIR
fi
check_region_pbf_file $NOMINATIM_PBF_AFRICA_ENABLE $NOMINATIM_PBF_AFRICA_REGION_URL $NOMINATIM_PBF_AFRICA_REGION
check_region_pbf_file $NOMINATIM_PBF_ASIA_ENABLE $NOMINATIM_PBF_ASIA_REGION_URL $NOMINATIM_PBF_ASIA_REGION
check_region_pbf_file $NOMINATIM_PBF_AUSTRALIA_OCEANIA_ENABLE $NOMINATIM_PBF_AUSTRALIA_OCEANIA_REGION_URL $NOMINATIM_PBF_AUSTRALIA_OCEANIA_REGION
check_region_pbf_file $NOMINATIM_PBF_CENTRAL_AMERICA_ENABLE $NOMINATIM_PBF_CENTRAL_AMERICA_REGION_URL $NOMINATIM_PBF_CENTRAL_AMERICA_REGION
check_region_pbf_file $NOMINATIM_PBF_EUROPE_ENABLE $NOMINATIM_PBF_EUROPE_REGION_URL $NOMINATIM_PBF_EUROPE_REGION
check_region_pbf_file $NOMINATIM_PBF_NORTH_AMERICA_ENABLE $NOMINATIM_PBF_NORTH_AMERICA_REGION_URL $NOMINATIM_PBF_NORTH_AMERICA_REGION
check_region_pbf_file $NOMINATIM_PBF_SOUTH_AMERICA_ENABLE $NOMINATIM_PBF_SOUTH_AMERICA_REGION_URL $NOMINATIM_PBF_SOUTH_AMERICA_REGION
check_continent_pbf_file $NOMINATIM_PBF_AFRICA_ENABLE $NOMINATIM_PBF_AFRICA_URL
check_continent_pbf_file $NOMINATIM_PBF_ASIA_ENABLE $NOMINATIM_PBF_ASIA_URL
check_continent_pbf_file $NOMINATIM_PBF_ANTARCTICA_ENABLE $NOMINATIM_PBF_ANTARCTICA_URL
check_continent_pbf_file $NOMINATIM_PBF_AUSTRALIA_OCEANIA_ENABLE $NOMINATIM_PBF_AUSTRALIA_OCEANIA_URL
check_continent_pbf_file $NOMINATIM_PBF_CENTRAL_AMERICA_ENABLE $NOMINATIM_PBF_CENTRAL_AMERICA_URL
check_continent_pbf_file $NOMINATIM_PBF_EUROPE_ENABLE $NOMINATIM_PBF_EUROPE_URL
check_continent_pbf_file $NOMINATIM_PBF_NORTH_AMERICA_ENABLE $NOMINATIM_PBF_NORTH_AMERICA_URL
check_continent_pbf_file $NOMINATIM_PBF_SOUTH_AMERICA_ENABLE $NOMINATIM_PBF_SOUTH_AMERICA_URL
check_planet_pbf_file $NOMINATIM_PBF_PLANET_ENABLE $NOMINATIM_PBF_PLANET_URL
}
# Check if PDF file enabled and exists
function check_region_pbf_file() {
BPF_REGION_ENABLE=$1
BPF_REGION_URL=$2
BPF_REGION=$3
if [[ "x${BPF_REGION}" != "x" ]]; then
if [[ "x${BPF_REGION_ENABLE}" != "xtrue" ]]; then
for REGION in $(echo $BPF_REGION | tr "," "\n")
do
BPF_FILE="${REGION}-latest.osm.pbf"
BPF_URL=$BPF_REGION_URL/$BPF_FILE
if [ ! -f $NOMINATIM_PBF_DIR/$BPF_FILE ]; then
download_pbf_file $BPF_URL $BPF_FILE
fi
#Add to update list
REGION_PATH=${BPF_REGION_URL##*/}
echo $REGION_PATH"/"$REGION >> $NOMINATIM_PBF_UPDATE_LIST_FILE
done
else
echo "ERROR: Continent PBF can't be enabled if continent region is set. Cannot continue...."
exit 1
fi
fi
}
function check_continent_pbf_file() {
BPF_CONTINENT_ENABLE=$1
BPF_CONTINENT_URL=$2
if [[ "x${BPF_CONTINENT_ENABLE}" == "xtrue" ]]; then
if [[ "x${NOMINATIM_PBF_PLANET_ENABLE}" != "xtrue" ]]; then
BPF_CONTINENT_FILE=${BPF_CONTINENT_URL##*/}
if [ ! -f $NOMINATIM_PBF_DIR/$BPF_CONTINENT_FILE ]; then
download_pbf_file $BPF_CONTINENT_URL $BPF_CONTINENT_FILE
fi
#Add to update list
CONTINENT=${BPF_CONTINENT_FILE%-*}
echo $CONTINENT >> $NOMINATIM_PBF_UPDATE_LIST_FILE
else
echo "ERROR: Planet PBF can't be enabled if continent is set. Cannot continue...."
exit 1
fi
fi
}
function check_planet_pbf_file() {
BPF_PLANET_ENABLE=$1
BPF_PLANET_URL=$2
if [[ "x${BPF_PLANET_ENABLE}" == "xtrue" ]]; then
BPF_PLANET_FILE=${BPF_PLANET_URL##*/}
if [ ! -f $NOMINATIM_PBF_DIR/$BPF_PLANET_FILE ]; then
download_pbf_file $BPF_PLANET_URL $BPF_PLANET_FILE
fi
fi
}
# Download the PDF file
function download_pbf_file() {
BPF_URL=$1
BPF_FILE=$2
echo "Downloading ${BPF_URL} to ${NOMINATIM_PBF_DIR}"
sudo -u $NOMINATIM_SYSTEM_USER curl -k -# -L "${BPF_URL}" -o "${NOMINATIM_PBF_DIR}/${BPF_FILE}"
if [[ "x${NOMINATIM_PBF_VERIFY_MD5}" == "xtrue" ]]; then
check_pbf_md5 $BPF_URL $BPF_FILE
fi
}
# Verify md5 if enabled
function check_pbf_md5() {
BPF_URL=$1
BPF_FILE=$2
echo "Verifying ${BPF_FILE} MD5 sum...."
echo "Downloading ${BPF_URL}.md5 to ${NOMINATIM_PBF_DIR}"
sudo -u $NOMINATIM_SYSTEM_USER curl -k -# -L "${BPF_URL}.md5" -o "${NOMINATIM_PBF_DIR}/${BPF_FILE}.md5"
MD5SUM=$(cat ${NOMINATIM_PBF_DIR}/${BPF_FILE}.md5 | cut -d " " -f1)
MD5SUM_PBF=$(md5sum ${NOMINATIM_PBF_DIR}/${BPF_FILE} | cut -d " " -f1)
if [[ "x${MD5SUM}" == "x${MD5SUM_PBF}" ]]; then
echo "${BPF_FILE} MD5 sum matches, proceeding...."
else
echo "MD5 sum does not match. Redownloading ${BPF_URL}"
download_pbf_file $BPF_URL $BPF_FILE
fi
}
# Count PBF files and merge if more than one
function set_nominatim_pbf_import_file() {
PBF_COUNT=$(ls -l ${NOMINATIM_PBF_DIR} | grep osm.pbf | grep -v md5 | wc -l )
if [[ $PBF_COUNT -gt 1 ]]; then
echo ""
echo "More than one PBF file found. Merging the below files to temp import file: ${NOMINATIM_DATA_DIR}/import.osm.pbf"
ls $NOMINATIM_PBF_DIR | grep osm.pbf | grep -v md5
echo ""
sudo -u $NOMINATIM_SYSTEM_USER osmium merge --progress --overwrite $NOMINATIM_PBF_DIR/*.osm.pbf -o $NOMINATIM_DATA_DIR/import.osm.pbf
NOMINATIM_PBF_IMPORT_FILE=$NOMINATIM_DATA_DIR/import.osm.pbf
echo "PBF merge complete!"
elif [[ $PBF_COUNT -eq 1 ]]; then
NOMINATIM_PBF_IMPORT_FILE=$NOMINATIM_PBF_DIR/$(ls ${NOMINATIM_PBF_DIR} | grep osm.pbf | grep -v md5)
fi
}
##### END MANAGE NOMINATIM DATA/PDF FILES #####
function run_nominatim_setup() {
if [[ "x${NOMINATIM_PBF_IMPORT_FILE}" != "x" ]]; then
NOMINATIM_SETUP_OPTS=$NOMINATIM_SETUP_OPTS" --all --threads ${NOMINATIM_THREADS}"
# if [[ "x${NOMINATIM_FLATNODE_ENABLE}" == "xfalse" ]]; then
#Get size of PBF file in MB
PBF_IMPORT_SIZE=$(($( stat -c '%s' $NOMINATIM_PBF_IMPORT_FILE ) / 1000000))
if [[ $PBF_IMPORT_SIZE -lt $NOMINATIM_OSM2PGSQL_CACHE ]]; then
OSM2PGSQL_CACHE=$PBF_IMPORT_SIZE
else
OSM2PGSQL_CACHE=$NOMINATIM_OSM2PGSQL_CACHE
fi
NOMINATIM_SETUP_OPTS=$NOMINATIM_SETUP_OPTS" --osm2pgsql-cache ${OSM2PGSQL_CACHE}"
# fi
if [[ "x${NOMINATIM_REVERSE_ONLY_ENABLE}" == "xtrue" ]]; then
NOMINATIM_SETUP_OPTS=$NOMINATIM_SETUP_OPTS" --reverse-only"
fi
echo "Running Nominatim setup: ${NOMINATIM_BUILD_DIR}/utils/setup.php --osm-file ${NOMINATIM_PBF_IMPORT_FILE} ${NOMINATIM_SETUP_OPTS}"
sudo -u postgres psql postgres -tAc "ALTER SYSTEM SET fsync TO 'off'"
sudo -u postgres psql postgres -tAc "ALTER SYSTEM SET full_page_writes TO 'off'"
echo "Dropping Postgres DB: ${NOMINATIM_POSTGRES_DB}"
sudo -u postgres psql postgres -c "DROP DATABASE IF EXISTS ${NOMINATIM_POSTGRES_DB}"
sudo -u $NOMINATIM_SYSTEM_USER $NOMINATIM_BUILD_DIR/utils/setup.php --osm-file $NOMINATIM_PBF_IMPORT_FILE $NOMINATIM_SETUP_OPTS 2>&1 | tee $NOMINATIM_DATA_DIR/setup.log
if [[ "x${NOMINATIM_PBF_UPDATE_ENABLE}" == "xtrue" ]]; then
echo "Running PBF data update script."
sh /opt/update-pbf-data.sh 2>&1 | tee $NOMINATIM_DATA_DIR/update.log
fi
sudo -u postgres psql postgres -tAc "ALTER SYSTEM SET fsync TO 'on'"
sudo -u postgres psql postgres -tAc "ALTER SYSTEM SET full_page_writes TO 'on'"
fi
}
function clean_nominatim_setup() {
if [ -f $NOMINATIM_DATA_DIR/import.osm.pbf ]; then
rm -rf $NOMINATIM_DATA_DIR/import.osm.pbf
fi
sudo -u postgres pg_ctl --silent stop -D $POSTGRES_DATA_DIR
}
function config_pbf_update_schedule() {
if [ "x${NOMINATIM_PBF_UPDATE_SCHEDULE}" == "xdaily" ] || [ "x${NOMINATIM_PBF_UPDATE_SCHEDULE}" == "xweekly" ] || [ "x${NOMINATIM_PBF_UPDATE_SCHEDULE}" == "xmonthly" ]; then
ln -s /opt/update-pbf-data.sh /etc/periodic/$NOMINATIM_PBF_UPDATE_SCHEDULE/update-pbf-data
fi
}
#Set Timezone
cp /usr/share/zoneinfo/$TZ /etc/localtime
echo "${TZ}" > /etc/timezone
write_fpmpool_conf
update_nginx_conf
# Check if external DB host
if [[ "x${NOMINATIM_POSTGRES_HOST}" == "x" ]]; then
config_postgres
fi
write_supervisord_conf
manage_nominatim_creds
write_nominatim_local
if [[ "x${NOMINATIM_SETUP_ENABLE}" == "xtrue" ]]; then
check_nominatim_data_files
check_nominatim_pbf_files
set_nominatim_pbf_import_file
run_nominatim_setup
clean_nominatim_setup
fi
manage_nominatim_perms
if [[ "x${NOMINATIM_PBF_UPDATE_ENABLE}" == "xtrue" ]]; then
config_pbf_update_schedule
fi
exec /usr/bin/supervisord -c $SUPERVISORD_CONF_FILE