Allow configuring services with multiple domains

This commit is contained in:
Manuel Thalmann 2024-11-07 11:54:50 +01:00
parent 83f70f4040
commit b59eec418d
3 changed files with 63 additions and 40 deletions

View file

@ -1,5 +1,6 @@
#!/bin/env fish #!/bin/env fish
begin begin
set -l name anki
set -l dir (status dirname) set -l dir (status dirname)
set -l source "$dir/docker-compose.secrets.yml" set -l source "$dir/docker-compose.secrets.yml"
source "$dir/../service.fish" source "$dir/../service.fish"
@ -21,8 +22,12 @@ begin
configureDockerService $argv configureDockerService $argv
end end
function getServiceConfigs function getServiceServers -V name
printf "%s\0" anki anki "" / printf "$name" ""
end
function getServiceLocations -V name
printf "%s\0" "$name" /
end end
runInstaller --force $argv runInstaller --force $argv

View file

@ -40,36 +40,38 @@ begin
end end
function installDockerService -V dir -V nginxRoot -V portPattern function installDockerService -V dir -V nginxRoot -V portPattern
set -l services (getServiceConfigs $argv | string split0)
for i in (seq 1 4 (count $services))
set -l file (mktemp)
set -l config (getServiceSecretsConfig $argv) set -l config (getServiceSecretsConfig $argv)
set -l service $services[$i] set -l servers (getServiceServers $argv | string split0)
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 port (random 49152 65535)
set -l service $locations[$j]
set -l portKey (__getServicePortKey "$service") set -l portKey (__getServicePortKey "$service")
set -l exposedPort set -l exposedPort
sudo mkdir -p (getServiceRoot $argv) sudo mkdir -p (getServiceRoot $argv)
sudo mkdir -p "$nginxRoot" sudo mkdir -p "$nginxRoot"
cp "$config" "$file" cp "$config" "$file"
set exposedPort (yq --raw-output "$portKey" "$file" | sed "s/$portPattern/\1$port\3/") 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 PORT=$exposedPort yq -y "$portKey = env.PORT" "$file" | sudo tee "$config" >/dev/null
end end
end end
end
function configureDockerService -V portPattern function configureDockerService -V portPattern
set -l services (getServiceConfigs $argv | string split0)
for i in (seq 1 4 (count $services))
set -l config (getServiceSecretsConfig $argv) set -l config (getServiceSecretsConfig $argv)
set -l service $services[$i] set -l servers (getServiceServers $argv | string split0)
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) 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" ] if [ -z "$domain" ]
set domain (getMachineFQDN) set domain (getMachineFQDN)
end end
@ -78,20 +80,32 @@ begin
set domain "$subdomain.$domain" set domain "$subdomain.$domain"
end end
begin
printf "%s\n" \ printf "%s\n" \
"server {" \ "server {" \
"listen 80;" \ "listen 80;" \
"server_name $domain;" \ "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 {" \ "location $location {" \
"proxy_pass http://127.0.0.1:$port;" \ "proxy_pass http://127.0.0.1:$port;" \
'proxy_set_header Host $host;' \ 'proxy_set_header Host $host;' \
'proxy_set_header X-Real-IP $remote_addr;' \ 'proxy_set_header X-Real-IP $remote_addr;' \
'proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' \ 'proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' \
'proxy_set_header X-Forwarded-Proto $scheme;' \ 'proxy_set_header X-Forwarded-Proto $scheme;' \
"}" \ "}"
"}" | nix-shell -p crossplane --run "crossplane format /dev/stdin" | sudo tee "$nginxConfig" >/dev/null
end end
echo "}"
end
end | nix-shell -p crossplane --run "crossplane format /dev/stdin" | sudo tee "$nginxConfig" >/dev/null
sudo systemctl restart nginx sudo systemctl restart nginx
end end

View file

@ -28,9 +28,13 @@ begin
configureDockerService $argv configureDockerService $argv
end end
function getServiceConfigs -V domain function getServiceServers -V domain
printf "%s\0" "$domain" ""
end
function getServiceLocations
argparse -i "name=" -- $argv argparse -i "name=" -- $argv
printf "%s\0" "$_flag_name" "$domain" "" / printf "%s\0" "$_flag_name" /
end end
function getBackupArgs function getBackupArgs