feat(deploy,gateway): add dual-edition deployment/migration/rollback and TUN route reconciliation

- Add standalone edition (`Dockerfile.standalone`, `docker-compose.standalone.yml`) alongside sandbox edition and publish both in `.github/workflows/release.yml`.
- Implement `scripts/deploy-lib.sh`, `install.sh`, `update.sh`, and `scripts/artex-sandbox-firewall.sh --cleanup` with edition detection, legacy standalone -> sandbox migration (symlink-safe workspace permissions), automatic snapshots with pinned `artex-rollback/*` images + PG dump, auto/manual cross-edition rollback, and post-rollback upstream unpinning.
- Update `ensureWritableDir` in `internal/sandbox/manager.go` to safely migrate nested root-owned workspace files without following symlinks.
- Expose `deploy_edition` in `/api/update/check` and update `UpdateCard` UI/i18n warnings for sandbox vs standalone in-app update/rollback.
- Reconcile `artex-gateway` TUN policy routing (`priority 218`, `raw PREROUTING`, `nat POSTROUTING`, `DOCKER-USER`) every 15s to self-heal after host network rule flushes or bridge changes.
- Add unit and shell tests (`scripts/deploy_test.sh`, `cmd/artex-gateway/tun_linux_test.go`, `internal/sandbox/sandbox_test.go`, `server/update_test.go`) and update `README.md` / `.env.example`.
This commit is contained in:
ji ruo committed 2026-12-02 13:31:05 +08:00
1 parent d786de90af
commit 07309e4530
23 files changed
+2107 -144

No files matched your search

