Skip to content

Commit

Permalink
Merge pull request #9 from sdr-enthusiasts/s6-migrate
Browse files Browse the repository at this point in the history
initial
  • Loading branch information
fredclausen authored Aug 28, 2023
2 parents d71bcdf + 5d4b471 commit 5f8db78
Show file tree
Hide file tree
Showing 32 changed files with 196 additions and 8 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions rootfs/etc/s6-overlay/s6-rc.d/01-vdlm2dec/type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oneshot
2 changes: 2 additions & 0 deletions rootfs/etc/s6-overlay/s6-rc.d/01-vdlm2dec/up
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
exec /etc/s6-overlay/scripts/01-vdlm2dec
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions rootfs/etc/s6-overlay/s6-rc.d/vdlm2dec/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
exec /etc/s6-overlay/scripts/vdlm2dec
1 change: 1 addition & 0 deletions rootfs/etc/s6-overlay/s6-rc.d/vdlm2dec/type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
longrun
Empty file.
2 changes: 2 additions & 0 deletions rootfs/etc/s6-overlay/s6-rc.d/vdlm_feeder/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
exec /etc/s6-overlay/scripts/vdlm_feeder
1 change: 1 addition & 0 deletions rootfs/etc/s6-overlay/s6-rc.d/vdlm_feeder/type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
longrun
Empty file.
2 changes: 2 additions & 0 deletions rootfs/etc/s6-overlay/s6-rc.d/vdlm_server/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
exec /etc/s6-overlay/scripts/vdlm_server
1 change: 1 addition & 0 deletions rootfs/etc/s6-overlay/s6-rc.d/vdlm_server/type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
longrun
Empty file.
2 changes: 2 additions & 0 deletions rootfs/etc/s6-overlay/s6-rc.d/vdlm_stats/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
exec /etc/s6-overlay/scripts/vdlm_stats
1 change: 1 addition & 0 deletions rootfs/etc/s6-overlay/s6-rc.d/vdlm_stats/type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
longrun
101 changes: 101 additions & 0 deletions rootfs/etc/s6-overlay/scripts/01-vdlm2dec
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/command/with-contenv bash
# shellcheck shell=bash

# FEED_ID needs to be set

if [[ -z "${FEED_ID}" ]]; then
echo "FEED_ID is not set, exiting"
exit 1
fi

# FREQUENCIES needs to be set

if [[ -z "${FREQUENCIES}" ]]; then
echo "FREQUENCIES is not set, exiting"
exit 1
fi

IFS=';'
read -ra SPLIT_FREQS <<< "${FREQUENCIES}"

# loop through SPLIT_FREQS

# We can only have 6 total frequencies

if [[ "${#SPLIT_FREQS[@]}" -gt 8 ]]; then
echo "FREQUENCIES is too long, exiting"
exit 1
fi

# FREQUENCIES needs to be in the range of 118.0 - 137.0.

FREQ_STRING=""
for i in "${SPLIT_FREQS[@]}"
do
:
if [[ $(echo "$i > 118.0" | bc) -eq 0 || $(echo "$i < 137.0" | bc) -eq 0 ]]; then
echo "FREQUENCY $i is not in the range of 118.0 - 137.0, exiting"
exit 1
fi
FREQ_STRING+=" $i"
done

# Make sure mode is valid

if [[ "${MODE}" != @(J|j|P|p|A|a) ]]; then
echo "MODE is not valid, exiting"
exit 1
fi

if [[ -n "${SERVER}" && -z "${SERVER_PORT}" ]]; then
echo "SERVER is set but SERVER_PORT is not set, exiting"
exit 1
fi

# DEVICE_ID or SERIAL needs to be set

if [[ -z "${SERIAL}" ]]; then
echo "SERIAL is not set, exiting"
exit 1
fi

RTL_TEST_OUTPUT=$(timeout 1s rtl_test -d 0 2>&1 | grep -P '^\s+\d+:\s+\S+?,\s+\S+?,\s+SN:\s+\S+?\s*$' || true)

