forked from wiedehopf/tar1090
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·481 lines (406 loc) · 18.1 KB
/
install.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/bin/bash
set -e
trap 'echo "[ERROR] Error in line $LINENO when executing: $BASH_COMMAND"' ERR
renice 10 $$
if [ -d /bup ]; then
echo Talk to @PIL. Dieses Skript ist nichts fuer dich!
exit 1
fi
srcdir=/run/dump1090-fa
repo="https://github.com/flyitalyadsb/tar1090"
db_repo="https://github.com/wiedehopf/tar1090-db"
ipath=/usr/local/share/tar1090
lighttpd=no
nginx=no
mkdir -p $ipath
mkdir -p $ipath/aircraft_sil
if ! id -u tar1090 &>/dev/null
then
adduser --system --home $ipath --no-create-home --quiet tar1090 || adduser --system --home-dir $ipath --no-create-home tar1090
fi
# terminate with /
command_package="git git/jq jq/wget wget"
packages=()
while read -r -d '/' CMD PKG
do
if ! command -v "$CMD" &>/dev/null
then
#echo "command $CMD not found, will try to install package $PKG"
packages+=("$PKG")
fi
done < <(echo "$command_package")
if [[ -n "${packages[*]}" ]]; then
if ! command -v "apt-get" &>/dev/null; then
echo "Please install the following packages and rerun the install:"
echo "${packages[*]}"
exit 1
fi
echo "Installing required packages: ${packages[*]}"
if ! apt-get install -y --no-install-suggests --no-install-recommends "${packages[@]}"; then
apt-get update || true
apt-get install -y --no-install-suggests --no-install-recommends "${packages[@]}" || true
fi
hash -r || true
while read -r -d '/' CMD PKG
do
if ! command -v "$CMD" &>/dev/null
then
echo "command $CMD not found, seems we failed to install package $PKG"
echo "FATAL: Exiting!"
exit 1
fi
done < <(echo "$command_package")
fi
if [ -d /etc/lighttpd/conf.d/ ] && ! [ -d /etc/lighttpd/conf-enabled/ ] && ! [ -d /etc/lighttpd/conf-available ] && command -v lighttpd &>/dev/null
then
ln -s /etc/lighttpd/conf.d /etc/lighttpd/conf-enabled
mkdir -p /etc/lighttpd/conf-available
fi
if [ -d /etc/lighttpd/conf-enabled/ ] && [ -d /etc/lighttpd/conf-available ] && command -v lighttpd &>/dev/null
then
lighttpd=yes
fi
if command -v nginx &>/dev/null
then
nginx=yes
fi
dir=$(pwd)
if (( $( { du -s "$ipath/git-db" 2>/dev/null || echo 0; } | cut -f1) > 150000 )); then
rm -rf "$ipath/git-db"
fi
function getGIT() {
# getGIT $REPO $BRANCH $TARGET (directory)
if [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]]; then echo "getGIT wrong usage, check your script or tell the author!" 1>&2; return 1; fi
REPO="$1"; BRANCH="$2"; TARGET="$3"; pushd . >/dev/null
if cd "$TARGET" &>/dev/null && git fetch --depth 1 origin "$BRANCH" 2>/dev/null && git reset --hard FETCH_HEAD; then popd >/dev/null && return 0; fi
if ! cd /tmp || ! rm -rf "$TARGET"; then popd > /dev/null; return 1; fi
if git clone --depth 1 --single-branch --branch "$BRANCH" "$REPO" "$TARGET"; then popd > /dev/null; return 0; fi
rm -rf "$TARGET"; tmp=/tmp/getGIT-tmp-tar1090
if wget -O "$tmp" "$REPO/archive/refs/heads/$BRANCH.zip" && unzip "$tmp" -d "$tmp.folder" >/dev/null; then
if mv -fT "$tmp.folder/$(ls "$tmp.folder")" "$TARGET"; then rm -rf "$tmp" "$tmp.folder"; popd > /dev/null; return 0; fi
fi
rm -rf "$tmp" "$tmp.folder"; popd > /dev/null; return 1;
}
function revision() {
git rev-parse --short HEAD 2>/dev/null || echo "$RANDOM-$RANDOM"
}
if ! { [[ "$1" == "test" ]] && cd "$ipath/git-db"; }; then
getGIT "$db_repo" "master" "$ipath/git-db" || true
fi
if ! cd $ipath/git-db
then
echo "Unable to download files, exiting! (Maybe try again?)"
exit 1
fi
DB_VERSION=$(revision)
cd "$dir"
if [[ "$1" == "test" ]]
then
rm -r /tmp/tar1090-test 2>/dev/null || true
mkdir -p /tmp/tar1090-test
cp -r ./* /tmp/tar1090-test
cd /tmp/tar1090-test
TAR_VERSION=$(date +%s)
else
if ! getGIT "$repo" "master" "$ipath/git" || ! cd "$ipath/git"
then
echo "Unable to download files, exiting! (Maybe try again?)"
exit 1
fi
TAR_VERSION="$(revision)"
fi
if [[ -n $1 ]] && [ "$1" != "test" ] ; then
srcdir=$1
elif [ -f /etc/default/tar1090_instances ]; then
true
elif [[ -f /run/dump1090-fa/aircraft.json ]] ; then
srcdir=/run/dump1090-fa
elif [[ -f /run/readsb/aircraft.json ]]; then
srcdir=/run/readsb
elif [[ -f /run/adsbexchange-feed/aircraft.json ]]; then
srcdir=/run/adsbexchange-feed
elif [[ -f /run/dump1090/aircraft.json ]]; then
srcdir=/run/dump1090
elif [[ -f /run/dump1090-mutability/aircraft.json ]]; then
srcdir=/run/dump1090-mutability
elif [[ -f /run/skyaware978/aircraft.json ]]; then
srcdir=/run/skyaware978
else
echo --------------
echo FATAL: could not find aircraft.json in any of the usual places!
echo "checked these: /run/readsb /run/dump1090-fa /run/dump1090 /run/dump1090-mutability /run/adsbexchange-feed /run/skyaware978"
echo --------------
echo "You need to have a decoder installed first, readsb is recommended:"
echo "https://github.com/wiedehopf/adsb-scripts/wiki/Automatic-installation-for-readsb"
echo --------------
exit 1
fi
if [[ -n $2 ]]; then
instances="$srcdir $2"
elif [[ -n $1 ]] && [ "$1" != "test" ] ; then
instances="$srcdir tar1090"
elif [ -f /etc/default/tar1090_instances ]; then
instances=$(</etc/default/tar1090_instances)
else
instances="$srcdir tar1090"
fi
if [[ -d /usr/local/share/adsbexchange-978 ]]; then
instances+="\n /run/adsbexchange-978 ax978"
fi
instances=$(echo -e "$instances" | grep -v -e '^#')
if ! diff tar1090.sh /usr/local/share/tar1090/tar1090.sh &>/dev/null; then
changed=yes
while read -r srcdir instance; do
if [[ -z "$srcdir" || -z "$instance" ]]; then
continue
fi
if [[ "$instance" != "tar1090" ]]; then
service="tar1090-$instance"
else
service="tar1090"
fi
systemctl stop $service 2>/dev/null || true
done < <(echo "$instances")
cp tar1090.sh $ipath
fi
# copy over base files
cp install.sh uninstall.sh getupintheair.sh LICENSE README.md $ipath
cp default $ipath/example_config_dont_edit
rm -f $ipath/default
# create 95-tar1090-otherport.conf
{
echo '# serve tar1090 directly on port 8504'
echo '$SERVER["socket"] == ":8504" {'
cat 88-tar1090.conf
echo '}'
} > 95-tar1090-otherport.conf
services=""
names=""
otherport=""
while read -r srcdir instance
do
if [[ -z "$srcdir" || -z "$instance" ]]; then
continue
fi
TMP="$ipath/.instance_tmp"
rm -rf "$TMP"
mkdir -p "$TMP"
chmod 755 "$TMP"
if [[ "$instance" != "tar1090" ]]; then
html_path="$ipath/html-$instance"
service="tar1090-$instance"
else
html_path="$ipath/html"
service="tar1090"
fi
services+="$service "
names+="$instance "
# don't overwrite existing configuration
cp -n default /etc/default/$service
sed -i -e 's/skyview978/skyaware978/' /etc/default/$service
sed -i.orig -e "s?SOURCE_DIR?$srcdir?g" -e "s?SERVICE?$service?g" \
-e "s?/INSTANCE??g" -e "s?HTMLPATH?$html_path?g" 95-tar1090-otherport.conf
if [[ "$instance" == "webroot" ]]; then
sed -i.orig -e "s?SOURCE_DIR?$srcdir?g" -e "s?SERVICE?$service?g" \
-e "s?/INSTANCE??g" -e "s?HTMLPATH?$html_path?g" 88-tar1090.conf
sed -i.orig -e "s?SOURCE_DIR?$srcdir?g" -e "s?SERVICE?$service?g" \
-e "s?/INSTANCE/?/?g" -e "s?HTMLPATH?$html_path?g" nginx.conf
sed -i -e "s?/INSTANCE?/?g" nginx.conf
else
sed -i.orig -e "s?SOURCE_DIR?$srcdir?g" -e "s?SERVICE?$service?g" \
-e "s?INSTANCE?$instance?g" -e "s?HTMLPATH?$html_path?g" 88-tar1090.conf
sed -i.orig -e "s?SOURCE_DIR?$srcdir?g" -e "s?SERVICE?$service?g" \
-e "s?INSTANCE?$instance?g" -e "s?HTMLPATH?$html_path?g" nginx.conf
fi
if [[ $lighttpd == yes ]] && lighttpd -v | grep -E 'lighttpd/1.4.(5[6-9]|[6-9])' -qs; then
sed -i -e 's/compress.filetype/deflate.mimetypes/' 88-tar1090.conf
sed -i -e 's/compress.filetype/deflate.mimetypes/' 95-tar1090-otherport.conf
if ! grep -qs -e '^[^#]*"mod_deflate"' /etc/lighttpd/lighttpd.conf /etc/lighttpd/conf-enabled/*; then
sed -i -e 's/^[^#]*deflate.mimetypes/#\0/' 88-tar1090.conf
sed -i -e 's/^[^#]*deflate.mimetypes/#\0/' 95-tar1090-otherport.conf
fi
fi
sed -i.orig -e "s?SOURCE_DIR?$srcdir?g" -e "s?SERVICE?$service?g" tar1090.service
cp -r -T html "$TMP"
cp -r -T "$ipath/git-db/db" "$TMP/db-$DB_VERSION"
sed -i -e "s/let databaseFolder = .*;/let databaseFolder = \"db-$DB_VERSION\";/" "$TMP/index.html"
echo "{ \"tar1090Version\": \"$TAR_VERSION\", \"databaseVersion\": \"$DB_VERSION\" }" > "$TMP/version.json"
# keep some stuff around
mv "$html_path/config.js" "$TMP/config.js" 2>/dev/null || true
mv "$html_path/upintheair.json" "$TMP/upintheair.json" 2>/dev/null || true
# in case we have offlinemaps installed, modify config.js
MAX_OFFLINE=""
for i in {0..15}; do
if [[ -d /usr/local/share/osm_tiles_offline/$i ]]; then
MAX_OFFLINE=$i
fi
done
if [[ -n "$MAX_OFFLINE" ]]; then
if ! grep "$TMP/config.js" -e '^offlineMapDetail.*' -qs &>/dev/null; then
echo "offlineMapDetail=$MAX_OFFLINE;" >> "$TMP/config.js"
else
sed -i -e "s/^offlineMapDetail.*/offlineMapDetail=$MAX_OFFLINE;/" "$TMP/config.js"
fi
fi
cp "$ipath/customIcon.png" "$TMP/images/tar1090-favicon.png" &>/dev/null || true
# bust cache for all css and js files
dir=$(pwd)
cd "$TMP"
sed -i -e "s/tar1090 on github/tar1090 on github ($(date +%y%m%d))/" index.html
# cache busting sprites.png
{
spritename="sprites_$(md5sum images/sprites.png | cut -d' ' -f1).png"
mv "images/sprites.png" "images/${spritename}"
sed -i -e "s#sprites.png#${spritename}#g" script.js
sed -i -e "s#sprites.png#${spritename}#g" index.html
}
# cache busting js / css
{
sedargs=("sed" "-i" "index.html")
for file in dbloader.js defaults.js early.js flags.js formatter.js layers.js markers.js planeObject.js registrations.js script.js style.css; do
md5sum=$(md5sum $file | cut -d' ' -f1)
prefix=$(cut -d '.' -f1 <<< "$file")
postfix=$(cut -d '.' -f2 <<< "$file")
newname="${prefix}_${md5sum}.${postfix}"
mv "$file" "$newname"
if [[ $nginx == yes ]]; then
gzip -k -3 "$newname"
fi
sedargs+=("-e" "s#${file}#${newname}#")
done
"${sedargs[@]}"
}
if [[ $nginx == yes ]]; then
gzip -k -3 ./libs/*.js
fi
rm -rf "$html_path"
mv "$TMP" "$html_path"
cd "$dir"
cp nginx.conf "$ipath/nginx-$service.conf"
if [[ $lighttpd == yes ]]; then
# clean up broken symlinks in conf-enabled ...
for link in /etc/lighttpd/conf-enabled/*; do [[ -e "$link" ]] || rm -f "$link"; done
if [[ "$otherport" != "done" ]]; then
cp 95-tar1090-otherport.conf /etc/lighttpd/conf-available/
ln -f -s /etc/lighttpd/conf-available/95-tar1090-otherport.conf /etc/lighttpd/conf-enabled/95-tar1090-otherport.conf
otherport="done"
if [ -f /etc/lighttpd/conf.d/69-skybup.conf ]; then
mv /etc/lighttpd/conf-enabled/95-tar1090-otherport.conf /etc/lighttpd/conf-enabled/68-tar1090-otherport.conf
fi
fi
if [ -f /etc/lighttpd/conf.d/69-skybup.conf ] && [[ "$instance" == "webroot" ]]; then
true
elif [[ "$instance" == "webroot" ]]
then
cp 88-tar1090.conf /etc/lighttpd/conf-available/99-$service.conf
ln -f -s /etc/lighttpd/conf-available/99-$service.conf /etc/lighttpd/conf-enabled/99-$service.conf
else
cp 88-tar1090.conf /etc/lighttpd/conf-available/88-$service.conf
ln -f -s /etc/lighttpd/conf-available/88-$service.conf /etc/lighttpd/conf-enabled/88-$service.conf
if [ -f /etc/lighttpd/conf.d/69-skybup.conf ]; then
mv /etc/lighttpd/conf-enabled/88-$service.conf /etc/lighttpd/conf-enabled/66-$service.conf
fi
fi
fi
if [[ $changed == yes ]] || ! diff tar1090.service /lib/systemd/system/$service.service &>/dev/null
then
cp tar1090.service /lib/systemd/system/$service.service
if systemctl enable $service
then
echo "Restarting $service ..."
systemctl restart $service || ! pgrep systemd
else
echo "$service.service is masked, could not start it!"
fi
fi
# restore sed modified configuration files
mv 88-tar1090.conf.orig 88-tar1090.conf
mv 95-tar1090-otherport.conf.orig 95-tar1090-otherport.conf
mv nginx.conf.orig nginx.conf
mv tar1090.service.orig tar1090.service
done < <(echo "$instances")
if [[ $lighttpd == yes ]]; then
if lighttpd -tt -f /etc/lighttpd/lighttpd.conf 2>&1 | grep -i duplicate >/dev/null; then
mv -f /etc/lighttpd/conf-available/89-dump1090-fa.conf.dpkg-dist /etc/lighttpd/conf-available/89-dump1090-fa.conf &>/dev/null || true
fi
if ! grep -qs -E -e '^[^#]*"mod_alias"' /etc/lighttpd/lighttpd.conf /etc/lighttp/conf-enabled/* /etc/lighttpd/external.conf; then
echo 'server.modules += ( "mod_alias" )' > /etc/lighttpd/conf-available/07-mod_alias.conf
ln -s -f /etc/lighttpd/conf-available/07-mod_alias.conf /etc/lighttpd/conf-enabled/07-mod_alias.conf
else
rm -f /etc/lighttpd/conf-enabled/07-mod_alias.conf
fi
rm -f /etc/lighttpd/conf-available/87-mod_setenv.conf /etc/lighttpd/conf-enabled/87-mod_setenv.conf
while read -r FILE; do
sed -i -e 's/^server.modules.*mod_setenv.*/#\0/' "$FILE"
sed -i -e 's/^server.stat-cache-engine.*disable.*/#\0/' "$FILE"
done < <(find /etc/lighttpd/conf-available/* | grep -v setenv)
# add mod_setenv to lighttpd modules, check if it's one too much
echo 'server.modules += ( "mod_setenv" )' > /etc/lighttpd/conf-available/07-mod_setenv.conf
echo 'server.stat-cache-engine = "disable"' > /etc/lighttpd/conf-available/47-stat-cache.conf
ln -s -f /etc/lighttpd/conf-available/07-mod_setenv.conf /etc/lighttpd/conf-enabled/07-mod_setenv.conf
ln -s -f /etc/lighttpd/conf-available/47-stat-cache.conf /etc/lighttpd/conf-enabled/47-stat-cache.conf
if (( $(cat /etc/lighttpd/conf-enabled/* | grep -c -E -e '^server.stat-cache-engine *\= *"disable")') > 1 )); then
rm -f /etc/lighttpd/conf-enabled/47-stat-cache.conf
fi
if (( $(cat /etc/lighttpd/conf-enabled/* | grep -c -E -e '^server.modules.?\+=.?\(.?"mod_setenv".?\)') > 1 )); then
rm -f /etc/lighttpd/conf-available/07-mod_setenv.conf /etc/lighttpd/conf-enabled/07-mod_setenv.conf
fi
if lighttpd -tt -f /etc/lighttpd/lighttpd.conf 2>&1 | grep mod_setenv >/dev/null; then
rm -f /etc/lighttpd/conf-available/07-mod_setenv.conf /etc/lighttpd/conf-enabled/07-mod_setenv.conf
fi
if lighttpd -tt -f /etc/lighttpd/lighttpd.conf 2>&1 | grep stat-cache >/dev/null; then
rm -f /etc/lighttpd/conf-enabled/47-stat-cache.conf
fi
#lighttpd -tt -f /etc/lighttpd/lighttpd.conf && echo success || true
if lighttpd -tt -f /etc/lighttpd/lighttpd.conf 2>&1 | grep mod_setenv >/dev/null
then
rm -f /etc/lighttpd/conf-available/07-mod_setenv.conf /etc/lighttpd/conf-enabled/07-mod_setenv.conf
fi
#lighttpd -tt -f /etc/lighttpd/lighttpd.conf && echo success || true
if ! lighttpd -tt -f /etc/lighttpd/lighttpd.conf &>/dev/null; then
echo ----------------
echo "Lighttpd error, tar1090 will probably not work correctly:"
lighttpd -tt -f /etc/lighttpd/lighttpd.conf
fi
if grep -qs -e '^compress.cache-dir' /etc/lighttpd/lighttpd.conf; then
echo -----
echo "Disabling compress.cache-dir in /etc/lighttpd/lighttpd.conf due to often causing full disk issues as there is no automatic cleanup mechanism. Add a leading space to the compress.cache-dir line if you don't want tar1090 to mess with it in the future."
echo -----
sed -i -e 's$^compress.cache-dir.*$#\0 # disabled by tar1090, often causes full disk due to not having a cleanup mechanism$' /etc/lighttpd/lighttpd.conf
elif ! grep -qs -e 'disabled by tar1090' /etc/lighttpd/lighttpd.conf; then
sed -i -e 's$^compress.cache-dir.*$# CAUTION, enabling cache-dir and filetype json will cause full disk when using tar1090\n\0$' /etc/lighttpd/lighttpd.conf
fi
if grep -qs -e '^deflate.cache-dir' /etc/lighttpd/lighttpd.conf; then
echo -----
echo "Disabling deflate.cache-dir in /etc/lighttpd/lighttpd.conf due to often causing full disk issues as there is no automatic cleanup mechanism. Add a leading space to the deflate.cache-dir line if you don't want tar1090 to mess with it in the future."
echo -----
sed -i -e 's$^deflate.cache-dir.*$#\0 # disabled by tar1090, often causes full disk due to not having a cleanup mechanism$' /etc/lighttpd/lighttpd.conf
elif ! grep -qs -e 'disabled by tar1090' /etc/lighttpd/lighttpd.conf; then
sed -i -e 's$^deflate.cache-dir.*$# CAUTION, enabling cache-dir and filetype json will cause full disk when using tar1090\n\0$' /etc/lighttpd/lighttpd.conf
fi
fi
if systemctl show lighttpd 2>/dev/null | grep -qs -F -e 'UnitFileState=enabled' -e 'ActiveState=active'; then
echo "Restarting lighttpd ..."
systemctl restart lighttpd || ! pgrep systemd
fi
echo --------------
if [[ $nginx == yes ]]; then
echo
echo "To configure nginx for tar1090, please add the following line(s) in the server {} section:"
echo
for service in $services; do
echo "include /usr/local/share/tar1090/nginx-$service.conf;"
done
fi
echo --------------
if [[ $lighttpd == yes ]]; then
for name in $names; do
echo "All done! Webinterface available at http://$(ip route get 1.2.3.4 | grep -m1 -o -P 'src \K[0-9,.]*')/$name"
done
elif [[ $nginx == yes ]]; then
for name in $names; do
echo "All done! Webinterface once nginx is configured will be available at http://$(ip route get 1.2.3.4 | grep -m1 -o -P 'src \K[0-9,.]*')/$name"
done
else
echo "All done! You'll need to configure your webserver yourself, see /usr/local/share/tar1090/nginx-tar1090.conf for a reference nginx configuration"
fi