Initial import of streamline

This commit is contained in:
2026-11-16 17:26:44 -08:00
commit 8e4aec1855
1221 changed files with 274256 additions and 0 deletions
+262
View File
@@ -0,0 +1,262 @@
#!/usr/bin/env bash
#
# executor-restore.sh — restore a snapshot produced by executor-snapshot.sh onto
# a remote host, killing any running executor first and starting a fresh one.
#
# What it does, in order:
# 1. sshpass + scp the local tar.gz to the remote host (/tmp).
# 2. Read the embedded snapshot/MANIFEST.txt to learn the original REPO path
# and which HOME entries were captured.
# 3. Kill any running satellite process on the remote (`pgrep -f satellite/src/index.ts`,
# then SIGTERM → SIGKILL if still alive after 5s).
# 4. Back up the existing repo dir and $HOME/<captured entries> to
# $HOME/.streamline-restore-backup/<stamp>/ (in case a rollback is needed).
# 5. Extract the archive:
# snapshot/streamline/* -> <REPO> (default) or --repo <path>
# snapshot/home/* -> $HOME/*
# 6. Run `bun install` if node_modules is missing.
# 7. Start the satellite in the background via nohup, logging to
# $HOME/streamline-satellite.log.
#
# Usage:
# scripts/executor-restore.sh <user@host> <password> <snapshot.tar.gz> \
# [--repo <remote_repo_path>] [--port 22] [--no-install] [--no-start] \
# [--env KEY=VALUE ...]
#
# Examples:
# scripts/executor-restore.sh fei@fei-ios-01.shiro0.icu 'hunter2' \
# ~/streamline-snapshots/executor-fei-ios-01_shiro0_icu-20260723-140102.tar.gz
#
# # Override the location and inject env before start:
# scripts/executor-restore.sh fei@10.0.0.5 'hunter2' snap.tar.gz \
# --repo /home/fei/streamline \
# --env AGENT_HOST=fei-ios-02.shiro0.icu --env AGENT_PLATFORM=ios
#
set -euo pipefail
# --- pre-scan for --local so we can accept a shorter argv in that mode ------
LOCAL_MODE=0
for a in "$@"; do
[ "$a" = "--local" ] && LOCAL_MODE=1
done
SSH_TARGET=""; SSH_PASS=""; SNAPSHOT=""
if [ "$LOCAL_MODE" = "1" ]; then
# In local mode only the snapshot path is required (first non-flag arg).
# Peel it off; user@host / password are ignored.
for arg in "$@"; do
case "$arg" in
--*) ;;
*) if [ -z "$SNAPSHOT" ] && [ -e "$arg" ]; then SNAPSHOT="$arg"; fi ;;
esac
done
[ -n "$SNAPSHOT" ] || { echo "usage: $0 --local <snapshot.tar.gz> [--repo <path>] [--skip-home] [--no-install] [--no-start] [--env K=V ...]" >&2; exit 2; }
# Drop the snapshot from argv so the option parser below only sees flags.
new=(); for arg in "$@"; do
[ "$arg" = "$SNAPSHOT" ] && continue
new+=("$arg")
done
set -- "${new[@]:-}"
else
if [ $# -lt 3 ]; then
sed -n '3,34p' "$0"; exit 2
fi
SSH_TARGET="$1"; SSH_PASS="$2"; SNAPSHOT="$3"; shift 3
fi
REPO_OVERRIDE=""
SSH_PORT=22
DO_INSTALL=1
DO_START=1
SKIP_HOME=0
EXTRA_ENV=()
while [ $# -gt 0 ]; do
case "$1" in
--repo) REPO_OVERRIDE="$2"; shift 2 ;;
--port) SSH_PORT="$2"; shift 2 ;;
--no-install) DO_INSTALL=0; shift ;;
--no-start) DO_START=0; shift ;;
--skip-home) SKIP_HOME=1; shift ;;
--local) shift ;; # already handled in pre-scan
--env) EXTRA_ENV+=("$2"); shift 2 ;;
-h|--help) sed -n '3,34p' "$0"; exit 0 ;;
*) echo "unknown arg: $1" >&2; exit 2 ;;
esac
done
[ -s "$SNAPSHOT" ] || { echo "snapshot not found or empty: $SNAPSHOT" >&2; exit 2; }
if [ "$LOCAL_MODE" = "0" ]; then
command -v sshpass >/dev/null 2>&1 || { echo "sshpass not found (brew install hudochenkov/sshpass/sshpass)" >&2; exit 1; }
fi
SSH_OPTS=(-p "$SSH_PORT" -o StrictHostKeyChecking=accept-new -o ConnectTimeout=15 -o ServerAliveInterval=30 -o LogLevel=ERROR)
if [ "$LOCAL_MODE" = "1" ]; then
run_ssh() {
# Split KEY=VALUE prefix pairs (used before bash -s) from the rest,
# and re-exec via env so they land in the child's environment.
local envs=()
while [ $# -gt 0 ]; do
case "$1" in
[A-Za-z_][A-Za-z0-9_]*=*) envs+=("$1"); shift ;;
*) break ;;
esac
done
env "${envs[@]}" "$@"
}
run_scp() { cp "$@"; }
else
run_ssh() { sshpass -p "$SSH_PASS" ssh "${SSH_OPTS[@]}" "$SSH_TARGET" "$@"; }
run_scp() { sshpass -p "$SSH_PASS" scp -P "$SSH_PORT" -o StrictHostKeyChecking=accept-new -o LogLevel=ERROR "$@"; }
fi
# ---------- read manifest locally to learn original REPO ----------
MANIFEST=$(tar -xzf "$SNAPSHOT" -O snapshot/MANIFEST.txt 2>/dev/null || true)
[ -n "$MANIFEST" ] || { echo "snapshot has no snapshot/MANIFEST.txt (wrong archive?)" >&2; exit 3; }
ORIG_REPO=$(echo "$MANIFEST" | awk -F= '/^REPO=/{print $2; exit}')
[ -n "$ORIG_REPO" ] || { echo "manifest missing REPO=" >&2; exit 3; }
REMOTE_REPO="${REPO_OVERRIDE:-$ORIG_REPO}"
echo "[restore] target host: $SSH_TARGET"
echo "[restore] target repo: $REMOTE_REPO (snapshot REPO=$ORIG_REPO)"
# ---------- ship archive ----------
STAMP="$(date +%Y%m%d-%H%M%S)"
if [ "$LOCAL_MODE" = "1" ]; then
REMOTE_TGZ="/tmp/streamline-snapshot-$STAMP.tar.gz"
echo "[restore] LOCAL mode — copying archive → $REMOTE_TGZ"
cp "$SNAPSHOT" "$REMOTE_TGZ"
else
REMOTE_TGZ="/tmp/streamline-snapshot-$STAMP.tar.gz"
echo "[restore] uploading archive → $REMOTE_TGZ"
run_scp "$SNAPSHOT" "$SSH_TARGET:$REMOTE_TGZ"
fi
# ---------- serialize extra env for injection ----------
EXTRA_ENV_STR=""
for kv in "${EXTRA_ENV[@]:-}"; do
# very light escaping — we forbid single-quotes in the value.
case "$kv" in *\'*) echo "reject env with single quote: $kv" >&2; exit 2 ;; esac
EXTRA_ENV_STR+="$kv"$'\n'
done
# ---------- remote plan ----------
run_ssh REMOTE_TGZ="$REMOTE_TGZ" REMOTE_REPO="$REMOTE_REPO" DO_INSTALL="$DO_INSTALL" DO_START="$DO_START" \
SKIP_HOME="$SKIP_HOME" EXTRA_ENV_STR="$EXTRA_ENV_STR" STAMP="$STAMP" bash -s <<'REMOTE'
set -euo pipefail
: "${REMOTE_TGZ:?}"
: "${REMOTE_REPO:?}"
: "${STAMP:?}"
echo "[restore] pids before: $(pgrep -f 'satellite/src/index.ts' | tr '\n' ' ' || true)"
# 1. Kill running satellite(s).
if pgrep -f 'satellite/src/index.ts' >/dev/null 2>&1; then
echo "[restore] stopping running satellite(s) with SIGTERM"
pkill -TERM -f 'satellite/src/index.ts' || true
for _ in 1 2 3 4 5; do
pgrep -f 'satellite/src/index.ts' >/dev/null 2>&1 || break
sleep 1
done
if pgrep -f 'satellite/src/index.ts' >/dev/null 2>&1; then
echo "[restore] still alive after 5s — SIGKILL"
pkill -KILL -f 'satellite/src/index.ts' || true
fi
fi
# 2. Back up existing repo + captured $HOME entries.
BACKUP="$HOME/.streamline-restore-backup/$STAMP"
mkdir -p "$BACKUP"
if [ -d "$REMOTE_REPO" ]; then
echo "[restore] backing up existing repo → $BACKUP/streamline-prev"
mv "$REMOTE_REPO" "$BACKUP/streamline-prev"
fi
# 3. Extract snapshot into a staging dir first so we can inspect HOME_ENTRIES.
STAGE=$(mktemp -d)
tar -xzf "$REMOTE_TGZ" -C "$STAGE"
[ -d "$STAGE/snapshot/streamline" ] || { echo "archive missing snapshot/streamline"; exit 3; }
[ -f "$STAGE/snapshot/MANIFEST.txt" ] || { echo "archive missing MANIFEST"; exit 3; }
# Move repo into place.
mkdir -p "$(dirname "$REMOTE_REPO")"
mv "$STAGE/snapshot/streamline" "$REMOTE_REPO"
# Restore captured HOME entries — but back the old copy up first.
if [ "${SKIP_HOME:-0}" = "1" ]; then
echo "[restore] --skip-home: not touching \$HOME/*"
elif [ -f "$STAGE/snapshot/HOME_ENTRIES.txt" ]; then
while IFS= read -r rel; do
[ -z "$rel" ] && continue
src="$STAGE/snapshot/home/$rel"
dst="$HOME/$rel"
[ -e "$src" ] || continue
if [ -e "$dst" ] || [ -L "$dst" ]; then
mkdir -p "$BACKUP/home/$(dirname "$rel")"
mv "$dst" "$BACKUP/home/$rel" 2>/dev/null || cp -R "$dst" "$BACKUP/home/$rel"
rm -rf "$dst" 2>/dev/null || true
fi
mkdir -p "$(dirname "$dst")"
mv "$src" "$dst"
echo "[restore] restored ~/$rel"
done < "$STAGE/snapshot/HOME_ENTRIES.txt"
fi
rm -rf "$STAGE"
rm -f "$REMOTE_TGZ"
# 4. Inject extra env into .env (append; overrides existing definitions on read).
if [ -n "${EXTRA_ENV_STR:-}" ]; then
ENV_FILE="$REMOTE_REPO/.env"
touch "$ENV_FILE"
{
echo ""
echo "# --- injected by executor-restore.sh @ $STAMP ---"
printf '%s' "$EXTRA_ENV_STR"
} >> "$ENV_FILE"
echo "[restore] appended $(printf '%s' "$EXTRA_ENV_STR" | grep -c '^') env overrides to $ENV_FILE"
fi
# 5. Ensure bun is available.
if ! command -v bun >/dev/null 2>&1; then
if [ -x "$HOME/.bun/bin/bun" ]; then
export PATH="$HOME/.bun/bin:$PATH"
else
echo "[restore] WARNING: bun not on PATH. Install it: curl -fsSL https://bun.sh/install | bash"
fi
fi
# 6. bun install if requested and needed.
if [ "$DO_INSTALL" = "1" ]; then
if [ ! -d "$REMOTE_REPO/node_modules" ] || [ ! -d "$REMOTE_REPO/satellite/node_modules" ]; then
echo "[restore] running bun install …"
(cd "$REMOTE_REPO" && bun install --frozen-lockfile 2>&1 | tail -n 30) || \
(cd "$REMOTE_REPO" && bun install 2>&1 | tail -n 30)
else
echo "[restore] node_modules present, skipping bun install"
fi
fi
# 7. Start satellite in the background.
if [ "$DO_START" = "1" ]; then
LOG="$HOME/streamline-satellite.log"
echo "[restore] starting satellite → $LOG"
(
cd "$REMOTE_REPO"
# Detach cleanly so ssh can exit.
nohup bash -lc 'bun run start:satellite' >>"$LOG" 2>&1 </dev/null &
disown || true
)
sleep 2
pid=$(pgrep -f 'satellite/src/index.ts' | head -n1 || true)
if [ -n "$pid" ]; then
echo "[restore] satellite running as PID $pid (tail -f $LOG)"
else
echo "[restore] WARNING: satellite did not appear in pgrep after 2s — check $LOG"
tail -n 40 "$LOG" || true
exit 5
fi
fi
echo "[restore] backup kept at: $BACKUP"
echo "[restore] done."
REMOTE
echo "[restore] complete."