nginx-envsubst/start-nginx.sh

49 lines
1,016 B
Bash
Raw Normal View History

2021-02-05 15:13:20 +01:00
#!/usr/bin/env sh
2022-03-07 17:58:18 +01:00
pids=""
run_prog() {
"$@" &
pids="$! $pids"
2021-02-05 15:13:20 +01:00
}
2022-03-07 17:58:18 +01:00
trap_sig() {
printf '%s' "$pids" | while IFS= read -r pid; do
echo "pid=$pid"
kill -s $1 $pid
2021-02-05 15:13:20 +01:00
done
}
2022-03-07 17:58:18 +01:00
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
2021-02-05 15:13:20 +01:00
2022-03-07 17:58:18 +01:00
run_nginx() {
find "$dstdir"
nginx -g 'daemon off;'
}
run_inotifywait() {
while find "$dstdir" -type f -exec \
sed -En '/ssl_certificate/ s/^\s*ssl_certificate(_key)? (.*);.*$/\2/p' {} \; | sort | uniq | \
inotifywait --fromfile=- -e close_write -e modify; do
2022-03-07 17:58:18 +01:00
nginx -s reload
2021-02-05 15:13:20 +01:00
done
2022-03-07 17:58:18 +01:00
}
2021-02-05 15:13:20 +01:00
2022-03-07 17:58:18 +01:00
run_prog run_nginx
run_prog run_inotifywait
wait $pids