2023-09-23 12:17:09 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
2023-09-24 13:12:22 +00:00
|
|
|
# shellcheck disable=SC3040
|
|
|
|
set -eo pipefail
|
|
|
|
|
2023-09-23 12:17:09 +00:00
|
|
|
# shellcheck disable=SC1091
|
|
|
|
. /usr/local/lib/log.sh
|
|
|
|
|
|
|
|
start_server() {
|
|
|
|
log_info "Start nginx server"
|
|
|
|
if ! nginx -q -g 'daemon off;' -t; then
|
|
|
|
log_error 'Nginx config validation failed, exit'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
exec nginx -g "daemon off;" &
|
|
|
|
PID=$!
|
|
|
|
|
|
|
|
{
|
|
|
|
while true; do
|
|
|
|
inotifywait -rq --timefmt "%F %T" --format "%T [INFO] [$(basename "$0")] %e %f" -e modify,move,create,delete /etc/nginx/
|
|
|
|
log_info 'Detected nginx config update, run validation'
|
|
|
|
|
|
|
|
if ! nginx -q -g 'daemon off;' -t; then
|
|
|
|
log_warn 'Nginx config validation failed, skip reload'
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
log_info 'Reload nginx to apply config update'
|
|
|
|
kill -HUP $PID
|
|
|
|
done
|
|
|
|
} &
|
|
|
|
|
|
|
|
wait $PID
|
|
|
|
}
|
|
|
|
|
|
|
|
run_config() {
|
|
|
|
log_info "Start nginx config service"
|
2023-09-24 13:12:22 +00:00
|
|
|
/usr/local/bin/gomplate -d config=/etc/nginx-s3/config.yaml -o /etc/nginx/conf.d/vhosts.conf -f /etc/templates/vhosts.conf.tmpl --chmod "0640"
|
2023-09-23 12:17:09 +00:00
|
|
|
|
2023-09-24 13:12:22 +00:00
|
|
|
while inotifywait -q --timefmt "%F %T" --format "%T [INFO] [$(basename "$0")] %e %f" -e modify,move,create,delete /etc/nginx-s3/config.yaml; do
|
2023-09-23 12:17:09 +00:00
|
|
|
log_info "Regenerate nginx config"
|
2023-09-24 13:12:22 +00:00
|
|
|
/usr/local/bin/gomplate -d config=/etc/nginx-s3/config.yaml -o /etc/nginx/conf.d/vhosts.conf -f /etc/templates/vhosts.conf.tmpl --chmod "0640"
|
2023-09-23 12:17:09 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
case "$1" in
|
|
|
|
server)
|
|
|
|
start_server
|
|
|
|
;;
|
|
|
|
config)
|
|
|
|
run_config
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
log_info "Unknown entrypoint option $1" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|