nginx-envsubst/start-nginx.sh

54 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env bash
pids=""
run_prog() {
"$@" &
pids="$! $pids"
}
trap_sig() {
printf '%s' "$pids" | while IFS= read -r pid; do
echo "pid=$pid"
kill -s $1 $pid
done
}
trap 'trap_sig TERM' SIGTERM
srcdir=/etc/nginx.tmpl/
dstdir=/etc/nginx/
find "$srcdir" -type d | while read -r src_directory; do
dst_directory="$(echo "$src_directory" | sed "s|^$srcdir|$dstdir|")"
mkdir -p "$dst_directory"
done
find "$srcdir" -type f | while read -r src_file; do
dst_file="$(echo "$src_file" | sed "s|^$srcdir|$dstdir|")"
envsubst "$(cat /etc/envsubst.conf)" < "$src_file" > "$dst_file"
done
run_nginx() {
find "$dstdir"
nginx -g 'daemon off;'
}
run_inotifywait() {
while :; do
wait_files="$(find "$dstdir" -type f -exec \
sed -En '/ssl_certificate/ s/^\s*ssl_certificate(_key)? (.*);.*$/\2/p' {} \; | sort | uniq)"
wait_directories="$(echo "$wait_files" | xargs -0 dirname | sort | uniq)"
echo "monitoring the following files:"
echo "$wait_files"
matched_files="$(echo "$wait_directories" | inotifywait --fromfile=- -e close_write -e modify -e create)"
if [[ -n "$(comm -12 <(sort <<< "$matched_files") <(echo "$wait_files"))" ]]; then
nginx -s reload
fi
done
}
run_prog run_nginx
run_prog run_inotifywait
wait $pids