From 5e3b9d1f08c3bb71e71998a34723bf77c17c370a Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Thu, 7 Nov 2024 11:54:50 +0100 Subject: [PATCH] Allow configuring services with multiple domains --- .../docker/services/anki-sync/main.fish | 9 +- .../Software/docker/services/service.fish | 86 +++++++++++-------- .../docker/services/vaultwarden/main.fish | 8 +- 3 files changed, 63 insertions(+), 40 deletions(-) diff --git a/scripts/Common/Software/docker/services/anki-sync/main.fish b/scripts/Common/Software/docker/services/anki-sync/main.fish index c94174af..b68be125 100644 --- a/scripts/Common/Software/docker/services/anki-sync/main.fish +++ b/scripts/Common/Software/docker/services/anki-sync/main.fish @@ -1,5 +1,6 @@ #!/bin/env fish begin + set -l name anki set -l dir (status dirname) set -l source "$dir/docker-compose.secrets.yml" source "$dir/../service.fish" @@ -21,8 +22,12 @@ begin configureDockerService $argv end - function getServiceConfigs - printf "%s\0" anki anki "" / + function getServiceServers -V name + printf "$name" "" + end + + function getServiceLocations -V name + printf "%s\0" "$name" / end runInstaller --force $argv diff --git a/scripts/Common/Software/docker/services/service.fish b/scripts/Common/Software/docker/services/service.fish index 36ea5c11..a5ca5029 100644 --- a/scripts/Common/Software/docker/services/service.fish +++ b/scripts/Common/Software/docker/services/service.fish @@ -40,35 +40,37 @@ begin end function installDockerService -V dir -V nginxRoot -V portPattern - set -l services (getServiceConfigs $argv | string split0) + set -l config (getServiceSecretsConfig $argv) + set -l servers (getServiceServers $argv | string split0) - for i in (seq 1 4 (count $services)) - set -l file (mktemp) - set -l config (getServiceSecretsConfig $argv) - set -l service $services[$i] - set -l port (random 49152 65535) - set -l portKey (__getServicePortKey "$service") - set -l exposedPort - sudo mkdir -p (getServiceRoot $argv) - sudo mkdir -p "$nginxRoot" - cp "$config" "$file" - set exposedPort (yq --raw-output "$portKey" "$file" | sed "s/$portPattern/\1$port\3/") - PORT=$exposedPort yq -y "$portKey = env.PORT" "$file" | sudo tee "$config" >/dev/null + for i in (seq 1 2 (count $servers)) + set -l locations (getServiceLocations $i $argv) + + for j in (seq 1 4 (count $locations)) + set -l file (mktemp) + set -l port (random 49152 65535) + set -l service $locations[$j] + set -l portKey (__getServicePortKey "$service") + set -l exposedPort + sudo mkdir -p (getServiceRoot $argv) + sudo mkdir -p "$nginxRoot" + cp "$config" "$file" + + set exposedPort (yq --raw-output "$portKey" "$file" | sed "s/$portPattern/\1$port\3/") + PORT=$exposedPort yq -y "$portKey = env.PORT" "$file" | sudo tee "$config" >/dev/null + end end end function configureDockerService -V portPattern - set -l services (getServiceConfigs $argv | string split0) + set -l config (getServiceSecretsConfig $argv) + set -l servers (getServiceServers $argv | string split0) + set -l nginxConfig (__getServiceNginxConfig $argv) - for i in (seq 1 4 (count $services)) - set -l config (getServiceSecretsConfig $argv) - set -l service $services[$i] - set -l subdomain $services[(math $i + 1)] - set -l domain $services[(math $i + 2)] - set -l location $services[(math $i + 3)] - set -l portKey (__getServicePortKey "$service") - set -l port (yq --raw-output "$portKey" "$config" | sed "s/$portPattern/\2/") - set -l nginxConfig (__getServiceNginxConfig $argv) + for i in (seq 1 2 (count $servers)) + set -l domain $servers[(math $i + 1)] + set -l subdomain $servers[(math $i)] + set -l locations (getServiceLocations $i $argv | string split0) if [ -z "$domain" ] set domain (getMachineFQDN) @@ -78,19 +80,31 @@ begin set domain "$subdomain.$domain" end - printf "%s\n" \ - "server {" \ - "listen 80;" \ - "server_name $domain;" \ - "location $location {" \ - "proxy_pass http://127.0.0.1:$port;" \ - 'proxy_set_header Host $host;' \ - 'proxy_set_header X-Real-IP $remote_addr;' \ - 'proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' \ - 'proxy_set_header X-Forwarded-Proto $scheme;' \ - "}" \ - "}" | nix-shell -p crossplane --run "crossplane format /dev/stdin" | sudo tee "$nginxConfig" >/dev/null - end + begin + printf "%s\n" \ + "server {" \ + "listen 80;" \ + "server_name $domain;" + + for j in (seq 1 2 (count $locations)) + set -l service $locations[$j] + set -l location $locations[(math $j + 1)] + set -l portKey (__getServicePortKey "$service") + set -l port (yq --raw-output "$portKey" "$config" | sed "s/$portPattern/\2/") + + printf "%s\n" \ + "location $location {" \ + "proxy_pass http://127.0.0.1:$port;" \ + 'proxy_set_header Host $host;' \ + 'proxy_set_header X-Real-IP $remote_addr;' \ + 'proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' \ + 'proxy_set_header X-Forwarded-Proto $scheme;' \ + "}" + end + + echo "}" + end + end | nix-shell -p crossplane --run "crossplane format /dev/stdin" | sudo tee "$nginxConfig" >/dev/null sudo systemctl restart nginx end diff --git a/scripts/Common/Software/docker/services/vaultwarden/main.fish b/scripts/Common/Software/docker/services/vaultwarden/main.fish index 645384a1..ab7482cc 100644 --- a/scripts/Common/Software/docker/services/vaultwarden/main.fish +++ b/scripts/Common/Software/docker/services/vaultwarden/main.fish @@ -28,9 +28,13 @@ begin configureDockerService $argv end - function getServiceConfigs -V domain + function getServiceServers -V domain + printf "%s\0" "$domain" "" + end + + function getServiceLocations argparse -i "name=" -- $argv - printf "%s\0" "$_flag_name" "$domain" "" / + printf "%s\0" "$_flag_name" / end function getBackupArgs -V root