1#!/bin/sh
2set -eu
3
4instance=${1:?usage: tools/import-jackett.sh jackett|jackett-preview-XXXXXXXX}
5case $instance in
6 jackett)
7 test -n "${STUDIO_DEPLOY_HOST:-}" || { echo 'Set STUDIO_DEPLOY_HOST to the production target' >&2; exit 1; }
8 production=true
9 ;;
10 jackett-preview-*)
11 printf '%s\n' "$instance" | grep -Eq '^jackett-preview-[0-9a-f]{8}$' || {
12 echo 'Expected a Jackett preview ID' >&2; exit 1;
13 }
14 production=false
15 ;;
16 *) echo 'Expected jackett or its preview ID' >&2; exit 1 ;;
17esac
18
19source_host=${STUDIO_MIGRATION_SOURCE:-zenith}
20target_host=${STUDIO_DEPLOY_HOST:-root@127.0.0.1}
21target_port=${STUDIO_DEPLOY_PORT:-2222}
22offline=false
23copy_host=$source_host
24copy_rsh=ssh
25if [ -n "${STUDIO_LEGACY_HANDOFF:-}" ]; then
26 test "$production" = true || { echo 'Offline handoff requires production Jackett' >&2; exit 1; }
27 sh "$(dirname "$0")/check-legacy-handoff.sh" "$STUDIO_LEGACY_HANDOFF" "$target_host" "$target_port"
28 offline=true
29 copy_host=$target_host
30 copy_rsh="ssh -p $target_port"
31fi
32if [ "$production" = true ]; then
33 root=/srv/prod/jackett/config/Jackett
34 mount=/srv/prod/jackett
35 source_root=/mnt/storage1/apps/jackett
36 if [ "$offline" = false ]; then
37 test "$(ssh "$source_host" "sudo -n docker inspect -f '{{.State.Running}}' jackett")" = false || {
38 echo 'Stop Zenith jackett before importing production data' >&2; exit 1;
39 }
40 fi
41 response=$(ssh -p "$target_port" "$target_host" 'curl -s -w "\n%{http_code}" -H "X-Nomad-Token: $(cat /var/lib/studio/nomad.token)" http://127.0.0.1:4646/v1/job/jackett')
42 status=$(printf '%s\n' "$response" | tail -n 1)
43 test "$status" = 200 || { echo "Could not verify jackett on the home server: $status" >&2; exit 1; }
44 stopped=$(printf '%s\n' "$response" | sed '$d' | python3 -c 'import json,sys; print(str(json.load(sys.stdin)["Stop"]).lower())')
45 test "$stopped" = true || { echo 'Stop jackett on the home server before importing production data' >&2; exit 1; }
46else
47 root="/srv/staging/$instance/config/Jackett"
48 mount="/srv/staging/$instance"
49 source_id=$(ssh -p "$target_port" "$target_host" "python3 -c 'import json; print(json.load(open(\"/var/lib/studio/stages/$instance.json\"))[\"sourceId\"])'")
50 test "$source_id" = jackett || { echo 'Preview belongs to another service' >&2; exit 1; }
51 source_snapshot=$(ssh "$source_host" '/usr/sbin/zfs list -H -t snapshot -o name -s creation -r storage1/apps | tail -n 1')
52 case $source_snapshot in storage1/apps@*) ;; *) echo 'No apps ZFS snapshot is available' >&2; exit 1 ;; esac
53 source_root="/mnt/storage1/apps/.zfs/snapshot/${source_snapshot#*@}/jackett"
54fi
55
56dataset=$(ssh -p "$target_port" "$target_host" "findmnt -n -o SOURCE --mountpoint $mount")
57if [ "$production" = true ]; then
58 case $dataset in */prod/jackett) ;; *) echo 'Production dataset is not mounted' >&2; exit 1 ;; esac
59else
60 case $dataset in */staging/"$instance") ;; *) echo 'Preview dataset is not mounted' >&2; exit 1 ;; esac
61fi
62
63scratch=$(mktemp -d)
64trap 'rm -rf "$scratch"' EXIT
65rsync -a --exclude='/log.txt*' -e "$copy_rsh" "$copy_host:$source_root/" "$scratch/"
66python3 - "$scratch" <<'PY'
67import json
68from pathlib import Path
69import sys
70
71root = Path(sys.argv[1])
72config = json.loads((root / "ServerConfig.json").read_text())
73if not config.get("APIKey") or not list((root / "Indexers").glob("*.json")):
74 raise ValueError("Jackett configuration is incomplete")
75PY
76
77if [ "$production" = false ]; then
78 ssh -p "$target_port" "$target_host" "NOMAD_TOKEN=\$(cat /var/lib/studio/nomad.token) nomad job stop -yes $instance"
79fi
80snapshot="$dataset@before-jackett-import-$(date +%s)-$$"
81ssh -p "$target_port" "$target_host" "set -eu; for i in \$(seq 1 30); do running=\$(curl -fsS -H \"X-Nomad-Token: \$(cat /var/lib/studio/nomad.token)\" http://127.0.0.1:4646/v1/job/$instance/allocations | python3 -c 'import json,sys; print(sum(a[\"ClientStatus\"] == \"running\" for a in json.load(sys.stdin)))'); test \"\$running\" = 0 && break; sleep 2; done; test \"\$running\" = 0; zfs snapshot $snapshot"
82
83rsync -a --delete -e "ssh -p $target_port" "$scratch/" "$target_host:$root/"
84owner=$(ssh -p "$target_port" "$target_host" "python3 -c 'import json; print(json.load(open(\"/var/lib/studio/identities.json\"))[\"jackett\"])'")
85ssh -p "$target_port" "$target_host" "chown -R $owner:$owner $root"
86drift=$(rsync -rlnc --delete --out-format='%n' -e "ssh -p $target_port" "$scratch/" "$target_host:$root/")
87test -z "$drift" || { echo "Jackett copy differs from source; restore $snapshot" >&2; exit 1; }
88
89if [ "$production" = true ]; then
90 if [ "$offline" = false ]; then
91 test "$(ssh "$source_host" "sudo -n docker inspect -f '{{.State.Running}}' jackett")" = false || {
92 echo 'Zenith jackett restarted during import; leave the home server stopped' >&2; exit 1;
93 }
94 fi
95 echo "Imported production Jackett. Previous dataset state: $snapshot"
96else
97 python3 "$(dirname "$0")/deploy.py" stage jackett
98 ssh -p "$target_port" "$target_host" "zfs destroy $snapshot"
99 echo "Imported and tested $instance from $source_snapshot"
100fi