From 36ca35d68005c3f759be624331140a335438fc29 Mon Sep 17 00:00:00 2001 From: aptalca <541623+aptalca@users.noreply.github.com> Date: Thu, 5 Dec 2024 13:09:01 -0500 Subject: [PATCH 1/3] add support for new label swag_preset_conf --- README.md | 1 + root/app/auto-proxy.sh | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 57ba600..79957d0 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ This mod gives SWAG the ability to auto-detect running containers via labels and - `swag_auth=authelia` - *optional* - enables auth methods (options are `authelia`, `authentik`, `ldap` and `http` for basic http auth) - `swag_auth_bypass=/api,/othersubfolder` - *optional* - bypasses auth for selected subfolders. Comma separated, no spaces. - `swag_server_custom_directive=custom_directive;` - *optional* - injects the label value as is into the server block of the generated conf. Must be a valid nginx directive, ending with a semi colon. +- `swag_preset_conf=confname` - *optional* - allows defining a preset conf to use if the container name does not match one (if conf name is `radarr.subdomain.conf`, set this value to `radarr`). If container name matches an existing conf, this var will be ignored. In SWAG docker arguments, set an environment variable `DOCKER_MODS=linuxserver/mods:universal-docker|linuxserver/mods:swag-auto-proxy` and either add a volume mapping for `/var/run/docker.sock:/var/run/docker.sock:ro`, or set an environment var `DOCKER_HOST=remoteaddress`. diff --git a/root/app/auto-proxy.sh b/root/app/auto-proxy.sh index 6aca077..afc5155 100755 --- a/root/app/auto-proxy.sh +++ b/root/app/auto-proxy.sh @@ -13,7 +13,7 @@ else AUTO_GEN="${CONTAINER} ${AUTO_GEN}" else INSPECTION=$(docker inspect ${CONTAINER}) - for VAR in swag_address swag_port swag_proto swag_url swag_auth swag_auth_bypass swag_server_custom_directive; do + for VAR in swag_address swag_port swag_proto swag_url swag_auth swag_auth_bypass swag_server_custom_directive swag_preset_conf; do VAR_VALUE=$(echo ${INSPECTION} | jq -r ".[0].Config.Labels[\"${VAR}\"]") if [ "${VAR_VALUE}" == "null" ]; then VAR_VALUE="" @@ -40,7 +40,7 @@ fi for CONTAINER in ${AUTO_GEN}; do INSPECTION=$(docker inspect ${CONTAINER}) rm -rf "/auto-proxy/${CONTAINER}.conf" - for VAR in swag_address swag_port swag_proto swag_url swag_auth swag_auth_bypass swag_server_custom_directive; do + for VAR in swag_address swag_port swag_proto swag_url swag_auth swag_auth_bypass swag_server_custom_directive swag_preset_conf; do VAR_VALUE=$(echo ${INSPECTION} | jq -r ".[0].Config.Labels[\"${VAR}\"]") if [ "${VAR_VALUE}" == "null" ]; then VAR_VALUE="" @@ -48,9 +48,14 @@ for CONTAINER in ${AUTO_GEN}; do echo "${VAR}=\"${VAR_VALUE}\"" >> "/auto-proxy/${CONTAINER}.conf" done . /auto-proxy/${CONTAINER}.conf - if [ -f "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" ]; then - cp "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf" - echo "**** Using preset proxy conf for ${CONTAINER} ****" + if [ -f "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" ] || [ -f "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then + if [ -f "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" ]; then + echo "**** Container name matches a preset proxy conf, using ${CONTAINER}.subdomain.conf for container ${CONTAINER} ****" + cp "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf" + elif [ -f "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then + echo "**** Label swag_preset_conf matches a preset proxy conf, using ${swag_preset_conf}.subdomain.conf for container ${CONTAINER} ****" + cp "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf" + fi if [ -n "${swag_auth_bypass}" ]; then echo "**** Swag auth bypass is auto managed via preset confs and cannot be overridden via env vars ****" fi @@ -96,6 +101,9 @@ for CONTAINER in ${AUTO_GEN}; do echo "**** Enabling basic http auth for ${CONTAINER} ****" fi else + if [ -n "${swag_preset_conf}" ] && [ ! "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then + echo "**** Label swag_preset_conf is set, but no preset proxy conf found with the name ${swag_preset_conf}.subdomain.conf.sample ****" + fi echo "**** No preset proxy conf found for ${CONTAINER}, generating from scratch ****" cp "/config/nginx/proxy-confs/_template.subdomain.conf.sample" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf" if [ -n "${swag_auth_bypass}" ]; then From 63db34a4620ed542d5cf9f59470efee0a9f13b35 Mon Sep 17 00:00:00 2001 From: aptalca <541623+aptalca@users.noreply.github.com> Date: Thu, 5 Dec 2024 14:47:10 -0500 Subject: [PATCH 2/3] default `swag_address` and `swag_url` to container name if `swag_proxy_conf` is set --- root/app/auto-proxy.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/root/app/auto-proxy.sh b/root/app/auto-proxy.sh index afc5155..79f23a6 100755 --- a/root/app/auto-proxy.sh +++ b/root/app/auto-proxy.sh @@ -62,6 +62,9 @@ for CONTAINER in ${AUTO_GEN}; do if [ -n "${swag_address}" ]; then sed -i "s|set \$upstream_app .*|set \$upstream_app ${swag_address};|g" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf" echo "**** Overriding address as ${swag_address} for ${CONTAINER} ****" + elif [ -n "${swag_preset_conf}" ] && [ -f "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then + sed -i "s|set \$upstream_app .*|set \$upstream_app ${CONTAINER};|g" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf" + echo "**** Overriding address as ${CONTAINER} for ${CONTAINER} ****" fi if [ -n "${swag_port}" ]; then sed -i "s|set \$upstream_port .*|set \$upstream_port ${swag_port};|g" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf" @@ -75,6 +78,9 @@ for CONTAINER in ${AUTO_GEN}; do SED_swag_url=$(sed -e 's/[&\\|]/\\&/g; s|$|\\|; $s|\\$||' <<<"${swag_url}") sed -i "s|server_name .*|server_name ${SED_swag_url};|" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf" echo "**** Overriding url as ${swag_url} for ${CONTAINER} ****" + elif [ -n "${swag_preset_conf}" ] && [ -f "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then + sed -i "s|server_name .*|server_name ${CONTAINER}.*;|" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf" + echo "**** Overriding url as ${CONTAINER}.* for ${CONTAINER} ****" fi if [ -n "${swag_server_custom_directive}" ]; then SED_swag_server_custom_directive=$(sed -e 's/[&\\|]/\\&/g; s|$|\\|; $s|\\$||' <<<"${swag_server_custom_directive}") From 70268ef04a92bf683971181d5bea92acad836a03 Mon Sep 17 00:00:00 2001 From: aptalca <541623+aptalca@users.noreply.github.com> Date: Thu, 5 Dec 2024 14:49:59 -0500 Subject: [PATCH 3/3] use more selective if condition --- root/app/auto-proxy.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/root/app/auto-proxy.sh b/root/app/auto-proxy.sh index 79f23a6..875337d 100755 --- a/root/app/auto-proxy.sh +++ b/root/app/auto-proxy.sh @@ -62,7 +62,7 @@ for CONTAINER in ${AUTO_GEN}; do if [ -n "${swag_address}" ]; then sed -i "s|set \$upstream_app .*|set \$upstream_app ${swag_address};|g" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf" echo "**** Overriding address as ${swag_address} for ${CONTAINER} ****" - elif [ -n "${swag_preset_conf}" ] && [ -f "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then + elif [ ! -f "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" ] && [ -n "${swag_preset_conf}" ] && [ -f "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then sed -i "s|set \$upstream_app .*|set \$upstream_app ${CONTAINER};|g" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf" echo "**** Overriding address as ${CONTAINER} for ${CONTAINER} ****" fi @@ -78,7 +78,7 @@ for CONTAINER in ${AUTO_GEN}; do SED_swag_url=$(sed -e 's/[&\\|]/\\&/g; s|$|\\|; $s|\\$||' <<<"${swag_url}") sed -i "s|server_name .*|server_name ${SED_swag_url};|" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf" echo "**** Overriding url as ${swag_url} for ${CONTAINER} ****" - elif [ -n "${swag_preset_conf}" ] && [ -f "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then + elif [ ! -f "/config/nginx/proxy-confs/${CONTAINER}.subdomain.conf.sample" ] && [ -n "${swag_preset_conf}" ] && [ -f "/config/nginx/proxy-confs/${swag_preset_conf}.subdomain.conf.sample" ]; then sed -i "s|server_name .*|server_name ${CONTAINER}.*;|" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf" echo "**** Overriding url as ${CONTAINER}.* for ${CONTAINER} ****" fi