2024-11-06 02:32:45 +00:00
|
|
|
#!/bin/env fish
|
|
|
|
begin
|
|
|
|
set -l dir (status dirname)
|
|
|
|
set -l root /usr/local/lib
|
2024-11-27 01:54:44 +00:00
|
|
|
set -l overrides "docker-compose.overrides.yml"
|
2024-11-06 02:32:45 +00:00
|
|
|
set -l nginxRoot "/etc/nginx/conf.d"
|
|
|
|
source "$dir/../../../../lib/software.fish"
|
|
|
|
|
2024-11-07 22:58:06 +00:00
|
|
|
function getRandomPort
|
|
|
|
random 49152 65535
|
|
|
|
end
|
|
|
|
|
|
|
|
function getPortPattern
|
2024-11-13 15:25:39 +00:00
|
|
|
echo "^\([.[:digit:]]\+:\)\?\([[:digit:]]\+\)\(:[[:digit:]]\+\(\/tcp\|udp\)\?\)"
|
2024-11-07 22:58:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function __substitutePort -a substitution
|
|
|
|
sed "s/$(getPortPattern)/$substitution/"
|
|
|
|
end
|
|
|
|
|
|
|
|
function extractPort
|
|
|
|
__substitutePort "\2"
|
|
|
|
end
|
|
|
|
|
|
|
|
function mutatePort -a port
|
|
|
|
__substitutePort "\1$port\3"
|
|
|
|
end
|
|
|
|
|
2024-11-06 02:32:45 +00:00
|
|
|
function getServiceName
|
|
|
|
argparse -i "name=" -- $argv
|
|
|
|
echo "$_flag_name"
|
|
|
|
end
|
|
|
|
|
2024-11-07 02:01:45 +00:00
|
|
|
function getMachineFQDN -V dir
|
|
|
|
source "$dir/../../../../lib/settings.fish"
|
|
|
|
getOSConfig fqdn
|
|
|
|
end
|
|
|
|
|
2024-11-06 02:32:45 +00:00
|
|
|
function getServiceRoot -V root
|
|
|
|
echo "$root/$(getServiceName $argv)"
|
|
|
|
end
|
|
|
|
|
2024-11-07 17:33:16 +00:00
|
|
|
function getServiceKey -a name
|
|
|
|
echo ".services.[$(echo "{}" | NAME="$name" jq "env.NAME")]"
|
|
|
|
end
|
|
|
|
|
2024-11-06 02:32:45 +00:00
|
|
|
function __getServiceNginxConfig -V nginxRoot
|
|
|
|
echo "$nginxRoot/$(getServiceName $argv).conf"
|
|
|
|
end
|
|
|
|
|
2024-11-27 01:54:44 +00:00
|
|
|
function getServiceOverrides -V overrides
|
|
|
|
echo "$(getServiceRoot $argv)/$overrides"
|
2024-11-06 02:32:45 +00:00
|
|
|
end
|
|
|
|
|
2024-11-27 01:54:44 +00:00
|
|
|
function getServicePortKey -V overrides -a name
|
2024-11-07 17:33:16 +00:00
|
|
|
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"
|
2024-11-06 02:32:45 +00:00
|
|
|
end
|
|
|
|
|
2024-12-04 15:48:35 +00:00
|
|
|
function getExtraServerConfig -a domain
|
2024-11-10 11:33:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function getServiceLocationConfig -a domain service location
|
2024-12-04 15:48:51 +00:00
|
|
|
getServiceDefaultProxy $domain $service $location $argv
|
2024-11-10 11:33:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function getServiceDefaultProxy -a domain service location
|
2024-12-04 15:55:56 +00:00
|
|
|
argparse -i "comment=" "path=" "scheme=" "url=" -- $argv
|
|
|
|
set -l scheme
|
2024-11-10 11:33:36 +00:00
|
|
|
set -l url
|
2024-11-27 01:54:44 +00:00
|
|
|
set -l config (getServiceOverrides $argv)
|
2024-11-13 15:25:39 +00:00
|
|
|
set -l portKey (getServicePortKey "$service")
|
2024-11-19 23:30:08 +00:00
|
|
|
set -l port (yq "$portKey" "$config" | extractPort)
|
2024-11-10 11:33:36 +00:00
|
|
|
|
2024-12-04 15:55:56 +00:00
|
|
|
if [ -n "$_flag_scheme" ]
|
|
|
|
set scheme "$_flag_scheme"
|
|
|
|
else
|
|
|
|
set scheme http
|
|
|
|
end
|
|
|
|
|
2024-11-10 11:33:36 +00:00
|
|
|
if [ -n "$_flag_url" ]
|
|
|
|
set url "$_flag_url"
|
|
|
|
else
|
2024-12-04 15:55:56 +00:00
|
|
|
set url "$scheme://127.0.0.1:$port"
|
2024-11-10 11:33:36 +00:00
|
|
|
|
|
|
|
if [ -n "$_flag_path" ]
|
|
|
|
set url "$url$_flag_path"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
printf "%s\n" \
|
2024-12-04 15:54:54 +00:00
|
|
|
(if [ -n "$_flag_comment" ]
|
|
|
|
echo "# $_flag_comment"
|
|
|
|
end) \
|
2024-11-10 11:33:36 +00:00
|
|
|
"location $location {" \
|
|
|
|
"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
|
|
|
|
|
2024-11-13 15:22:57 +00:00
|
|
|
function initializeServiceInstallation -V dir -V nginxRoot
|
2024-11-13 15:25:39 +00:00
|
|
|
set -l root (getServiceRoot $argv)
|
2024-11-10 11:33:36 +00:00
|
|
|
sudo mkdir -p (getServiceRoot $argv)
|
|
|
|
sudo mkdir -p "$nginxRoot"
|
2024-11-27 01:54:44 +00:00
|
|
|
sudo mkdir -p (dirname (getServiceOverrides $argv))
|
2024-11-26 14:47:38 +00:00
|
|
|
sudo touch "$root/docker-compose.overrides.yml"
|
2024-11-13 15:22:57 +00:00
|
|
|
sudo cp "$dir/docker-compose.yml" "$root"
|
2024-11-07 01:39:37 +00:00
|
|
|
end
|
|
|
|
|
2024-11-07 22:58:06 +00:00
|
|
|
function installDockerService -V dir -V nginxRoot
|
2024-11-27 01:54:44 +00:00
|
|
|
set -l config (getServiceOverrides $argv)
|
2024-11-07 10:54:50 +00:00
|
|
|
set -l servers (getServiceServers $argv | string split0)
|
|
|
|
|
|
|
|
for i in (seq 1 2 (count $servers))
|
2024-11-10 11:33:36 +00:00
|
|
|
set -l locations (getServiceLocations $i $argv | string split0)
|
2024-11-07 10:54:50 +00:00
|
|
|
|
2024-12-04 15:57:25 +00:00
|
|
|
for j in (seq 1 3 (count $locations))
|
2024-11-07 10:54:50 +00:00
|
|
|
set -l file (mktemp)
|
2024-11-07 22:58:06 +00:00
|
|
|
set -l port (getRandomPort)
|
2024-11-07 10:54:50 +00:00
|
|
|
set -l service $locations[$j]
|
2024-11-13 15:25:39 +00:00
|
|
|
set -l portKey (getServicePortKey "$service")
|
2024-11-07 10:54:50 +00:00
|
|
|
set -l exposedPort
|
|
|
|
sudo mkdir -p (getServiceRoot $argv)
|
|
|
|
sudo mkdir -p "$nginxRoot"
|
|
|
|
cp "$config" "$file"
|
|
|
|
|
2024-11-19 23:30:08 +00:00
|
|
|
set exposedPort (yq "$portKey" "$file" | mutatePort $port)
|
|
|
|
PORT=$exposedPort yq "$portKey = env(PORT)" "$file" | sudo tee "$config" >/dev/null
|
2024-11-07 10:54:50 +00:00
|
|
|
end
|
2024-11-06 02:32:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-07 22:58:06 +00:00
|
|
|
function configureDockerService
|
2024-11-07 10:54:50 +00:00
|
|
|
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)
|
2024-11-07 17:33:16 +00:00
|
|
|
set domain (getServiceDomain "$subdomain" "$domain")
|
2024-11-07 02:31:48 +00:00
|
|
|
|
2024-11-07 10:54:50 +00:00
|
|
|
begin
|
|
|
|
printf "%s\n" \
|
|
|
|
"server {" \
|
|
|
|
"listen 80;" \
|
2024-11-10 11:33:36 +00:00
|
|
|
"server_name $domain;" \
|
2024-12-04 15:48:35 +00:00
|
|
|
(getExtraServerConfig $domain $argv)
|
2024-11-07 10:54:50 +00:00
|
|
|
|
2024-12-04 15:57:25 +00:00
|
|
|
for j in (seq 1 3 (count $locations))
|
2024-11-07 10:54:50 +00:00
|
|
|
set -l service $locations[$j]
|
|
|
|
set -l location $locations[(math $j + 1)]
|
2024-12-04 15:57:25 +00:00
|
|
|
set -l comment $locations[(math $j + 2)]
|
|
|
|
|
|
|
|
if [ -n "$comment" ]
|
|
|
|
set -a argv --comment "$comment"
|
|
|
|
end
|
|
|
|
|
2024-11-10 11:33:36 +00:00
|
|
|
getServiceLocationConfig $domain $service $location $argv
|
2024-11-07 10:54:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
echo "}"
|
|
|
|
end
|
|
|
|
end | nix-shell -p crossplane --run "crossplane format /dev/stdin" | sudo tee "$nginxConfig" >/dev/null
|
2024-11-06 02:32:45 +00:00
|
|
|
|
|
|
|
sudo systemctl restart nginx
|
|
|
|
end
|
|
|
|
|
2024-11-26 14:44:49 +00:00
|
|
|
function getExtraBackupPatterns
|
|
|
|
echo ""
|
|
|
|
end
|
|
|
|
|
2024-11-07 17:33:16 +00:00
|
|
|
function getBackupArgs
|
2024-11-26 14:44:49 +00:00
|
|
|
set -l extraPatterns (getExtraBackupPatterns)
|
|
|
|
|
|
|
|
if [ -n "$extraPatterns" ]
|
|
|
|
set extraPatterns "|$extraPatterns"
|
|
|
|
end
|
|
|
|
|
2024-11-27 01:54:44 +00:00
|
|
|
printf "%s\n" --base-directory (getServiceRoot $argv) --hidden --no-ignore "^(docker-compose\.overrides\.yml|data)\$$extraPatterns"
|
2024-11-06 02:32:45 +00:00
|
|
|
end
|
|
|
|
end
|