Files
leleka/scripts/gpu-bringup/remote_sync.sh
T

203 lines
9.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Mirror this worktree to the GPU host and build it there. Runs on the Mac (or any machine that can ssh to the host).
#
# remote_sync.sh check # local checks + the plan, no network
# remote_sync.sh sync [--dry-run] # rsync the tree to HOST:REMOTE_DIR (never deletes anything)
# remote_sync.sh build # ssh: cargo fetch + release build of the three binaries + test --no-run
# remote_sync.sh all # sync, then build
# remote_sync.sh pull-sealed [--dry-run] # bring the sealed PTX / patched Rust listed in the host's sealed
# # manifest back into this worktree (so they can be reviewed + committed)
# remote_sync.sh pull-evidence [LOCAL_DIR] # copy HOST:REMOTE_DIR/target/gpu-bringup (logs, manifests) here
#
# Options: --host H (env LELEKA_GPU_HOST, default rnr5090) --remote-dir D (env LELEKA_REMOTE_DIR, default
# ~/work/leleka-port-bringup; must live under the remote $HOME) --dry-run --allow-dirty
# --release-tests (build test binaries with --release; default debug, which is what run_gpu_smokes.sh runs)
# --jobs N (cargo -j N and nice -n 10, to leave CPU to a live service)
#
# Safety: no --delete, ever. The host tree is a dedicated directory; the only thing `sync` can overwrite is a file
# of that tree. Because sealing patches files in the HOST tree, `sync` refuses while the host holds a sealed
# manifest that has not been pulled (pull-sealed first), unless --force-overwrite-sealed.
set -euo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
repo="$(cd "$here/../.." && pwd)"
host="${LELEKA_GPU_HOST:-rnr5090}"
remote_dir="${LELEKA_REMOTE_DIR:-~/work/leleka-port-bringup}"
dry=0
allow_dirty=0
force_sealed=0
test_profile=""
jobs=""
cmd="${1:-}"
[[ $# -gt 0 ]] && shift
extra_args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--host) host="$2"; shift 2 ;;
--remote-dir) remote_dir="$2"; shift 2 ;;
--dry-run|-n) dry=1; shift ;;
--allow-dirty) allow_dirty=1; shift ;;
--force-overwrite-sealed) force_sealed=1; shift ;;
--release-tests) test_profile="--release"; shift ;;
--jobs) jobs="$2"; shift 2 ;;
-h|--help) sed -n '2,22p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 0 ;;
-*) echo "unknown option $1" >&2; exit 2 ;;
*) extra_args+=("$1"); shift ;;
esac
done
[[ -n "$cmd" ]] || { sed -n '2,22p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//' >&2; exit 2; }
ssh_opts=(-o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=30)
# shellcheck disable=SC2029 # the remote command strings are built (and validated) on the client on purpose
ssh_cmd() { ssh "${ssh_opts[@]}" "$host" "$@"; }
# the remote dir must be a dedicated directory below the remote home
# shellcheck disable=SC2088 # a literal "~/" prefix is what we mean; it is resolved by the remote shell
case "$remote_dir" in
"~/"?*|/home/*/?*) ;;
*) echo "remote dir '$remote_dir' must be ~/something or /home/<user>/something" >&2; exit 2 ;;
esac
case "$remote_dir" in *..*|*" "*|*"'"*|*'"'*|*'$'*|*'`'*|*';'*) echo "remote dir contains unsafe characters" >&2; exit 2 ;; esac
remote_rel="${remote_dir#\~/}" # relative to $HOME for `ssh host cd`
remote_arg="$remote_dir"
# shellcheck disable=SC2088
[[ "$remote_dir" == "~/"* ]] && remote_arg="$remote_rel"
# rsync excludes: VCS (.git is a file in a worktree, hence no trailing slash), build output, weights, anything huge.
# *.bin is NOT excluded (test fixtures use it). Patterns stay simple so macOS openrsync and GNU rsync agree.
excludes=(--exclude .git --exclude target --exclude .DS_Store --exclude node_modules
--exclude '*.gguf' --exclude '*.safetensors' --exclude '*.kar' --exclude results-private
--exclude '*.pyc' --exclude __pycache__ --max-size=200m)
local_checks() {
echo "worktree : $repo"
echo "branch : $(git -C "$repo" rev-parse --abbrev-ref HEAD) head $(git -C "$repo" rev-parse --short HEAD)"
local dirty
dirty="$(git -C "$repo" status --porcelain --untracked-files=no)"
if [[ -n "$dirty" ]]; then
echo "tracked files modified:"; printf '%s\n' "${dirty//$'\n'/$'\n' }"
[[ "$allow_dirty" == 1 ]] || { echo "refusing: commit first or pass --allow-dirty (the host tree must be traceable to a commit)" >&2; return 1; }
fi
if git -C "$repo" ls-files | grep -Ei '\.(gguf|safetensors|kar)$|\.ptx\.new$' | head -1 | grep -q .; then
echo "refusing: model/weight files are tracked" >&2; return 1
fi
echo "target : $host:$remote_dir"
du -sh --exclude=.git --exclude=target "$repo" 2>/dev/null | sed 's/^/size : /' || true
}
remote_has_unpulled_seal() {
ssh_cmd "test -e $remote_arg/target/gpu-bringup/SEALED-MANIFEST.json" 2>/dev/null
}
do_sync() {
local_checks
if [[ "$dry" == 0 && "$force_sealed" == 0 ]] && remote_has_unpulled_seal; then
echo "refusing: $host:$remote_dir/target/gpu-bringup/SEALED-MANIFEST.json exists, so the host tree holds sealed PTX/pins" >&2
echo "that the Mac does not have. Run: $0 pull-sealed (or --force-overwrite-sealed to discard them)" >&2
return 1
fi
[[ "$dry" == 1 ]] && echo "(dry run: rsync -n; no seal check, nothing is written)"
ssh_cmd "mkdir -p $remote_arg" 2>/dev/null || [[ "$dry" == 1 ]] || { echo "ssh/mkdir failed" >&2; return 1; }
local flags=(-az --itemize-changes --human-readable)
[[ "$dry" == 1 ]] && flags+=(-n)
rsync "${flags[@]}" "${excludes[@]}" -e "ssh ${ssh_opts[*]}" "$repo/" "$host:$remote_dir/"
if [[ "$dry" == 0 ]]; then
git -C "$repo" rev-parse HEAD | ssh_cmd "cat > $remote_arg/.synced-from-head"
echo "synced; head recorded in $remote_dir/.synced-from-head"
fi
}
remote_build_script() {
local jf="" nice=""
[[ -n "$jobs" ]] && { [[ "$jobs" =~ ^[0-9]+$ ]] || { echo "--jobs needs a number" >&2; exit 2; }; jf="-j $jobs"; nice="nice -n 10 "; }
cat <<EOF
set -euo pipefail
cd $remote_arg
mkdir -p target/gpu-bringup/logs
log=target/gpu-bringup/logs/build-\$(date -u +%Y%m%dT%H%M%SZ).log
exec > >(tee -a "\$log") 2>&1
echo "== host \$(hostname) user \$(id -un) \$(date -u +%FT%TZ)"
[ -f "\$HOME/.cargo/env" ] && . "\$HOME/.cargo/env"
command -v cargo >/dev/null || { echo "cargo not installed on the host: run scripts/install-pinned-rustup.sh (see docs/gpu-bringup.md step d)"; exit 3; }
command -v cc >/dev/null || { echo "no C compiler (cc): apt install build-essential"; exit 3; }
echo "head: \$(cat .synced-from-head 2>/dev/null || echo unknown)"
rustc --version; cargo --version; cc --version | head -1
echo "== cargo fetch (needs crates.io; fails fast if the host is offline)"
cargo fetch --locked
echo "== release build: leleka-cli leleka-daemon leleka-inference"
${nice}cargo build --release --locked $jf -p strata-desktop-core --bin leleka-cli --bin leleka-daemon --bin leleka-inference
echo "== test binaries (not run): cuda-driver with --features cuda-driver"
${nice}cargo test --no-run --locked $jf $test_profile -p leleka-cuda-driver --features cuda-driver
echo "== test binaries (not run): leleka-engine-qwen35-cuda"
${nice}cargo test --no-run --locked $jf $test_profile -p leleka-engine-qwen35-cuda
echo "== test binaries (not run): native-cpu-expert (build.rs has no C++ step; Rust only)"
${nice}cargo test --no-run --locked $jf $test_profile -p leleka-native-cpu-expert
echo "== digests"
sha256sum target/release/leleka-cli target/release/leleka-daemon target/release/leleka-inference | tee target/gpu-bringup/binaries.sha256
echo "BUILD OK"
EOF
}
do_build() {
if [[ "$dry" == 1 ]]; then
echo "# would run on $host:"
remote_build_script | sed 's/^/ /'
return 0
fi
remote_build_script | ssh_cmd "bash -s"
}
do_pull_sealed() {
local list manifest
manifest="$(mktemp)"; list="$(mktemp)"
ssh_cmd "cat $remote_arg/target/gpu-bringup/SEALED-MANIFEST.json" > "$manifest" || { echo "no sealed manifest on the host" >&2; return 1; }
python3 - "$manifest" "$list" <<'PY'
import json, sys
m = json.load(open(sys.argv[1]))
files = m["touched_files"]
if any(f.startswith("/") or ".." in f.split("/") for f in files):
raise SystemExit("unsafe path in sealed manifest")
open(sys.argv[2], "w").write("\n".join(files) + "\n")
print(f"{len(files)} files: {m.get('mode')} manifest from {m['created_utc']}")
PY
if [[ "$allow_dirty" == 0 ]] && git -C "$repo" status --porcelain --untracked-files=no | grep -q .; then
echo "refusing: local tracked changes would be overwritten; commit/stash or --allow-dirty" >&2; return 1
fi
local flags=(-a --itemize-changes --human-readable --files-from="$list")
[[ "$dry" == 1 ]] && flags+=(-n)
rsync "${flags[@]}" -e "ssh ${ssh_opts[*]}" "$host:$remote_dir/" "$repo/"
if [[ "$dry" == 0 ]]; then
mkdir -p "$repo/target/gpu-bringup"
cp "$manifest" "$repo/target/gpu-bringup/SEALED-MANIFEST.pulled.json"
echo "pulled. Review with: git -C $repo diff --stat ; git status --short"
echo "Then clear the host guard: ssh $host 'mv $remote_dir/target/gpu-bringup/SEALED-MANIFEST.json $remote_dir/target/gpu-bringup/SEALED-MANIFEST.pulled.json'"
fi
rm -f "$manifest" "$list"
}
do_pull_evidence() {
local dest="${extra_args[0]:-$repo/target/gpu-bringup-evidence/$(date -u +%Y%m%dT%H%M%SZ)}"
mkdir -p "$dest"
local flags=(-az --itemize-changes --human-readable)
[[ "$dry" == 1 ]] && flags+=(-n)
rsync "${flags[@]}" -e "ssh ${ssh_opts[*]}" "$host:$remote_dir/target/gpu-bringup/" "$dest/"
# parity reports must live outside the repository (bench-chat-paired.py refuses otherwise): ~/gpu-bringup-evidence
# shellcheck disable=SC2016 # $HOME must expand on the remote side
if ssh_cmd 'test -d "$HOME/gpu-bringup-evidence"' 2>/dev/null; then
mkdir -p "$dest/home-evidence"
rsync "${flags[@]}" -e "ssh ${ssh_opts[*]}" "$host:gpu-bringup-evidence/" "$dest/home-evidence/"
fi
echo "evidence in $dest"
}
case "$cmd" in
check) local_checks; echo; dry=1; do_build ;;
sync) do_sync ;;
build) do_build ;;
all) do_sync; do_build ;;
pull-sealed) do_pull_sealed ;;
pull-evidence) do_pull_evidence ;;
*) echo "unknown command '$cmd'" >&2; exit 2 ;;
esac