PortValhalla/scripts/Common/Software/docker/services/service.fish

101 lines
3.5 KiB
Fish

#!/bin/env fish
begin
set -l dir (status dirname)
set -l root /usr/local/lib
set -l secretsFile "docker-compose.secrets.yml"
set -l nginxRoot "/etc/nginx/conf.d"
set -l portPattern "^\([.[:digit:]]\+:\)\([[:digit:]]\+\)\(:[[:digit:]]\+\)"
source "$dir/../../../../lib/software.fish"
function getServiceName
argparse -i "name=" -- $argv
echo "$_flag_name"
end
function getMachineFQDN -V dir
source "$dir/../../../../lib/settings.fish"
getOSConfig fqdn
end
function getServiceRoot -V root
echo "$root/$(getServiceName $argv)"
end
function __getServiceNginxConfig -V nginxRoot
echo "$nginxRoot/$(getServiceName $argv).conf"
end
function getServiceSecretsConfig -V secretsFile
echo "$(getServiceRoot $argv)/$secretsFile"
end
function __getServicePortKey -V secretsFile -a name
echo ".services.$name.ports[0]"
end
function initializeServiceInstallation -V nginxRoot
mkdir -p (getServiceRoot $argv)
mkdir -p "$nginxRoot"
mkdir -p (dirname (getServiceSecretsConfig $argv))
end
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 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
end
end
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 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)
if [ -z "$domain" ]
set domain (getMachineFQDN)
end
if [ -n "$subdomain" ]
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
sudo systemctl restart nginx
end
function getBackupArgs -V root
printf "%s\n" --hidden --no-ignore . --exclude "docker-compose.yml" "$root"
end
end