| 1 | #!/bin/sh |
| 2 | set -eu |
| 3 | |
| 4 | instance=${1:?usage: tools/import-jackett.sh jackett|jackett-preview-XXXXXXXX} |
| 5 | case $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 ;; |
| 17 | esac |
| 18 | |
| 19 | source_host=${STUDIO_MIGRATION_SOURCE:-zenith} |
| 20 | target_host=${STUDIO_DEPLOY_HOST:-root@127.0.0.1} |
| 21 | target_port=${STUDIO_DEPLOY_PORT:-2222} |
| 22 | offline=false |
| 23 | copy_host=$source_host |
| 24 | copy_rsh=ssh |
| 25 | if [ -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" |
| 31 | fi |
| 32 | if [ "$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; } |
| 46 | else |
| 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" |
| 54 | fi |
| 55 | |
| 56 | dataset=$(ssh -p "$target_port" "$target_host" "findmnt -n -o SOURCE --mountpoint $mount") |
| 57 | if [ "$production" = true ]; then |
| 58 | case $dataset in */prod/jackett) ;; *) echo 'Production dataset is not mounted' >&2; exit 1 ;; esac |
| 59 | else |
| 60 | case $dataset in */staging/"$instance") ;; *) echo 'Preview dataset is not mounted' >&2; exit 1 ;; esac |
| 61 | fi |
| 62 | |
| 63 | scratch=$(mktemp -d) |
| 64 | trap 'rm -rf "$scratch"' EXIT |
| 65 | rsync -a --exclude='/log.txt*' -e "$copy_rsh" "$copy_host:$source_root/" "$scratch/" |
| 66 | python3 - "$scratch" <<'PY' |
| 67 | import json |
| 68 | from pathlib import Path |
| 69 | import sys |
| 70 | |
| 71 | root = Path(sys.argv[1]) |
| 72 | config = json.loads((root / "ServerConfig.json").read_text()) |
| 73 | if not config.get("APIKey") or not list((root / "Indexers").glob("*.json")): |
| 74 | raise ValueError("Jackett configuration is incomplete") |
| 75 | PY |
| 76 | |
| 77 | if [ "$production" = false ]; then |
| 78 | ssh -p "$target_port" "$target_host" "NOMAD_TOKEN=\$(cat /var/lib/studio/nomad.token) nomad job stop -yes $instance" |
| 79 | fi |
| 80 | snapshot="$dataset@before-jackett-import-$(date +%s)-$$" |
| 81 | ssh -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 | |
| 83 | rsync -a --delete -e "ssh -p $target_port" "$scratch/" "$target_host:$root/" |
| 84 | owner=$(ssh -p "$target_port" "$target_host" "python3 -c 'import json; print(json.load(open(\"/var/lib/studio/identities.json\"))[\"jackett\"])'") |
| 85 | ssh -p "$target_port" "$target_host" "chown -R $owner:$owner $root" |
| 86 | drift=$(rsync -rlnc --delete --out-format='%n' -e "ssh -p $target_port" "$scratch/" "$target_host:$root/") |
| 87 | test -z "$drift" || { echo "Jackett copy differs from source; restore $snapshot" >&2; exit 1; } |
| 88 | |
| 89 | if [ "$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" |
| 96 | else |
| 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" |
| 100 | fi |