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

+97 -36
View File
@@ -1,14 +1,18 @@
#!/usr/bin/env bash
# ARTEX 安装脚本:① 全部 Docker ② 本地编译运行
# ARTEX 安装脚本:
# 1) Docker 沙箱隔离版(推荐,当前完整版:独立沙箱容器 + 全流量透明网关 + 出口代理控制)
# 2) Docker 单容器版(兼容上一版本:仅 postgres + artex,无需 docker-socket-proxy / /dev/net/tun)
# 3) 本地编译运行(Go 编译)
#
# 非交互用法示例:
# ./install.sh --edition sandbox --yes
# ./install.sh --edition standalone --tag standalone --yes
# ./install.sh --local
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}"; }
rand(){ head -c 18 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c 24; }
# shellcheck source=scripts/deploy-lib.sh
. ./scripts/deploy-lib.sh
# ── docker 环境检测 / 自动安装 ───────────────────
ensure_docker(){
@@ -30,27 +34,41 @@ ensure_docker(){
esac
}
# ── ① 全部 Docker ───────────────────────────────
# ── ① Docker 安装(支持 sandbox / standalone 双形态)───────────────
install_docker(){
local edition="${1:-sandbox}" tag="${2:-}"
ensure_docker
umask 077
if [ ! -f .env ]; then
cp .env.example .env 2>/dev/null || true
local pw key
pw="$(ask 'Postgres 密码(回车随机生成)' "$(rand)")"
key="$(ask 'ANTHROPIC_API_KEY(可留空,后续在 UI 配)' '')"
sed -i.bak "s|^POSTGRES_PASSWORD=.*|POSTGRES_PASSWORD=${pw}|" .env
sed -i.bak "s|^ANTHROPIC_API_KEY=.*|ANTHROPIC_API_KEY=${key}|" .env
rm -f .env.bak
ok "已生成 .env(POSTGRES_PASSWORD 已设置)"
else
info "沿用已存在的 .env"
if [ "$edition" = "sandbox" ]; then
preflight_sandbox
fi
info "拉取镜像并启动…"
docker compose pull
docker compose up -d
ok "启动完成 → http://localhost:8787"
info "查看日志:docker compose logs -f artex"
umask 077
local is_fresh=0
[ -f .env ] || is_fresh=1
if [ "$edition" = "sandbox" ]; then
prepare_env_for_sandbox "$tag" .env
else
prepare_env_for_standalone "$tag" .env
fi
if [ "$is_fresh" -eq 1 ] && [ "${ARTEX_NONINTERACTIVE:-0}" != "1" ]; then
local key
key="$(ask 'ANTHROPIC_API_KEY(可留空,后续在 UI 配)' '')"
[ -n "$key" ] && env_set ANTHROPIC_API_KEY "$key" .env
fi
local compose_file
compose_file="$(compose_file_for_edition "$edition")"
pull_edition_images "$edition"
info "启动 $(edition_label "$edition")(${compose_file})…"
docker compose -f "$compose_file" up -d --remove-orphans
wait_for_health "$compose_file" || warn "服务仍在启动中,请执行 docker compose -f ${compose_file} logs -f artex 观察"
ok "启动完成($(edition_label "$edition"))→ http://localhost:8787"
info "查看日志:docker compose -f ${compose_file} logs -f artex"
info "后续升级 / 切换版本形态 / 回滚:./update.sh"
}
# ── ② 本地编译运行 ──────────────────────────────
@@ -75,7 +93,6 @@ install_local(){
DB_SSL="$(ask 'sslmode (disable/require)' disable)" ;;
esac
# 生成 config.json
cat > config.json <<JSON
{
"database": {
@@ -90,11 +107,9 @@ install_local(){
JSON
ok "已生成 config.json"
# go 环境检查
command -v go >/dev/null 2>&1 || die "未检测到 Go,请先安装 Go(>=1.26):https://go.dev/dl/"
ok "Go: $(go version)"
# 内嵌前端需要 node 出静态产物
if command -v npm >/dev/null 2>&1; then
info "构建前端静态产物…"
( cd web && npm ci && npm run build:static )
@@ -108,16 +123,62 @@ JSON
ok "编译完成 → ./artex"
info "启动…(Ctrl-C 退出)"
./artex
./start.sh
}
echo "=============================="
echo " ARTEX 安装"
echo " 1) 全部 Docker 安装"
echo " 2) 本地运行(go 编译)"
echo "=============================="
usage(){
cat <<'USAGE'
用法: ./install.sh [选项]
选项:
--edition sandbox|standalone 直接指定 Docker 部署形态(sandbox=沙箱隔离版,standalone=单容器版)
--tag <TAG> 指定镜像版本 tag(如 v0.3.33 / v0.3.32 / latest / standalone)
--local 使用本地 Go 编译运行
--skip-pull 跳过镜像拉取(适用于本地已构建镜像)
-y, --yes 非交互模式(自动填充默认值)
-h, --help 显示本帮助
USAGE
}
OPT_EDITION=""
OPT_TAG=""
OPT_LOCAL=0
while [ "$#" -gt 0 ]; do
case "$1" in
--edition) shift; [ "$#" -gt 0 ] || die "--edition 需要参数"; OPT_EDITION="$1" ;;
--tag) shift; [ "$#" -gt 0 ] || die "--tag 需要参数"; OPT_TAG="$1" ;;
--local) OPT_LOCAL=1 ;;
--skip-pull) export ARTEX_SKIP_PULL=1 ;;
-y|--yes) export ARTEX_NONINTERACTIVE=1 ;;
-h|--help) usage; exit 0 ;;
*) die "未知参数:$1(使用 --help 查看帮助)" ;;
esac
shift
done
if [ "$OPT_LOCAL" -eq 1 ]; then
install_local
exit 0
fi
if [ -n "$OPT_EDITION" ]; then
case "$OPT_EDITION" in
sandbox|standalone) install_docker "$OPT_EDITION" "$OPT_TAG" ;;
*) die "无效 --edition 值:${OPT_EDITION}(仅支持 sandbox 或 standalone)" ;;
esac
exit 0
fi
echo "=========================================="
echo " ARTEX 安装部署"
echo " 1) Docker 沙箱隔离版(推荐:独立沙箱 + 全流量网关)"
echo " 2) Docker 单容器版(兼容上一版本:仅 postgres + artex)"
echo " 3) 本地运行(Go 编译)"
echo "=========================================="
case "$(ask '选择' 1)" in
1) install_docker ;;
2) install_local ;;
1) install_docker sandbox "$OPT_TAG" ;;
2) install_docker standalone "$OPT_TAG" ;;
3) install_local ;;
*) die "无效选择" ;;
esac