1+ #! /bin/sh
2+
3+ set -e
4+
5+ ME=$( basename " $0 " )
6+
7+ entrypoint_log () {
8+ if [ -z " ${NGINX_ENTRYPOINT_QUIET_LOGS:- } " ]; then
9+ echo " $@ "
10+ fi
11+ }
12+
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+
31+ auto_envsubst () {
32+ local template_dir=" ${NGINX_ENVSUBST_TEMPLATE_DIR:-/ etc/ nginx/ templates} "
33+ local suffix=" ${NGINX_ENVSUBST_TEMPLATE_SUFFIX:- .template} "
34+ 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} "
37+ local filter=" ${NGINX_ENVSUBST_FILTER:- } "
38+
39+ local template defined_envs relative_path output_path subdir
40+ defined_envs=$( printf ' ${%s} ' $( awk " END { for (name in ENVIRON) { print ( name ~ /${filter} / ) ? name : \"\" } }" < /dev/null ) )
41+ [ -d " $template_dir " ] || return 0
42+ if [ ! -w " $output_dir " ]; then
43+ entrypoint_log " $ME : ERROR: $template_dir exists, but $output_dir is not writable"
44+ return 0
45+ fi
46+ find " $template_dir " -follow -type f -name " *$suffix " -print | while read -r template; do
47+ relative_path=" ${template# " $template_dir /" } "
48+ output_path=" $output_dir /${relative_path% " $suffix " } "
49+ subdir=$( dirname " $relative_path " )
50+ # create a subdirectory where the template file exists
51+ mkdir -p " $output_dir /$subdir "
52+ entrypoint_log " $ME : Running envsubst on $template to $output_path "
53+ envsubst " $defined_envs " < " $template " > " $output_path "
54+ 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
74+ }
75+
76+ auto_envsubst
77+
78+ exit 0
0 commit comments