From cd98e75ce640e77633cd79035e41c9a122897fdf Mon Sep 17 00:00:00 2001
From: Konstantin Pavlov <thresh@videolan.org>
Date: Tue, 26 May 2020 16:38:20 +0300
Subject: [PATCH] Run envsubst on container start to make templating easier

This entrypoint script will look for files in /etc/nginx/templates/ with
a ".conf.template" suffix, run them through envsubst replacing defined
environment variables, and output to /etc/nginx/conf.d/ preserving file
name sans .template.

Fixes #232.
---
 .../alpine-perl/20-envsubst-on-templates.sh   | 32 +++++++++++++++++++
 mainline/alpine-perl/Dockerfile               |  1 +
 mainline/alpine/20-envsubst-on-templates.sh   | 32 +++++++++++++++++++
 mainline/alpine/Dockerfile                    |  1 +
 .../buster-perl/20-envsubst-on-templates.sh   | 32 +++++++++++++++++++
 mainline/buster-perl/Dockerfile               |  1 +
 mainline/buster/20-envsubst-on-templates.sh   | 32 +++++++++++++++++++
 mainline/buster/Dockerfile                    |  1 +
 .../alpine-perl/20-envsubst-on-templates.sh   | 32 +++++++++++++++++++
 stable/alpine-perl/Dockerfile                 |  1 +
 stable/alpine/20-envsubst-on-templates.sh     | 32 +++++++++++++++++++
 stable/alpine/Dockerfile                      |  1 +
 .../buster-perl/20-envsubst-on-templates.sh   | 32 +++++++++++++++++++
 stable/buster-perl/Dockerfile                 |  1 +
 stable/buster/20-envsubst-on-templates.sh     | 32 +++++++++++++++++++
 stable/buster/Dockerfile                      |  1 +
 16 files changed, 264 insertions(+)
 create mode 100755 mainline/alpine-perl/20-envsubst-on-templates.sh
 create mode 100755 mainline/alpine/20-envsubst-on-templates.sh
 create mode 100755 mainline/buster-perl/20-envsubst-on-templates.sh
 create mode 100755 mainline/buster/20-envsubst-on-templates.sh
 create mode 100755 stable/alpine-perl/20-envsubst-on-templates.sh
 create mode 100755 stable/alpine/20-envsubst-on-templates.sh
 create mode 100755 stable/buster-perl/20-envsubst-on-templates.sh
 create mode 100755 stable/buster/20-envsubst-on-templates.sh

diff --git a/mainline/alpine-perl/20-envsubst-on-templates.sh b/mainline/alpine-perl/20-envsubst-on-templates.sh
new file mode 100755
index 0000000..b2b6e12
--- /dev/null
+++ b/mainline/alpine-perl/20-envsubst-on-templates.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+set -e
+
+ME=$(basename $0)
+
+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 template defined_envs relative_path output_path subdir
+  defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
+  [ -d "$template_dir" ] || return 0
+  if [ ! -w "$output_dir" ]; then
+    echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable, exiting" 1>&2
+    return 0
+  fi
+  for template in $(find "$template_dir" -follow -name "*$suffix"); 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"
+    echo "$ME: Running envsubst on $template to $output_path"
+    envsubst "$defined_envs" < "$template" > "$output_path"
+  done
+}
+
+auto_envsubst
+
+exit 0
diff --git a/mainline/alpine-perl/Dockerfile b/mainline/alpine-perl/Dockerfile
index 3ac6f43..3202a05 100644
--- a/mainline/alpine-perl/Dockerfile
+++ b/mainline/alpine-perl/Dockerfile
@@ -110,6 +110,7 @@ RUN set -x \
 
 COPY docker-entrypoint.sh /
 COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
+COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
 ENTRYPOINT ["/docker-entrypoint.sh"]
 
 EXPOSE 80
