Add a script for installing anki-sync

This commit is contained in:
Manuel Thalmann 2024-11-06 03:32:45 +01:00
parent 491c2864d3
commit 45d491bc46
6 changed files with 152 additions and 22 deletions

View file

@ -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;
}
}

View file

@ -0,0 +1,6 @@
services:
anki:
environment:
SYNC_USER1: scott:tiger
ports:
- 127.0.0.1:1337:8080

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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