Files
tar1090/install.sh

365 lines
12 KiB
Bash
Raw Normal View History

#!/bin/bash
set -e
srcdir=/run/dump1090-fa
repo="https://github.com/wiedehopf/tar1090"
db_repo="https://github.com/wiedehopf/tar1090-db"
ipath=/usr/local/share/tar1090
lighttpd=no
nginx=no
mkdir -p $ipath
if ! id -u tar1090 &>/dev/null
then
2020-03-23 00:29:07 +01:00
adduser --system --home $ipath --no-create-home --quiet tar1090
fi
command_package="git git/jq jq/7za p7zip-full/"
packages=""
while read -r -d '/' CMD PKG
do
2020-03-23 00:29:07 +01:00
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
2020-03-23 00:29:07 +01:00
echo "Installing required packages: $packages"
apt-get update || true
if ! apt-get install -y $packages
then
echo "Failed to install required packages: $packages"
echo "Exiting ..."
exit 1
fi
hash -r || true
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
2020-03-23 00:29:07 +01:00
lighttpd=yes
fi
if command -v nginx &>/dev/null
then
2020-03-23 00:29:07 +01:00
nginx=yes
fi
dir=$(pwd)
if { ! [ -d "$ipath/git-db" ] && git clone --depth 1 $db_repo $ipath/git-db; } || { cd $ipath/git-db && ! [[ "$1" == "test" ]] ; }
then
2020-03-23 00:29:07 +01:00
cd $ipath/git-db
git fetch --depth 1 origin master
git reset --hard origin/master
fi
2020-02-23 17:07:36 +01:00
cd $ipath/git-db
DB_VERSION=$(git rev-parse --short HEAD)
cd "$dir"
if [[ "$1" == "test" ]]
then
2020-03-23 00:29:07 +01:00
rm -r /tmp/tar1090-test 2>/dev/null || true
mkdir -p /tmp/tar1090-test
cp -r ./* /tmp/tar1090-test
cd /tmp/tar1090-test
2020-01-03 21:34:49 +01:00
elif { ! [ -d "$ipath/git" ] && git clone --single-branch -b master --depth 1 $repo $ipath/git; } || cd $ipath/git
then
2020-03-23 00:29:07 +01:00
cd $ipath/git
git fetch origin master
git reset --hard origin/master
elif wget --timeout=30 -q -O /tmp/master.zip $repo/archive/master.zip && unzip -q -o master.zip
then
2020-03-23 00:29:07 +01:00
cd /tmp/tar1090-master
else
2020-03-23 00:29:07 +01:00
echo "Unable to download files, exiting! (Maybe try again?)"
exit 1
fi
if git rev-parse --short HEAD &>/dev/null; then
TAR_VERSION=$(git rev-parse --short HEAD)
else
TAR_VERSION=$(date +%s)
fi
if [[ -n $1 ]] && [ "$1" != "test" ] ; then
2020-03-23 00:29:07 +01:00
srcdir=$1
elif ! [[ -d /run/dump1090-fa ]] ; then
2020-03-23 00:29:07 +01:00
if [[ -d /run/readsb ]]; then
srcdir=/run/readsb
elif [[ -d /run/dump1090 ]]; then
srcdir=/run/dump1090
elif [[ -d /run/dump1090-mutability ]]; then
srcdir=/run/dump1090-mutability
elif [[ -d /run/skyaware978 ]]; then
srcdir=/run/skyaware978
fi
fi
if [ -f /etc/default/tar1090_instances ]; then
2020-03-23 00:29:07 +01:00
instances=$(</etc/default/tar1090_instances)
else
2020-03-23 00:29:07 +01:00
instances="$srcdir tar1090"
fi
if ! diff tar1090.sh /usr/local/share/tar1090/tar1090.sh &>/dev/null; then
2020-03-23 00:29:07 +01:00
changed=yes
while read -r srcdir instance; do
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
2020-01-19 12:38:39 +01:00
cp default install.sh uninstall.sh LICENSE README.md \
$ipath
services=""
names=""
otherport=""
while read -r srcdir instance
do
TMP=$(mktemp -d -p $ipath)
chmod 755 $TMP
2020-03-23 00:29:07 +01:00
if [[ "$instance" != "tar1090" ]]; then
html_path="$ipath/html-$instance"
service="tar1090-$instance"
else
html_path="$ipath/html"
service="tar1090"
fi
services+="$service "
names+="$instance "
2020-03-23 00:29:07 +01:00
# 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
2020-01-19 12:38:39 +01:00
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
2020-03-23 00:29:07 +01:00
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
2020-01-19 12:38:39 +01:00
2020-03-23 00:29:07 +01:00
else
2020-01-19 12:38:39 +01:00
2020-03-23 00:29:07 +01:00
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
2020-01-19 12:38:39 +01:00
fi
2020-03-23 00:29:07 +01:00
sed -i.orig -e "s?SOURCE_DIR?$srcdir?g" -e "s?SERVICE?$service?g" tar1090.service
2020-03-23 00:29:07 +01:00
cp -r -T html $TMP
cp -r -T $ipath/git-db/db $TMP/db-$DB_VERSION
sed -i -e "s/var databaseFolder = .*/var databaseFolder = \"db-$DB_VERSION\";/" $TMP/early.js
echo "{ \"tar1090Version\": \"$TAR_VERSION\", \"databaseVersion\": \"$DB_VERSION\" }" > $TMP/version.json
2020-03-23 00:29:07 +01:00
# keep some stuff around
if [ -f $html_path/defaults*.js ]; then
mv $html_path/config.js $TMP/config.js 2>/dev/null || true
2020-03-23 00:29:07 +01:00
fi
mv $html_path/color*.css $TMP/colors.css 2>/dev/null || true
mv $html_path/upintheair.json $TMP/upintheair.json 2>/dev/null || true
2020-03-23 00:29:07 +01:00
# bust cache for all css and js files
2020-03-23 00:29:07 +01:00
dir=$(pwd)
cd $TMP
sed -i -e "s/tar1090 on github/tar1090 on github ($(date +%y%m%d))/" index.html
2020-03-23 00:29:07 +01:00
sed -i \
-e "s/dbloader.js/dbloader_$TAR_VERSION.js/" \
-e "s/defaults.js/defaults_$TAR_VERSION.js/" \
-e "s/early.js/early_$TAR_VERSION.js/" \
-e "s/flags.js/flags_$TAR_VERSION.js/" \
-e "s/formatter.js/formatter_$TAR_VERSION.js/" \
-e "s/layers.js/layers_$TAR_VERSION.js/" \
-e "s/markers.js/markers_$TAR_VERSION.js/" \
-e "s/planeObject.js/planeObject_$TAR_VERSION.js/" \
-e "s/registrations.js/registrations_$TAR_VERSION.js/" \
-e "s/script.js/script_$TAR_VERSION.js/" \
-e "s/colors.css/colors_$TAR_VERSION.css/" \
-e "s/style.css/style_$TAR_VERSION.css/" \
index.html
mv dbloader.js dbloader_$TAR_VERSION.js
mv defaults.js defaults_$TAR_VERSION.js
mv early.js early_$TAR_VERSION.js
mv flags.js flags_$TAR_VERSION.js
mv formatter.js formatter_$TAR_VERSION.js
mv layers.js layers_$TAR_VERSION.js
mv markers.js markers_$TAR_VERSION.js
mv planeObject.js planeObject_$TAR_VERSION.js
mv registrations.js registrations_$TAR_VERSION.js
mv script.js script_$TAR_VERSION.js
mv colors.css colors_$TAR_VERSION.css
mv style.css style_$TAR_VERSION.css
2020-01-31 02:47:01 +01:00
if [[ $nginx == yes ]]; then
gzip -k -9 dbloader_$TAR_VERSION.js
gzip -k -9 defaults_$TAR_VERSION.js
gzip -k -9 early_$TAR_VERSION.js
gzip -k -9 flags_$TAR_VERSION.js
gzip -k -9 formatter_$TAR_VERSION.js
gzip -k -9 layers_$TAR_VERSION.js
gzip -k -9 markers_$TAR_VERSION.js
gzip -k -9 planeObject_$TAR_VERSION.js
gzip -k -9 registrations_$TAR_VERSION.js
gzip -k -9 script_$TAR_VERSION.js
gzip -k -9 colors_$TAR_VERSION.css
gzip -k -9 style_$TAR_VERSION.css
2020-01-31 02:47:01 +01:00
gzip -k -9 jquery/*.js
gzip -k -9 ol/*.js
#gzip -k -9 db2/*.json .... already exists compressed
fi
GARBAGE="$ipath/$RANDOM.$RANDOM"
2020-03-24 09:14:46 +01:00
mv $html_path $GARBAGE 2>/dev/null || true
mv $TMP $html_path
2020-03-23 00:29:07 +01:00
rm -rf $GARBAGE 2>/dev/null || true
2020-01-31 02:47:01 +01:00
2020-03-23 00:29:07 +01:00
cd "$dir"
2020-03-23 00:29:07 +01:00
cp nginx.conf $ipath/nginx-$service.conf
2020-03-23 00:29:07 +01:00
if [[ $lighttpd == yes ]] &>/dev/null
then
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"
fi
2020-01-19 12:38:39 +01:00
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
2020-01-19 12:38:39 +01:00
cp 88-tar1090.conf /etc/lighttpd/conf-available/88-$service.conf
if [ -f /etc/lighttpd/conf.d/69-skybup.conf ]; then
ln -f -s /etc/lighttpd/conf-available/88-$service.conf /etc/lighttpd/conf-enabled/66-$service.conf
else
ln -f -s /etc/lighttpd/conf-available/88-$service.conf /etc/lighttpd/conf-enabled/88-$service.conf
fi
fi
2020-03-23 00:29:07 +01:00
fi
2020-03-23 00:29:07 +01:00
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
2020-01-14 17:18:45 +01:00
then
echo "Restarting $service ..."
systemctl restart $service
else
echo "$service.service is masked, could not start it!"
fi
2020-03-23 00:29:07 +01:00
fi
2020-03-23 00:29:07 +01:00
# restore sed modified configuration files
mv 88-tar1090.conf.orig 88-tar1090.conf
mv 95-tar1090-otherport.conf.orig 95-tar1090-otherport.conf
2020-03-23 00:29:07 +01:00
mv nginx.conf.orig nginx.conf
mv tar1090.service.orig tar1090.service
done < <(echo "$instances")
if [ -d /etc/lighttpd/conf-enabled/ ] && [ -f /etc/lighttpd/lighttpd.conf ]
then
2020-03-23 00:29:07 +01:00
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 dump1090-fa | grep -v readsb)
# add mod_setenv to lighttpd modules, check if it's one too much
echo 'server.modules += ( "mod_setenv" )' > /etc/lighttpd/conf-available/87-mod_setenv.conf
echo 'server.stat-cache-engine = "disable"' > /etc/lighttpd/conf-available/47-stat-cache.conf
2020-01-16 08:21:03 +01:00
ln -s -f /etc/lighttpd/conf-available/87-mod_setenv.conf /etc/lighttpd/conf-enabled/87-mod_setenv.conf
ln -s -f /etc/lighttpd/conf-available/47-stat-cache.conf /etc/lighttpd/conf-enabled/47-stat-cache.conf
if lighttpd -tt -f /etc/lighttpd/lighttpd.conf 2>&1 | grep mod_setenv >/dev/null
then
rm /etc/lighttpd/conf-enabled/87-mod_setenv.conf
fi
if lighttpd -tt -f /etc/lighttpd/lighttpd.conf 2>&1 | grep stat-cache >/dev/null
then
rm /etc/lighttpd/conf-enabled/47-stat-cache.conf
fi
2020-03-12 14:30:31 +01:00
if lighttpd -tt -f /etc/lighttpd/lighttpd.conf 2>&1 | grep mod_setenv >/dev/null
then
rm /etc/lighttpd/conf-enabled/87-mod_setenv.conf
fi
fi
2020-03-19 12:34:58 +01:00
if systemctl status lighttpd &>/dev/null; then
2020-03-23 00:29:07 +01:00
echo "Restarting lighttpd ..."
systemctl restart lighttpd
fi
echo --------------
if [[ $nginx == yes ]]; then
2020-03-23 00:29:07 +01:00
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
2020-03-23 00:29:07 +01:00
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
2020-03-23 00:29:07 +01:00
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
2020-03-23 00:29:07 +01:00
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