diff --git a/overlay/etc/nu/scripts/bf/nginx/proxy/auto.nu b/overlay/etc/nu/scripts/bf/nginx/proxy/auto.nu index b71b39a..51c4a4b 100644 --- a/overlay/etc/nu/scripts/bf/nginx/proxy/auto.nu +++ b/overlay/etc/nu/scripts/bf/nginx/proxy/auto.nu @@ -1,4 +1,5 @@ use bf +use bf-s6 # Generate conf.json as part of auto setup export def generate_conf_json []: nothing -> nothing { @@ -36,3 +37,6 @@ export def is_enabled []: nothing -> bool { # all conditions must be true return ($ssl_does_not_exist and $auto_primary_is_set and $auto_upstream_is_set) } + +# Disable the auto request service +export def disable_svc []: nothing -> nothing { bf-s6 svc down ssl-auto-request } diff --git a/overlay/etc/nu/scripts/bf/nginx/proxy/init.nu b/overlay/etc/nu/scripts/bf/nginx/proxy/init.nu index b8c6631..bf269a4 100644 --- a/overlay/etc/nu/scripts/bf/nginx/proxy/init.nu +++ b/overlay/etc/nu/scripts/bf/nginx/proxy/init.nu @@ -44,15 +44,12 @@ export def main [ # initialise domain(s) if $all { get_all | each $init_domain - } - else if $root { + } else if $root { do $init_domain (get_root) - } - else if $domain { + } else if $domain { do $init_domain (get_single $domain) - } - else { - main --help + } else { + bf write error "Incorrect usage - try `init --help`." init } # done diff --git a/overlay/etc/nu/scripts/bf/nginx/proxy/maintenance.nu b/overlay/etc/nu/scripts/bf/nginx/proxy/maintenance.nu index b80ee45..975709a 100644 --- a/overlay/etc/nu/scripts/bf/nginx/proxy/maintenance.nu +++ b/overlay/etc/nu/scripts/bf/nginx/proxy/maintenance.nu @@ -6,7 +6,7 @@ export def generate_helper_conf []: nothing -> string { let e = { PUBLIC: (bf env NGINX_PUBLIC) } - with-env $e { template $"(bf env NGINX_ETC_MODULES)/proxy-maintenance.conf" } + with-env $e { template $"(bf env NGINX_ETC_HELPERS)/proxy-maintenance.conf" } } # Generate maintenance HTML file diff --git a/overlay/etc/nu/scripts/tests/auto.nu b/overlay/etc/nu/scripts/tests/auto.nu index 246eb66..cb48082 100644 --- a/overlay/etc/nu/scripts/tests/auto.nu +++ b/overlay/etc/nu/scripts/tests/auto.nu @@ -130,4 +130,3 @@ export def generate_conf_json__does_not_output_custom [] { assert equal null $result } - diff --git a/overlay/etc/nu/scripts/tests/maintenance.nu b/overlay/etc/nu/scripts/tests/maintenance.nu index d340893..9e58da7 100644 --- a/overlay/etc/nu/scripts/tests/maintenance.nu +++ b/overlay/etc/nu/scripts/tests/maintenance.nu @@ -9,15 +9,15 @@ use vars.nu * #====================================================================================================================== export def generate_helper_conf__outputs_conf [] { - let modules = mktemp -d -t + let helpers = mktemp -d -t let public = random chars let e = { BF_ETC_TEMPLATES: $ETC_TEMPLATES - BF_NGINX_ETC_MODULES: $modules + BF_NGINX_ETC_HELPERS: $helpers BF_NGINX_PUBLIC: $public } - let result = with-env $e { generate_helper_conf } | open --raw $"($modules)/proxy-maintenance.conf" + let result = with-env $e { generate_helper_conf } | open --raw $"($helpers)/proxy-maintenance.conf" assert str contains $result $"root ($public);" } diff --git a/overlay/etc/s6-overlay/s6-rc.d/ssl-auto-request/finish b/overlay/etc/s6-overlay/s6-rc.d/ssl-auto-request/finish index 3187e2b..899239f 100644 --- a/overlay/etc/s6-overlay/s6-rc.d/ssl-auto-request/finish +++ b/overlay/etc/s6-overlay/s6-rc.d/ssl-auto-request/finish @@ -1,11 +1,8 @@ -#!/command/with-contenv bash +#!/usr/bin/nu -set -euo pipefail -export BF_E="${PWD##*/}/$(basename ${0})" +use bf +use bf-s6 +bf env load -x ssl-auto-request - -#====================================================================================================================== -# Show helpful log message. -#====================================================================================================================== - -bf-svc-finish +# Bring service down without terminating container +def main [...args] { bf-s6 svc finish } diff --git a/overlay/etc/s6-overlay/s6-rc.d/ssl-auto-request/run b/overlay/etc/s6-overlay/s6-rc.d/ssl-auto-request/run index 2b03e5f..4cf91eb 100644 --- a/overlay/etc/s6-overlay/s6-rc.d/ssl-auto-request/run +++ b/overlay/etc/s6-overlay/s6-rc.d/ssl-auto-request/run @@ -1,38 +1,28 @@ -#!/command/with-contenv bash - -set -euo pipefail -export BF_E="${PWD##*/}/$(basename ${0})" - - -#====================================================================================================================== -# Run request executable and then disable the service. -# First, wait until the Nginx service is running. -#====================================================================================================================== - -if [ "${PROXY_AUTO-}" = "1" ] ; then - - if [ -n "$(pidof nginx)" ]; then - - # run upgrade executable - bf-echo "Requesting SSL certificates using auto-generated conf.json." - ssl-request -a - +#!/usr/bin/nu + +use bf +use bf/nginx/proxy auto +bf env load -x ssl-auto-request + +# Request SSL certificates and then disable the service +def main [...args] { + if (auto is_enabled) { + # get the pid of the nginx process - if the pid is empty, nginx is not running + let pid = { ^pidof nginx } | bf handle + if $pid == "" { + # wait 2s before exiting the service - S6 will keep restarting it until Nginx comes online + # on first run, it will disable this upgrade service itself + let sleep_for = 2sec + bf write debug $"Waiting ($sleep_for) for Nginx to come online." + sleep $sleep_for + } else { + # request SSL certificates for all configured domains + + # disable the auto request service + auto disable_svc + } + } else { # disable the auto request service - ssl-auto-request-disable - - else - - # wait 2s before exiting the service - S6 will keep restarting it until Nginx comes online - # on first run, it will disable this upgrade service itself - SLEEP=2 - bf-debug "Waiting ${SLEEP}s for Nginx to come online..." - sleep ${SLEEP} - - fi - -else - - # disable the auto request service - ssl-auto-request-disable - -fi + auto disable_svc + } +} diff --git a/overlay/tmp/install b/overlay/tmp/install index 7421965..b545b02 100644 --- a/overlay/tmp/install +++ b/overlay/tmp/install @@ -27,5 +27,5 @@ def main [] { ^mkdir -p /ssl/certs # add bf-nginx-proxy module to config - bf config use bf-nginx-proxy + bf config use bf/nginx/proxy } diff --git a/overlay/usr/bin/bf/ssl-auto-request-disable b/overlay/usr/bin/bf/ssl-auto-request-disable deleted file mode 100644 index 4c8a3e2..0000000 --- a/overlay/usr/bin/bf/ssl-auto-request-disable +++ /dev/null @@ -1,11 +0,0 @@ -#!/command/with-contenv bash - -set -euo pipefail -export BF_E=`basename ${0}` - - -#====================================================================================================================== -# Use base executable to disable auto request service. -#====================================================================================================================== - -bf-svc-down ssl-auto-request