forked from utagawal/MTBmaptiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_planet.sh
executable file
·159 lines (133 loc) · 4.38 KB
/
update_planet.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
#! /bin/bash
ADMINEMAIL="[email protected]"
CLEARDATE=$(date "+%d%m%y_%Hh%M")
CTNAME="tileserver-gl"
LOGDIR="/var/data/logs/"
LOGFILE="update_planet.log"
LOCKFILE="update_planet.lock"
MBTILESNAME="planet.mbtiles"
MBTILESDIR="/var/data/mbtiles/"
TMPDIR="/tmp/mbtiles/"
# Send a signal to tileserver-gl container, to reload it
ctReload() {
docker kill -s HUP ${1} >/dev/null 2>&1
}
# Start tileserver-gl container
ctStart() {
docker run --name ${1} -d --rm -it -v /var/data:/data -p 8080:80 maptiler/tileserver-gl
}
# Check if tileserver-gl is running
ctStatus() {
docker ps -f NAME=${1} -f STATUS=running -q
}
# Check the presence of a lock file and if it is obsolete
#
# If a lock file exists, but no process is running with the
# pid written inside, we erase it and return that the lock
# file does not exists.
lockCheck() {
if [ -f ${TMPDIR}${LOCKFILE} ]; then
if [ -z "$(ps --no-headers $(cat ${TMPDIR}${LOCKFILE}))" ]; then
rm ${TMPDIR}${LOCKFILE}
return 1
fi
return 0
else
return 1
fi
}
# Remove the lock file
lockRemove() {
rm -f ${TMPDIR}${LOCKFILE}
}
# Write the lock file
lockWrite() {
echo $$ > ${TMPDIR}${LOCKFILE}
}
writeToLog() {
local LOGTIME=$(date "+%d-%m-%y %H:%M:%S")
echo ${LOGTIME} ${1} >> ${LOGDIR}${LOGFILE}
}
if [ -z $(lockCheck) ]; then
lockWrite
else
writeToLog "The script is already running"
exit
fi
mkdir -p ${TMPDIR} ${LOGDIR}
cd ${TMPDIR}
# Downloading planet.mbtiles.lz4
writeToLog "Downloading ${MBTILESNAME}"
wget -q -nd -np -r --timestamping --accept-regex='.*-planet\.mbtiles\.lz4$' -e robots=off https://osm.dbtc.link/mbtiles/
if [ $? -eq 0 ]; then
writeToLog "${MBTILESNAME} downloaded."
# Extracting planet.mbtiles.lz4 to planet.mbtiles
writeToLog "extracting ${MBTILESNAME} ..."
lz4 -d -f -q --rm *-planet.mbtiles.lz4
if [ $? -eq 0 ]; then
writeToLog "${MBTILESNAME} successfully extracted."
# Dowloading its checksum
wget -q -nd -np -r --accept-regex='.*-planet\.mbtiles\.sha256$' -e robots=off https://osm.dbtc.link/mbtiles/
if [ $? -eq 0 ]; then
# Verifying the checksum
writeToLog "verifying ${MBTILESNAME} checksum ..."
sha256sum --status -c *-planet.mbtiles.sha256
if [ $? -eq 0 ]; then
writeToLog "${MBTILESNAME} checksum OK."
rm *-planet.mbtiles.sha256
# Saving the current mbtiles
writeToLog "Saving old planet.mbtiles to ${MBTILESNAME}.${CLEARDATE} ..."
mv ${MBTILESDIR}${MBTILESNAME} ${MBTILESDIR}${MBTILESNAME}.${CLEARDATE}
if [ $? -eq 0 ]; then
# Deploying the new mbtiles
writeToLog "Deploying new ${MBTILESNAME}."
mv ${TMPDIR}/*-planet.mbtiles ${MBTILESDIR}${MBTILESNAME}
if [ $? -eq 0 ]; then
if [ -z $(ctStatus $CTNAME) ]; then
# Container is not running, so we start it
writeToLog "Starting tileserver-gl"
ctStart $CTNAME
if [ $? -eq 0 ]; then
writeToLog "Started tileserver-gl successfully"
else
writeToLog "An error occur while starting tileserver-gl"
fi
else
# Container is running, we ask it to reload tileserver-gl config file
writeToLog "Reloading tileserver-gl"
ctReload $CTNAME
if [ $? -eq 0 ]; then
writeToLog "Reloaded tileserver-gl successfully"
else
writeToLog "An error occured while reloading tileserver-gl"
fi
fi
sleep 15
if [ -z $(ctStatus $CTNAME) ]; then
writeToLog "Unable to launch tileserver-gl with the new mbtiles file."
echo "Unable to launch tileserver-gl with the new mbtiles file" | mail -s "tilserver-gl problem" ${ADMINEMAIL}
else
writeToLog "tileserver-gl started successfully with the new mbtiles file."
echo "The new ${MBTILESNAME} has been applied successfully" | mail -s "The new ${MBTILESNAME} has been applied successfully" ${ADMINEMAIL}
fi
else
writeToLog "A problem occured while moving ${MBTILESNAME} to ${MBTILESDIR}."
writeToLog "Restoring the old ${MBTILESNAME} ..."
mv ${MBTILESDIR}${MBTILESNAME}.${CLEARDATE} ${MBTILESDIR}${MBTILESNAME}
fi
else
writeToLog "Unable to backup the current mbtiles file."
fi
else
writeToLog "The checksum of ${MBTILESNAME} is incorrect."
fi
else
writeToLog "A problem occured while downloading the checksum file."
fi
else
writeToLog "A problem occured while uncompressing ${MBTILESNAME}.lz4."
fi
else
writeToLog "A problem occured while downloading ${MBTILESNAME}.lz4."
fi
lockRemove