PortValhalla/scripts/Common/Software/docker/services/vaultwarden/main.fish

40 lines
1.8 KiB
Fish

#!/bin/env fish
begin
set -l dir (status dirname)
set -l root "/usr/local/lib/vaultwarden"
set -l source "$dir/docker-compose.secrets.yml"
set -l config "$root/"(basename "$source")
set -l nginxRoot "/etc/nginx/conf.d"
set -l nginxConfig "$nginxRoot/vaultwarden.conf"
set -l portKey ".services.vaultwarden.ports[0]"
set -l portPattern "^\([.[:digit:]]\+:\)\([[:digit:]]\+\)\(:[[:digit:]]\+\)"
source "$dir/../../../../../lib/software.fish"
function installSW -V dir -V root -V source -V config -V nginxRoot -V nginxConfig -V portKey -V portPattern
set -l pw (nix-shell -p keepassxc --run "keepassxc-cli generate --length 32")
set -l dbKey ".services.vaultwarden.environment.DATABASE_URL"
set -l dbUrl (yq "$dbKey" "$source" --raw-output | sed "s/^\(.*:\/\/.*:\).*\(@.*\/.*\)\$/\1$pw\2/")
set -l port (random 49152 65535)
set -l exposedPort (yq --raw-output "$portKey" "$source" | sed "s/$portPattern/\1$port\3/")
sudo mkdir -p "$root"
sudo mkdir -p "$nginxRoot"
sudo cp "$dir/docker-compose.yml" "$root"
URL=$dbUrl yq "$dbKey = env.URL" "$source" | \
PW=$pw yq ".services.db.environment.MARIADB_PASSWORD = env.PW" | \
PORT=$exposedPort yq -y "$portKey = env.PORT" | \
sudo tee "$config" >/dev/null
end
function configureSW -V dir -V config -V nginxConfig -V portKey -V portPattern
set port (yq --raw-output "$portKey" "$config" | sed "s/$portPattern/\2/")
cat "$dir/$(basename "$nginxConfig")" | sed "s/\(proxy_pass \)\(.\+:\)\?[[:digit:]]\+\(;\)/\1\2$port\3/" | sudo tee "$nginxConfig"
sudo systemctl restart nginx
end
function getBackupArgs -V root
printf "%s\n" --hidden --no-ignore . --exclude "docker-compose.yml" "$root"
end
runInstaller --force $argv
end