diff --git a/mainline/alpine/20-envsubst-on-templates.sh b/mainline/alpine/20-envsubst-on-templates.sh
new file mode 100755
index 0000000..b2b6e12
--- /dev/null
+++ b/mainline/alpine/20-envsubst-on-templates.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+set -e
+
+ME=$(basename $0)
+
+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 template defined_envs relative_path output_path subdir
+  defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
+  [ -d "$template_dir" ] || return 0
+  if [ ! -w "$output_dir" ]; then
+    echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable, exiting" 1>&2
+    return 0
+  fi
+  for template in $(find "$template_dir" -follow -name "*$suffix"); 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"
+    echo "$ME: Running envsubst on $template to $output_path"
+    envsubst "$defined_envs" < "$template" > "$output_path"
+  done
+}
+
+auto_envsubst
+
+exit 0
diff --git a/mainline/alpine/Dockerfile b/mainline/alpine/Dockerfile
index afc8a8f..e660c4c 100644
--- a/mainline/alpine/Dockerfile
+++ b/mainline/alpine/Dockerfile
@@ -109,6 +109,7 @@ RUN set -x \
 
 COPY docker-entrypoint.sh /
 COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
+COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
 ENTRYPOINT ["/docker-entrypoint.sh"]
 
 EXPOSE 80
diff --git a/mainline/buster-perl/20-envsubst-on-templates.sh b/mainline/buster-perl/20-envsubst-on-templates.sh
new file mode 100755
index 0000000..b2b6e12
--- /dev/null
+++ b/mainline/buster-perl/20-envsubst-on-templates.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+set -e
+
+ME=$(basename $0)
+
+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 template defined_envs relative_path output_path subdir
+  defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
+  [ -d "$template_dir" ] || return 0
+  if [ ! -w "$output_dir" ]; then
+    echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable, exiting" 1>&2
+    return 0
+  fi
+  for template in $(find "$template_dir" -follow -name "*$suffix"); 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"
+    echo "$ME: Running envsubst on $template to $output_path"
+    envsubst "$defined_envs" < "$template" > "$output_path"
+  done
+}
+
+auto_envsubst
+
+exit 0
diff --git a/mainline/buster-perl/Dockerfile b/mainline/buster-perl/Dockerfile
index 8bf66cf..e7ce873 100644
--- a/mainline/buster-perl/Dockerfile
+++ b/mainline/buster-perl/Dockerfile
@@ -101,6 +101,7 @@ RUN set -x \
 
 COPY docker-entrypoint.sh /
 COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
+COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
 ENTRYPOINT ["/docker-entrypoint.sh"]
 
 EXPOSE 80
diff --git a/mainline/buster/20-envsubst-on-templates.sh b/mainline/buster/20-envsubst-on-templates.sh
new file mode 100755
index 0000000..b2b6e12
--- /dev/null
+++ b/mainline/buster/20-envsubst-on-templates.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+set -e
+
+ME=$(basename $0)
+
+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 template defined_envs relative_path output_path subdir
+  defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
+  [ -d "$template_dir" ] || return 0
+  if [ ! -w "$output_dir" ]; then
+    echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable, exiting" 1>&2
+    return 0
+  fi
+  for template in $(find "$template_dir" -follow -name "*$suffix"); 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"
+    echo "$ME: Running envsubst on $template to $output_path"
+    envsubst "$defined_envs" < "$template" > "$output_path"
+  done
+}
+
+auto_envsubst
+
+exit 0
diff --git a/mainline/buster/Dockerfile b/mainline/buster/Dockerfile
index 4529d06..856f3fd 100644
--- a/mainline/buster/Dockerfile
+++ b/mainline/buster/Dockerfile
@@ -100,6 +100,7 @@ RUN set -x \
 
 COPY docker-entrypoint.sh /
 COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
+COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
 ENTRYPOINT ["/docker-entrypoint.sh"]
 
 EXPOSE 80
diff --git a/stable/alpine-perl/20-envsubst-on-templates.sh b/stable/alpine-perl/20-envsubst-on-templates.sh
new file mode 100755
index 0000000..b2b6e12
--- /dev/null
+++ b/stable/alpine-perl/20-envsubst-on-templates.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+set -e
+
+ME=$(basename $0)
+
+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 template defined_envs relative_path output_path subdir
+  defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
+  [ -d "$template_dir" ] || return 0
+  if [ ! -w "$output_dir" ]; then
+    echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable, exiting" 1>&2
+    return 0
+  fi
+  for template in $(find "$template_dir" -follow -name "*$suffix"); 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"
+    echo "$ME: Running envsubst on $template to $output_path"
+    envsubst "$defined_envs" < "$template" > "$output_path"
+  done
+}
+
+auto_envsubst
+
+exit 0
diff --git a/stable/alpine-perl/Dockerfile b/stable/alpine-perl/Dockerfile
index f2556fb..c4e7d17 100644
--- a/stable/alpine-perl/Dockerfile
+++ b/stable/alpine-perl/Dockerfile
@@ -110,6 +110,7 @@ RUN set -x \
 
 COPY docker-entrypoint.sh /
 COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
+COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
 ENTRYPOINT ["/docker-entrypoint.sh"]
 
 EXPOSE 80
diff --git a/stable/alpine/20-envsubst-on-templates.sh b/stable/alpine/20-envsubst-on-templates.sh
new file mode 100755
index 0000000..b2b6e12
--- /dev/null
+++ b/stable/alpine/20-envsubst-on-templates.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+set -e
+
+ME=$(basename $0)
+
+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 template defined_envs relative_path output_path subdir
+  defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
+  [ -d "$template_dir" ] || return 0
+  if [ ! -w "$output_dir" ]; then
+    echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable, exiting" 1>&2
+    return 0
+  fi
+  for template in $(find "$template_dir" -follow -name "*$suffix"); 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"
+    echo "$ME: Running envsubst on $template to $output_path"
+    envsubst "$defined_envs" < "$template" > "$output_path"
+  done
+}
+
+auto_envsubst
+
+exit 0
diff --git a/stable/alpine/Dockerfile b/stable/alpine/Dockerfile
index 4626704..09e6b61 100644
--- a/stable/alpine/Dockerfile
+++ b/stable/alpine/Dockerfile
@@ -109,6 +109,7 @@ RUN set -x \
 
 COPY docker-entrypoint.sh /
 COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
+COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
 ENTRYPOINT ["/docker-entrypoint.sh"]
 
 EXPOSE 80
diff --git a/stable/buster-perl/20-envsubst-on-templates.sh b/stable/buster-perl/20-envsubst-on-templates.sh
new file mode 100755
index 0000000..b2b6e12
--- /dev/null
+++ b/stable/buster-perl/20-envsubst-on-templates.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+set -e
+
+ME=$(basename $0)
+
+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 template defined_envs relative_path output_path subdir
+  defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
+  [ -d "$template_dir" ] || return 0
+  if [ ! -w "$output_dir" ]; then
+    echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable, exiting" 1>&2
+    return 0
+  fi
+  for template in $(find "$template_dir" -follow -name "*$suffix"); 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"
+    echo "$ME: Running envsubst on $template to $output_path"
+    envsubst "$defined_envs" < "$template" > "$output_path"
+  done
+}
+
+auto_envsubst
+
+exit 0
diff --git a/stable/buster-perl/Dockerfile b/stable/buster-perl/Dockerfile
index 5212e28..bb58247 100644
--- a/stable/buster-perl/Dockerfile
+++ b/stable/buster-perl/Dockerfile
@@ -101,6 +101,7 @@ RUN set -x \
 
 COPY docker-entrypoint.sh /
 COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
+COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
 ENTRYPOINT ["/docker-entrypoint.sh"]
 
 EXPOSE 80
diff --git a/stable/buster/20-envsubst-on-templates.sh b/stable/buster/20-envsubst-on-templates.sh
new file mode 100755
index 0000000..b2b6e12
--- /dev/null
+++ b/stable/buster/20-envsubst-on-templates.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+set -e
+
+ME=$(basename $0)
+
+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 template defined_envs relative_path output_path subdir
+  defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
+  [ -d "$template_dir" ] || return 0
+  if [ ! -w "$output_dir" ]; then
+    echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable, exiting" 1>&2
+    return 0
+  fi
+  for template in $(find "$template_dir" -follow -name "*$suffix"); 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"
+    echo "$ME: Running envsubst on $template to $output_path"
+    envsubst "$defined_envs" < "$template" > "$output_path"
+  done
+}
+
+auto_envsubst
+
+exit 0
diff --git a/stable/buster/Dockerfile b/stable/buster/Dockerfile
index 3050aa8..6c98f2f 100644
--- a/stable/buster/Dockerfile
+++ b/stable/buster/Dockerfile
@@ -100,6 +100,7 @@ RUN set -x \
 
 COPY docker-entrypoint.sh /
 COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
+COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
 ENTRYPOINT ["/docker-entrypoint.sh"]
 
 EXPOSE 80
-- 
GitLab