2023-08-19 02:43:59 +02:00
|
|
|
#!/usr/bin/env bash
|
2021-02-05 15:13:20 +01:00
|
|
|
|
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() {
|
2023-08-19 02:43:59 +02:00
|
|
|
while :; do
|
|
|
|
wait_files="$(find "$dstdir" -type f -exec \
|
2024-01-26 00:27:04 +01:00
|
|
|
sed -En '/ssl_certificate/ s/^\s*ssl_certificate(_key)? (.*);.*$/\2/p' {} \; | sort -u)"
|
|
|
|
wait_directories="$(echo "$wait_files" | xargs -n 1 dirname | sort -u)"
|
2023-08-19 02:43:59 +02:00
|
|
|
echo "monitoring the following files:"
|
|
|
|
echo "$wait_files"
|
|
|
|
matched_files="$(echo "$wait_directories" | inotifywait --fromfile=- -e close_write -e modify -e create)"
|
2024-01-26 00:27:04 +01:00
|
|
|
if [[ -n "$(comm -12 <(sort -u <<< "$matched_files") <(echo "$wait_files"))" ]]; then
|
2022-03-07 17:58:18 +01:00
|
|
|
nginx -s reload
|
2023-08-19 02:43:59 +02:00
|
|
|
fi
|
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
|