@@ -10,10 +10,30 @@ entrypoint_log() {
1010 fi
1111}
1212
13+ add_stream_block () {
14+ local conffile=" /etc/nginx/nginx.conf"
15+
16+ if grep -q -E " \s*stream\s*\{" " $conffile " ; then
17+ entrypoint_log " $ME : $conffile contains a stream block; include $stream_output_dir /*.conf to enable stream templates"
18+ else
19+ # check if the file can be modified, e.g. not on a r/o filesystem
20+ touch " $conffile " 2> /dev/null || { entrypoint_log " $ME : info: can not modify $conffile (read-only file system?)" ; exit 0; }
21+ entrypoint_log " $ME : Appending stream block to $conffile to include $stream_output_dir /*.conf"
22+ cat << END >> "$conffile "
23+ # added by "$ME " on "$( date) "
24+ stream {
25+ include $stream_output_dir /*.conf;
26+ }
27+ END
28+ fi
29+ }
30+
1331auto_envsubst () {
1432 local template_dir=" ${NGINX_ENVSUBST_TEMPLATE_DIR:-/ etc/ nginx/ templates} "
1533 local suffix=" ${NGINX_ENVSUBST_TEMPLATE_SUFFIX:- .template} "
1634 local output_dir=" ${NGINX_ENVSUBST_OUTPUT_DIR:-/ etc/ nginx/ conf.d} "
35+ local stream_suffix=" ${NGINX_ENVSUBST_STREAM_TEMPLATE_SUFFIX:- .stream-template} "
36+ local stream_output_dir=" ${NGINX_ENVSUBST_STREAM_OUTPUT_DIR:-/ etc/ nginx/ stream-conf.d} "
1737 local filter=" ${NGINX_ENVSUBST_FILTER:- } "
1838
1939 local template defined_envs relative_path output_path subdir
@@ -32,6 +52,25 @@ auto_envsubst() {
3252 entrypoint_log " $ME : Running envsubst on $template to $output_path "
3353 envsubst " $defined_envs " < " $template " > " $output_path "
3454 done
55+
56+ # Print the first file with the stream suffix, this will be false if there are none
57+ if test -n " $( find " $template_dir " -name " *$stream_suffix " -print -quit) " ; then
58+ mkdir -p " $stream_output_dir "
59+ if [ ! -w " $stream_output_dir " ]; then
60+ entrypoint_log " $ME : ERROR: $template_dir exists, but $stream_output_dir is not writable"
61+ return 0
62+ fi
63+ add_stream_block
64+ find " $template_dir " -follow -type f -name " *$stream_suffix " -print | while read -r template; do
65+ relative_path=" ${template# $template_dir / } "
66+ output_path=" $stream_output_dir /${relative_path% $stream_suffix } "
67+ subdir=$( dirname " $relative_path " )
68+ # create a subdirectory where the template file exists
69+ mkdir -p " $stream_output_dir /$subdir "
70+ entrypoint_log " $ME : Running envsubst on $template to $output_path "
71+ envsubst " $defined_envs " < " $template " > " $output_path "
72+ done
73+ fi
3574}
3675
3776auto_envsubst
0 commit comments