+251 -44
View File
@@ -1,61 +1,170 @@
#!/usr/bin/env bash
# ARTEX 更新脚本:① Docker 更新(拉新镜像重建) ② 本地编译更新(重建二进制)
# 与 install.sh 对应:install 负责首次落地,update 负责升级到新版本。
# DB 迁移无需手动执行——artex 每次启动都会幂等重跑 schema.sql(含 ADD COLUMN/CREATE
# INDEX IF NOT EXISTS),所以“重启即迁移”。数据(pgdata 卷、./data、./skills)不受影响。
# ARTEX 升级、双版本迁移与回滚脚本:
# - 升级当前部署形态(继续使用单容器版 standalone,或升级沙箱隔离版 sandbox)
# - 从上一版本单容器部署无缝迁移至当前沙箱隔离版(保留 pgdata / ./data / ./skills)
# - 从沙箱隔离版切回单容器版(优雅停止沙箱与透明网关,清理 TUN/iptables 规则)
# - 回滚到任意历史部署快照(本地固定镜像 ID,离线/覆盖 latest 也可 100% 精准回滚;健康检查失败自动回滚)
# - 本地 Go 编译更新(自动备份 ./artex -> ./artex.old 支持回滚)
set -euo pipefail
cd "$(cd "$(dirname "$0")" && pwd)"
info(){ printf '\033[36m[*]\033[0m %s\n' "$*"; }
ok(){ printf '\033[32m[+]\033[0m %s\n' "$*"; }
warn(){ printf '\033[33m[!]\033[0m %s\n' "$*"; }
die(){ printf '\033[31m[x]\033[0m %s\n' "$*" >&2; exit 1; }
ask(){ local p="$1" d="${2:-}" a; read -rp "$p${d:+ [$d]}: " a; echo "${a:-$d}"; }
# shellcheck source=scripts/deploy-lib.sh
. ./scripts/deploy-lib.sh
# ── 可选:同步仓库到最新代码(compose/脚本/本地编译源码都靠它更新)───────
sync_repo(){
[ -d .git ] && command -v git >/dev/null 2>&1 || { warn "非 git 工作副本,跳过 git pull"; return; }
[ "$(ask '拉取最新代码 (git pull --ff-only)? (y/n)' y)" = y ] || return
[ "${ARTEX_SKIP_GIT_PULL:-0}" = "1" ] && return 0
[ -d .git ] && command -v git >/dev/null 2>&1 || { warn "非 git 工作副本,跳过 git pull"; return 0; }
[ "$(ask '拉取最新代码 (git pull --ff-only)? (y/n)' y)" = y ] || return 0
if ! git pull --ff-only; then
warn "git pull 未能快进(本地有改动或分支分叉)——请手动处理后重试,本次沿用当前代码"
fi
}
# ── ① Docker 更新 ───────────────────────────────
update_docker(){
require_docker_env(){
command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1 \
|| die "未检测到 docker / docker compose,请先用 ./install.sh 安装部署"
[ -f .env ] || die "未找到 .env,请先运行 ./install.sh 完成首次部署"
# 可选:升级到指定版本 tag(不填则沿用 .env 中的 ARTEX_TAG,缺省为 latest)
local tag; tag="$(ask '目标镜像 tag(回车沿用 .env / latest)' '')"
if [ -n "$tag" ]; then
if grep -q '^ARTEX_TAG=' .env; then
sed -i.bak "s|^ARTEX_TAG=.*|ARTEX_TAG=${tag}|" .env && rm -f .env.bak
else
printf '\nARTEX_TAG=%s\n' "$tag" >> .env
fi
ok "已将 ARTEX_TAG 设为 ${tag}"
fi
# 只动 artex:postgres 是固定的 16-alpine,不需要跟着升级(拉它纯属浪费带宽,
# 且大版本变动还会有兼容风险)。artex 声明了 depends_on postgres,所以带服务名
# up 时若 pg 没起会自动拉起,已在跑的则原样保留、不重建。
info "拉取新镜像(仅 artex)…"
docker compose pull artex
info "重建并启动(artex 重启时自动迁移 schema)…"
docker compose up -d artex
ok "更新完成 → http://localhost:8787"
info "查看日志:docker compose logs -f artex"
info "清理旧镜像(可选):docker image prune -f"
}
# ── ② 本地编译更新 ──────────────────────────────
# ── ① 原地升级当前形态(保持 standalone 或 sandbox)──────────────────────
update_docker_in_place(){
local target_ed="${1:-}" tag="${2:-}" skip_db="${3:-0}"
require_docker_env
local cur_ed
cur_ed="$(detect_current_edition .env)"
if [ -z "$target_ed" ]; then
if [ "$cur_ed" = "sandbox" ] || [ "$cur_ed" = "standalone" ]; then
target_ed="$cur_ed"
else
target_ed="sandbox"
fi
fi
info "当前部署形态:$(edition_label "$cur_ed") → 原地升级:$(edition_label "$target_ed")"
if [ -z "$tag" ] && [ "${ARTEX_NONINTERACTIVE:-0}" != "1" ]; then
if [ "$target_ed" = "standalone" ]; then
tag="$(ask '目标镜像 tag(回车沿用 .env;如 standalone / v0.3.33 / v0.3.32)' '')"
else
tag="$(ask '目标镜像 tag(回车沿用 .env / latest;如 v0.3.33)' '')"
fi
fi
info "正在创建升级前部署快照(用于失败自动回滚或手动回滚)…"
local snap_id
snap_id="$(create_deploy_snapshot "upgrade-${target_ed}" "$skip_db")"
ok "已创建回滚快照:${snap_id}"
if [ "$target_ed" = "sandbox" ]; then
prepare_env_for_sandbox "$tag" .env
else
prepare_env_for_standalone "$tag" .env
fi
deploy_edition_with_rollback "$target_ed" "$snap_id"
local compose_file
compose_file="$(compose_file_for_edition "$target_ed")"
ok "升级完成($(edition_label "$target_ed"))→ http://localhost:8787"
info "如需回滚到升级前状态:./update.sh --rollback ${snap_id}"
info "查看日志:docker compose -f ${compose_file} logs -f artex"
}
# ── ② 从单容器版迁移至沙箱隔离版(Standalone -> Sandbox)────────────────
migrate_to_sandbox(){
local tag="${1:-}" skip_db="${2:-0}"
require_docker_env
preflight_sandbox
local cur_ed
cur_ed="$(detect_current_edition .env)"
if [ "$cur_ed" = "sandbox" ]; then
info "当前已是沙箱隔离版,将直接执行沙箱版配置校准与升级…"
else
info "准备从 $(edition_label "$cur_ed") 迁移至 $(edition_label sandbox)(pgdata / ./data / ./skills 全量保留)"
fi
if [ -z "$tag" ] && [ "${ARTEX_NONINTERACTIVE:-0}" != "1" ]; then
tag="$(ask '目标沙箱版镜像 tag(回车使用 latest)' '')"
fi
info "正在创建迁移前完整快照(含 .env / 当前镜像 ID / PostgreSQL 备份)…"
local snap_id
snap_id="$(create_deploy_snapshot "migrate-to-sandbox" "$skip_db")"
ok "已创建回滚快照:${snap_id}"
prepare_env_for_sandbox "$tag" .env
prepare_migrated_workspaces ./data
deploy_edition_with_rollback sandbox "$snap_id"
ok "已成功迁移至沙箱隔离版 → http://localhost:8787"
info "查看主服务日志:docker compose logs -f artex"
info "查看透明网关日志:docker compose logs -f artex-gateway"
info "若需无损回滚回单容器版:./update.sh --rollback ${snap_id}"
}
# ── ③ 从沙箱隔离版切回单容器版(Sandbox -> Standalone)──────────────────
switch_to_standalone(){
local tag="${1:-}" skip_db="${2:-0}"
require_docker_env
if [ -z "$tag" ] && [ "${ARTEX_NONINTERACTIVE:-0}" != "1" ]; then
tag="$(ask '目标单容器版镜像 tag(回车使用 standalone;回退历史版本可填如 v0.3.32)' '')"
fi
info "正在创建切换前部署快照…"
local snap_id
snap_id="$(create_deploy_snapshot "switch-to-standalone" "$skip_db")"
ok "已创建回滚快照:${snap_id}"
stop_sandbox_runtime
prepare_env_for_standalone "$tag" .env
deploy_edition_with_rollback standalone "$snap_id"
ok "已切换至单容器版(standalone)→ http://localhost:8787"
info "查看日志:docker compose -f ${COMPOSE_STANDALONE_FILE} logs -f artex"
info "若需恢复回沙箱隔离版:./update.sh --rollback ${snap_id}"
}
# ── ④ 回滚到历史部署快照 ────────────────────────────────────────────────
interactive_rollback(){
local snap_id="${1:-}" restore_db="${2:-0}"
require_docker_env
if [ -z "$snap_id" ]; then
echo "可用的部署回滚快照:"
list_deploy_snapshots
local latest=""
[ -f "${DEPLOY_STATE_DIR}/latest" ] && latest="$(cat "${DEPLOY_STATE_DIR}/latest")"
[ -n "$latest" ] || die "当前没有任何可回滚的部署快照"
snap_id="$(ask '要回滚的快照 ID(回车选最新)' "$latest")"
fi
local snap_dir="${DEPLOY_SNAP_DIR}/${snap_id}"
[ -f "${snap_dir}/meta.env" ] || die "快照不存在:${snap_id}"
if [ "$restore_db" != "1" ] && [ "$(env_get HAS_PG_DUMP "${snap_dir}/meta.env")" = "1" ] && [ "${ARTEX_NONINTERACTIVE:-0}" != "1" ]; then
warn "当前版本与上一版本数据库结构前向/后向兼容,通常无需覆盖数据库。"
if [ "$(ask '是否同时用快照中的 postgres.sql.gz 覆盖恢复数据库? (y/n)' n)" = "y" ]; then
restore_db=1
fi
fi
rollback_to_snapshot "$snap_id" "$restore_db"
}
# ── ⑤ 本地编译更新 ──────────────────────────────────────────────────────
update_local(){
command -v go >/dev/null 2>&1 || die "未检测到 Go(>=1.26):https://go.dev/dl/"
[ -f config.json ] || warn "未找到 config.json——若首次部署请改用 ./install.sh"
ok "Go: $(go version)"
if [ -x ./artex ]; then
cp ./artex ./artex.old
ok "已备份当前二进制 → ./artex.old(如需回滚可执行 mv ./artex.old ./artex 后重启)"
fi
if command -v npm >/dev/null 2>&1; then
info "重建前端静态产物…"
( cd web && npm ci && npm run build:static )
@@ -70,13 +179,111 @@ update_local(){
warn "请重启正在运行的 artex 进程以生效(重启时会自动迁移 schema)"
}
echo "=============================="
echo " ARTEX 更新"
echo " 1) Docker 更新(拉新镜像重建)"
echo " 2) 本地更新(go 重新编译)"
echo "=============================="
usage(){
cat <<'USAGE'
用法: ./update.sh [动作] [选项]
动作(不传则进入交互菜单):
--upgrade 升级当前部署形态(保持 standalone 或 sandbox 不变)
--migrate-sandbox 从上一版本单容器部署迁移至当前沙箱隔离版(保留全部数据)
--switch-standalone 从沙箱隔离版切换回单容器版(自动清理沙箱与透明网关规则)
--rollback [SNAPSHOT_ID] 回滚到最新或指定的历史部署快照
--list-snapshots 列出所有可用的部署回滚快照
--local 本地 Go 重新编译更新
选项:
--edition sandbox|standalone 配合 --upgrade 指定升级目标形态
--tag <TAG> 指定目标镜像版本 tag(如 v0.3.33 / v0.3.32 / latest / standalone)
--restore-db 配合 --rollback 同时恢复快照中的 PostgreSQL 备份
--no-db-backup 创建快照时跳过 pg_dump
--skip-pull 跳过镜像拉取
--skip-git-pull 跳过 git pull
-y, --yes 非交互模式
-h, --help 显示本帮助
USAGE
}
ACTION=""
OPT_EDITION=""
OPT_TAG=""
OPT_SNAP=""
OPT_RESTORE_DB=0
OPT_SKIP_DB=0
while [ "$#" -gt 0 ]; do
case "$1" in
--upgrade) ACTION="upgrade" ;;
--migrate-sandbox) ACTION="migrate-sandbox" ;;
--switch-standalone) ACTION="switch-standalone" ;;
--rollback)
ACTION="rollback"
if [ "${2:-}" != "" ] && [ "${2#-}" = "${2}" ]; then
shift
OPT_SNAP="$1"
fi
;;
--list-snapshots) ACTION="list-snapshots" ;;
--local) ACTION="local" ;;
--edition) shift; [ "$#" -gt 0 ] || die "--edition 需要参数"; OPT_EDITION="$1" ;;
--tag) shift; [ "$#" -gt 0 ] || die "--tag 需要参数"; OPT_TAG="$1" ;;
--restore-db) OPT_RESTORE_DB=1 ;;
--no-db-backup) OPT_SKIP_DB=1 ;;
--skip-pull) export ARTEX_SKIP_PULL=1 ;;
--skip-git-pull) export ARTEX_SKIP_GIT_PULL=1 ;;
-y|--yes) export ARTEX_NONINTERACTIVE=1 ;;
-h|--help) usage; exit 0 ;;
*) die "未知参数:$1(使用 --help 查看帮助)" ;;
esac
shift
done
case "$ACTION" in
upgrade)
sync_repo
update_docker_in_place "$OPT_EDITION" "$OPT_TAG" "$OPT_SKIP_DB"
exit 0
;;
migrate-sandbox)
sync_repo
migrate_to_sandbox "$OPT_TAG" "$OPT_SKIP_DB"
exit 0
;;
switch-standalone)
sync_repo
switch_to_standalone "$OPT_TAG" "$OPT_SKIP_DB"
exit 0
;;
rollback)
interactive_rollback "$OPT_SNAP" "$OPT_RESTORE_DB"
exit 0
;;
list-snapshots)
list_deploy_snapshots
exit 0
;;
local)
sync_repo
update_local
exit 0
;;
esac
CUR_ED="$(detect_current_edition .env)"
echo "=========================================="
echo " ARTEX 升级 / 迁移 / 回滚"
echo " 当前检测到形态:$(edition_label "$CUR_ED")"
echo "------------------------------------------"
echo " 1) 升级当前版本(保持 $(edition_label "$CUR_ED"))"
echo " 2) 从单容器版迁移至沙箱隔离版(保留数据 + 自动快照)"
echo " 3) 从沙箱隔离版切换回单容器版(清理网关 + 自动快照)"
echo " 4) 回滚到上一部署快照(支持跨形态无损回滚)"
echo " 5) 本地更新(Go 重新编译)"
echo "=========================================="
case "$(ask '选择' 1)" in
1) sync_repo; update_docker ;;
2) sync_repo; update_local ;;
1) sync_repo; update_docker_in_place "$OPT_EDITION" "$OPT_TAG" "$OPT_SKIP_DB" ;;
2) sync_repo; migrate_to_sandbox "$OPT_TAG" "$OPT_SKIP_DB" ;;
3) sync_repo; switch_to_standalone "$OPT_TAG" "$OPT_SKIP_DB" ;;
4) interactive_rollback "$OPT_SNAP" "$OPT_RESTORE_DB" ;;
5) sync_repo; update_local ;;
*) die "无效选择" ;;
esac