Files
hassio-addons/birdnet-pi/rootfs/etc/cont-init.d/91-nginx_ingress.sh
Alexandre 988cecb122 fix(birdnet-pi): restore the ingress Caddy site (502 Bad Gateway) (#2931)
* fix(birdnet-pi): restore the ingress Caddy site, ingress returned 502

nginx forwards ingress traffic to 127.0.0.1:8082 (rootfs/etc/nginx/servers/
ingress.conf:11), a site appended to the Caddyfile by helpers/caddy_ingress.sh.
The upstream script $HOME/BirdNET-Pi/scripts/update_caddyfile.sh regenerates
/etc/caddy/Caddyfile from scratch and 02-caddy.sh runs it immediately before
`exec caddy run`, so 91-nginx_ingress.sh injected a call to caddy_ingress.sh
into that script to re-add the site. The injection was anchored on
`sudo caddy fmt --overwrite`.

Since 2026.07.10-1 the Dockerfile strips `sudo ` from every BirdNET-Pi script at
build time (Dockerfile:110), so the shipped line is `caddy fmt --overwrite` and
the anchor stopped matching. sed reports success when a pattern matches nothing,
so this failed silently: Caddy came up listening only on :8081 and every ingress
request got connection-refused on 8082. Confirmed by extracting the script from
the published ghcr.io/alexbelgium/birdnet-pi-amd64 image - it contains no `sudo`.

- 91-nginx_ingress.sh: make the anchor accept the line with or without `sudo`,
  skip the injection when it is already there (cont-init re-runs on restart),
  and verify afterwards, falling back to appending the call if the anchor is
  ever gone again.
- caddy_ingress.sh: return early when a `:8082` site already exists. The script
  now runs from more than one place, and a duplicate site address makes Caddy
  refuse to start.
- 02-caddy.sh: re-add the ingress site just before starting Caddy if it is
  missing, so a future upstream change to update_caddyfile.sh cannot silently
  bring back the 502.
- 91-nginx_ingress.sh: drop /ingress_url when ingress is off, so that marker is
  a truthful signal for the check above even across an in-container restart.

Verified with a harness that replays the boot sequence (build-time sudo strip,
81-modifications.sh, 91-nginx_ingress.sh, 02-caddy.sh) against the real upstream
update_caddyfile.sh: master ends with no :8082 site, this branch ends with
exactly one, on fresh boot, on restart, and when the `caddy fmt` anchor is
removed entirely; standalone mode still gets no ingress site.

Closes #2928

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(birdnet-pi): harden the ingress-site recovery check in 02-caddy.sh

Review feedback on the pre-start check:

- Require /etc/caddy/Caddyfile to exist and silence grep's stderr. `! grep` is
  also true for grep's exit code 2, so an unreadable or missing Caddyfile read
  as "ingress site missing" and would have produced an ingress-only Caddyfile
  out of an error state. Letting caddy fail on the missing config is easier to
  diagnose.
- Report a failure of caddy_ingress.sh instead of swallowing it. Do not exit:
  /custom-services.d scripts are LSIO longruns that s6 restarts when they
  return, so exiting would flap the service and take the WebUI down on 8081 as
  well, which is strictly worse than ingress alone being broken.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-02 17:56:19 +02:00

95 lines
3.5 KiB
Bash
Executable File

#!/command/with-contenv bashio
# shellcheck shell=bash
set -e
##################
# ALLOW RESTARTS #
##################
if [[ "${BASH_SOURCE[0]}" == /etc/cont-init.d/* ]]; then
mkdir -p /etc/scripts-init
sed -i "s|/etc/cont-init.d|/etc/scripts-init|g" /ha_entrypoint.sh
sed -i "/ rm/d" /ha_entrypoint.sh
cp "${BASH_SOURCE[0]}" /etc/scripts-init/
fi
#################
# NGINX SETTING #
#################
# Variables
ingress_port=$(bashio::addon.ingress_port)
ingress_interface=$(bashio::addon.ip_address)
ingress_entry=$(bashio::addon.ingress_entry)
# Quits if ingress is not active
if [[ "$ingress_entry" != "/api"* ]]; then
bashio::log.info "Ingress entry is not set, exiting configuration."
sed -i "1a sleep infinity" /custom-services.d/02-nginx.sh
# This script re-runs from /etc/scripts-init on an add-on restart, so drop
# any marker left by an earlier run: it is what 02-caddy.sh reads to decide
# whether the ingress site belongs in the Caddyfile.
rm -f /ingress_url
exit 0
fi
bashio::log.info "Adapting for ingress"
echo "... setting up nginx"
# Check if the NGINX configuration file exists
nginx_conf="/etc/nginx/servers/ingress.conf"
if [ -f "$nginx_conf" ]; then
sed -i "s/%%port%%/${ingress_port}/g" "$nginx_conf"
sed -i "s/%%interface%%/${ingress_interface}/g" "$nginx_conf"
sed -i "s|%%ingress_entry%%|${ingress_entry}|g" "$nginx_conf"
else
bashio::log.error "NGINX configuration file not found: $nginx_conf"
exit 1
fi
# Disable log
sed -i "/View Log/d" "$HOME/BirdNET-Pi/homepage/views.php"
echo "... ensuring restricted area access"
echo "${ingress_entry}" > /ingress_url
# Modify PHP file safely
php_file="$HOME/BirdNET-Pi/scripts/common.php"
if [ -f "$php_file" ]; then
sed -i "/function is_authenticated/a if (strpos(\$_SERVER['HTTP_REFERER'], '/api/hassio_ingress') !== false && strpos(\$_SERVER['HTTP_REFERER'], trim(file_get_contents('/ingress_url'))) !== false) { \$ret = true; return \$ret; }" "$php_file"
else
bashio::log.warning "PHP file not found: $php_file"
fi
echo "... adapting Caddyfile for ingress"
chmod +x /helpers/caddy_ingress.sh
# Correct script execution
/helpers/caddy_ingress.sh
# Correct API images
sed -i "s|localhost|localhost:8082|g" "$HOME/BirdNET-Pi/scripts/utils/notifications.py"
# Update the Caddyfile if update script exists
caddy_update_script="$HOME/BirdNET-Pi/scripts/update_caddyfile.sh"
if [ ! -f "$caddy_update_script" ]; then
bashio::log.error "Caddy update script not found: $caddy_update_script"
exit 1
fi
# update_caddyfile.sh rewrites /etc/caddy/Caddyfile from scratch, which drops
# the ingress site added just above. 02-caddy.sh runs it right before starting
# caddy, so the hook below has to re-add the site from inside that script, just
# before it formats and reloads the config.
# The anchor must not require "sudo": the Dockerfile strips it from every
# BirdNET-Pi script at build time, so the shipped line is "caddy fmt --overwrite".
if ! grep -qF "/helpers/caddy_ingress.sh" "$caddy_update_script"; then
sed -i -E "/^[[:space:]]*(sudo[[:space:]]+)?caddy[[:space:]]+fmt[[:space:]]+--overwrite/i /helpers/caddy_ingress.sh" "$caddy_update_script"
fi
# sed is silent when the anchor is missing; make sure the hook is really there
if ! grep -qF "/helpers/caddy_ingress.sh" "$caddy_update_script"; then
bashio::log.warning "Could not anchor the ingress site in $caddy_update_script, appending it instead"
printf '\n/helpers/caddy_ingress.sh\n' >> "$caddy_update_script"
fi