176 lines
5.5 KiB
Fish
176 lines
5.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"
|
|
source "$dir/../../../../lib/software.fish"
|
|
|
|
function getRandomPort
|
|
random 49152 65535
|
|
end
|
|
|
|
function getPortPattern
|
|
echo "^\([.[:digit:]]\+:\)\([[:digit:]]\+\)\(:[[:digit:]]\+\)"
|
|
end
|
|
|
|
function __substitutePort -a substitution
|
|
sed "s/$(getPortPattern)/$substitution/"
|
|
end
|
|
|
|
function extractPort
|
|
__substitutePort "\2"
|
|
end
|
|
|
|
function mutatePort -a port
|
|
__substitutePort "\1$port\3"
|
|
end
|
|
|
|
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 getServiceKey -a name
|
|
echo ".services.[$(echo "{}" | NAME="$name" jq "env.NAME")]"
|
|
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 "$(getServiceKey "$name").ports[0]"
|
|
end
|
|
|
|
function getServiceDomain -a subdomain domain
|
|
if [ -z "$domain" ]
|
|
set domain (getMachineFQDN)
|
|
end
|
|
|
|
if [ -n "$subdomain" ]
|
|
set domain "$subdomain.$domain"
|
|
end
|
|
|
|
echo "$domain"
|
|
end
|
|
|
|
function getExtraServerConfig -a subdomain domain
|
|
end
|
|
|
|
function getServiceLocationConfig -a domain service location
|
|
getServiceDefaultProxy $domain $service $location "" $argv
|
|
end
|
|
|
|
function getServiceDefaultProxy -a domain service location
|
|
argparse -i "comment=" "path=" "url=" -- $argv
|
|
set -l url
|
|
set -l config (getServiceSecretsConfig $argv)
|
|
set -l portKey (__getServicePortKey "$service")
|
|
set -l port (yq --raw-output "$portKey" "$config" | extractPort)
|
|
|
|
if [ -n "$_flag_url" ]
|
|
set url "$_flag_url"
|
|
else
|
|
set url "http://127.0.0.1:$port"
|
|
|
|
if [ -n "$_flag_path" ]
|
|
set url "$url$_flag_path"
|
|
end
|
|
end
|
|
|
|
printf "%s\n" \
|
|
"location $location {" \
|
|
(if [ -n "$_flag_comment" ]
|
|
echo "# $_flag_comment"
|
|
end) \
|
|
"proxy_pass $url;" \
|
|
'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;' \
|
|
'proxy_set_header X-Forwarded-Protocol $scheme;' \
|
|
'proxy_set_header X-Forwarded-Host $http_host;' \
|
|
(getExtraLocationSettings $argv) \
|
|
"}"
|
|
end
|
|
|
|
function getExtraLocationSettings -a domain service location
|
|
end
|
|
|
|
function initializeServiceInstallation -V nginxRoot
|
|
sudo mkdir -p (getServiceRoot $argv)
|
|
sudo mkdir -p "$nginxRoot"
|
|
sudo mkdir -p (dirname (getServiceSecretsConfig $argv))
|
|
end
|
|
|
|
function installDockerService -V dir -V nginxRoot
|
|
set -l config (getServiceSecretsConfig $argv)
|
|
set -l servers (getServiceServers $argv | string split0)
|
|
|
|
for i in (seq 1 2 (count $servers))
|
|
set -l locations (getServiceLocations $i $argv | string split0)
|
|
|
|
for j in (seq 1 2 (count $locations))
|
|
set -l file (mktemp)
|
|
set -l port (getRandomPort)
|
|
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" | mutatePort $port)
|
|
PORT=$exposedPort yq -y "$portKey = env.PORT" "$file" | sudo tee "$config" >/dev/null
|
|
end
|
|
end
|
|
end
|
|
|
|
function configureDockerService
|
|
set -l servers (getServiceServers $argv | string split0)
|
|
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)
|
|
set domain (getServiceDomain "$subdomain" "$domain")
|
|
|
|
begin
|
|
printf "%s\n" \
|
|
"server {" \
|
|
"listen 80;" \
|
|
"server_name $domain;" \
|
|
(getExtraServerConfig $subdomain $domain $argv)
|
|
|
|
for j in (seq 1 2 (count $locations))
|
|
set -l service $locations[$j]
|
|
set -l location $locations[(math $j + 1)]
|
|
getServiceLocationConfig $domain $service $location $argv
|
|
end
|
|
|
|
echo "}"
|
|
end
|
|
end | nix-shell -p crossplane --run "crossplane format /dev/stdin" | sudo tee "$nginxConfig" >/dev/null
|
|
|
|
sudo systemctl restart nginx
|
|
end
|
|
|
|
function getBackupArgs
|
|
printf "%s\n" --hidden --no-ignore . --exclude "docker-compose.yml" (getServiceRoot $argv)
|
|
end
|
|
end
|