Files
docker-planefence/rootfs/etc/s6-overlay/scripts/socket30003

110 lines
4.7 KiB
Plaintext
Executable File

#!/command/with-contenv bash
#shellcheck shell=bash disable=SC1091,SC2154,SC2015
# redirect stderr to stdout so it's picked up in the docker logs
source /scripts/common
[[ "$LOGLEVEL" != "ERROR" ]] && "${s6wrap[@]}" echo "Socket30003 started as an s6 service" || true
# -----------------------------------------------------------------------------------
# Copyright 2020-2025 Ramon F. Kolb - licensed under the terms and conditions
# of GPLv3. The terms and conditions of this license are included with the Github
# distribution of this package, and are also available here:
# https://github.com/sdr-enthusiasts/planefence4docker/
#
# The package contains parts of, and modifications or derivatives to the following:
# Dump1090.Socket30003 by Ted Sluis: https://github.com/tedsluis/dump1090.socket30003
# These packages may incorporate other software and license terms.
# -----------------------------------------------------------------------------------
#
# Make sure the /run directory exists
mkdir -p /run/socket30003
#
# Load parameters from the config file:
if [[ -f /usr/share/planefence/persist/planefence.config ]]; then
set -o allexport
source /usr/share/planefence/persist/planefence.config
set +o allexport
fi
# If things went wrong then simply sleep forever.
# No need to notify -- .../run/planefence is doing the same thing and is
# screaming all over the logs by now.
if [[ -z "$FEEDER_LAT" ]] || [[ "$FEEDER_LAT" == "90.12345" ]]
then
sleep infinity
fi
# $LOOPTIME is used to delay a restart after socket30003 exits for any reason, as not to
# spam the system with restarts:
LOOPTIME=15
#
# set params in socket30003.conf
sed -i 's/\(^\s*latitude=\).*/\1'"$FEEDER_LAT"'/' /usr/share/socket30003/socket30003.cfg
sed -i 's/\(^\s*longitude=\).*/\1'"$FEEDER_LONG"'/' /usr/share/socket30003/socket30003.cfg
sed -i 's|\(^\s*PEER_HOST=\).*|\1'"$PF_SOCK30003HOST"'|' /usr/share/socket30003/socket30003.cfg
if [[ "x$PF_DISTUNIT" != "x" ]]
then
sed -i 's/\(^\s*distanceunit=\).*/\1'"$PF_DISTUNIT"'/' /usr/share/socket30003/socket30003.cfg
else
sed -i 's/\(^\s*distanceunit=\).*/\1nauticalmile/' /usr/share/socket30003/socket30003.cfg
fi
if [[ "x$PF_ALTUNIT" != "x" ]]
then
sed -i 's/\(^\s*altitudeunit=\).*/\1'"$PF_ALTUNIT"'/' /usr/share/socket30003/socket30003.cfg
else
sed -i 's/\(^\s*altitudeunit=\).*/\1feet/' /usr/share/socket30003/socket30003.cfg
fi
if [[ "x$PF_SPEEDUNIT" != "x" ]]
then
sed -i 's/\(^\s*speedunit=\).*/\1'"$PF_SPEEDUNIT"'/' /usr/share/socket30003/socket30003.cfg
else
sed -i 's/\(^\s*speedunit=\).*/\1knotph/' /usr/share/socket30003/socket30003.cfg
fi
PF_SOCK30003PORT="${PF_SOCK30003PORT:-30003}"
sed -i 's|\(^\s*PEER_PORT=\).*|\1'"$PF_SOCK30003PORT"'|' /usr/share/socket30003/socket30003.cfg
# -----------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# Now start dump1090.socket30003 in a loop that restarts it $LOOPTIME seconds after it exits or crashes:
[[ "$LOGLEVEL" != "ERROR" ]] && "${s6wrap[@]}" echo "socket30003 starting its initial run now" || true
while true
do
# First make sure that $PF_SOCK30003HOST is defined
if [[ -z "$PF_SOCK30003HOST" ]]
then
"${s6wrap[@]}" echo "\$PF_SOCK30003HOST is not set in the .env or docker-compose.yml file. Cannot run! Please update and restart the container."
else
# if $PF_SOCK30003HOST is defined, then check if we can reach it. Execute socket30003 if we can reach it, complain if we can't
# we do this inside this endless loop so it will show in the logs regularly
if timeout --preserve-status 5 netcat -z -v "$PF_SOCK30003HOST" "$PF_SOCK30003PORT" >/dev/null 2>&1
then
sleep .4 # delay startup a little bit as to not interrupt file pruning
"${s6wrap[@]}" echo "socket30003 starting up."
touch /run/socket30003.up
/usr/share/socket30003/socket30003.pl >/dev/null 2>&1
exitcode="$?"
if [[ ! -f /tmp/socket-cleanup ]]; then
"${s6wrap[@]}" echo "/usr/share/socket30003/socket30003.pl exited with code $exitcode, if this repeats please run this command to get more detailed output on the failure: docker exec -it planefence /usr/share/socket30003/socket30003.pl"
fi
rm -f /run/socket30003.up
else
"${s6wrap[@]}" echo "We cannot reach \"$PF_SOCK30003HOST\" on port $PF_SOCK30003PORT."
"${s6wrap[@]}" echo "If this error keeps on repeating, please make sure that"
"${s6wrap[@]}" echo "readsb/dump1090[-fa]/ultrafeeder is running and producing SBS data on port $PF_SOCK30003PORT!"
fi
fi
if [[ ! -f /tmp/socket-cleanup ]]
then
"${s6wrap[@]}" echo "socket30003 has exited... restarting in $LOOPTIME"
sleep $LOOPTIME
else
sleep 1s
rm -f /tmp/socket-cleanup
fi
[[ "$LOGLEVEL" != "ERROR" ]] && "${s6wrap[@]}" echo "socket30003 restarting now" || true
done