From 7a234a8830f3a8c6393a93c600d0aa50fa710851 Mon Sep 17 00:00:00 2001 From: Eric Nemchik Date: Thu, 22 Sep 2022 13:34:53 +0000 Subject: [PATCH 1/4] Adjust for compatibility with standard-base --- .../s6-rc.d/svc-mod-swag-auto-reload/run | 38 +++++++++---------- root/etc/services.d/inotify/run | 37 +++++++++--------- 2 files changed, 35 insertions(+), 40 deletions(-) diff --git a/root/etc/s6-overlay/s6-rc.d/svc-mod-swag-auto-reload/run b/root/etc/s6-overlay/s6-rc.d/svc-mod-swag-auto-reload/run index af3e5cd..d4674de 100755 --- a/root/etc/s6-overlay/s6-rc.d/svc-mod-swag-auto-reload/run +++ b/root/etc/s6-overlay/s6-rc.d/svc-mod-swag-auto-reload/run @@ -1,45 +1,41 @@ #!/usr/bin/with-contenv bash +# shellcheck shell=bash -DEFAULT_WATCH=( \ - /config/nginx/authelia-location.conf \ - /config/nginx/authelia-server.conf \ - /config/nginx/geoip2.conf \ - /config/nginx/ldap.conf \ - /config/nginx/nginx.conf \ - /config/nginx/proxy-confs \ - /config/nginx/proxy.conf \ - /config/nginx/site-confs \ - /config/nginx/ssl.conf \ -) +DEFAULT_WATCH=() +mapfile -t DEFAULT_WATCH < <(find /config/nginx -type f -name '*.conf' -print) +DEFAULT_WATCH+=("/config/nginx/proxy-confs" "/config/nginx/site-confs") +# start with an empty array for active watch +ACTIVE_WATCH=() echo "MOD Auto-reload: Watching the following files/folders for changes" -for i in ${DEFAULT_WATCH[@]}; do +for i in "${DEFAULT_WATCH[@]}"; do if [ -f "${i}" ] || [ -d "${i}" ]; then echo "${i}" - ACTIVE_WATCH="${ACTIVE_WATCH} ${i}" + ACTIVE_WATCH+=("${i}") fi done -for i in $(echo "$WATCHLIST" | tr "|" " "); do +for i in $(echo "${WATCHLIST}" | tr "|" " "); do if [ -f "${i}" ] || [ -d "${i}" ]; then echo "${i}" - ACTIVE_WATCH="${ACTIVE_WATCH} ${i}" + ACTIVE_WATCH+=("${i}") fi done function wait_for_changes { inotifywait -rq \ --event modify,move,create,delete \ - ${ACTIVE_WATCH} + "${ACTIVE_WATCH[@]}" } while wait_for_changes; do - if ! cat /etc/nginx/nginx.conf | grep "/config/nginx/nginx.conf"; then - export NGINX_CONF="-c /config/nginx/nginx.conf" + NGINX_CONF=() + if ! grep -q "/config/nginx/nginx.conf" /etc/nginx/nginx.conf; then + NGINX_CONF=("-c" "/config/nginx/nginx.conf") fi - if /usr/sbin/nginx ${NGINX_CONF} -t; then + if /usr/sbin/nginx "${NGINX_CONF[@]}" -t; then echo "Changes to nginx config detected and the changes are valid, reloading nginx" - /usr/sbin/nginx ${NGINX_CONF} -s reload + /usr/sbin/nginx "${NGINX_CONF[@]}" -s reload else echo "Changes to nginx config detected but the changes are not valid, skipping nginx reload. Please fix your config." - fi + fi done diff --git a/root/etc/services.d/inotify/run b/root/etc/services.d/inotify/run index 0996168..d4674de 100755 --- a/root/etc/services.d/inotify/run +++ b/root/etc/services.d/inotify/run @@ -1,42 +1,41 @@ #!/usr/bin/with-contenv bash +# shellcheck shell=bash -DEFAULT_WATCH=( \ - /config/nginx/authelia-location.conf \ - /config/nginx/authelia-server.conf \ - /config/nginx/geoip2.conf \ - /config/nginx/ldap.conf \ - /config/nginx/nginx.conf \ - /config/nginx/proxy-confs \ - /config/nginx/proxy.conf \ - /config/nginx/site-confs \ - /config/nginx/ssl.conf \ -) +DEFAULT_WATCH=() +mapfile -t DEFAULT_WATCH < <(find /config/nginx -type f -name '*.conf' -print) +DEFAULT_WATCH+=("/config/nginx/proxy-confs" "/config/nginx/site-confs") +# start with an empty array for active watch +ACTIVE_WATCH=() echo "MOD Auto-reload: Watching the following files/folders for changes" -for i in ${DEFAULT_WATCH[@]}; do +for i in "${DEFAULT_WATCH[@]}"; do if [ -f "${i}" ] || [ -d "${i}" ]; then echo "${i}" - ACTIVE_WATCH="${ACTIVE_WATCH} ${i}" + ACTIVE_WATCH+=("${i}") fi done -for i in $(echo "$WATCHLIST" | tr "|" " "); do +for i in $(echo "${WATCHLIST}" | tr "|" " "); do if [ -f "${i}" ] || [ -d "${i}" ]; then echo "${i}" - ACTIVE_WATCH="${ACTIVE_WATCH} ${i}" + ACTIVE_WATCH+=("${i}") fi done function wait_for_changes { inotifywait -rq \ --event modify,move,create,delete \ - ${ACTIVE_WATCH} + "${ACTIVE_WATCH[@]}" } while wait_for_changes; do - if /usr/sbin/nginx -c /config/nginx/nginx.conf -t; then + NGINX_CONF=() + if ! grep -q "/config/nginx/nginx.conf" /etc/nginx/nginx.conf; then + NGINX_CONF=("-c" "/config/nginx/nginx.conf") + fi + if /usr/sbin/nginx "${NGINX_CONF[@]}" -t; then echo "Changes to nginx config detected and the changes are valid, reloading nginx" - /usr/sbin/nginx -c /config/nginx/nginx.conf -s reload + /usr/sbin/nginx "${NGINX_CONF[@]}" -s reload else echo "Changes to nginx config detected but the changes are not valid, skipping nginx reload. Please fix your config." - fi + fi done From a5b5dbe4f56ea0c8ecf995c53dee1ec9936a8395 Mon Sep 17 00:00:00 2001 From: Eric Nemchik Date: Thu, 22 Sep 2022 13:53:54 +0000 Subject: [PATCH 2/4] Update README --- README.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a92290e..9ad2587 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,9 @@ # Auto-reload - Docker mod for Nginx based images -This mod allows Nginx to be reloaded automatically whenever there are valid changes to the following files and folders: -- /config/nginx/authelia-location.conf -- /config/nginx/authelia-server.conf -- /config/nginx/geoip2.conf -- /config/nginx/ldap.conf -- /config/nginx/nginx.conf +This mod allows Nginx to be reloaded automatically whenever there are valid changes to the `*.conf` files in `/config/nginx` or any files in the following folders: + - /config/nginx/proxy-confs -- /config/nginx/proxy.conf - /config/nginx/site-confs -- /config/nginx/ssl.conf In the container's docker arguments, set an environment variable `DOCKER_MODS=linuxserver/mods:swag-auto-reload` From a6de1bfc199da042adac24a6b642656d5157f322 Mon Sep 17 00:00:00 2001 From: Eric Nemchik Date: Thu, 22 Sep 2022 14:42:52 +0000 Subject: [PATCH 3/4] Watch the whole folder Exclude sample and md --- README.md | 5 +--- .../s6-rc.d/svc-mod-swag-auto-reload/run | 23 ++----------------- root/etc/services.d/inotify/run | 23 ++----------------- 3 files changed, 5 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 9ad2587..011ba64 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,6 @@ # Auto-reload - Docker mod for Nginx based images -This mod allows Nginx to be reloaded automatically whenever there are valid changes to the `*.conf` files in `/config/nginx` or any files in the following folders: - -- /config/nginx/proxy-confs -- /config/nginx/site-confs +This mod allows Nginx to be reloaded automatically whenever there are new files, or valid changes to the files in `/config/nginx`. In the container's docker arguments, set an environment variable `DOCKER_MODS=linuxserver/mods:swag-auto-reload` diff --git a/root/etc/s6-overlay/s6-rc.d/svc-mod-swag-auto-reload/run b/root/etc/s6-overlay/s6-rc.d/svc-mod-swag-auto-reload/run index d4674de..06d3b88 100755 --- a/root/etc/s6-overlay/s6-rc.d/svc-mod-swag-auto-reload/run +++ b/root/etc/s6-overlay/s6-rc.d/svc-mod-swag-auto-reload/run @@ -1,30 +1,11 @@ #!/usr/bin/with-contenv bash # shellcheck shell=bash -DEFAULT_WATCH=() -mapfile -t DEFAULT_WATCH < <(find /config/nginx -type f -name '*.conf' -print) -DEFAULT_WATCH+=("/config/nginx/proxy-confs" "/config/nginx/site-confs") - -# start with an empty array for active watch -ACTIVE_WATCH=() -echo "MOD Auto-reload: Watching the following files/folders for changes" -for i in "${DEFAULT_WATCH[@]}"; do - if [ -f "${i}" ] || [ -d "${i}" ]; then - echo "${i}" - ACTIVE_WATCH+=("${i}") - fi -done -for i in $(echo "${WATCHLIST}" | tr "|" " "); do - if [ -f "${i}" ] || [ -d "${i}" ]; then - echo "${i}" - ACTIVE_WATCH+=("${i}") - fi -done - function wait_for_changes { inotifywait -rq \ --event modify,move,create,delete \ - "${ACTIVE_WATCH[@]}" + --excludei '\.(sample|md)' \ + "/config/nginx" } while wait_for_changes; do diff --git a/root/etc/services.d/inotify/run b/root/etc/services.d/inotify/run index d4674de..06d3b88 100755 --- a/root/etc/services.d/inotify/run +++ b/root/etc/services.d/inotify/run @@ -1,30 +1,11 @@ #!/usr/bin/with-contenv bash # shellcheck shell=bash -DEFAULT_WATCH=() -mapfile -t DEFAULT_WATCH < <(find /config/nginx -type f -name '*.conf' -print) -DEFAULT_WATCH+=("/config/nginx/proxy-confs" "/config/nginx/site-confs") - -# start with an empty array for active watch -ACTIVE_WATCH=() -echo "MOD Auto-reload: Watching the following files/folders for changes" -for i in "${DEFAULT_WATCH[@]}"; do - if [ -f "${i}" ] || [ -d "${i}" ]; then - echo "${i}" - ACTIVE_WATCH+=("${i}") - fi -done -for i in $(echo "${WATCHLIST}" | tr "|" " "); do - if [ -f "${i}" ] || [ -d "${i}" ]; then - echo "${i}" - ACTIVE_WATCH+=("${i}") - fi -done - function wait_for_changes { inotifywait -rq \ --event modify,move,create,delete \ - "${ACTIVE_WATCH[@]}" + --excludei '\.(sample|md)' \ + "/config/nginx" } while wait_for_changes; do From d83f92e0077ee4992d522bb196366f27b8dc6f78 Mon Sep 17 00:00:00 2001 From: Eric Nemchik Date: Thu, 22 Sep 2022 14:52:32 +0000 Subject: [PATCH 4/4] Fix logic --- .../s6-overlay/s6-rc.d/svc-mod-swag-auto-reload/run | 12 +++++++++++- root/etc/services.d/inotify/run | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/root/etc/s6-overlay/s6-rc.d/svc-mod-swag-auto-reload/run b/root/etc/s6-overlay/s6-rc.d/svc-mod-swag-auto-reload/run index 06d3b88..1e92149 100755 --- a/root/etc/s6-overlay/s6-rc.d/svc-mod-swag-auto-reload/run +++ b/root/etc/s6-overlay/s6-rc.d/svc-mod-swag-auto-reload/run @@ -1,11 +1,21 @@ #!/usr/bin/with-contenv bash # shellcheck shell=bash +echo "MOD Auto-reload: Watching the following files/folders for changes (excluding .sample and .md files):" +echo "/config/nginx" +ACTIVE_WATCH=("/config/nginx") +for i in $(echo "${WATCHLIST}" | tr "|" " "); do + if [ -f "${i}" ] || [ -d "${i}" ]; then + echo "${i}" + ACTIVE_WATCH+=("${i}") + fi +done + function wait_for_changes { inotifywait -rq \ --event modify,move,create,delete \ --excludei '\.(sample|md)' \ - "/config/nginx" + "${ACTIVE_WATCH[@]}" } while wait_for_changes; do diff --git a/root/etc/services.d/inotify/run b/root/etc/services.d/inotify/run index 06d3b88..1e92149 100755 --- a/root/etc/services.d/inotify/run +++ b/root/etc/services.d/inotify/run @@ -1,11 +1,21 @@ #!/usr/bin/with-contenv bash # shellcheck shell=bash +echo "MOD Auto-reload: Watching the following files/folders for changes (excluding .sample and .md files):" +echo "/config/nginx" +ACTIVE_WATCH=("/config/nginx") +for i in $(echo "${WATCHLIST}" | tr "|" " "); do + if [ -f "${i}" ] || [ -d "${i}" ]; then + echo "${i}" + ACTIVE_WATCH+=("${i}") + fi +done + function wait_for_changes { inotifywait -rq \ --event modify,move,create,delete \ --excludei '\.(sample|md)' \ - "/config/nginx" + "${ACTIVE_WATCH[@]}" } while wait_for_changes; do