Skip to content

Commit f709a0f

Browse files
committed
swag: docker_seed_swag_proxy_conf helper, wire netbox subfolder proxy
linuxserver/reverse-proxy-confs:master doesn't ship a sample for every service we package — netbox and immich are the obvious gaps. The existing docker_configure_swag_proxy() returns 1 when no sample is present, so the swag-aware install paths in those modules silently no-op on real deployments. Add a generic helper next to docker_configure_swag_proxy: docker_seed_swag_proxy_conf <servicename> <<'NGINX' location ^~ /<svc> { … } NGINX Reads the conf body from stdin and writes it as /config/nginx/proxy-confs/<svc>.subfolder.conf.sample inside the SWAG container. Returns 2 on no-SWAG (no-op), 0 on success or if a sample already exists (defer to LSIO upstream when they eventually ship one, keep an admin's hand-edited sample intact across re-installs), 1 on docker exec failure. module_netbox now seeds its own subfolder proxy-conf before calling docker_configure_swag_proxy, and conditionally adds BASE_PATH=netbox to the netbox container env when a SWAG container is present at install time. Without BASE_PATH the rendered HTML emits absolute /static/ /api/ … URLs that 404 once SWAG serves NetBox at /netbox. Trade-off: direct port access (http://host:port/) stops working — only http://host:port/netbox/ — but SWAG is the intended way in once it's set up. Same pattern can be reused for immich and any future no-LSIO-sample service in a follow-up; the helper is generic. Depends on #908 (passes postgres image+tag separately) for the netbox install to reach this code path.
1 parent dcd1c52 commit f709a0f

2 files changed

Lines changed: 94 additions & 2 deletions

File tree

tools/modules/functions/module_docker_utils.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,3 +525,50 @@ docker_configure_swag_proxy() {
525525

526526
return 1
527527
}
528+
529+
#
530+
# Seed a custom SWAG subfolder proxy-conf sample inside the SWAG container.
531+
# Usage: docker_seed_swag_proxy_conf <servicename> <<'NGINX'
532+
# location ^~ /<servicename> { … }
533+
# NGINX
534+
#
535+
# Used for services that linuxserver/reverse-proxy-confs:master does NOT
536+
# ship a stock sample for (e.g. netbox, immich). Reads the conf body from
537+
# stdin and writes it to:
538+
# /config/nginx/proxy-confs/<servicename>.subfolder.conf.sample
539+
# inside the SWAG container, where docker_configure_swag_proxy() picks
540+
# it up on its next call.
541+
#
542+
# Behaviour:
543+
# - If the SWAG container isn't installed, returns 2 (no-op).
544+
# - If a `.sample` is already present (whether stock LSIO or seeded by
545+
# a prior call), returns 0 without overwriting — defer to upstream
546+
# when it eventually ships one, and keep an admin's hand-edited
547+
# sample intact across re-installs.
548+
# - Otherwise writes the body and returns 0 on success, 1 on docker
549+
# exec failure.
550+
#
551+
# Returns: 0 on success / no-op skip, 1 on failure, 2 if no SWAG.
552+
#
553+
docker_seed_swag_proxy_conf() {
554+
local servicename="$1"
555+
556+
if ! docker container ls -a --format "{{.Names}}" | grep -q "^swag$"; then
557+
return 2
558+
fi
559+
560+
local proxy_sample="/config/nginx/proxy-confs/${servicename}.subfolder.conf.sample"
561+
562+
# Already there — let it be (LSIO upstream may have started shipping
563+
# one between releases; the admin may have hand-edited it).
564+
if docker exec swag test -f "$proxy_sample" 2>/dev/null; then
565+
return 0
566+
fi
567+
568+
# `docker exec -i ... sh -c "cat > FILE"` is the portable way to
569+
# stream stdin into a file inside the container.
570+
if docker exec -i swag sh -c "cat > '${proxy_sample}'" 2>/dev/null; then
571+
return 0
572+
fi
573+
return 1
574+
}

tools/modules/software/module_netbox.sh

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,34 @@ function module_netbox () {
6666
# Generate a random secret key (50+ chars)
6767
NETBOX_SECRET_KEY=$(tr -dc 'A-Za-z0-9!@#$%^&*()-_=+' </dev/urandom | head -c 64)
6868

69+
# When SWAG is on this host, NetBox needs to know it lives
70+
# at /netbox/ — otherwise Django renders absolute paths
71+
# (form action="/login/?next=/netbox", static URIs like
72+
# /static/…) that 404 once SWAG strips them at /netbox.
73+
# Same applies to CSRF: Django rejects POSTs whose Origin
74+
# header isn't in CSRF_TRUSTED_ORIGINS, so the SWAG host
75+
# has to be listed there.
76+
#
77+
# Both settings are real Python in configuration.py, not
78+
# env vars — netboxcommunity/netbox reads /etc/netbox/
79+
# config/configuration.py, not BASE_PATH/CSRF_TRUSTED_*
80+
# from the container env.
81+
local netbox_base_path=""
82+
local netbox_csrf_origins=""
83+
if docker container ls -a --format "{{.Names}}" 2>/dev/null | grep -q "^swag$"; then
84+
netbox_base_path="BASE_PATH = 'netbox/'"
85+
if [[ -n "${SWAG_URL:-}" ]]; then
86+
netbox_csrf_origins="CSRF_TRUSTED_ORIGINS = ['https://${SWAG_URL}']"
87+
fi
88+
fi
89+
6990
# Create configuration directory and file
7091
mkdir -p "$base_dir/config"
7192
if [[ ! -f "$base_dir/config/configuration.py" ]]; then
7293
cat > "$base_dir/config/configuration.py" <<- EOT
7394
ALLOWED_HOSTS = ['*']
95+
${netbox_base_path}
96+
${netbox_csrf_origins}
7497
DATABASE = {
7598
'NAME': '$DATABASE_NAME',
7699
'USER': '$DATABASE_USER',
@@ -102,7 +125,10 @@ function module_netbox () {
102125
# Pull image
103126
docker_operation_progress pull "$dockerimage"
104127

105-
# Run container
128+
# Run container. The BASE_PATH / CSRF_TRUSTED_ORIGINS
129+
# settings that make NetBox SWAG-aware are baked into
130+
# configuration.py above (env vars are not consumed by
131+
# upstream NetBox).
106132
if ! docker_operation_progress run "$dockername" \
107133
-d \
108134
--name="$dockername" \
@@ -144,7 +170,26 @@ function module_netbox () {
144170
done
145171
fi
146172

147-
# Auto-configure SWAG reverse proxy if available
173+
# Auto-configure SWAG reverse proxy if available.
174+
# linuxserver/reverse-proxy-confs:master doesn't ship a
175+
# netbox sample, so seed our own first. No-op on hosts
176+
# without SWAG, and skipped if a sample is already in
177+
# place (LSIO upstream / hand-edited admin override).
178+
docker_seed_swag_proxy_conf "netbox" <<- 'NGINX'
179+
## Custom Armbian seed — netbox subfolder proxy.
180+
## upstream NetBox runs with BASE_PATH=netbox so the
181+
## upstream URI is /netbox, not /. No path rewriting.
182+
location ^~ /netbox {
183+
include /config/nginx/proxy.conf;
184+
include /config/nginx/resolver.conf;
185+
186+
set $upstream_app netbox;
187+
set $upstream_port 8080;
188+
set $upstream_proto http;
189+
190+
proxy_pass $upstream_proto://$upstream_app:$upstream_port;
191+
}
192+
NGINX
148193
docker_configure_swag_proxy "netbox" "8080"
149194

150195
# Delete default API Token

0 commit comments

Comments
 (0)