From d16e90858400a2b8d56c0a8d03162faed235bc60 Mon Sep 17 00:00:00 2001 From: Matthias Wirth Date: Tue, 22 Oct 2019 16:23:48 +0200 Subject: [PATCH] improve install script to allow instancing using /etc/default/tar1090_instances --- 88-tar1090.conf | 2 +- install.sh | 205 +++++++++++++++++++++++++++--------------------- nginx.conf | 2 +- tar1090.service | 10 +-- tar1090.sh | 19 +++-- 5 files changed, 134 insertions(+), 104 deletions(-) diff --git a/88-tar1090.conf b/88-tar1090.conf index 95aabac..8581b5c 100644 --- a/88-tar1090.conf +++ b/88-tar1090.conf @@ -2,7 +2,7 @@ alias.url += ( "/INSTANCE/data/" => "SOURCE_DIR/", - "/INSTANCE/chunks/" => "/run/INSTANCE/", + "/INSTANCE/chunks/" => "/run/SERVICE/", "/INSTANCE/" => "HTMLPATH/" ) diff --git a/install.sh b/install.sh index 0dfb4db..9819876 100755 --- a/install.sh +++ b/install.sh @@ -3,68 +3,69 @@ set -e -instance=tar1090 srcdir=/run/dump1090-fa repo="https://github.com/wiedehopf/tar1090" ipath=/usr/local/share/tar1090 -html_path="$ipath/html" -install=0 lighttpd=no nginx=no -packages="unzip git p7zip-full perl jq" +command_package="unzip unzip/git git/perl perl/jq jq/7za p7zip-full/" +install="" -for i in $packages +while read -d '/' CMD PKG do - if ! dpkg -s $i 2>/dev/null | grep 'Status.*installed' &>/dev/null + if ! command -v "$CMD" &>/dev/null then - install=1 + echo "command $CMD not found, will try to install package $PKG" + install="$PKG $install" fi -done +done < <(echo "$command_package") -if [ $install == 1 ] +if [[ -n "$install" ]] then echo "Installing required packages: $packages" apt-get update - if ! apt-get install -y $packages + if ! apt-get install -y $install then - echo "Failed to install required packages: $packages" + echo "Failed to install required packages: $install" echo "Exiting ..." exit 1 fi hash -r fi -if dpkg -s lighttpd 2>/dev/null | grep 'Status.*installed' &>/dev/null +if command -v lighttpd &>/dev/null then lighttpd=yes fi -if dpkg -s nginx 2>/dev/null | grep 'Status.*installed' &>/dev/null +if command -v nginx &>/dev/null then nginx=yes fi - mkdir -p $ipath -if [ -z $1 ] || [ $1 != "test" ] +if [[ "$1" == "test" ]] then - cd /tmp - if git clone --depth 1 $repo $ipath/git 2>/dev/null || cd $ipath/git; then - cd $ipath/git - git checkout -f master - git fetch - git reset --hard origin/master - else - cd /tmp - if ! wget --timeout=30 -q -O master.zip $repo/archive/master.zip || ! unzip -q -o master.zip - then - echo "Unable to download files, exiting! (Maybe try again?)" - exit 1 - fi - cd tar1090-master - fi + rm -r /tmp/tar1090-test 2>/dev/null || true + mkdir -p /tmp/tar1090-test + cp -r * /tmp/tar1090-test + cd /tmp/tar1090-test + +elif git clone --depth 1 $repo $ipath/git 2>/dev/null || cd $ipath/git +then + cd $ipath/git + git checkout -f master + git fetch + git reset --hard origin/master + +elif wget --timeout=30 -q -O /tmp/master.zip $repo/archive/master.zip && unzip -q -o master.zip +then + cd /tmp/tar1090-master +else + echo "Unable to download files, exiting! (Maybe try again?)" + exit 1 fi if [[ -n $1 ]] && [ $1 != "test" ] ; then @@ -81,79 +82,103 @@ elif ! [[ -d /run/dump1090-fa ]] ; then fi fi -if [[ -n $2 ]]; then - instance=$2 - html_path="$ipath/$instance-html" +if [ -f /etc/default/tar1090_instances ]; then + instances=$(/dev/null \ - || ! diff tar1090.service /lib/systemd/system/$instance.service &>/dev/null -then +if ! diff tar1090.sh /usr/local/share/tar1090/tar1090.sh &>/dev/null; then changed=yes - systemctl stop tar1090 -fi -if ! diff 88-tar1090.conf /etc/lighttpd/conf-enabled/88-$instance.conf &>/dev/null; then - changed_lighttpd=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 -# keep some stuff around -if [ -f $html_path/defaults.js ]; then - cp $html_path/config.js /tmp/tar1090_config.js -fi -cp $html_path/colors.css html/ 2>/dev/null || true -cp -r -T html $html_path -cp 88-tar1090.conf default install.sh nginx.conf tar1090.service \ - uninstall.sh 99-tar1090-webroot.conf LICENSE README.md \ - tar1090.sh 95-tar1090-otherport.conf $ipath - -mv /tmp/tar1090_config.js $html_path/config.js 2>/dev/null || true - -# bust cache for all css and js files -sed -i -e "s/__cache_version__/$(date +%s)/g" $html_path/index.html - -# don't overwrite existing configuration -cp -n default /etc/default/$instance -sed -i -e 's/skyview978/skyaware978/' /etc/default/$instance +# copy over base files +cp default install.sh uninstall.sh 99-tar1090-webroot.conf LICENSE README.md \ + 95-tar1090-otherport.conf $ipath -cp nginx.conf $ipath/nginx-$instance.conf -if [[ $lighttpd == yes ]]; then - cp 88-tar1090.conf /etc/lighttpd/conf-available/88-$instance.conf - lighty-enable-mod $instance >/dev/null || true +while read -r srcdir instance +do + if [[ "$instance" != "tar1090" ]]; then + html_path="$ipath/html-$instance" + service="tar1090-$instance" + else + html_path="$ipath/html" + service="tar1090" + fi + + # 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?$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 + sed -i.orig -e "s?SOURCE_DIR?$srcdir?g" -e "s?SERVICE?$service?g" tar1090.service + + # keep some stuff around + if [ -f $html_path/defaults.js ]; then + cp $html_path/config.js /tmp/tar1090_config.js + fi + cp $html_path/colors.css /tmp/tar1090_colors.css/ 2>/dev/null || true + + cp -r -T html $html_path + + mv /tmp/tar1090_config.js $html_path/config.js 2>/dev/null || true + mv /tmp/tar1090_colors.css $html_path/colors.css 2>/dev/null || true + + # bust cache for all css and js files + sed -i -e "s/__cache_version__/$(date +%s)/g" $html_path/index.html + + cp nginx.conf $ipath/nginx-$service.conf + + if [[ $lighttpd == yes ]] && ! diff 88-tar1090.conf /etc/lighttpd/conf-enabled/88-$service.conf &>/dev/null + then + changed_lighttpd=yes + cp 88-tar1090.conf /etc/lighttpd/conf-available/88-$service.conf + lighty-enable-mod $service >/dev/null || true + fi + + if [[ $changed == yes ]] || ! diff tar1090.service /lib/systemd/system/$service.service &>/dev/null + then + cp tar1090.service /lib/systemd/system/$service.service + systemctl enable $service + echo + echo "Restarting $service ..." + systemctl restart $service + fi + + if [[ $nginx == yes ]]; then + echo + echo "To configure nginx for tar1090, please add the following line in the server {} section:" + echo + echo "include /usr/local/share/tar1090/nginx-$service.conf;" + fi + + # restore sed modified configuration files + mv 88-tar1090.conf.orig 88-tar1090.conf + mv nginx.conf.orig nginx.conf + mv tar1090.service.orig tar1090.service + + echo -------------- + echo "All done! Webinterface available at http://$(ip route | grep -m1 -o -P 'src \K[0-9,.]*')/$instance" +done < <(echo "$instances") + +if [[ $changed_lighttpd == yes ]] && systemctl status lighttpd >/dev/null; then if grep -q '^server.modules += ( "mod_setenv" )' /etc/lighttpd/conf-available/89-dump1090-fa.conf then sed -i -e 's/^server.modules += ( "mod_setenv" )/#server.modules += ( "mod_setenv" )/' $(find /etc/lighttpd/conf-available/* | grep -v dump1090-fa) fi -fi - - -if [[ $changed == yes ]]; then - cp tar1090.service /lib/systemd/system/$instance.service - systemctl enable $instance - echo "Restarting tar1090 ..." - systemctl restart $instance -fi -if [[ $changed_lighttpd == yes ]] && systemctl status lighttpd >/dev/null; then echo "Restarting lighttpd ..." systemctl restart lighttpd fi - -if [[ $nginx == yes ]]; then - echo - echo "To configure nginx for tar1090, please add the following line in the server {} section:" - echo - echo "include /usr/local/share/tar1090/nginx-$instance.conf;" - echo -fi - -echo -------------- -echo "All done! Webinterface available at http://$(ip route | grep -m1 -o -P 'src \K[0-9,.]*')/$instance" diff --git a/nginx.conf b/nginx.conf index c2f3544..173b04b 100644 --- a/nginx.conf +++ b/nginx.conf @@ -4,7 +4,7 @@ location /INSTANCE/data/ { } location /INSTANCE/chunks/ { - alias /run/INSTANCE/; + alias /run/SERVICE/; location ~* \.gz$ { add_header Cache-Control "must-revalidate"; add_header Content-Type "application/json"; diff --git a/tar1090.service b/tar1090.service index f138aa7..8a88d1b 100644 --- a/tar1090.service +++ b/tar1090.service @@ -1,17 +1,17 @@ [Unit] -Description=INSTANCE - compress dump1090 json data +Description=SERVICE - compress dump1090 json data Wants=network.target After=network.target [Service] User=dump1090 -SyslogIdentifier=INSTANCE -RuntimeDirectory=INSTANCE +SyslogIdentifier=SERVICE +RuntimeDirectory=SERVICE RuntimeDirectoryMode=0755 EnvironmentFile=/usr/local/share/tar1090/default -EnvironmentFile=/etc/default/INSTANCE -ExecStart=-/usr/local/share/tar1090/tar1090.sh /run/INSTANCE SOURCE_DIR $INTERVAL $HISTORY_SIZE $CHUNK_SIZE $ENABLE_978 $URL_978 $INT_978 $PF_URL $COMPRESS_978 +EnvironmentFile=/etc/default/SERVICE +ExecStart=-/usr/local/share/tar1090/tar1090.sh /run/SERVICE SOURCE_DIR $INTERVAL $HISTORY_SIZE $CHUNK_SIZE $ENABLE_978 $URL_978 $INT_978 $PF_URL $COMPRESS_978 Type=simple Restart=always RestartSec=30 diff --git a/tar1090.sh b/tar1090.sh index d5dd821..89e84ec 100755 --- a/tar1090.sh +++ b/tar1090.sh @@ -48,8 +48,11 @@ chunks=$(( hist/CHUNK_SIZE + 2 )) CHUNK_SIZE=$(( CHUNK_SIZE - ( (CHUNK_SIZE - hist % CHUNK_SIZE)/(chunks-1) ) )) new_chunk() { - cur_chunk="chunk_$(date +%s%N | head -c-7).gz" - echo "$cur_chunk" >> chunk_list + if [[ $1 != "refresh" ]]; then + cur_chunk="chunk_$(date +%s%N | head -c-7).gz" + echo "$cur_chunk" >> chunk_list + echo "{ \"files\" : [ ] }" | gzip -1 > "$cur_chunk" + fi for iterator in $(head -n-$chunks chunk_list); do rm -f "$RUN_DIR/$iterator"; done tail -n$chunks chunk_list > chunk_list.tmp mv chunk_list.tmp chunk_list @@ -64,7 +67,6 @@ new_chunk() { JSON="$JSON""$(while read -r i; do echo -n "\"$i\", "; done < chunk_list)" JSON="$JSON"' "chunk_recent.gz" ] }' - echo "{ \"files\" : [ ] }" | gzip -1 > "$cur_chunk" echo "$JSON" > "$RUN_DIR/chunks.json" } @@ -93,10 +95,10 @@ do if sed -e '1i{ "files" : [' -e '$a]}' -e '$d' history_*.json | 7za a -si temp.gz >/dev/null; then mv temp.gz "$cur_chunk" - # cleanup - rm -f history_*.json new_chunk fi + # cleanup + rm -f history_*.json i=0 @@ -176,15 +178,18 @@ if [[ $ENABLE_978 == "yes" ]]; then done & fi -sleep 3 +sleep 10 while [[ -n $PF_URL ]] do sleep 10 & TMP="pf.$RANDOM$RANDOM" - if cd "$RUN_DIR" && wget -T 5 -q -O $TMP "$PF_URL" 2>/dev/null; then + if cd "$RUN_DIR" && wget -T 5 -q -O $TMP "$PF_URL" &>/dev/null; then sed -i -e 's/"user_l[a-z]*":"[0-9,.,-]*",//g' $TMP mv $TMP pf.json + if ! grep -qs -e pf_data chunks.json; then + new_chunk refresh + fi else sleep 120 fi