IFS=$'\n'
for RTL_TEST_OUTPUT_LINE in $RTL_TEST_OUTPUT; do
# Unset variables in case any regexes fail
unset RTL_DEVICE_ID RTL_DEVICE_MAKE RTL_DEVICE_MODEL RTL_DEVICE_SERIAL

# Pull variables from output via regex
RTL_DEVICE_NUMBER=$(echo "$RTL_TEST_OUTPUT_LINE" | grep -oP '^\s+\K\d+(?=:\s+\S+?,\s+\S+?,\s+SN:\s+\S+?\s*$)')
RTL_DEVICE_SERIAL=$(echo "$RTL_TEST_OUTPUT_LINE" | grep -oP '^\s+\d+:\s+\S+?,\s+\S+?,\s+SN:\s+\K\S+?(?=\s*$)')

# See if we've found the device we're looking for
if [[ -n "$SERIAL" && "$SERIAL" == "$RTL_DEVICE_SERIAL" ]]; then
OUTPUT_DEVICE_ID="$RTL_DEVICE_NUMBER"
fi
done

if [[ -z "${OUTPUT_DEVICE_ID}" ]]; then
echo "Could not find device ID for serial '$SERIAL'"
exit 1
fi

rm -rf /etc/s6-overlay/scripts/vdlm2dec > /dev/null 2>&1
if [[ -n "${OUTPUT_DEVICE_ID}" ]]; then
cp /etc/templates/run /etc/s6-overlay/scripts/vdlm2dec
else
cp /etc/templates/bad /etc/s6-overlay/scripts/vdlm2dec
fi

s6-chmod 0755 /etc/s6-overlay/scripts/vdlm2dec

mkdir -p /run/acars
touch /run/acars/vdlm2.past5min.json

#sed -i "s/DEVICE_ID=\"\"/DEVICE_ID=\"$OUTPUT_DEVICE_ID\"/g" /etc/s6-overlay/scripts/vdlm2dec
sed -i "s/FREQ_STRING=\"\"/FREQ_STRING=\"$FREQ_STRING\"/g" /etc/s6-overlay/scripts/vdlm2dec

# Everything is good to go. Exit with 0

exit 0
21 changes: 21 additions & 0 deletions rootfs/etc/s6-overlay/scripts/vdlm_feeder
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/command/with-contenv bash
# shellcheck shell=bash

set -o pipefail

# Require that vdlm_server is running
if ! netstat -an | grep -P '^\s*tcp\s+\d+\s+\d+\s+0\.0\.0\.0:15555\s+(?>\d{1,3}\.{0,1}){4}:\*\s+LISTEN\s*$' > /dev/null; then
sleep 1
if [[ ! ${QUIET_LOGS,,} =~ true ]]; then
echo "[vdlm_feeder] vdlm_server not running, exiting"
fi
exit
fi
set -e

SERVER_ADDR="UDP:${SERVER}:${SERVER_PORT}"
# shellcheck disable=SC2016
socat -d TCP:127.0.0.1:15555 "$SERVER_ADDR" \
2>&1 | stdbuf -oL awk '{print "[vdlm_feeder] " strftime("%Y/%m/%d %H:%M:%S", systime()) " " $0}'

sleep 5
12 changes: 12 additions & 0 deletions rootfs/etc/s6-overlay/scripts/vdlm_server
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/command/with-contenv bash
#shellcheck shell=bash

set -o pipefail
set -e

# Listens for the output of vdlm2dec (UDP), and makes it available for multiple processes at TCP port 15555
# shellcheck disable=SC2016
socat -u udp-listen:5555,fork stdout | ncat -4 --keep-open --listen 0.0.0.0 15555 \
2>&1 | stdbuf -oL awk '{print "[acars_server] " strftime("%Y/%m/%d %H:%M:%S", systime()) " " $0}'

