Files
ARTEX-chixuweihu/update.sh
T
ji ruo f5b446e3bf refactor(deploy,update): drop standalone edition and add sandbox upgrade confirmation
- Remove standalone Docker edition (Dockerfile.standalone, docker-compose.standalone.yml, release.yml standalone jobs, and dual-edition env/script branches).
- Remove deploy_edition field from /api/update/check and dual-edition UI/i18n branching in UpdateCard.
- Replace window.confirm on upgrade with AlertDialog informing users of the irreversible Standalone -> Sandbox security upgrade.
2026-12-04 15:14:06 +08:00

187 lines
6.8 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# ARTEX 升级与回滚脚本:
# - 升级 Docker 部署(保留 pgdata / ./data / ./skills,兼容旧版自动校准目录权限)
# - 回滚到任意历史部署快照(本地固定镜像 ID,离线/覆盖 latest 也可 100% 精准回滚;健康检查失败自动回滚)
# - 本地 Go 编译更新(自动备份 ./artex -> ./artex.old 支持回滚)
set -euo pipefail
cd "$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=scripts/deploy-lib.sh
. ./scripts/deploy-lib.sh
sync_repo(){
[ "${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
}
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 完成首次部署"
}
# ── ① Docker 升级(自动快照 + 失败自动回滚)─────────────────────────────
update_docker(){
local tag="${1:-}" skip_db="${2:-0}"
require_docker_env
preflight_docker
if [ -z "$tag" ] && [ "${ARTEX_NONINTERACTIVE:-0}" != "1" ]; then
tag="$(ask '目标镜像 tag(回车沿用 .env / latest;如 v0.3.33)' '')"
fi
info "正在创建升级前部署快照(用于失败自动回滚或手动回滚)…"
local snap_id
snap_id="$(create_deploy_snapshot "upgrade" "$skip_db")"
ok "已创建回滚快照:${snap_id}"
prepare_env_for_docker "$tag" .env
prepare_migrated_workspaces ./data
deploy_with_rollback "$snap_id"
ok "升级完成 → http://localhost:8787"
info "如需回滚到升级前状态:./update.sh --rollback ${snap_id}"
info "查看日志:docker compose -f ${COMPOSE_FILE} logs -f artex"
}
# ── ④ 回滚到历史部署快照 ────────────────────────────────────────────────
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 )
rm -rf server/webui/dist && cp -r web/out server/webui/dist
info "重新编译内嵌单二进制…"
CGO_ENABLED=0 go build -tags embedui -trimpath -o artex ./cmd/artex
else
warn "未检测到 npm:编译**不内嵌前端**的后端(前端需另跑 npm run dev)"
CGO_ENABLED=0 go build -o artex ./cmd/artex
fi
ok "编译完成 → ./artex"
warn "请重启正在运行的 artex 进程以生效(重启时会自动迁移 schema)"
}
usage(){
cat <<'USAGE'
用法: ./update.sh [动作] [选项]
动作(不传则进入交互菜单):
--upgrade 升级 Docker 部署(自动创建回滚快照)
--rollback [SNAPSHOT_ID] 回滚到最新或指定的历史部署快照
--list-snapshots 列出所有可用的部署回滚快照
--local 本地 Go 重新编译更新
选项:
--tag <TAG> 指定目标镜像版本 tag(如 v0.3.33 / latest)
--restore-db 配合 --rollback 同时恢复快照中的 PostgreSQL 备份
--no-db-backup 创建快照时跳过 pg_dump
--skip-pull 跳过镜像拉取
--skip-git-pull 跳过 git pull
-y, --yes 非交互模式
-h, --help 显示本帮助
USAGE
}
ACTION=""
OPT_TAG=""
OPT_SNAP=""
OPT_RESTORE_DB=0
OPT_SKIP_DB=0
while [ "$#" -gt 0 ]; do
case "$1" in
--upgrade) ACTION="upgrade" ;;
--rollback)
ACTION="rollback"
if [ "${2:-}" != "" ] && [ "${2#-}" = "${2}" ]; then
shift
OPT_SNAP="$1"
fi
;;
--list-snapshots) ACTION="list-snapshots" ;;
--local) ACTION="local" ;;
--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 "$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
echo "=========================================="
echo " ARTEX 升级与回滚"
echo " 1) Docker 升级(自动创建回滚快照)"
echo " 2) 回滚到历史部署快照"
echo " 3) 本地更新(Go 重新编译)"
echo "=========================================="
case "$(ask '选择' 1)" in
1) sync_repo; update_docker "$OPT_TAG" "$OPT_SKIP_DB" ;;
2) interactive_rollback "$OPT_SNAP" "$OPT_RESTORE_DB" ;;
3) sync_repo; update_local ;;
*) die "无效选择" ;;
esac