From 295756b798924aaea35892a24c3e00028672ea03 Mon Sep 17 00:00:00 2001 From: kicer Date: Tue, 15 Sep 2026 21:33:26 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E7=BD=B2=E8=84=9A=E6=9C=AC=EF=BC=9Ain?= =?UTF-8?q?it=5Frepos.sh=20=E9=9A=8F=E4=BB=93=E5=BA=93=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=EF=BC=88=E8=B7=AF=E5=BE=84=E6=8C=89=E8=87=AA=E8=BA=AB=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E6=8E=A8=E5=AF=BC=EF=BC=89+=20deploy.conf=20=E6=94=B9?= =?UTF-8?q?=E6=88=90=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - init_repos.sh 现在位于 agent/scripts/:AGENT_DIR / DEST_ROOT 全部按脚本位置推导,不再写死 /root/workspace/tools - 仓库已存在就 pull 到最新后继续;有未提交改动则跳过 pull(不覆盖本地改动)并继续,不再计为失败 - 部署配置搜索顺序:<两仓库父目录>/deploy.conf → scripts/deploy.conf(已 gitignore)→ /root/deploy.conf;找不到时提示从模板复制 - scripts/deploy.conf(原被 git 跟踪)改为 scripts/deploy.conf.example 模板;真身 deploy.conf 进 .gitignore (原文件里 4 个 key 均为空、只有默认 admin 密码,无密钥泄漏) - init.sh: docker 缺失时提示可先跑 scripts/init_host.sh --- .gitignore | 2 + scripts/deploy.conf | 18 ------- scripts/deploy.conf.example | 25 ++++++++++ scripts/init.sh | 3 +- scripts/init_repos.sh | 97 ++++++++++++++++++++----------------- 5 files changed, 82 insertions(+), 63 deletions(-) delete mode 100644 scripts/deploy.conf create mode 100644 scripts/deploy.conf.example diff --git a/.gitignore b/.gitignore index 3e4f231..549e9cd 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ skills/*/__pycache__/ .DS_Store app/server/version.json *.apk +# 部署配置真身(含密钥,模板见 scripts/deploy.conf.example); +deploy.conf diff --git a/scripts/deploy.conf b/scripts/deploy.conf deleted file mode 100644 index e0aeefc..0000000 --- a/scripts/deploy.conf +++ /dev/null @@ -1,18 +0,0 @@ -# 模型平台 key -BIGMODEL_KEY= -DEEPSEEK_KEY= -QWEN_KEY= - -# 智谱 key:image-gen / video-gen 用 -# 可配置为与BIGMODEL_KEY一致 -ZHIPU_KEY= - -# 默认用户信息 -AUTH_USER=admin -AUTH_PASSWORD=admin - -# 本服务器公网地址,默认自动探测 -# PUBLIC_IP= -# AGENT_PORT=8090 - -# LOG_LEVEL=info diff --git a/scripts/deploy.conf.example b/scripts/deploy.conf.example new file mode 100644 index 0000000..b59380b --- /dev/null +++ b/scripts/deploy.conf.example @@ -0,0 +1,25 @@ +# 部署配置模板 —— 复制到仓库外再填(里面有密钥,别提交进仓库) +# +# cp scripts/deploy.conf.example ../../deploy.conf # 推荐:放在两个仓库的父目录 +# chmod 600 ../../deploy.conf && vi ../../deploy.conf +# +# init_repos.sh 会按顺序找第一个存在的: +# 1) <两个仓库的父目录>/deploy.conf 2) scripts/deploy.conf(已 gitignore) 3) /root/deploy.conf +# 也可以直接指定:CONF=/path/to/xxx.conf ./scripts/init_repos.sh + +# ---------- 模型平台 key(填几个启用几个,可 0~3 个)---------- +BIGMODEL_KEY= +DEEPSEEK_KEY= +QWEN_KEY= + +# ---------- 智谱 key:image-gen / video-gen 用 ---------- +# 留空 = 这两个服务照常注册但 enable: false(工具不加载,生图/图生视频用不了) +ZHIPU_KEY= + +# ---------- 登录(默认 admin/admin,记得改)---------- +AUTH_USER=admin +AUTH_PASSWORD=admin + +# ---------- 对外地址与端口 ---------- +PUBLIC_IP= # 留空则自动探测(curl ifconfig.me / api.ipify.org);探测不到则图生视频不可用 +AGENT_PORT=8090 diff --git a/scripts/init.sh b/scripts/init.sh index 796df92..18f31c7 100755 --- a/scripts/init.sh +++ b/scripts/init.sh @@ -62,7 +62,8 @@ gen_hex() { # $1 = 字节数(默认 32) } if [ "$UP" = "1" ] && { ! command -v docker >/dev/null 2>&1 || ! docker compose version >/dev/null 2>&1; }; then - err "要起服务需要 docker + docker compose;只生成配置请用: UP=0 $0" + err "要起服务需要 docker + docker compose。可先跑本仓库的 scripts/init_host.sh(宿主机初始化:装 Docker、swap 等)。" + err "只生成配置请用: UP=0 $0" exit 1 fi diff --git a/scripts/init_repos.sh b/scripts/init_repos.sh index 4a2eddc..30b7b8c 100755 --- a/scripts/init_repos.sh +++ b/scripts/init_repos.sh @@ -1,18 +1,38 @@ #!/bin/bash -# init_repos.sh — 两件事(如果你已手动 clone 了仓库,直接用 agent/scripts/init.sh 即可): -# 1) 拉取/更新仓库(幂等,可重复执行) -# 2) 读 /root/deploy.conf,把变量 export 出去,然后调用 agent/scripts/init.sh -# (后者只认环境变量,生成 config.yml/.env 与起服务都在那边;见其头注释) +# init_repos.sh — 从零把 agent + mcp 拉到本地并跑起来(幂等,可重复执行)。 # -# 用法:填好 /root/deploy.conf 后执行 ./init_repos.sh +# 位置:agent/scripts/init_repos.sh(随 agent 仓库一起走,路径全部按脚本自身位置推导) +# +# 行为: +# 1) 按下面 REPOS 表:仓库不存在就 clone;已存在就 pull 到最新。 +# 有未提交改动时跳过 pull(不覆盖你的东西),直接用当前代码继续。 +# 2) 读部署配置(默认 <两个仓库的父目录>/deploy.conf,可用 CONF=... 覆盖),export 给 scripts/init.sh +# 3) 调 scripts/init.sh:生成 config.yml/.env → 起 agent + mcp +# +# 用法: +# ./scripts/init_repos.sh # 配置默认在 ../deploy.conf +# CONF=/root/deploy.conf ./scripts/init_repos.sh # 或显式指定 +# +# 注:本脚本自己也在被 pull 的 agent 仓库里。bash 已打开的 FD 指向旧 inode,所以“边跑边被 +# git pull 更新”不会错乱:本次仍按旧内容执行,下次运行才生效。 set -euo pipefail # ============================================================ -# 配置 +# 路径与配置(按脚本位置推导:<父目录>/agent/scripts/init_repos.sh) # ============================================================ -DEST_ROOT="/root/workspace/tools" -CONF="${CONF:-/root/deploy.conf}" -AGENT_DIR="$DEST_ROOT/agent" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +AGENT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" +DEST_ROOT="$(cd "$AGENT_DIR/.." && pwd)" # 两个仓库按同级目录存放 +AGENT_INIT="$AGENT_DIR/scripts/init.sh" + +# 部署配置(含密钥,不进仓库):按顺序找第一个存在的 +# 1) <两个仓库的父目录>/deploy.conf 2) scripts/deploy.conf(已 gitignore) 3) /root/deploy.conf +if [ -z "${CONF:-}" ]; then + for c in "$DEST_ROOT/deploy.conf" "$AGENT_DIR/scripts/deploy.conf" /root/deploy.conf; do + if [ -f "$c" ]; then CONF="$c"; break; fi + done +fi +CONF="${CONF:-$DEST_ROOT/deploy.conf}" # 格式:"本地目录名|完整URL|分支"(分支留空 = 跟随远端默认分支) REPOS=( @@ -25,7 +45,7 @@ REPOS=( # ============================================================ log() { echo -e "\n\033[1;32m===== $* =====\033[0m"; } info() { echo -e "\033[1;36m[INFO]\033[0m $*"; } -warn() { echo -e "\033[1;33m[WARN]\033[0m $*"; } +warn() { echo -e "\n\033[1;33m[WARN]\033[0m $*"; } err() { echo -e "\033[1;31m[ERROR]\033[0m $*"; } # ============================================================ @@ -38,12 +58,10 @@ if ! command -v git >/dev/null 2>&1; then fi # ============================================================ -# 1. 拉仓库 +# 1. 拉仓库(不存在则 clone;存在则 pull 到最新后继续) # ============================================================ -mkdir -p "$DEST_ROOT" -cd "$DEST_ROOT" +log "准备仓库(父目录:$DEST_ROOT)" -ok=0 fail=0 for item in "${REPOS[@]}"; do @@ -55,37 +73,32 @@ for item in "${REPOS[@]}"; do info "目标: $dest" if [ -d "$dest/.git" ]; then - # 已存在 → 有未提交改动就跳过 pull(不覆盖你的东西) + # 已存在:拉到最新。有未提交改动就跳过 pull,直接用当前代码继续 if ! git -C "$dest" diff --quiet || \ ! git -C "$dest" diff --cached --quiet; then - warn "$name 有未提交改动,跳过 pull。" - fail=$((fail+1)) - continue - fi - - if git -C "$dest" pull --ff-only; then - info "$name 更新完成" - ok=$((ok+1)) + warn "$name 有未提交改动,跳过 pull,直接用当前代码继续。" + elif git -C "$dest" pull --ff-only; then + info "$name 已是最新" else - warn "$name pull 失败(本地可能已有未推送的提交,属正常;不影响后续部署)" - ok=$((ok+1)) + warn "$name pull 失败(本地可能有未推送的提交,属正常;用当前代码继续)" fi elif [ -e "$dest" ]; then err "$name: $dest 已存在但不是 Git 仓库,跳过。" fail=$((fail+1)) + continue else if git clone "$url" "$dest"; then info "$name 克隆完成" - ok=$((ok+1)) else err "$name 克隆失败" fail=$((fail+1)) + continue fi fi - if [ -n "$branch" ] && [ -d "$dest/.git" ]; then + if [ -n "$branch" ]; then cur=$(git -C "$dest" rev-parse --abbrev-ref HEAD) if [ "$cur" != "$branch" ]; then info "切换分支: $cur → $branch" @@ -94,24 +107,26 @@ for item in "${REPOS[@]}"; do fi fi - if [ -d "$dest/.git" ]; then - info "分支: $(git -C "$dest" rev-parse --abbrev-ref HEAD)" - info "Commit: $(git -C "$dest" rev-parse --short HEAD) $(git -C "$dest" log -1 --pretty=%s)" - fi + info "分支: $(git -C "$dest" rev-parse --abbrev-ref HEAD)" + info "Commit: $(git -C "$dest" rev-parse --short HEAD) $(git -C "$dest" log -1 --pretty=%s)" done -if [ ! -d "$AGENT_DIR" ]; then - err "缺 $AGENT_DIR,无法继续。" - exit 1 -fi - # ============================================================ -# 2. 读 deploy.conf → 传给 agent 的部署脚本 +# 2. 读部署配置 → 传给 scripts/init.sh # ============================================================ log "读取部署配置" +if [ ! -f "$AGENT_INIT" ]; then + err "找不到 $AGENT_INIT(agent 仓库不完整?)" + exit 1 +fi + if [ ! -f "$CONF" ]; then - err "找不到 $CONF(配置就在这个文件里,模板见仓库外说明)。" + err "找不到部署配置:$CONF" + echo + echo "从模板复制一份填上即可:" + echo " cp $AGENT_DIR/scripts/deploy.conf.example $CONF" + echo " chmod 600 $CONF && vi $CONF" exit 1 fi @@ -122,12 +137,6 @@ set -a set +a export CONF -AGENT_INIT="$AGENT_DIR/scripts/init.sh" -if [ ! -f "$AGENT_INIT" ]; then - err "找不到 $AGENT_INIT(agent 仓库可能太旧,先手动 git pull 一次)。" - exit 1 -fi - log "调用 $AGENT_INIT" info "配置来源: $CONF"