From 502cb54ce2593aad3deba111639cbaef78a16cc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Wed, 23 Jun 2021 10:24:27 +0200 Subject: [PATCH] __nginx_vhost: make configuration reload more robust --- type/__nginx_vhost/gencode-remote | 51 ++++++++++++++++++------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/type/__nginx_vhost/gencode-remote b/type/__nginx_vhost/gencode-remote index d634d83..dd6539d 100644 --- a/type/__nginx_vhost/gencode-remote +++ b/type/__nginx_vhost/gencode-remote @@ -1,26 +1,35 @@ #!/bin/sh os="$(cat "${__global:?}"/explorer/os)" +init=$(cat "$__global/explorer/init") +nginx_confdir="/etc/nginx" -case "$os" in - alpine) - reload_hook="service nginx --ifstopped start;\ - service nginx --ifstarted reload" - ;; - debian|ubuntu|*) - reload_hook="systemctl reload-or-restart nginx" - ;; -esac - -# Check configuration and reload if valid. -# TODO: only check if configuration was changed (= listen for __file's -# messages). -cat << EOF -if nginx -t; then - $reload_hook -else - echo "NGINX configuration is invalid. Exiting." >2& - nginx -t >2& - exit 1 +# The nginx service is not automatically started on alpine. +if [ "$os" = "alpine" ]; then + echo "service nginx --ifstopped start" +fi + +if grep -qE "^__file$nginx_confdir" "${__messages_in:?}"; then + case "$init" in + systemd) + reload_hook="systemctl reload-or-restart nginx" + ;; + busybox-init+openrc) + reload_hook="service nginx reload" + ;; + *) + echo "Unknown init $init." >&2 + exit 1 + ;; + esac + + cat <<- EOF + if nginx -t; then + $reload_hook + else + echo "NGINX configuration is invalid. Exiting." >2& + nginx -t >2& + exit 1 + fi + EOF fi -EOF