sleep 5
39 changes: 39 additions & 0 deletions rootfs/etc/s6-overlay/scripts/vdlm_stats
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/command/with-contenv bash
#shellcheck shell=bash


set -o pipefail

# Require that vdlm_server is running
if ! netstat -an | grep -P '^\s*tcp\s+\d+\s+\d+\s+0\.0\.0\.0:15555\s+(?>\d{1,3}\.{0,1}){4}:\*\s+LISTEN\s*$' > /dev/null; then
sleep 1
if [[ ! ${QUIET_LOGS,,} =~ true ]]; then
echo "[vdlm_stats] vdlm_server not running, exiting"
fi
exit
fi

# Start our stats loop
while true; do

# capture 5 mins of flows
timeout --foreground 300s socat -u TCP:127.0.0.1:15555 CREATE:/run/acars/vdlm2.past5min.json

# if the port isn't reachable, this file isn't created, either container is shutting down or vdlm_server isn't reachable
# in both cases let's exit, if this should still be running it will be restarted
if ! [[ -f /run/acars/vdlm2.past5min.json ]]; then
exit
fi

# shellcheck disable=SC2016
echo "$(wc -l < /run/acars/vdlm.past5min.json) VDLM messages received in last 5 mins" | stdbuf -oL awk '{print "[vdlm_stats] " strftime("%Y/%m/%d %H:%M:%S", systime()) " " $0}'

# rotate files keeping last 2 hours
for i in {24..1}; do
mv "/run/acars/vdlm2.$((i-1)).json" "/run/acars/vdlm2.$i.json" > /dev/null 2>&1 || true
done
mv "/run/acars/vdlm2.past5min.json" "/run/acars/vdlm2.0.json" > /dev/null 2>&1 || true

done

sleep 5
2 changes: 1 addition & 1 deletion rootfs/etc/templates/bad
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
#shellcheck shell=bash

# shellcheck disable=SC2001,SC2034
Expand Down
2 changes: 1 addition & 1 deletion rootfs/etc/templates/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
#shellcheck shell=bash

# Require that vdlm_server is running
Expand Down
12 changes: 6 additions & 6 deletions rootfs/scripts/healthcheck.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash
# shellcheck shell=bash

# Import healthchecks-framework
Expand All @@ -19,13 +19,13 @@ function get_pid_of_decoder {
unset DEVICE_ID VDLM_BIN FREQ_STRING VDLM_BIN

# Get DEVICE_ID
eval "$(grep "DEVICE_ID=\"" "$service_dir"/run)"
eval "$(grep "DEVICE_ID=\"" "$service_dir")"

# Get FREQS_STRING
eval "$(grep "FREQ_STRING=\"" "$service_dir"/run)"
eval "$(grep "FREQ_STRING=\"" "$service_dir")"

# Get VDLM_BIN
eval "$(grep "VDLM_BIN=\"" "$service_dir"/run)"
eval "$(grep "VDLM_BIN=\"" "$service_dir")"

# Get PS output for the relevant process
if [[ -n "$VDLM_BIN" ]]; then
Expand All @@ -44,7 +44,7 @@ function get_pid_of_decoder {
# ===== Check vdlmdec processes =====

# For each service...
for service_dir in /etc/services.d/*; do
for service_dir in /etc/s6-overlay/scripts/*; do
service_name=$(basename "$service_dir")

# If the service is vdlmdec-*...
Expand Down Expand Up @@ -120,7 +120,7 @@ fi
echo "==== Check Service Death Tallies ====="

# Check service death tally
mapfile -t SERVICES < <(find /run/s6/legacy-services -maxdepth 1 -type d -not -name "*s6-*" | tail +2)
mapfile -t SERVICES < <(find /run/service -maxdepth 1 -not -name "*s6*" | tail +2)
for service in "${SERVICES[@]}"; do
SVDT=$(s6-svdt "$service" | grep -cv 'exitcode 0')
if [[ "$SVDT" -gt 0 ]]; then
Expand Down

0 comments on commit 5f8db78

Please sign in to comment.