Skip to content

Commit

Permalink
Switching ssl-auto-request service to Nu
Browse files Browse the repository at this point in the history
  • Loading branch information
bfren committed Dec 13, 2024
1 parent 06cb2d0 commit 51078ee
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 70 deletions.
4 changes: 4 additions & 0 deletions overlay/etc/nu/scripts/bf/nginx/proxy/auto.nu
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bf
use bf-s6

# Generate conf.json as part of auto setup
export def generate_conf_json []: nothing -> nothing {
Expand Down Expand Up @@ -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 }
11 changes: 4 additions & 7 deletions overlay/etc/nu/scripts/bf/nginx/proxy/init.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion overlay/etc/nu/scripts/bf/nginx/proxy/maintenance.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion overlay/etc/nu/scripts/tests/auto.nu
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,3 @@ export def generate_conf_json__does_not_output_custom [] {

assert equal null $result
}

6 changes: 3 additions & 3 deletions overlay/etc/nu/scripts/tests/maintenance.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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);"
}
Expand Down
15 changes: 6 additions & 9 deletions overlay/etc/s6-overlay/s6-rc.d/ssl-auto-request/finish
Original file line number Diff line number Diff line change
@@ -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 }
64 changes: 27 additions & 37 deletions overlay/etc/s6-overlay/s6-rc.d/ssl-auto-request/run
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 1 addition & 1 deletion overlay/tmp/install
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 0 additions & 11 deletions overlay/usr/bin/bf/ssl-auto-request-disable

This file was deleted.

0 comments on commit 51078ee

Please sign in to comment.