Allow configuring services with multiple domains

This commit is contained in:
Manuel Thalmann 2024-11-07 11:54:50 +01:00
parent 62ecabf471
commit 5b1695c764
3 changed files with 63 additions and 40 deletions

View file

@ -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

View file

@ -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

View file

@ -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