1#!/bin/sh
2set -eu
3
4instance=${1:?usage: tools/import-navidrome.sh navidrome|navidrome-preview-XXXXXXXX}
5case $instance in
6 navidrome)
7 test -n "${STUDIO_DEPLOY_HOST:-}" || { echo 'Set STUDIO_DEPLOY_HOST to the production target' >&2; exit 1; }
8 production=true
9 ;;
10 navidrome-preview-*)
11 printf '%s\n' "$instance" | grep -Eq '^navidrome-preview-[0-9a-f]{8}$' || {
12 echo 'Expected a Navidrome preview ID' >&2; exit 1;
13 }
14 production=false
15 ;;
16 *) echo 'Expected navidrome 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
23if [ -n "${STUDIO_LEGACY_HANDOFF:-}" ]; then
24 test "$production" = true || { echo 'Offline handoff requires production Navidrome' >&2; exit 1; }
25 sh "$(dirname "$0")/check-legacy-handoff.sh" "$STUDIO_LEGACY_HANDOFF" "$target_host" "$target_port"
26 offline=true
27fi
28if [ "$production" = true ]; then
29 root=/srv/prod/navidrome/data
30 mount=/srv/prod/navidrome
31 source_root=/mnt/storage1/apps/navidrone
32 if [ "$offline" = false ]; then
33 test "$(ssh "$source_host" "sudo -n docker inspect -f '{{.State.Running}}' navidrome")" = false || {
34 echo 'Stop Zenith navidrome before importing production data' >&2; exit 1;
35 }
36 fi
37 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/navidrome')
38 status=$(printf '%s\n' "$response" | tail -n 1)
39 test "$status" = 200 || { echo "Could not verify navidrome on the home server: $status" >&2; exit 1; }
40 stopped=$(printf '%s\n' "$response" | sed '$d' | python3 -c 'import json,sys; print(str(json.load(sys.stdin)["Stop"]).lower())')
41 test "$stopped" = true || { echo 'Stop navidrome on the home server before importing production data' >&2; exit 1; }
42 media_fs=$(ssh -p "$target_port" "$target_host" 'findmnt -n -o FSTYPE --mountpoint /srv/clover/Media')
43 test "$media_fs" = zfs || { echo 'Mount the real Media ZFS dataset before importing production data' >&2; exit 1; }
44else
45 root="/srv/staging/$instance/data"
46 mount="/srv/staging/$instance"
47 source_id=$(ssh -p "$target_port" "$target_host" "python3 -c 'import json; print(json.load(open(\"/var/lib/studio/stages/$instance.json\"))[\"sourceId\"])'")
48 test "$source_id" = navidrome || { echo 'Preview belongs to another service' >&2; exit 1; }
49 source_snapshot=$(ssh "$source_host" '/usr/sbin/zfs list -H -t snapshot -o name -s creation -r storage1/apps | tail -n 1')
50 case $source_snapshot in storage1/apps@*) ;; *) echo 'No apps ZFS snapshot is available' >&2; exit 1 ;; esac
51 source_root="/mnt/storage1/apps/.zfs/snapshot/${source_snapshot#*@}/navidrone"
52fi
53
54dataset=$(ssh -p "$target_port" "$target_host" "findmnt -n -o SOURCE --mountpoint $mount")
55if [ "$production" = true ]; then
56 case $dataset in */prod/navidrome) ;; *) echo 'Production dataset is not mounted' >&2; exit 1 ;; esac
57else
58 case $dataset in */staging/"$instance") ;; *) echo 'Preview dataset is not mounted' >&2; exit 1 ;; esac
59fi
60
61if [ "$offline" = true ]; then
62 ssh -p "$target_port" "$target_host" "test -s '$source_root/navidrome.db'" || { echo 'Navidrome database is missing' >&2; exit 1; }
63else
64 scratch=$(mktemp -d)
65 trap 'rm -rf "$scratch"' EXIT
66 rsync -a --exclude='*-shm' "$source_host:$source_root/" "$scratch/"
67 test -s "$scratch/navidrome.db" || { echo 'Navidrome database is missing' >&2; exit 1; }
68fi
69
70if [ "$production" = false ]; then
71 ssh -p "$target_port" "$target_host" "NOMAD_TOKEN=\$(cat /var/lib/studio/nomad.token) nomad job stop -yes $instance"
72fi
73snapshot="$dataset@before-navidrome-import-$(date +%s)-$$"
74ssh -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"
75
76if [ "$offline" = true ]; then
77 ssh -p "$target_port" "$target_host" "rsync -a --delete --delete-excluded --exclude='*-shm' '$source_root/' '$root/'"
78else
79 rsync -a --delete -e "ssh -p $target_port" "$scratch/" "$target_host:$root/"
80fi
81owner=$(ssh -p "$target_port" "$target_host" "python3 -c 'import json; print(json.load(open(\"/var/lib/studio/identities.json\"))[\"navidrome\"])'")
82ssh -p "$target_port" "$target_host" "chown -R $owner:$owner $root"
83if [ "$offline" = true ]; then
84 drift=$(ssh -p "$target_port" "$target_host" "rsync -rlnc --delete --delete-excluded --exclude='*-shm' --out-format='%n' '$source_root/' '$root/'")
85else
86 drift=$(rsync -rlnc --delete --out-format='%n' -e "ssh -p $target_port" "$scratch/" "$target_host:$root/")
87fi
88test -z "$drift" || { echo "Navidrome copy differs from source; restore $snapshot" >&2; exit 1; }
89ssh -p "$target_port" "$target_host" "python3 -c 'import sqlite3; db=sqlite3.connect(\"file:$root/navidrome.db?mode=ro\", uri=True); assert db.execute(\"PRAGMA integrity_check\").fetchone()[0] == \"ok\"; assert db.execute(\"select count(*) from user where user_name=\\\"snow\\\" and is_admin=1\").fetchone()[0] == 1'"
90
91if [ "$production" = true ]; then
92 if [ "$offline" = false ]; then
93 test "$(ssh "$source_host" "sudo -n docker inspect -f '{{.State.Running}}' navidrome")" = false || {
94 echo 'Zenith navidrome restarted during import; leave the home server stopped' >&2; exit 1;
95 }
96 fi
97 echo "Imported production Navidrome. Previous dataset state: $snapshot"
98else
99 python3 "$(dirname "$0")/deploy.py" stage navidrome
100 ssh -p "$target_port" "$target_host" "zfs destroy $snapshot"
101 echo "Imported and tested $instance from $source_snapshot"
102fi