From 015c5c8b02ebbe559c9fe61d1140ef1af2b10ac4 Mon Sep 17 00:00:00 2001 From: Iain Henderson Date: Thu, 18 May 2023 07:08:35 -0400 Subject: [PATCH 1/2] Add an entrypoint for configuring and templating streams --- entrypoint/25-ensubst-on-stream-templates.sh | 55 ++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 entrypoint/25-ensubst-on-stream-templates.sh diff --git a/entrypoint/25-ensubst-on-stream-templates.sh b/entrypoint/25-ensubst-on-stream-templates.sh new file mode 100644 index 00000000..997789aa --- /dev/null +++ b/entrypoint/25-ensubst-on-stream-templates.sh @@ -0,0 +1,55 @@ +#!/bin/sh + +set -e + +ME=$(basename $0) + +entrypoint_log() { + if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then + echo "$@" + fi +} + +add_stream_block() { + if ! grep -q -E "\s*stream\s*\{" /etc/nginx/nginx.conf; then + cat << END >> /etc/nginx/nginx.conf +stream { + include /etc/nginx/stream_conf.d/*.conf; +} +END + entrypoint_log "Appended stream block to /etc/nginx/nginx.conf to include /etc/nginx/stream_conf.d/*.conf" + fi +} + +auto_envsubst() { + local template_dir="${NGINX_ENVSUBST_STREAM_TEMPLATE_DIR:-/etc/nginx/stream_templates}" + local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}" + local 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 )) + [ -d "$template_dir" ] || return 0 + mkdir -p "$output_dir" + if [ ! -w "$output_dir" ]; then + entrypoint_log "$ME: ERROR: $template_dir exists, but $output_dir is not writable" + return 0 + fi + + # Add a block to include stream configurations in the core config + add_stream_block + + 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 +} + +auto_envsubst + +exit 0 \ No newline at end of file From 5ade6cb3b819549153008578b521c89da8310c25 Mon Sep 17 00:00:00 2001 From: Iain Henderson Date: Tue, 23 May 2023 09:08:42 -0400 Subject: [PATCH 2/2] Consolidate into one templating entrypoint script --- entrypoint/20-envsubst-on-templates.sh | 35 +++++++++++++ entrypoint/25-ensubst-on-stream-templates.sh | 55 -------------------- 2 files changed, 35 insertions(+), 55 deletions(-) delete mode 100644 entrypoint/25-ensubst-on-stream-templates.sh diff --git a/entrypoint/20-envsubst-on-templates.sh b/entrypoint/20-envsubst-on-templates.sh index d0398b1e..b13daf11 100755 --- a/entrypoint/20-envsubst-on-templates.sh +++ b/entrypoint/20-envsubst-on-templates.sh @@ -10,12 +10,28 @@ entrypoint_log() { fi } +add_stream_block() { + if grep -q -E "\s*stream\s*\{" /etc/nginx/nginx.conf; then + entrypoint_log "/etc/nginx/nginx.conf contains a stream block; include /etc/nginx/stream_conf.d/*.conf to enable stream templates" + else + cat << END >> /etc/nginx/nginx.conf +stream { + include /etc/nginx/stream_conf.d/*.conf; +} +END + entrypoint_log "Appended stream block to /etc/nginx/nginx.conf to include /etc/nginx/stream_conf.d/*.conf" + fi +} + 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 filter="${NGINX_ENVSUBST_FILTER:-}" + 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 template defined_envs relative_path output_path subdir defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null )) [ -d "$template_dir" ] || return 0 @@ -32,6 +48,25 @@ auto_envsubst() { entrypoint_log "$ME: Running envsubst on $template to $output_path" envsubst "$defined_envs" < "$template" > "$output_path" done + + # Print the first file with the stream suffix, this will be false if there are none + if test -n "$(find "$template_dir" -maxdepth 1 -name "*$stream_suffix" -print -quit)"; then + # http templates will still have been processed and available + 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 + fi } auto_envsubst diff --git a/entrypoint/25-ensubst-on-stream-templates.sh b/entrypoint/25-ensubst-on-stream-templates.sh deleted file mode 100644 index 997789aa..00000000 --- a/entrypoint/25-ensubst-on-stream-templates.sh +++ /dev/null @@ -1,55 +0,0 @@ -#!/bin/sh - -set -e - -ME=$(basename $0) - -entrypoint_log() { - if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then - echo "$@" - fi -} - -add_stream_block() { - if ! grep -q -E "\s*stream\s*\{" /etc/nginx/nginx.conf; then - cat << END >> /etc/nginx/nginx.conf -stream { - include /etc/nginx/stream_conf.d/*.conf; -} -END - entrypoint_log "Appended stream block to /etc/nginx/nginx.conf to include /etc/nginx/stream_conf.d/*.conf" - fi -} - -auto_envsubst() { - local template_dir="${NGINX_ENVSUBST_STREAM_TEMPLATE_DIR:-/etc/nginx/stream_templates}" - local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}" - local 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 )) - [ -d "$template_dir" ] || return 0 - mkdir -p "$output_dir" - if [ ! -w "$output_dir" ]; then - entrypoint_log "$ME: ERROR: $template_dir exists, but $output_dir is not writable" - return 0 - fi - - # Add a block to include stream configurations in the core config - add_stream_block - - 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 -} - -auto_envsubst - -exit 0 \ No newline at end of file