From 357bdd287d7998fda582cb2f024cf1226d36461a Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 6 Nov 2024 03:32:45 +0100 Subject: [PATCH] Add a script for installing `anki-sync` --- .../docker/services/anki-sync/anki.conf | 12 +++ .../anki-sync/docker-compose.secrets.yml | 6 ++ .../services/anki-sync/docker-compose.yml | 9 +++ .../docker/services/anki-sync/main.fish | 32 ++++++++ .../Software/docker/services/service.fish | 73 +++++++++++++++++++ .../docker/services/vaultwarden/main.fish | 42 +++++------ 6 files changed, 152 insertions(+), 22 deletions(-) create mode 100644 scripts/Common/Software/docker/services/anki-sync/anki.conf create mode 100644 scripts/Common/Software/docker/services/anki-sync/docker-compose.secrets.yml create mode 100644 scripts/Common/Software/docker/services/anki-sync/docker-compose.yml create mode 100644 scripts/Common/Software/docker/services/anki-sync/main.fish create mode 100644 scripts/Common/Software/docker/services/service.fish diff --git a/scripts/Common/Software/docker/services/anki-sync/anki.conf b/scripts/Common/Software/docker/services/anki-sync/anki.conf new file mode 100644 index 00000000..798e4939 --- /dev/null +++ b/scripts/Common/Software/docker/services/anki-sync/anki.conf @@ -0,0 +1,12 @@ +server { + listen 80; + server_name anki.nuth.ch; + + location / { + proxy_pass http://127.0.0.1:1337; + 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; + } +} diff --git a/scripts/Common/Software/docker/services/anki-sync/docker-compose.secrets.yml b/scripts/Common/Software/docker/services/anki-sync/docker-compose.secrets.yml new file mode 100644 index 00000000..b4281af9 --- /dev/null +++ b/scripts/Common/Software/docker/services/anki-sync/docker-compose.secrets.yml @@ -0,0 +1,6 @@ +services: + anki: + environment: + SYNC_USER1: scott:tiger + ports: + - 127.0.0.1:1337:8080 diff --git a/scripts/Common/Software/docker/services/anki-sync/docker-compose.yml b/scripts/Common/Software/docker/services/anki-sync/docker-compose.yml new file mode 100644 index 00000000..a5c42254 --- /dev/null +++ b/scripts/Common/Software/docker/services/anki-sync/docker-compose.yml @@ -0,0 +1,9 @@ +services: + anki: + image: yangchuansheng/anki-sync-server + restart: unless-stopped + extends: + file: docker-compose.secrets.yml + service: anki + volumes: + - ./data:/ankisyncdir diff --git a/scripts/Common/Software/docker/services/anki-sync/main.fish b/scripts/Common/Software/docker/services/anki-sync/main.fish new file mode 100644 index 00000000..1d224a26 --- /dev/null +++ b/scripts/Common/Software/docker/services/anki-sync/main.fish @@ -0,0 +1,32 @@ +#!/bin/env fish +begin + set -l dir (status dirname) + set -l source "$dir/docker-compose.secrets.yml" + source "$dir/../service.fish" + + function installSW -V dir -V source + set -l pw (nix-shell -p keepassxc --run "keepassxc-cli generate --length 32") + set -l userKey ".services.anki.environment.SYNC_USER1" + set -l user (yq --raw-output "$userKey" "$source" | sed "s/:.*\$/:$pw/") + sudo cp "$dir/docker-compose.yml" (getServiceRoot $argv) + + USER=$user yq "$userKey = env.USER" "$source" | \ + sudo tee (__getServiceSecretsConfig $argv) >/dev/null + + installDockerService $argv + end + + function configureSW -V dir + configureDockerService $argv + end + + function getServiceNginxConfigSource -V dir + echo "$dir/anki.conf" + end + + function getServiceConfigs + printf "%s\0" "anki" "/" + end + + runInstaller --force $argv +end diff --git a/scripts/Common/Software/docker/services/service.fish b/scripts/Common/Software/docker/services/service.fish new file mode 100644 index 00000000..3dfd4d3f --- /dev/null +++ b/scripts/Common/Software/docker/services/service.fish @@ -0,0 +1,73 @@ +#!/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 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 installDockerService -V dir -V nginxRoot -V portPattern + set -l services (getServiceConfigs $argv | string split0) + sudo cp (getServiceNginxConfigSource $argv) (__getServiceNginxConfig $argv) + + for i in (seq 1 2 (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 2 (count $services)) + set -l config (__getServiceSecretsConfig $argv) + set -l service $services[$i] + set -l location $services[(math $i + 1)] + set -l portKey (__getServicePortKey "$service") + set -l port (yq --raw-output "$portKey" "$config" | sed "s/$portPattern/\2/") + set -l nginxConfig (__getServiceNginxConfig $argv) + + cat "$nginxConfig" | sed \ + -e "\;location $location {;,/}/{" \ + -e "s/\(proxy_pass \)\(.\+:\)\?[[:digit:]]\+\(;\)/\1\2$port\3/;" \ + -e "}" | 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 diff --git a/scripts/Common/Software/docker/services/vaultwarden/main.fish b/scripts/Common/Software/docker/services/vaultwarden/main.fish index c7a0156d..2113eaea 100644 --- a/scripts/Common/Software/docker/services/vaultwarden/main.fish +++ b/scripts/Common/Software/docker/services/vaultwarden/main.fish @@ -1,39 +1,37 @@ #!/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" + source "$dir/../service.fish" - function installSW -V dir -V root -V source -V config -V nginxRoot -V nginxConfig -V portKey -V portPattern + function installSW -V dir -V source 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" + set -l dbUrl (yq --raw-output "$dbKey" "$source" | sed "s/^\(.*:\/\/.*:\).*\(@.*\/.*\)\$/\1$pw\2/") + sudo cp "$dir/docker-compose.yml" (getServiceRoot $argv) 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 + sudo tee (__getServiceSecretsConfig $argv) >/dev/null + + installDockerService $argv 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 + function configureSW -V dir + configureDockerService $argv end - function getBackupArgs -V root - printf "%s\n" --hidden --no-ignore . --exclude "docker-compose.yml" "$root" + function getServiceNginxConfigSource -V dir + echo "$dir/vaultwarden.conf" + end + + function getServiceConfigs + argparse -i "name=" -- $argv + printf "%s\0" "$_flag_name" "/" + end + + function getBackupArgs + printf "%s\n" --hidden --no-ignore . --exclude "docker-compose.yml" (getServiceRoot $argv) end runInstaller --force $argv