From 45a9254145a76d4d00938babf1cb023676978869 Mon Sep 17 00:00:00 2001 From: "Benjamin W. Broersma" Date: Fri, 10 Oct 2025 19:46:56 +0200 Subject: [PATCH] Fix #1027 - Support templating mail Added functions to keep it DRY Added mail template test --- .test/config.sh | 1 + .test/tests/templates-mail/run.sh | 46 +++++++++++ .../templates-mail/smtp.conf.mail-template | 6 ++ .../smtp_auth_http.conf.template | 9 +++ entrypoint/20-envsubst-on-templates.sh | 80 +++++++++++-------- .../alpine-slim/20-envsubst-on-templates.sh | 80 +++++++++++-------- mainline/debian/20-envsubst-on-templates.sh | 80 +++++++++++-------- .../alpine-slim/20-envsubst-on-templates.sh | 80 +++++++++++-------- stable/debian/20-envsubst-on-templates.sh | 80 +++++++++++-------- 9 files changed, 287 insertions(+), 175 deletions(-) create mode 100755 .test/tests/templates-mail/run.sh create mode 100644 .test/tests/templates-mail/smtp.conf.mail-template create mode 100644 .test/tests/templates-mail/smtp_auth_http.conf.template diff --git a/.test/config.sh b/.test/config.sh index e371f4043..07c31adc9 100755 --- a/.test/config.sh +++ b/.test/config.sh @@ -3,6 +3,7 @@ imageTests+=( ipv6 static templates + templates-mail templates-resolver templates-resolver-ipv6 workers diff --git a/.test/tests/templates-mail/run.sh b/.test/tests/templates-mail/run.sh new file mode 100755 index 000000000..1d11e2554 --- /dev/null +++ b/.test/tests/templates-mail/run.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +[ "$DEBUG" ] && set -x + +set -eo pipefail + +dir="$(dirname "$(readlink -f "$BASH_SOURCE")")" + +image="$1" + +clientImage='alpine:latest' +# ensure the clientImage is ready and available +if ! docker image inspect "$clientImage" &> /dev/null; then + docker pull "$clientImage" > /dev/null +fi + +# Create an instance of the container-under-test +serverImage="$("$HOME/oi/test/tests/image-name.sh" librarytest/nginx-template "$image")" +"$HOME/oi/test/tests/docker-build.sh" "$dir" "$serverImage" < /dev/null" EXIT + +_request() { + if [ "$(docker inspect -f '{{.State.Running}}' "$cid" 2>/dev/null)" != 'true' ]; then + echo >&2 "$image stopped unexpectedly!" + ( set -x && docker logs "$cid" ) >&2 || true + false + fi + + docker run --rm \ + --link "$cid":nginx \ + "$clientImage" \ + nc -w 1 nginx 25 +} + +. "$HOME/oi/test/retry.sh" '[ "$(_request --output /dev/null || echo $?)" != 7 ]' + +# Check that we can open an SMTP connection +_request | grep 'ESMTP ready' diff --git a/.test/tests/templates-mail/smtp.conf.mail-template b/.test/tests/templates-mail/smtp.conf.mail-template new file mode 100644 index 000000000..502ea7692 --- /dev/null +++ b/.test/tests/templates-mail/smtp.conf.mail-template @@ -0,0 +1,6 @@ +server_name server-name; +auth_http http://127.0.0.1:9000/; +server { + listen 25; + protocol smtp; +} diff --git a/.test/tests/templates-mail/smtp_auth_http.conf.template b/.test/tests/templates-mail/smtp_auth_http.conf.template new file mode 100644 index 000000000..5d3b4a9a5 --- /dev/null +++ b/.test/tests/templates-mail/smtp_auth_http.conf.template @@ -0,0 +1,9 @@ +server { + listen 127.0.0.1:9000; + location / { + default_type text/plain; + add_header Auth-Status "Login not supported since this is a dummy nginx smtp handler"; + add_header Auth-Error-Code "550 5.3.5"; + return 200; + } +} diff --git a/entrypoint/20-envsubst-on-templates.sh b/entrypoint/20-envsubst-on-templates.sh index 3804165c9..48c6ca82b 100755 --- a/entrypoint/20-envsubst-on-templates.sh +++ b/entrypoint/20-envsubst-on-templates.sh @@ -10,66 +10,76 @@ entrypoint_log() { fi } -add_stream_block() { +ensure_output_writable() { + local test_dir=$1 + if [ ! -w "$test_dir" ]; then + entrypoint_log "$ME: ERROR: $template_dir exists, but $test_dir is not writable" + exit 0 + fi +} + +add_extra_block() { + local extra=$1 + local extra_output_dir=$2 local conffile="/etc/nginx/nginx.conf" - if grep -q -E "\s*stream\s*\{" "$conffile"; then - entrypoint_log "$ME: $conffile contains a stream block; include $stream_output_dir/*.conf to enable stream templates" + if grep -q -E "\s*$extra\s*\{" "$conffile"; then + entrypoint_log "$ME: $conffile contains a $extra block; include $extra_output_dir/*.conf to enable $extra templates" else # check if the file can be modified, e.g. not on a r/o filesystem touch "$conffile" 2>/dev/null || { entrypoint_log "$ME: info: can not modify $conffile (read-only file system?)"; exit 0; } - entrypoint_log "$ME: Appending stream block to $conffile to include $stream_output_dir/*.conf" + entrypoint_log "$ME: Appending $extra block to $conffile to include $extra_output_dir/*.conf" cat << END >> "$conffile" # added by "$ME" on "$(date)" -stream { - include $stream_output_dir/*.conf; +$extra { + include $extra_output_dir/*.conf; } END fi } +write_template_conf() { + local select_suffix=$1 + local conf_output_dir=$2 + local template relative_path output_path subdir + find "$template_dir" -follow -type f -name "*$select_suffix" -print | while read -r template; do + relative_path="${template#"$template_dir/"}" + output_path="$conf_output_dir/${relative_path%"$select_suffix"}" + subdir=$(dirname "$relative_path") + # create a subdirectory where the template file exists + mkdir -p "$conf_output_dir/$subdir" + entrypoint_log "$ME: Running envsubst on $template to $output_path" + envsubst "$defined_envs" < "$template" > "$output_path" + done +} + auto_envsubst() { local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}" local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}" local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}" + local mail_suffix="${NGINX_ENVSUBST_MAIL_TEMPLATE_SUFFIX:-.mail-template}" + local mail_output_dir="${NGINX_ENVSUBST_MAIL_OUTPUT_DIR:-/etc/nginx/mail-conf.d}" local stream_suffix="${NGINX_ENVSUBST_STREAM_TEMPLATE_SUFFIX:-.stream-template}" local stream_output_dir="${NGINX_ENVSUBST_STREAM_OUTPUT_DIR:-/etc/nginx/stream-conf.d}" local filter="${NGINX_ENVSUBST_FILTER:-}" - local template defined_envs relative_path output_path subdir - defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null )) + local defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null )) [ -d "$template_dir" ] || return 0 - if [ ! -w "$output_dir" ]; then - entrypoint_log "$ME: ERROR: $template_dir exists, but $output_dir is not writable" - return 0 - fi - find "$template_dir" -follow -type f -name "*$suffix" -print | while read -r template; do - relative_path="${template#"$template_dir/"}" - output_path="$output_dir/${relative_path%"$suffix"}" - subdir=$(dirname "$relative_path") - # create a subdirectory where the template file exists - mkdir -p "$output_dir/$subdir" - entrypoint_log "$ME: Running envsubst on $template to $output_path" - envsubst "$defined_envs" < "$template" > "$output_path" - done + ensure_output_writable "$output_dir" + write_template_conf "$suffix" "$output_dir" # Print the first file with the stream suffix, this will be false if there are none if test -n "$(find "$template_dir" -name "*$stream_suffix" -print -quit)"; then mkdir -p "$stream_output_dir" - if [ ! -w "$stream_output_dir" ]; then - entrypoint_log "$ME: ERROR: $template_dir exists, but $stream_output_dir is not writable" - return 0 - fi - add_stream_block - find "$template_dir" -follow -type f -name "*$stream_suffix" -print | while read -r template; do - relative_path="${template#"$template_dir/"}" - output_path="$stream_output_dir/${relative_path%"$stream_suffix"}" - subdir=$(dirname "$relative_path") - # create a subdirectory where the template file exists - mkdir -p "$stream_output_dir/$subdir" - entrypoint_log "$ME: Running envsubst on $template to $output_path" - envsubst "$defined_envs" < "$template" > "$output_path" - done + ensure_output_writable "$stream_output_dir" + add_extra_block "stream" "$stream_output_dir" + write_template_conf "$stream_suffix" "$stream_output_dir" + fi + if test -n "$(find "$template_dir" -name "*$mail_suffix" -print -quit)"; then + mkdir -p "$mail_output_dir" + ensure_output_writable "$mail_output_dir" + add_extra_block "mail" "$mail_output_dir" + write_template_conf "$mail_suffix" "$mail_output_dir" fi } diff --git a/mainline/alpine-slim/20-envsubst-on-templates.sh b/mainline/alpine-slim/20-envsubst-on-templates.sh index 3804165c9..48c6ca82b 100755 --- a/mainline/alpine-slim/20-envsubst-on-templates.sh +++ b/mainline/alpine-slim/20-envsubst-on-templates.sh @@ -10,66 +10,76 @@ entrypoint_log() { fi } -add_stream_block() { +ensure_output_writable() { + local test_dir=$1 + if [ ! -w "$test_dir" ]; then + entrypoint_log "$ME: ERROR: $template_dir exists, but $test_dir is not writable" + exit 0 + fi +} + +add_extra_block() { + local extra=$1 + local extra_output_dir=$2 local conffile="/etc/nginx/nginx.conf" - if grep -q -E "\s*stream\s*\{" "$conffile"; then - entrypoint_log "$ME: $conffile contains a stream block; include $stream_output_dir/*.conf to enable stream templates" + if grep -q -E "\s*$extra\s*\{" "$conffile"; then + entrypoint_log "$ME: $conffile contains a $extra block; include $extra_output_dir/*.conf to enable $extra templates" else # check if the file can be modified, e.g. not on a r/o filesystem touch "$conffile" 2>/dev/null || { entrypoint_log "$ME: info: can not modify $conffile (read-only file system?)"; exit 0; } - entrypoint_log "$ME: Appending stream block to $conffile to include $stream_output_dir/*.conf" + entrypoint_log "$ME: Appending $extra block to $conffile to include $extra_output_dir/*.conf" cat << END >> "$conffile" # added by "$ME" on "$(date)" -stream { - include $stream_output_dir/*.conf; +$extra { + include $extra_output_dir/*.conf; } END fi } +write_template_conf() { + local select_suffix=$1 + local conf_output_dir=$2 + local template relative_path output_path subdir + find "$template_dir" -follow -type f -name "*$select_suffix" -print | while read -r template; do + relative_path="${template#"$template_dir/"}" + output_path="$conf_output_dir/${relative_path%"$select_suffix"}" + subdir=$(dirname "$relative_path") + # create a subdirectory where the template file exists + mkdir -p "$conf_output_dir/$subdir" + entrypoint_log "$ME: Running envsubst on $template to $output_path" + envsubst "$defined_envs" < "$template" > "$output_path" + done +} + auto_envsubst() { local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}" local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}" local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}" + local mail_suffix="${NGINX_ENVSUBST_MAIL_TEMPLATE_SUFFIX:-.mail-template}" + local mail_output_dir="${NGINX_ENVSUBST_MAIL_OUTPUT_DIR:-/etc/nginx/mail-conf.d}" local stream_suffix="${NGINX_ENVSUBST_STREAM_TEMPLATE_SUFFIX:-.stream-template}" local stream_output_dir="${NGINX_ENVSUBST_STREAM_OUTPUT_DIR:-/etc/nginx/stream-conf.d}" local filter="${NGINX_ENVSUBST_FILTER:-}" - local template defined_envs relative_path output_path subdir - defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null )) + local defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null )) [ -d "$template_dir" ] || return 0 - if [ ! -w "$output_dir" ]; then - entrypoint_log "$ME: ERROR: $template_dir exists, but $output_dir is not writable" - return 0 - fi - find "$template_dir" -follow -type f -name "*$suffix" -print | while read -r template; do - relative_path="${template#"$template_dir/"}" - output_path="$output_dir/${relative_path%"$suffix"}" - subdir=$(dirname "$relative_path") - # create a subdirectory where the template file exists - mkdir -p "$output_dir/$subdir" - entrypoint_log "$ME: Running envsubst on $template to $output_path" - envsubst "$defined_envs" < "$template" > "$output_path" - done + ensure_output_writable "$output_dir" + write_template_conf "$suffix" "$output_dir" # Print the first file with the stream suffix, this will be false if there are none if test -n "$(find "$template_dir" -name "*$stream_suffix" -print -quit)"; then mkdir -p "$stream_output_dir" - if [ ! -w "$stream_output_dir" ]; then - entrypoint_log "$ME: ERROR: $template_dir exists, but $stream_output_dir is not writable" - return 0 - fi - add_stream_block - find "$template_dir" -follow -type f -name "*$stream_suffix" -print | while read -r template; do - relative_path="${template#"$template_dir/"}" - output_path="$stream_output_dir/${relative_path%"$stream_suffix"}" - subdir=$(dirname "$relative_path") - # create a subdirectory where the template file exists - mkdir -p "$stream_output_dir/$subdir" - entrypoint_log "$ME: Running envsubst on $template to $output_path" - envsubst "$defined_envs" < "$template" > "$output_path" - done + ensure_output_writable "$stream_output_dir" + add_extra_block "stream" "$stream_output_dir" + write_template_conf "$stream_suffix" "$stream_output_dir" + fi + if test -n "$(find "$template_dir" -name "*$mail_suffix" -print -quit)"; then + mkdir -p "$mail_output_dir" + ensure_output_writable "$mail_output_dir" + add_extra_block "mail" "$mail_output_dir" + write_template_conf "$mail_suffix" "$mail_output_dir" fi } diff --git a/mainline/debian/20-envsubst-on-templates.sh b/mainline/debian/20-envsubst-on-templates.sh index 3804165c9..48c6ca82b 100755 --- a/mainline/debian/20-envsubst-on-templates.sh +++ b/mainline/debian/20-envsubst-on-templates.sh @@ -10,66 +10,76 @@ entrypoint_log() { fi } -add_stream_block() { +ensure_output_writable() { + local test_dir=$1 + if [ ! -w "$test_dir" ]; then + entrypoint_log "$ME: ERROR: $template_dir exists, but $test_dir is not writable" + exit 0 + fi +} + +add_extra_block() { + local extra=$1 + local extra_output_dir=$2 local conffile="/etc/nginx/nginx.conf" - if grep -q -E "\s*stream\s*\{" "$conffile"; then - entrypoint_log "$ME: $conffile contains a stream block; include $stream_output_dir/*.conf to enable stream templates" + if grep -q -E "\s*$extra\s*\{" "$conffile"; then + entrypoint_log "$ME: $conffile contains a $extra block; include $extra_output_dir/*.conf to enable $extra templates" else # check if the file can be modified, e.g. not on a r/o filesystem touch "$conffile" 2>/dev/null || { entrypoint_log "$ME: info: can not modify $conffile (read-only file system?)"; exit 0; } - entrypoint_log "$ME: Appending stream block to $conffile to include $stream_output_dir/*.conf" + entrypoint_log "$ME: Appending $extra block to $conffile to include $extra_output_dir/*.conf" cat << END >> "$conffile" # added by "$ME" on "$(date)" -stream { - include $stream_output_dir/*.conf; +$extra { + include $extra_output_dir/*.conf; } END fi } +write_template_conf() { + local select_suffix=$1 + local conf_output_dir=$2 + local template relative_path output_path subdir + find "$template_dir" -follow -type f -name "*$select_suffix" -print | while read -r template; do + relative_path="${template#"$template_dir/"}" + output_path="$conf_output_dir/${relative_path%"$select_suffix"}" + subdir=$(dirname "$relative_path") + # create a subdirectory where the template file exists + mkdir -p "$conf_output_dir/$subdir" + entrypoint_log "$ME: Running envsubst on $template to $output_path" + envsubst "$defined_envs" < "$template" > "$output_path" + done +} + auto_envsubst() { local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}" local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}" local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}" + local mail_suffix="${NGINX_ENVSUBST_MAIL_TEMPLATE_SUFFIX:-.mail-template}" + local mail_output_dir="${NGINX_ENVSUBST_MAIL_OUTPUT_DIR:-/etc/nginx/mail-conf.d}" local stream_suffix="${NGINX_ENVSUBST_STREAM_TEMPLATE_SUFFIX:-.stream-template}" local stream_output_dir="${NGINX_ENVSUBST_STREAM_OUTPUT_DIR:-/etc/nginx/stream-conf.d}" local filter="${NGINX_ENVSUBST_FILTER:-}" - local template defined_envs relative_path output_path subdir - defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null )) + local defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null )) [ -d "$template_dir" ] || return 0 - if [ ! -w "$output_dir" ]; then - entrypoint_log "$ME: ERROR: $template_dir exists, but $output_dir is not writable" - return 0 - fi - find "$template_dir" -follow -type f -name "*$suffix" -print | while read -r template; do - relative_path="${template#"$template_dir/"}" - output_path="$output_dir/${relative_path%"$suffix"}" - subdir=$(dirname "$relative_path") - # create a subdirectory where the template file exists - mkdir -p "$output_dir/$subdir" - entrypoint_log "$ME: Running envsubst on $template to $output_path" - envsubst "$defined_envs" < "$template" > "$output_path" - done + ensure_output_writable "$output_dir" + write_template_conf "$suffix" "$output_dir" # Print the first file with the stream suffix, this will be false if there are none if test -n "$(find "$template_dir" -name "*$stream_suffix" -print -quit)"; then mkdir -p "$stream_output_dir" - if [ ! -w "$stream_output_dir" ]; then - entrypoint_log "$ME: ERROR: $template_dir exists, but $stream_output_dir is not writable" - return 0 - fi - add_stream_block - find "$template_dir" -follow -type f -name "*$stream_suffix" -print | while read -r template; do - relative_path="${template#"$template_dir/"}" - output_path="$stream_output_dir/${relative_path%"$stream_suffix"}" - subdir=$(dirname "$relative_path") - # create a subdirectory where the template file exists - mkdir -p "$stream_output_dir/$subdir" - entrypoint_log "$ME: Running envsubst on $template to $output_path" - envsubst "$defined_envs" < "$template" > "$output_path" - done + ensure_output_writable "$stream_output_dir" + add_extra_block "stream" "$stream_output_dir" + write_template_conf "$stream_suffix" "$stream_output_dir" + fi + if test -n "$(find "$template_dir" -name "*$mail_suffix" -print -quit)"; then + mkdir -p "$mail_output_dir" + ensure_output_writable "$mail_output_dir" + add_extra_block "mail" "$mail_output_dir" + write_template_conf "$mail_suffix" "$mail_output_dir" fi } diff --git a/stable/alpine-slim/20-envsubst-on-templates.sh b/stable/alpine-slim/20-envsubst-on-templates.sh index 3804165c9..48c6ca82b 100755 --- a/stable/alpine-slim/20-envsubst-on-templates.sh +++ b/stable/alpine-slim/20-envsubst-on-templates.sh @@ -10,66 +10,76 @@ entrypoint_log() { fi } -add_stream_block() { +ensure_output_writable() { + local test_dir=$1 + if [ ! -w "$test_dir" ]; then + entrypoint_log "$ME: ERROR: $template_dir exists, but $test_dir is not writable" + exit 0 + fi +} + +add_extra_block() { + local extra=$1 + local extra_output_dir=$2 local conffile="/etc/nginx/nginx.conf" - if grep -q -E "\s*stream\s*\{" "$conffile"; then - entrypoint_log "$ME: $conffile contains a stream block; include $stream_output_dir/*.conf to enable stream templates" + if grep -q -E "\s*$extra\s*\{" "$conffile"; then + entrypoint_log "$ME: $conffile contains a $extra block; include $extra_output_dir/*.conf to enable $extra templates" else # check if the file can be modified, e.g. not on a r/o filesystem touch "$conffile" 2>/dev/null || { entrypoint_log "$ME: info: can not modify $conffile (read-only file system?)"; exit 0; } - entrypoint_log "$ME: Appending stream block to $conffile to include $stream_output_dir/*.conf" + entrypoint_log "$ME: Appending $extra block to $conffile to include $extra_output_dir/*.conf" cat << END >> "$conffile" # added by "$ME" on "$(date)" -stream { - include $stream_output_dir/*.conf; +$extra { + include $extra_output_dir/*.conf; } END fi } +write_template_conf() { + local select_suffix=$1 + local conf_output_dir=$2 + local template relative_path output_path subdir + find "$template_dir" -follow -type f -name "*$select_suffix" -print | while read -r template; do + relative_path="${template#"$template_dir/"}" + output_path="$conf_output_dir/${relative_path%"$select_suffix"}" + subdir=$(dirname "$relative_path") + # create a subdirectory where the template file exists + mkdir -p "$conf_output_dir/$subdir" + entrypoint_log "$ME: Running envsubst on $template to $output_path" + envsubst "$defined_envs" < "$template" > "$output_path" + done +} + auto_envsubst() { local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}" local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}" local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}" + local mail_suffix="${NGINX_ENVSUBST_MAIL_TEMPLATE_SUFFIX:-.mail-template}" + local mail_output_dir="${NGINX_ENVSUBST_MAIL_OUTPUT_DIR:-/etc/nginx/mail-conf.d}" local stream_suffix="${NGINX_ENVSUBST_STREAM_TEMPLATE_SUFFIX:-.stream-template}" local stream_output_dir="${NGINX_ENVSUBST_STREAM_OUTPUT_DIR:-/etc/nginx/stream-conf.d}" local filter="${NGINX_ENVSUBST_FILTER:-}" - local template defined_envs relative_path output_path subdir - defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null )) + local defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null )) [ -d "$template_dir" ] || return 0 - if [ ! -w "$output_dir" ]; then - entrypoint_log "$ME: ERROR: $template_dir exists, but $output_dir is not writable" - return 0 - fi - find "$template_dir" -follow -type f -name "*$suffix" -print | while read -r template; do - relative_path="${template#"$template_dir/"}" - output_path="$output_dir/${relative_path%"$suffix"}" - subdir=$(dirname "$relative_path") - # create a subdirectory where the template file exists - mkdir -p "$output_dir/$subdir" - entrypoint_log "$ME: Running envsubst on $template to $output_path" - envsubst "$defined_envs" < "$template" > "$output_path" - done + ensure_output_writable "$output_dir" + write_template_conf "$suffix" "$output_dir" # Print the first file with the stream suffix, this will be false if there are none if test -n "$(find "$template_dir" -name "*$stream_suffix" -print -quit)"; then mkdir -p "$stream_output_dir" - if [ ! -w "$stream_output_dir" ]; then - entrypoint_log "$ME: ERROR: $template_dir exists, but $stream_output_dir is not writable" - return 0 - fi - add_stream_block - find "$template_dir" -follow -type f -name "*$stream_suffix" -print | while read -r template; do - relative_path="${template#"$template_dir/"}" - output_path="$stream_output_dir/${relative_path%"$stream_suffix"}" - subdir=$(dirname "$relative_path") - # create a subdirectory where the template file exists - mkdir -p "$stream_output_dir/$subdir" - entrypoint_log "$ME: Running envsubst on $template to $output_path" - envsubst "$defined_envs" < "$template" > "$output_path" - done + ensure_output_writable "$stream_output_dir" + add_extra_block "stream" "$stream_output_dir" + write_template_conf "$stream_suffix" "$stream_output_dir" + fi + if test -n "$(find "$template_dir" -name "*$mail_suffix" -print -quit)"; then + mkdir -p "$mail_output_dir" + ensure_output_writable "$mail_output_dir" + add_extra_block "mail" "$mail_output_dir" + write_template_conf "$mail_suffix" "$mail_output_dir" fi } diff --git a/stable/debian/20-envsubst-on-templates.sh b/stable/debian/20-envsubst-on-templates.sh index 3804165c9..48c6ca82b 100755 --- a/stable/debian/20-envsubst-on-templates.sh +++ b/stable/debian/20-envsubst-on-templates.sh @@ -10,66 +10,76 @@ entrypoint_log() { fi } -add_stream_block() { +ensure_output_writable() { + local test_dir=$1 + if [ ! -w "$test_dir" ]; then + entrypoint_log "$ME: ERROR: $template_dir exists, but $test_dir is not writable" + exit 0 + fi +} + +add_extra_block() { + local extra=$1 + local extra_output_dir=$2 local conffile="/etc/nginx/nginx.conf" - if grep -q -E "\s*stream\s*\{" "$conffile"; then - entrypoint_log "$ME: $conffile contains a stream block; include $stream_output_dir/*.conf to enable stream templates" + if grep -q -E "\s*$extra\s*\{" "$conffile"; then + entrypoint_log "$ME: $conffile contains a $extra block; include $extra_output_dir/*.conf to enable $extra templates" else # check if the file can be modified, e.g. not on a r/o filesystem touch "$conffile" 2>/dev/null || { entrypoint_log "$ME: info: can not modify $conffile (read-only file system?)"; exit 0; } - entrypoint_log "$ME: Appending stream block to $conffile to include $stream_output_dir/*.conf" + entrypoint_log "$ME: Appending $extra block to $conffile to include $extra_output_dir/*.conf" cat << END >> "$conffile" # added by "$ME" on "$(date)" -stream { - include $stream_output_dir/*.conf; +$extra { + include $extra_output_dir/*.conf; } END fi } +write_template_conf() { + local select_suffix=$1 + local conf_output_dir=$2 + local template relative_path output_path subdir + find "$template_dir" -follow -type f -name "*$select_suffix" -print | while read -r template; do + relative_path="${template#"$template_dir/"}" + output_path="$conf_output_dir/${relative_path%"$select_suffix"}" + subdir=$(dirname "$relative_path") + # create a subdirectory where the template file exists + mkdir -p "$conf_output_dir/$subdir" + entrypoint_log "$ME: Running envsubst on $template to $output_path" + envsubst "$defined_envs" < "$template" > "$output_path" + done +} + auto_envsubst() { local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}" local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}" local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}" + local mail_suffix="${NGINX_ENVSUBST_MAIL_TEMPLATE_SUFFIX:-.mail-template}" + local mail_output_dir="${NGINX_ENVSUBST_MAIL_OUTPUT_DIR:-/etc/nginx/mail-conf.d}" local stream_suffix="${NGINX_ENVSUBST_STREAM_TEMPLATE_SUFFIX:-.stream-template}" local stream_output_dir="${NGINX_ENVSUBST_STREAM_OUTPUT_DIR:-/etc/nginx/stream-conf.d}" local filter="${NGINX_ENVSUBST_FILTER:-}" - local template defined_envs relative_path output_path subdir - defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null )) + local defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null )) [ -d "$template_dir" ] || return 0 - if [ ! -w "$output_dir" ]; then - entrypoint_log "$ME: ERROR: $template_dir exists, but $output_dir is not writable" - return 0 - fi - find "$template_dir" -follow -type f -name "*$suffix" -print | while read -r template; do - relative_path="${template#"$template_dir/"}" - output_path="$output_dir/${relative_path%"$suffix"}" - subdir=$(dirname "$relative_path") - # create a subdirectory where the template file exists - mkdir -p "$output_dir/$subdir" - entrypoint_log "$ME: Running envsubst on $template to $output_path" - envsubst "$defined_envs" < "$template" > "$output_path" - done + ensure_output_writable "$output_dir" + write_template_conf "$suffix" "$output_dir" # Print the first file with the stream suffix, this will be false if there are none if test -n "$(find "$template_dir" -name "*$stream_suffix" -print -quit)"; then mkdir -p "$stream_output_dir" - if [ ! -w "$stream_output_dir" ]; then - entrypoint_log "$ME: ERROR: $template_dir exists, but $stream_output_dir is not writable" - return 0 - fi - add_stream_block - find "$template_dir" -follow -type f -name "*$stream_suffix" -print | while read -r template; do - relative_path="${template#"$template_dir/"}" - output_path="$stream_output_dir/${relative_path%"$stream_suffix"}" - subdir=$(dirname "$relative_path") - # create a subdirectory where the template file exists - mkdir -p "$stream_output_dir/$subdir" - entrypoint_log "$ME: Running envsubst on $template to $output_path" - envsubst "$defined_envs" < "$template" > "$output_path" - done + ensure_output_writable "$stream_output_dir" + add_extra_block "stream" "$stream_output_dir" + write_template_conf "$stream_suffix" "$stream_output_dir" + fi + if test -n "$(find "$template_dir" -name "*$mail_suffix" -print -quit)"; then + mkdir -p "$mail_output_dir" + ensure_output_writable "$mail_output_dir" + add_extra_block "mail" "$mail_output_dir" + write_template_conf "$mail_suffix" "$mail_output_dir" fi }