Cotton code
121 lines · 5.6 KB · ShellRaw
#!/usr/bin/env bash# A previous-build device copy for a schema rise's proof: the database file a# device made by BUILD keeps of a synthetic household, so the rise can move a# real device file (not one shaped by hand) in place.## Usage:#   scripts/dev/device-copy.sh <build-sha> <synthetic-store-dir> <out.db>##   <build-sha>            the shipped build the device runs (e.g. the tip the#                          last Mac/phone cut was made from)#   <synthetic-store-dir>  a cotton-headless data directory holding an invented#                          household; seeded by BUILD here when it is absent or#                          empty (`seed --size small`), so its file is BUILD's shape#   <out.db>               where the device's copy is written (must not exist)## What it does: checks BUILD out in its own worktree, builds its# cotton-headless under the build lock, serves the synthetic store, joins a# fresh device directory to it as "Synthetic device", approves the join with# the six digits the joining device printed, waits for the device's# copy, stops both processes it started (by their own pids), and writes the# copy with sqlite3 .backup.## Environment: COTTON_DEVICE_COPY_WORK (default ~/.cache/cottondb/device-copy)# holds the worktree, the cargo target, the binary and the logs.## Never the owner's data: the store must not be in the real data folder or the# dev app's folder, and seed itself refuses the app's own folder.set -euo pipefaildie() { echo "device-copy: $*" >&2; exit 1; }[ $# -eq 3 ] || die "usage: $0 <build-sha> <synthetic-store-dir> <out.db>"sha_arg="$1"; store="$2"; out="$3"repo="$(git -C "$(dirname "$0")" rev-parse --show-toplevel)". "$repo/scripts/lib/real-data-root.sh"sha="$(git -C "$repo" rev-parse --verify "${sha_arg}^{commit}")" || die "unknown commit $sha_arg"short="${sha:0:12}"work="${COTTON_DEVICE_COPY_WORK:-$HOME/.cache/cottondb/device-copy}"mkdir -p "$work" "$(dirname "$out")"store="$(cd "$(dirname "$store")" 2>/dev/null && pwd)/$(basename "$store")" || die "no parent for $store"out="$(cd "$(dirname "$out")" && pwd)/$(basename "$out")"[ -e "$out" ] && die "$out exists; name a new file"refuse_real_data_root device-copy "$store"# 1. BUILD's cotton-headless, in its own worktree and target.src="$work/src-$short"if [ ! -d "$src" ]; then  git -C "$repo" worktree add --detach "$src" "$sha" >/dev/nullfi[ "$(git -C "$src" rev-parse HEAD)" = "$sha" ] || die "$src is not at $sha"bin="$work/cotton-headless-$short"if [ ! -x "$bin" ]; then  CARGO_TARGET_DIR="$work/target-$short" BUILD_LOCK_LABEL="device-copy-$short" \    "$repo/scripts/with-build-lock.sh" cargo +1.98.1 build \      --manifest-path "$src/crates/Cargo.toml" -p cotton-ffi --bin cotton-headless  cp "$work/target-$short/debug/cotton-headless" "$bin"fiecho "device-copy: build $short at $bin"# 2. The synthetic household, seeded by BUILD when absent.if [ ! -f "$store/registry.db" ]; then  [ -d "$store" ] && [ -n "$(ls -A "$store")" ] && die "$store is not empty and holds no household"  "$bin" seed --data-dir "$store" --size small --seed 138 | tee "$work/seed-$short.json"fiserve_pid=""; join_pid=""stop() {  for pid in $join_pid $serve_pid; do    if kill -0 "$pid" 2>/dev/null; then kill -TERM "$pid" 2>/dev/null || true; wait "$pid" 2>/dev/null || true; fi  done}trap stop EXIT# 3. Serve it and learn the port it bound."$bin" serve --data-dir "$store" >"$work/serve-$short.log" 2>&1 &serve_pid=$!port=""for _ in $(seq 1 120); do  kill -0 "$serve_pid" 2>/dev/null || die "serve exited: $(tail -5 "$work/serve-$short.log")"  port="$("$bin" status --json --data-dir "$store" 2>/dev/null | python3 -c 'import json,sys; p=json.load(sys.stdin).get("boundPort"); print(p or "")' 2>/dev/null || true)"  [ -n "$port" ] && break  sleep 1done[ -n "$port" ] || die "serve bound no port in 120 s"echo "device-copy: serving $store on 127.0.0.1:$port"# 4. A fresh device joins; the household approves it.device="$(mktemp -d "$work/device-$short.XXXXXX")""$bin" join "http://127.0.0.1:$port" --name "Synthetic device" --data-dir "$device" >"$work/join-$short.log" 2>&1 &join_pid=$!approved=""for _ in $(seq 1 120); do  kill -0 "$join_pid" 2>/dev/null || break  # The join prints "Join <household>: <six digits>"; approve names them, as a  # person at the household would (a build may refuse an approve without them).  digits="$(sed -nE 's/^Join [^ ]+: ([0-9]{6})$/\1/p' "$work/join-$short.log" | tail -1)"  if [ -n "$digits" ] && "$bin" approve --digits "$digits" --data-dir "$store" >>"$work/approve-$short.log" 2>&1; then approved=1; break; fi  sleep 2done[ -n "$approved" ] || die "no join to approve: $(tail -5 "$work/join-$short.log")"echo "device-copy: approved the synthetic device"# 5. Wait for the device's copy of the personal database.copy=""for _ in $(seq 1 900); do  copy="$(find "$device/files" -name '*.db' -type f 2>/dev/null | head -1 || true)"  if [ -n "$copy" ] && [ "$(sqlite3 -readonly "$copy" 'PRAGMA user_version' 2>/dev/null || echo 0)" -gt 0 ]; then    if ! kill -0 "$join_pid" 2>/dev/null || grep -q 'joined' "$work/join-$short.log"; then break; fi  fi  copy=""  sleep 1done[ -n "$copy" ] || die "the device holds no database copy after 15 min: $(tail -5 "$work/join-$short.log")"sleep 5stop; trap - EXITsqlite3 "$copy" ".backup '$out'"# Not read-only: a fresh .backup in WAL mode has no -shm yet to open read-only by.version="$(sqlite3 "$out" 'PRAGMA user_version')"echo "device-copy: $out · schema $version · $(wc -c <"$out" | tr -d ' ') bytes · sha256 $(shasum -a 256 "$out" | cut -d' ' -f1)"echo "device-copy: device directory kept at $device; logs in $work/*-$short.log"