forked from utagawal/MTBmaptiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_glstyle.sh
executable file
·142 lines (114 loc) · 4.31 KB
/
update_glstyle.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
#! /bin/bash
ADMINEMAIL="olivier@omrs.fr"
CLEARDATE=$(date "+%d%m%y_%Hh%M")
CTNAME="tileserver-gl"
LOGDIR="/var/data/logs/"
LOGFILE="update_glstyle.log"
LOCKFILE="update_glstyle.lock"
GLSTYLENAME="utagawavtt"
GLSTYLEDIR="/var/data/styles/"
TMPDIR="/tmp/glstyle/"
# 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}
# Downloading style file on the repository
writeToLog "Downloading ${GLSTYLENAME}.json"
wget -q https://raw.githubusercontent.com/utagawal/MTBmaptiles/main/${GLSTYLENAME}.json -P ${TMPDIR}
if [ $? -eq 0 ]; then
writeToLog "Style file downloaded"
writeToLog "Replacing URLs generated by maputnkik, with local ones"
# mbtiles
sed -i 's/https:\/\/maps\.utagawavtt\.com\/data\/europe-contours-copernicus\.json/mbtiles:\/\/{europe-contours-copernicus}/g' ${TMPDIR}${GLSTYLENAME}.json
sed -i 's/https:\/\/maps\.utagawavtt\.com\/data\/planet\.json/mbtiles:\/\/{planet}/g' ${TMPDIR}${GLSTYLENAME}.json
# sprites and fonts
sed -i 's/https:\/\/maps\.utagawavtt\.com\/styles\/utagawavtt\/sprite/{style}/g' ${TMPDIR}${GLSTYLENAME}.json
sed -i 's/https:\/\/maps\.utagawavtt\.com\/fonts\/{fontstack}\/{range}\.pbf/{fontstack}\/{range}/g' ${TMPDIR}${GLSTYLENAME}.json
# Comparing repository's style file and local style file
cmp -s ${GLSTYLEDIR}${GLSTYLENAME}.json ${TMPDIR}${GLSTYLENAME}.json
if [ $? -eq 0 ]; then # Files are identicals
writeToLog "Style has not been updated"
rm ${TMPDIR}${GLSTYLENAME}.json
else # Files are differents
writeToLog "Saving old style to ${GLSTYLEDIR}${GLSTYLENAME}.json_${CLEARDATE}"
cp ${GLSTYLEDIR}${GLSTYLENAME}.json ${GLSTYLEDIR}${GLSTYLENAME}.json_${CLEARDATE}
writeToLog "Applying new style"
cp ${TMPDIR}${GLSTYLENAME}.json ${GLSTYLEDIR}${GLSTYLENAME}.json
rm ${TMPDIR}${GLSTYLENAME}.json
if [ -z $(ctStatus $CTNAME) ]; then
# Container is not running, so we start it
writeToLog "Starting tileserver-gl"
ctStart $CTNAME
else
# Container is running, we ask it to reload tileserver-gl config file
writeToLog "Reloading tileserver-gl"
ctReload $CTNAME
fi
sleep 30
if [ -z $(ctStatus $CTNAME) ]; then
writeToLog "The tileserver-gl container is down."
writeToLog "Restoring the before last style file : ${GLSTYLENAME}.json_${CLEARDATE}"
cp ${GLSTYLEDIR}${GLSTYLENAME}.json_${CLEARDATE} ${GLSTYLEDIR}${GLSTYLENAME}.json
writeToLog "Trying to launch tilserver-gl, please wait."
ctStart $CTNAME
sleep 15
if [ -z $(ctStatus $CTNAME) ]; then
writeToLog "Unable to launch tileserver-gl with ${GLSTYLENAME}.json_${CLEARDATE}."
echo "Unable to launch tileserver-gl with the before last style file" | mail -s "tilserver-gl problem" ${ADMINEMAIL}
else
writeToLog "tileserver-gl started successfully with ${GLSTYLENAME}.json_${CLEARDATE}."
echo "${GLSTYLENAME}.json_${CLEARDATE} applied successfully" | mail -s "New style can not be applied on tilserver-gl" ${ADMINEMAIL}
fi
else
writeToLog "New style applied successfully."
echo "New style applied successfully" | mail -s "A new style has been applied on tilserver-gl" ${ADMINEMAIL}
fi
fi
else
writeToLog "Problem while downloading style from the repository : "${?}
echo "Problem while downloading style from the repository." | mail -s "Unable to download the new style" ${ADMINEMAIL}
fi
lockRemove