amce: use procd to restart services
Directly calling `/etc/init.d/<service> reload` in a hotplug script can inadvertently start a stopped service. Signed-off-by: Glen Huang <i@glenhuang.com>
This commit is contained in:
parent
035cc09e7f
commit
7f04710579
8 changed files with 48 additions and 27 deletions
|
@ -4,6 +4,7 @@ ACME=/usr/lib/acme/client/acme.sh
|
||||||
LOG_TAG=acme-acmesh
|
LOG_TAG=acme-acmesh
|
||||||
# webroot option deprecated, use the hardcoded value directly in the next major version
|
# webroot option deprecated, use the hardcoded value directly in the next major version
|
||||||
WEBROOT=${webroot:-/var/run/acme/challenge}
|
WEBROOT=${webroot:-/var/run/acme/challenge}
|
||||||
|
NOTIFY=/usr/lib/acme/notify
|
||||||
|
|
||||||
# shellcheck source=net/acme/files/functions.sh
|
# shellcheck source=net/acme/files/functions.sh
|
||||||
. /usr/lib/acme/functions.sh
|
. /usr/lib/acme/functions.sh
|
||||||
|
@ -12,9 +13,7 @@ WEBROOT=${webroot:-/var/run/acme/challenge}
|
||||||
export CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
|
export CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
|
||||||
export NO_TIMESTAMP=1
|
export NO_TIMESTAMP=1
|
||||||
|
|
||||||
cmd="$1"
|
case $1 in
|
||||||
|
|
||||||
case $cmd in
|
|
||||||
get)
|
get)
|
||||||
set --
|
set --
|
||||||
[ "$debug" = 1 ] && set -- "$@" --debug
|
[ "$debug" = 1 ] && set -- "$@" --debug
|
||||||
|
@ -38,20 +37,25 @@ get)
|
||||||
staging_moved=1
|
staging_moved=1
|
||||||
else
|
else
|
||||||
set -- "$@" --renew --home "$state_dir" -d "$main_domain"
|
set -- "$@" --renew --home "$state_dir" -d "$main_domain"
|
||||||
log info "$*"
|
log info "$ACME $*"
|
||||||
trap 'ACTION=renewed-failed hotplug-call acme;exit 1' INT
|
trap '$NOTIFY renew-failed;exit 1' INT
|
||||||
"$ACME" "$@"
|
$ACME "$@"
|
||||||
status=$?
|
status=$?
|
||||||
trap - INT
|
trap - INT
|
||||||
|
|
||||||
case $status in
|
case $status in
|
||||||
0) ;; # renewed ok, handled by acme.sh hook, ignore.
|
0)
|
||||||
2) ;; # renew skipped, ignore.
|
$NOTIFY renewed
|
||||||
|
exit;;
|
||||||
|
2)
|
||||||
|
# renew skipped, ignore.
|
||||||
|
exit
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
ACTION=renew-failed hotplug-call acme
|
$NOTIFY renew-failed
|
||||||
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
return 0
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -92,11 +96,11 @@ get)
|
||||||
|
|
||||||
set -- "$@" --issue --home "$state_dir"
|
set -- "$@" --issue --home "$state_dir"
|
||||||
|
|
||||||
log info "$*"
|
log info "$ACME $*"
|
||||||
trap 'ACTION=issue-failed hotplug-call acme;exit 1' INT
|
trap '$NOTIFY issue-failed;exit 1' INT
|
||||||
"$ACME" "$@" \
|
"$ACME" "$@" \
|
||||||
--pre-hook 'ACTION=prepare hotplug-call acme' \
|
--pre-hook "$NOTIFY prepare" \
|
||||||
--renew-hook 'ACTION=renewed hotplug-call acme'
|
--renew-hook "$NOTIFY renewed"
|
||||||
status=$?
|
status=$?
|
||||||
trap - INT
|
trap - INT
|
||||||
|
|
||||||
|
@ -106,7 +110,7 @@ get)
|
||||||
ln -s "$domain_dir/$main_domain.key" /etc/ssl/acme
|
ln -s "$domain_dir/$main_domain.key" /etc/ssl/acme
|
||||||
ln -s "$domain_dir/fullchain.cer" "/etc/ssl/acme/$main_domain.fullchain.cer"
|
ln -s "$domain_dir/fullchain.cer" "/etc/ssl/acme/$main_domain.fullchain.cer"
|
||||||
ln -s "$domain_dir/ca.cer" "/etc/ssl/acme/$main_domain.chain.cer"
|
ln -s "$domain_dir/ca.cer" "/etc/ssl/acme/$main_domain.chain.cer"
|
||||||
ACTION=issued hotplug-call acme
|
$NOTIFY issued
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
if [ "$staging_moved" = 1 ]; then
|
if [ "$staging_moved" = 1 ]; then
|
||||||
|
@ -117,8 +121,7 @@ get)
|
||||||
mv "$domain_dir" "$failed_dir"
|
mv "$domain_dir" "$failed_dir"
|
||||||
log err "State moved to $failed_dir"
|
log err "State moved to $failed_dir"
|
||||||
fi
|
fi
|
||||||
ACTION=issue-failed hotplug-call acme
|
$NOTIFY issue-failed
|
||||||
return 0
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
;;
|
;;
|
||||||
|
|
|
@ -41,10 +41,12 @@ define Package/acme-common/install
|
||||||
$(INSTALL_BIN) ./files/acme.sh $(1)/usr/bin/acme
|
$(INSTALL_BIN) ./files/acme.sh $(1)/usr/bin/acme
|
||||||
$(INSTALL_DIR) $(1)/usr/lib/acme
|
$(INSTALL_DIR) $(1)/usr/lib/acme
|
||||||
$(INSTALL_DATA) ./files/functions.sh $(1)/usr/lib/acme
|
$(INSTALL_DATA) ./files/functions.sh $(1)/usr/lib/acme
|
||||||
|
$(INSTALL_BIN) ./files/acme-notify.sh $(1)/usr/lib/acme/notify
|
||||||
$(INSTALL_DIR) $(1)/etc/init.d
|
$(INSTALL_DIR) $(1)/etc/init.d
|
||||||
$(INSTALL_BIN) ./files/acme.init $(1)/etc/init.d/acme
|
$(INSTALL_BIN) ./files/acme.init $(1)/etc/init.d/acme
|
||||||
$(INSTALL_DIR) $(1)/etc/uci-defaults
|
$(INSTALL_DIR) $(1)/etc/uci-defaults
|
||||||
$(INSTALL_DATA) ./files/acme.uci-defaults $(1)/etc/uci-defaults/acme
|
$(INSTALL_DATA) ./files/acme.uci-defaults $(1)/etc/uci-defaults/acme
|
||||||
|
$(INSTALL_DIR) $(1)/etc/hotplug.d/acme
|
||||||
endef
|
endef
|
||||||
|
|
||||||
define Package/acme/postinst
|
define Package/acme/postinst
|
||||||
|
|
17
net/acme-common/files/acme-notify.sh
Normal file
17
net/acme-common/files/acme-notify.sh
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#!/bin/sh
|
||||||
|
set -u
|
||||||
|
|
||||||
|
event="$1"
|
||||||
|
|
||||||
|
# Call hotplug first, giving scripts a chance to modify certificates before
|
||||||
|
# reloadaing the services
|
||||||
|
ACTION=$event hotplug-call acme
|
||||||
|
|
||||||
|
case $event in
|
||||||
|
renewed)
|
||||||
|
ubus call service event '{"type":"acme.renew","data":{}}'
|
||||||
|
;;
|
||||||
|
issued)
|
||||||
|
ubus call service event '{"type":"acme.issue","data":{}}'
|
||||||
|
;;
|
||||||
|
esac
|
|
@ -6,7 +6,3 @@ issued|renewed)
|
||||||
>"/etc/ssl/acme/$main_domain.combined.cer"
|
>"/etc/ssl/acme/$main_domain.combined.cer"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
if [ "$ACTION" = renewed ]; then
|
|
||||||
/etc/init.d/haproxy reload
|
|
||||||
fi
|
|
||||||
|
|
|
@ -18,6 +18,10 @@ start_service() {
|
||||||
procd_close_instance
|
procd_close_instance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
service_triggers() {
|
||||||
|
procd_add_raw_trigger acme.renew 5000 /etc/init.d/haproxy reload
|
||||||
|
}
|
||||||
|
|
||||||
extra_command "check" "Check haproxy config"
|
extra_command "check" "Check haproxy config"
|
||||||
check() {
|
check() {
|
||||||
$HAPROXY_BIN -c -q -V -f $HAPROXY_CONFIG
|
$HAPROXY_BIN -c -q -V -f $HAPROXY_CONFIG
|
||||||
|
|
|
@ -376,9 +376,6 @@ ifeq ($(CONFIG_NGINX_NAXSI),y)
|
||||||
endif
|
endif
|
||||||
$(if $(CONFIG_NGINX_NAXSI),$($(INSTALL_BIN) $(PKG_BUILD_DIR)/nginx-naxsi/naxsi_config/naxsi_core.rules $(1)/etc/nginx))
|
$(if $(CONFIG_NGINX_NAXSI),$($(INSTALL_BIN) $(PKG_BUILD_DIR)/nginx-naxsi/naxsi_config/naxsi_core.rules $(1)/etc/nginx))
|
||||||
$(if $(CONFIG_NGINX_NAXSI),$(chmod 0640 $(1)/etc/nginx/naxsi_core.rules))
|
$(if $(CONFIG_NGINX_NAXSI),$(chmod 0640 $(1)/etc/nginx/naxsi_core.rules))
|
||||||
|
|
||||||
$(INSTALL_DIR) $(1)/etc/hotplug.d/acme
|
|
||||||
$(INSTALL_DATA) ./files/acme.hotplug $(1)/etc/hotplug.d/acme/00-nginx
|
|
||||||
endef
|
endef
|
||||||
|
|
||||||
Package/nginx-all-module/install = $(Package/nginx-ssl/install)
|
Package/nginx-all-module/install = $(Package/nginx-ssl/install)
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
if [ "$ACTION" = renewed ]; then
|
|
||||||
/etc/init.d/nginx reload
|
|
||||||
fi
|
|
|
@ -66,6 +66,11 @@ reload_service() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
service_triggers() {
|
||||||
|
procd_add_raw_trigger acme.renew 5000 /etc/init.d/nginx reload
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
extra_command "relog" "Reopen log files (without reloading)"
|
extra_command "relog" "Reopen log files (without reloading)"
|
||||||
relog() {
|
relog() {
|
||||||
[ -d /var/log/nginx ] || mkdir -p /var/log/nginx
|
[ -d /var/log/nginx ] || mkdir -p /var/log/nginx
|
||||||
|
|
Loading…
Reference in a new issue