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
+489
View File
@@ -0,0 +1,489 @@
#!/usr/bin/env bash
# Deploy Streamline end to end: control plane (pull → install → build → restart → verify),
# then rebuild the satellite bundle, then push it to the executor nodes. One
# command takes a merged commit all the way to running code on every machine.
#
# Safe by construction:
# - every step runs as the repo owner (jiaopengwen), never as the SSH user
# - preflight refuses to proceed on a dirty tree or a wrong-looking host
# - dependencies are reconciled from bun.lock after every pull, so a newly
# added runtime package cannot be missing from an otherwise healthy deploy
# - build happens BEFORE the restart, so a broken build can't leave the
# service restarted onto a half-updated tree
# - the schema check runs between the pull and the restart: an unapplied
# migration (anyone's, not just this deploy's) stops the deploy while the old
# code is still serving, instead of 500ing after the restart
# - the satellite bundle is rebuilt AFTER the control plane is verified live,
# so a bundle failure can never hold the control plane hostage
# - the push to satellites uses 「空档部署」 for any BUSY node: the node parks
# the bundle, stops taking new work, and swaps in the gap after its current
# task. No running task is ever interrupted, which is what made this step
# safe to automate at all (it used to be a human click). 强制部署 — the one
# that does kill tasks — stays opt-in behind --force-push
# - the push covers the `default` node pool only (fei-android-01 / fei-ios-01 /
# fei-ios-02). Other teams' pools (#im-sg, #pns) run their own cadence and
# are not swept along by a routine deploy — see scripts/push-satellites.ts
# - --check does preflight only and writes nothing
# - --satellite-only rebuilds+pushes the bundle (no pull/restart); --skip-satellite
# deploys the control plane and leaves the bundle and nodes alone;
# --no-push builds the bundle but leaves the nodes for a human
#
# See SKILL.md for why each of these is not the obvious command.
set -uo pipefail
HOST="${STREAMLINE_DEPLOY_HOST:-fdbd:dc01:ff:31a:6973:d9b4:988a:23c0}"
# The SSH account is the CORP username, which on a Mac is usually NOT $(id -un)
# (that's often "bytedance"). git user.name is the corp id by convention here, so
# prefer it and fall back to the local user; override with STREAMLINE_DEPLOY_USER.
SSH_USER="${STREAMLINE_DEPLOY_USER:-$(git config user.name 2>/dev/null || true)}"
SSH_USER="${SSH_USER:-$(id -un)}"
# The deploy tree is owned by this account and the systemd unit runs as it.
OWNER="${STREAMLINE_DEPLOY_OWNER:-jiaopengwen}"
REPO="${STREAMLINE_DEPLOY_REPO:-/data00/home/jiaopengwen/streamline}"
UNIT="${STREAMLINE_DEPLOY_UNIT:-streamline-server}"
# bun is NOT on the owner's non-interactive PATH; the systemd unit hardcodes the
# same directory in its Environment=PATH.
BUN_BIN="${STREAMLINE_DEPLOY_BUN_BIN:-/home/jiaopengwen/.bun/bin}"
BRANCH="${STREAMLINE_DEPLOY_BRANCH:-main}"
CHECK_ONLY=0
SKIP_SATELLITE=0
SAT_ONLY=0
PUSH_SATELLITES=1
FORCE_PUSH=0
for arg in "$@"; do
case "$arg" in
--check|--dry-run) CHECK_ONLY=1 ;;
--skip-satellite) SKIP_SATELLITE=1 ;;
--satellite-only) SAT_ONLY=1 ;;
--no-push) PUSH_SATELLITES=0 ;;
--force-push) FORCE_PUSH=1 ;;
-h|--help)
# Print the header comment block, stopping at the first non-comment line.
awk 'NR==1{next} /^#/{sub(/^# ?/,""); print; next} {exit}' "$0"
exit 0 ;;
*) echo "unknown argument: $arg" >&2; exit 2 ;;
esac
done
if [ "$SAT_ONLY" = 1 ] && [ "$SKIP_SATELLITE" = 1 ]; then
echo "--satellite-only and --skip-satellite are contradictory" >&2; exit 2
fi
if [ "$FORCE_PUSH" = 1 ] && [ "$PUSH_SATELLITES" = 0 ]; then
echo "--force-push and --no-push are contradictory" >&2; exit 2
fi
# Connection multiplexing. Measured on this link: a cold handshake to the devbox
# costs ~3.8s, a reused one ~0.6s — and a full deploy makes ~30 remote calls, so
# without this the script spends ~115s doing nothing but handshakes (a --check,
# which is 11 calls, took 44s). The first call opens a master; the rest ride it.
#
# ControlPath uses %C (a hash of the connection tuple) rather than %h: the host is
# a 39-character IPv6 literal and macOS caps unix socket paths at ~104 bytes, so
# spelling the address into the path is how this breaks on someone else's machine.
# ControlPersist keeps the master briefly after the last call — the common
# "--check, then deploy for real" pair then reuses one connection.
CM_OPTS=(-o ControlMaster=auto -o ControlPath=/tmp/streamline-deploy-%C -o ControlPersist=60)
# -6 with the RAW IPv6 address. Bracketing it ("user@[fdbd:...]") makes ssh treat
# the whole thing as a hostname and fail to resolve.
ssh_raw() { ssh -6 -o ConnectTimeout=15 -o BatchMode=yes "${CM_OPTS[@]}" "${SSH_USER}@${HOST}" "$@"; }
# Run a command as the repo owner with bun on PATH.
# `set -o pipefail` must be set in the REMOTE shell: callers pipe through `tail`
# to trim output, and without it the remote pipeline returns tail's status (0),
# so a failed `git pull` / `bun run build` would sail past the caller's `|| fail`
# and we'd restart onto a half-updated tree or an empty dist/.
as_owner() {
ssh_raw "sudo -n -u ${OWNER} bash -lc 'set -o pipefail; export PATH=${BUN_BIN}:\$PATH; cd ${REPO} || exit 1; $1'"
}
say() { printf '\n\033[1m▶ %s\033[0m\n' "$1"; }
fail() { printf '\033[31m✗ %s\033[0m\n' "$1" >&2; exit 1; }
ok() { printf '\033[32m✓\033[0m %s\n' "$1"; }
warn() { printf '\033[33m!\033[0m %s\n' "$1"; }
# The git SHA stamped into the built satellite bundle (empty when never built).
# This is what the hot-deploy panel shows as "latest" and compares node versions
# against — NOT the control plane's HEAD, which is why the two can silently drift.
bundle_version() { as_owner "cat satellite/dist/VERSION 2>/dev/null || true"; }
# ── Preflight ────────────────────────────────────────────────────────────────
say "Preflight"
# Kerberos. The devbox sshd offers ONLY gssapi-keyex / gssapi-with-mic — no
# publickey — and a ticket lives ~10h, so every morning the SSH check below used
# to fail with an error that reads like a key or network problem. Renew it here:
# if there is no valid ticket, kinit with the corp password from the repo's local
# .env (KINIT_PASSWORD; .env is gitignored). The value is parsed out with sed, not
# `source`d, so nothing else in .env leaks into this shell. It reaches kinit over
# stdin (Heimdal's --password-file=STDIN): never on argv, never in a temp file,
# never echoed. This writes only to the local credential cache, so --check does it too.
ENV_FILE="${STREAMLINE_DEPLOY_ENV_FILE:-$(cd "$(dirname "$0")/../../.." && pwd)/.env}"
# git worktree(如 ~/orca/workspaces/streamline/*)里没有 .env —— 它被 gitignore,不跟着 worktree 走。
# 退到主检出的仓库根(worktree 共用的 .git 的上一级)去找,省得每次手动指 STREAMLINE_DEPLOY_ENV_FILE。
if [ -z "${STREAMLINE_DEPLOY_ENV_FILE:-}" ] && [ ! -f "$ENV_FILE" ]; then
MAIN_ROOT=$(cd "$(dirname "$0")" && git rev-parse --path-format=absolute --git-common-dir 2>/dev/null | sed 's#/\.git$##')
[ -n "$MAIN_ROOT" ] && [ -f "$MAIN_ROOT/.env" ] && ENV_FILE="$MAIN_ROOT/.env"
fi
KRB_PRINCIPAL="${STREAMLINE_DEPLOY_PRINCIPAL:-${SSH_USER}@BYTEDANCE.COM}"
if klist -s 2>/dev/null; then
ok "kerberos ticket valid"
else
KINIT_PW=$(sed -n 's/^KINIT_PASSWORD=//p' "$ENV_FILE" 2>/dev/null | tail -1)
KINIT_PW=${KINIT_PW%$'\r'}
# Tolerate one layer of matching quotes, as dotenv does.
if [ "${#KINIT_PW}" -ge 2 ]; then
case "$KINIT_PW" in \"*\"|\'*\') KINIT_PW=${KINIT_PW:1:${#KINIT_PW}-2} ;; esac
fi
[ -n "$KINIT_PW" ] || fail "kerberos ticket missing/expired and no KINIT_PASSWORD in ${ENV_FILE} — add it there, or run: kinit ${KRB_PRINCIPAL}"
printf '%s\n' "$KINIT_PW" | kinit --password-file=STDIN "$KRB_PRINCIPAL" >/dev/null 2>&1
KINIT_RC=$?
unset KINIT_PW
[ "$KINIT_RC" = 0 ] && klist -s 2>/dev/null \
|| fail "kinit ${KRB_PRINCIPAL} failed — KINIT_PASSWORD in ${ENV_FILE} is probably stale (password changed?)"
ok "kerberos ticket renewed for ${KRB_PRINCIPAL}"
fi
ssh_raw 'echo ok' >/dev/null 2>&1 || fail "cannot SSH to ${SSH_USER}@${HOST} — need corp network + a Kerberos ticket (klist); if the username is wrong set STREAMLINE_DEPLOY_USER=<corp id>"
ok "ssh ${SSH_USER}@${HOST}"
ssh_raw "test -d ${REPO}/.git" || fail "no git repo at ${REPO}"
ok "repo ${REPO}"
ssh_raw 'sudo -n true' 2>/dev/null || fail "passwordless sudo unavailable (needed to act as ${OWNER})"
ok "sudo -n"
ssh_raw "test -x ${BUN_BIN}/bun" || fail "bun not found at ${BUN_BIN}/bun"
ok "bun at ${BUN_BIN}"
ssh_raw "systemctl cat ${UNIT}.service >/dev/null 2>&1" || fail "no systemd unit ${UNIT}.service"
ok "unit ${UNIT}.service"
# A dirty tree means someone edited production in place. Pulling over it either
# fails or silently discards their work — stop and let a human decide.
DIRTY=$(as_owner 'git status --porcelain 2>/dev/null | grep -v "^?? " | head -20')
if [ -n "$DIRTY" ]; then
printf '\033[31m✗ working tree at %s has uncommitted tracked changes:\033[0m\n%s\n' "$REPO" "$DIRTY" >&2
fail "refusing to pull over local edits — resolve on the box first"
fi
ok "working tree clean (tracked files)"
# 一次往返取两个值:分支和 SHA 总是一起用,拆成两次调用只是多付一次握手。
HEAD_INFO=$(as_owner "git rev-parse --short HEAD; git rev-parse --abbrev-ref HEAD")
BEFORE=$(printf '%s\n' "$HEAD_INFO" | sed -n 1p)
CUR_BRANCH=$(printf '%s\n' "$HEAD_INFO" | sed -n 2p)
ok "at ${BEFORE} on ${CUR_BRANCH}"
[ "$CUR_BRANCH" = "$BRANCH" ] || fail "on branch '${CUR_BRANCH}', expected '${BRANCH}'"
# Report the satellite bundle's stamped SHA + age. It is a SEPARATE artifact from
# the control plane and nothing before this script rebuilt it, so it routinely sat
# several commits behind while the panel confidently labelled nodes 「最新」.
BUNDLE_BEFORE=$(bundle_version)
if [ -z "$BUNDLE_BEFORE" ]; then
warn "satellite bundle: never built"
else
# 年龄在远端一次算完:本地做减法要 stat 和 date 两次往返,而它们只为了得出一个分钟数。
MINS=$(as_owner "echo \$(( ( \$(date +%s) - \$(stat -c %Y satellite/dist/satellite.js 2>/dev/null || echo 0) ) / 60 ))")
if [ "$BUNDLE_BEFORE" = "$BEFORE" ]; then
ok "satellite bundle at ${BUNDLE_BEFORE} (built ${MINS}m ago, matches HEAD)"
else
warn "satellite bundle at ${BUNDLE_BEFORE} (built ${MINS}m ago) — HEAD is ${BEFORE}"
fi
fi
if [ "$CHECK_ONLY" = 1 ]; then
# Read-only, against the tree that is deployed right now. A real deploy runs
# the same check AFTER the pull, where it is blocking.
say "Schema check (current tree)"
as_owner "bun scripts/check-migrations.ts 2>&1 | tail -20" || warn "生产库缺少迁移声明过的对象(见上)"
say "Preflight only (--check) — nothing was changed"
exit 0
fi
# ── Deploy ───────────────────────────────────────────────────────────────────
# --satellite-only rebuilds just the bundle from the tree that is already
# deployed: no pull, no frontend build, no restart. For the common case of "the
# bundle drifted behind the control plane" there is no reason to bounce a healthy
# service to fix an artifact it never reads.
if [ "$SAT_ONLY" = 1 ]; then
AFTER="$BEFORE"
say "Satellite bundle only (--satellite-only) — control plane left untouched at ${BEFORE}"
else
say "git pull origin ${BRANCH}"
as_owner "git pull origin ${BRANCH} 2>&1 | tail -6" || fail "git pull failed"
AFTER=$(as_owner "git rev-parse --short HEAD")
ok "${BEFORE} → ${AFTER}"
[ "$BEFORE" = "$AFTER" ] && printf ' (already up to date — rebuilding anyway)\n'
say "bun install --frozen-lockfile"
# Pulling package.json/bun.lock does not update an existing node_modules tree.
# Reconcile it on every deploy: Bun's no-op path is cheap, while skipping this
# can leave the freshly restarted server unable to resolve a new runtime package.
# Keep this before schema/build/restart so an install failure leaves the old
# process serving and never restarts onto an incomplete dependency tree.
as_owner "bun install --frozen-lockfile 2>&1 | tail -20" \
|| fail "dependency install failed — old service is still running; fix registry/lockfile and re-run before restarting"
ok "dependencies match bun.lock"
# The schema check has to sit HERE: after the pull (so it sees the migrations the
# incoming code needs) and before the restart (so a missing column is a warning
# on a still-healthy service instead of a 500 on a restarted one).
#
# Why this step exists at all: this repo has no migration runner, so whoever
# deploys pays for EVERYONE's unapplied migrations — `git pull` brings along
# commits that were merged to main without their DDL ever being run. On
# 2026-08-22 a knowledge-base deploy carried `tasks.method_coverage` to
# production, that column had never been created, and /api/tasks returned 500
# for four minutes. The verify step caught it — one restart too late.
say "Schema check"
as_owner "bun scripts/check-migrations.ts 2>&1 | tail -20" \
|| fail "生产库缺少迁移声明过的对象(见上)。先按提示跑 apply-migration.ts,再重跑部署 —— 此刻服务仍在跑旧代码,是安全的"
ok "schema matches drizzle/"
say "bun run build"
# Vite empties dist/ before writing, so a failure here leaves NO frontend. Bail
# before the restart and report it loudly rather than restarting onto rubble.
as_owner "bun run build 2>&1 | tail -8" || fail "build failed — dist/ may be empty; fix and re-run BEFORE restarting"
ASSETS=$(as_owner "ls control-web/dist/assets/")
ok "built: $(echo "$ASSETS" | tr '\n' ' ')"
say "systemctl restart ${UNIT}"
ssh_raw "sudo -n systemctl restart ${UNIT}" || fail "restart failed"
# 固定 `sleep 6` 换成轮询,而且轮询的是**接口真的能应答**,不只是 unit active:
# - 快的时候不白等;
# - 慢的时候(机器忙 / DB 连接慢)不会因为固定秒数不够而把一次正常启动误判成失败 ——
# 旧写法在那一档是**假阴性**,而假阴性会让人以为部署炸了,去动一个其实好好的服务;
# - active 与「能服务」不是一回事:进程起来了但还没绑上 :5173 时,紧接着的验证步骤
# 会撞上连接被拒。等到 /api/tasks 应答才算就绪,后面的断言就不会踩到这个窗口。
# 循环跑在远端 —— 一次往返,不是每轮一次握手。unit 直接 failed 就立刻退出,不等满 30 秒。
WAIT_RC=0
ssh_raw "for i in \$(seq 1 60); do
st=\$(systemctl is-active ${UNIT} 2>/dev/null)
if [ \"\$st\" = failed ]; then exit 2; fi
if [ \"\$st\" = active ] && curl -sf -m 3 -o /dev/null http://127.0.0.1:5173/api/tasks; then exit 0; fi
sleep 0.5
done; exit 1" || WAIT_RC=$?
if [ "$WAIT_RC" != 0 ]; then
ssh_raw "sudo -n journalctl -u ${UNIT} -n 30 --no-pager" >&2
[ "$WAIT_RC" = 2 ] && fail "${UNIT} entered failed state after restart"
fail "${UNIT} did not answer on :5173 within 30s after restart"
fi
ok "${UNIT} active and answering"
# ── Verify ───────────────────────────────────────────────────────────────────
# The frontend is served BY the control-server from control-web/dist, so these
# two checks together prove both tiers picked up the new build.
say "Verify"
# LC_ALL=C on BOTH sides of this comparison, not for tidiness: the devbox sorts
# in C locale and a developer Mac sorts in en_US.UTF-8, which disagree about `_`
# versus a digit (`index-D_k….css` sorts before `index-D7Z….js` on the Mac and
# after it on Linux). The public-vs-served check below compares these two lists
# as STRINGS, so the same two assets in a different order looked like a stale
# Caddy cache — a warning that fires on a healthy deploy, which is the fastest
# way to teach everyone to ignore the one that matters.
SERVED=$(ssh_raw "curl -s -m 15 http://127.0.0.1:5173/ | grep -oE 'index-[A-Za-z0-9_-]+\.(js|css)' | LC_ALL=C sort -u")
[ -n "$SERVED" ] || fail "served index.html references no assets — frontend is broken"
ok "served assets: $(echo "$SERVED" | tr '\n' ' ')"
# The hashes in index.html must be the ones we just built, else a stale index is
# being served (or the build wrote somewhere else entirely).
for a in $SERVED; do
echo "$ASSETS" | grep -qx "$a" || fail "served asset ${a} is not in the fresh build — stale index.html"
done
ok "served assets match the fresh build"
CODE=$(ssh_raw "curl -s -m 20 -o /dev/null -w '%{http_code}' http://127.0.0.1:5173/api/tasks")
[ "$CODE" = "200" ] || fail "GET /api/tasks returned ${CODE}"
ok "GET /api/tasks 200"
PUB=$(curl -s -m 30 https://streamline.bytedance.net/ 2>/dev/null | grep -oE 'index-[A-Za-z0-9_-]+\.(js|css)' | LC_ALL=C sort -u)
if [ -n "$PUB" ]; then
if [ "$PUB" = "$SERVED" ]; then ok "public entrypoint serves the new build"
else printf '\033[33m!\033[0m public entrypoint serves %s (expected %s) — check Caddy cache\n' "$(echo "$PUB" | tr '\n' ' ')" "$(echo "$SERVED" | tr '\n' ' ')"; fi
else
printf '\033[33m!\033[0m could not reach https://streamline.bytedance.net from here (need corp network)\n'
fi
ok "control plane deployed: ${BEFORE} → ${AFTER}"
fi
# ── Satellite bundle ─────────────────────────────────────────────────────────
# `shared/` and `satellite/` are compiled INTO satellite/dist/satellite.js, which
# the hot-deploy panel pushes to the executor nodes. Deploying the control plane
# leaves that artifact untouched, so the panel kept offering an older SHA and
# every node looked up to date. Rebuild it unconditionally: it costs seconds,
# touches no running service, and makes the panel's "latest" mean "what is
# actually deployed" instead of "whatever someone last built by hand".
#
# Pushing is deliberately NOT done here — see the header.
#
# Two questions, not one — two mechanisms carry code to a node:
# bundled shared/ + satellite/, plus the bin/ wrappers that
# satellite/src/entry.ts imports (bun build folds those in).
# NOT bundled the rest of bin/ + scripts/run-satellite.sh. claude_launch.ts
# resolves those from the node's OWN checkout, which hot-deploy
# never touches — the push CANNOT carry them, and the node still
# reports itself current because dist/VERSION is all it reports.
# bin/claude-pty.ts moved into the bundled column once entry.ts started importing
# it. That was the one that actually hurt: the PTY engine runs every production
# job, and a measured deploy shipped 131 unbundled lines of it under an
# affirmative "the push covered it". What is left on the node side is
# bin/claude-cursor.ts (not used in production) and run-satellite.sh (the daemon
# loop itself, which cannot bundle itself).
# NODE_FILES is therefore computed UNCONDITIONALLY: the old `shared/ satellite/`
# pathspec missed bin/ both when it was the only change (silence) and when it
# rode along with real bundle changes (an affirmative "the push covered it").
# Worked example + sync procedure: SKILL.md 「但 bundle 不是全部」.
#
# The node-side list is derived by SUBTRACTION rather than enumerated: scan all
# of bin/, filter out what entry.ts bundles. A new wrapper dropped into bin/ then
# warns by default instead of being silently missed — which is the exact failure
# this block exists to prevent. Promote a file into BUNDLED_BIN when entry.ts
# starts importing it.
# Derived from entry.ts rather than typed out, because the hand-maintained list
# drifted the moment anyone added a wrapper: bin/claude-codex.ts and bin/kb.ts
# were both bundled and both absent from it, so a deploy that touched either one
# printed "NOT carried by the push" about code that WAS carried. A warning that
# cries wolf is worse than no warning — it is the one that gets ignored on the
# day it is right (which is exactly the claude-pty story this block was written
# for). Falls back to the old literal if the grep ever comes up empty, so a
# refactor of entry.ts degrades into over-warning rather than under-warning.
BUNDLED_BIN="$(grep -o 'import("\.\./\.\./bin/[a-z-]*")' satellite/src/entry.ts \
| sed -E 's|import\("\.\./\.\./(bin/[a-z-]*)"\)|\1.ts|' | sort -u | tr '\n' ' ')"
BUNDLED_BIN="${BUNDLED_BIN:-bin/record-evidence.ts bin/conditions.ts bin/claude-pty.ts}"
SAT_BUNDLED_PATHS="shared/ satellite/ ${BUNDLED_BIN}"
NODE_SCAN_PATHS="bin/ scripts/run-satellite.sh"
# Compare against the SHA the bundle was built from, not BEFORE: a skipped
# earlier deploy may already have left satellite changes unbundled.
SAT_BASE="${BUNDLE_BEFORE:-$BEFORE}"
SAT_NEEDED=""
EQUIV_ARGS=""
# One round trip; the bundled entries are filtered off locally, same reason the
# sha256sum below is trimmed locally (as_owner embeds its argument in quotes).
# grep exits 1 when it filters everything out, hence the `|| true`.
NODE_FILES=$(as_owner "git diff --name-only ${SAT_BASE}..${AFTER} -- ${NODE_SCAN_PATHS} 2>/dev/null" \
| grep -vxF "$(printf '%s\n' ${BUNDLED_BIN})" || true)
if [ "$SKIP_SATELLITE" = 1 ]; then
say "Satellite bundle (skipped via --skip-satellite)"
warn "bundle still at ${BUNDLE_BEFORE:-none} — the panel will offer that, not ${AFTER}"
elif [ "${BUNDLE_BEFORE:-}" = "$AFTER" ]; then
# 用预检读到的值,不再问一次:从预检到这里只有 pull / 前端构建 / 重启,没有任何一步
# 会重写 satellite/dist,所以再读一次必然得到同一个答案。
say "Satellite bundle"
ok "already built at ${AFTER} — nothing to do"
else
say "bun run build:satellite"
# Does this deploy actually change BUNDLED satellite behaviour? Answering it
# here is the judgement call the operator used to have to make by hand (and
# miss). Node-checkout files are a separate question — see NODE_FILES above.
SAT_DIFF=$(as_owner "git diff --stat ${SAT_BASE}..${AFTER} -- ${SAT_BUNDLED_PATHS} 2>/dev/null | tail -1")
[ -n "$SAT_DIFF" ] && SAT_NEEDED=1
# Keep the outgoing bundle as .bak-<sha> so a bad rollout can be reverted by
# copying it back and re-pushing; keep the 3 newest so this can't grow forever.
if [ -n "${BUNDLE_BEFORE:-}" ]; then
as_owner "cp -p satellite/dist/satellite.js satellite/dist/satellite.js.bak-${BUNDLE_BEFORE} 2>/dev/null; cp -p satellite/dist/VERSION satellite/dist/VERSION.bak-${BUNDLE_BEFORE} 2>/dev/null; true" >/dev/null
PRUNED=$(as_owner "ls -t satellite/dist/satellite.js.bak-* 2>/dev/null | tail -n +4")
if [ -n "$PRUNED" ]; then
as_owner "ls -t satellite/dist/satellite.js.bak-* 2>/dev/null | tail -n +4 | xargs -r rm -f; ls -t satellite/dist/VERSION.bak-* 2>/dev/null | tail -n +4 | xargs -r rm -f; true" >/dev/null
printf ' (pruned old backups: %s)\n' "$(echo "$PRUNED" | xargs -n99 echo | sed 's#satellite/dist/satellite.js.bak-##g')"
fi
ok "previous bundle kept as satellite.js.bak-${BUNDLE_BEFORE}"
fi
as_owner "bun run build:satellite 2>&1 | tail -4" || fail "satellite bundle build failed — control plane IS deployed and live; the panel still offers ${BUNDLE_BEFORE:-none}"
BUNDLE_AFTER=$(bundle_version)
BUNDLE_SIZE=$(as_owner "stat -c %s satellite/dist/satellite.js 2>/dev/null || echo 0")
# A truncated bundle would pass the node's sha256 check (that only proves the
# bytes arrived intact) and then fail to boot, so sanity-check it right here.
[ "$BUNDLE_AFTER" = "$AFTER" ] \
|| fail "bundle stamped ${BUNDLE_AFTER} but HEAD is ${AFTER} — check for a dirty tree on the box"
[ "${BUNDLE_SIZE:-0}" -ge 100000 ] \
|| fail "bundle is only ${BUNDLE_SIZE} bytes — looks truncated, do NOT push it"
ok "bundle built at ${BUNDLE_AFTER} ($(( BUNDLE_SIZE / 1024 )) KB)"
# A bundle's VERSION is the git SHA, so a deploy that touched only the control
# plane still produces a "new" bundle — byte-for-byte identical, stamped
# differently. Pushing that drains and restarts every node to change a string.
# Compare against the bundle we just backed up: when the bytes match, tell the
# push step that nodes on the OLD version are already up to date.
if [ -n "${BUNDLE_BEFORE:-}" ]; then
# 一次往返算两个文件的哈希,拆开只是多付一次握手。
# 拆解在**本地**做:`as_owner` 把参数塞进单引号包住的 `bash -lc '...'`,远端写
# `cut -d' '` 会提前闭合那个引号,静默得到空哈希(于是永远不跳过那次无谓的换装)。
# 按文件名取而不是按行序取:bak 不存在时 sha256sum 只输出一行,靠行序会把新 bundle
# 的哈希当成旧的,两边一比"相同",于是真该推的一次被跳过。
SHAS=$(as_owner "sha256sum satellite/dist/satellite.js satellite/dist/satellite.js.bak-${BUNDLE_BEFORE} 2>/dev/null")
SHA_NEW=$(printf '%s\n' "$SHAS" | grep " satellite/dist/satellite.js$" | cut -d' ' -f1)
SHA_OLD=$(printf '%s\n' "$SHAS" | grep "\.bak-${BUNDLE_BEFORE}$" | cut -d' ' -f1)
if [ -n "$SHA_OLD" ] && [ "$SHA_NEW" = "$SHA_OLD" ]; then
EQUIV_ARGS="--equivalent-version ${BUNDLE_BEFORE}"
ok "bundle is byte-identical to ${BUNDLE_BEFORE} — nodes on it will NOT be churned"
fi
fi
fi
# ── Push to the satellites ───────────────────────────────────────────────────
# The last mile: get the bundle onto the executor nodes. Runs unconditionally
# (not just when satellite/ changed) because the script is idempotent — a node
# already on this version is skipped by version compare — and because "did
# anything satellite-side change" is exactly the judgement humans got wrong.
#
# It runs ON the box, as the owner: it needs CLUSTER_SECRET from the repo's .env
# (bun loads it from cwd) and a route to the nodes. See scripts/push-satellites.ts
# for why it goes over HTTP rather than through /api/deploy (SSO-gated) — and for
# why it waits for a fresh heartbeat first (the control plane we just restarted
# re-learns each node's queued-deploy capability from heartbeats; pushing into
# that blind window would treat every busy node as unable to park a rollout).
PUSH_RC=0
if [ "$SKIP_SATELLITE" = 1 ] || [ "$PUSH_SATELLITES" = 0 ]; then
say "Satellite push (skipped)"
warn "nodes keep their current bundle — push from 管理 → 卫星热部署 when you want it live"
else
say "Push bundle to satellites"
PUSH_ARGS="${EQUIV_ARGS}"
[ "$FORCE_PUSH" = 1 ] && PUSH_ARGS="${PUSH_ARGS} --force"
as_owner "bun scripts/push-satellites.ts ${PUSH_ARGS} 2>&1" || PUSH_RC=$?
if [ "$PUSH_RC" != 0 ]; then
warn "some nodes did not take the bundle (see above) — the control plane IS deployed and live"
fi
fi
if [ "$SAT_ONLY" = 1 ]; then say "Satellite bundle refreshed (control plane unchanged at ${BEFORE})"
else say "Deployed: ${BEFORE} → ${AFTER}"; fi
if [ "$SKIP_SATELLITE" != 1 ] && [ -n "$SAT_NEEDED" ]; then
printf 'Bundled satellite code CHANGED in this deploy (%s) — that is what the push above was for.\n' "${SAT_DIFF# }"
elif [ "$SKIP_SATELLITE" != 1 ] && [ -n "$EQUIV_ARGS" ]; then
printf 'No bundled satellite changes: the new bundle is byte-identical to %s, so nodes already on it were left alone.\n' "${BUNDLE_BEFORE}"
elif [ "$SKIP_SATELLITE" != 1 ]; then
printf 'No bundled satellite changes in this deploy — any node that still took the bundle was behind for other reasons.\n'
fi
# The bundle push above cannot ship these, no matter how it exits. Say so loudly:
# the failure mode is silent by construction — the node keeps reporting
# dist/VERSION (which IS current), so nothing downstream ever looks wrong.
if [ -n "${NODE_FILES}" ]; then
warn "NODE-CHECKOUT files changed — the bundle push does NOT carry them: $(echo "$NODE_FILES" | tr '\n' ' ')"
# State the invariant, not a recipe: how these files reach a node depends on how
# that node was provisioned, and executor-snapshot.sh strips .git by default, so
# a snapshot-restored node cannot fast-forward anything.
cat <<NODEWARN
They are read from each executor's own checkout of this repo; hot-deploy replaces
only satellite/dist/satellite.js. Until every executor has them at ${AFTER} the
change is NOT live — and the panel still shows each node as up to date, because a
node reports satellite/dist/VERSION and nothing else. A checkout with git history
can fast-forward to ${BRANCH}; one restored from executor-snapshot.sh has no .git,
so the files have to be copied in.
NODEWARN
fi
cat <<NOTE
Still not covered by this script:
* Report backfill. Prompt/report changes only affect NEWLY generated reports;
existing rows keep their old HTML until backfilled (a production write).
* Files read from each node's own checkout (${NODE_SCAN_PATHS}, minus what
entry.ts bundles). Synced by hand; this script only WARNS, and only on the
deploy whose range touches them.
NOTE
exit "$PUSH_RC"