#!/usr/bin/env bash # 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)" # shellcheck source=scripts/deploy-lib.sh . ./scripts/deploy-lib.sh # ── docker 环境检测 / 自动安装 ─────────────────── ensure_docker(){ if command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then ok "已检测到 docker 与 docker compose"; return fi warn "未检测到 docker / docker compose" case "$(uname -s)" in Linux) if [ "$(ask '自动安装 Docker? (y/n)' y)" = y ]; then curl -fsSL https://get.docker.com | sh sudo usermod -aG docker "$USER" || true ok "Docker 安装完成(用户组变更需重新登录后免 sudo)" else die "请自行安装 docker 后重试" fi ;; Darwin) die "macOS 请安装 Docker Desktop:https://www.docker.com/products/docker-desktop/" ;; *) die "请自行安装 docker 后重试" ;; esac } # ── ① Docker 安装(支持 sandbox / standalone 双形态)─────────────── install_docker(){ local edition="${1:-sandbox}" tag="${2:-}" ensure_docker if [ "$edition" = "sandbox" ]; then preflight_sandbox fi 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" } # ── ② 本地编译运行 ────────────────────────────── install_local(){ echo "数据库安装方式:" echo " 1) 连接已有 PostgreSQL" echo " 2) 用 Docker 起一个 PostgreSQL(需要 docker)" case "$(ask '选择' 1)" in 2) ensure_docker local pw; pw="$(ask 'Postgres 密码(回车随机)' "$(rand)")" docker run -d --name artex-pg -p 5432:5432 \ -e POSTGRES_USER=artex -e POSTGRES_PASSWORD="$pw" -e POSTGRES_DB=artex \ -v artex-pg:/var/lib/postgresql/data postgres:16-alpine DB_HOST=127.0.0.1 DB_PORT=5432 DB_USER=artex DB_PASS="$pw" DB_NAME=artex DB_SSL=disable ;; *) DB_HOST="$(ask '数据库地址' 127.0.0.1)" DB_PORT="$(ask '端口' 5432)" DB_USER="$(ask '账号' artex)" DB_PASS="$(ask '密码' '')" DB_NAME="$(ask '数据库名' artex)" DB_SSL="$(ask 'sslmode (disable/require)' disable)" ;; esac cat > config.json </dev/null 2>&1 || die "未检测到 Go,请先安装 Go(>=1.26):https://go.dev/dl/" ok "Go: $(go version)" 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" info "启动…(Ctrl-C 退出)" ./start.sh } usage(){ cat <<'USAGE' 用法: ./install.sh [选项] 选项: --edition sandbox|standalone 直接指定 Docker 部署形态(sandbox=沙箱隔离版,standalone=单容器版) --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 sandbox "$OPT_TAG" ;; 2) install_docker standalone "$OPT_TAG" ;; 3) install_local ;; *) die "无效选择" ;; esac