move scripts to top repo

This commit is contained in:
2026-09-20 17:03:16 +08:00
parent 91baf37f52
commit 35ef5ca78e
3 changed files with 0 additions and 491 deletions
-25
View File
@@ -1,25 +0,0 @@
# 部署配置模板 —— 复制到仓库外再填(里面有密钥,别提交进仓库)
#
# 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
-289
View File
@@ -1,289 +0,0 @@
#!/bin/bash
# init.sh — 腾讯云 Debian 12 服务器初始化
# 特性:完全幂等,可重复执行;每步独立判断,不产生重复配置。
set -euo pipefail
# ============================================================
# 全局配置
# ============================================================
BASHRC_MARKER="# ===== init.sh custom config ====="
SWAPFILE="/swapfile"
SWAP_SIZE="2G"
SWAPPINESS_FILE="/etc/sysctl.d/99-swappiness.conf"
SWAPPINESS_VALUE="10"
DOCKER_KEYRING="/etc/apt/keyrings/docker.gpg"
DOCKER_LIST="/etc/apt/sources.list.d/docker.list"
DOCKER_DAEMON_JSON="/etc/docker/daemon.json"
TENCENT_DOCKER_BASE="https://mirrors.cloud.tencent.com/docker-ce/linux/debian"
REGISTRY_MIRRORS=(
"https://mirror.ccs.tencentyun.com"
"https://docker.1ms.run"
"https://docker.m.daocloud.io"
)
# 是否自动执行 apt upgrade(1=是,0=否)。默认自动,可用环境变量覆盖:
# SKIP_UPGRADE=1 ./init.sh
SKIP_UPGRADE="${SKIP_UPGRADE:-0}"
export DEBIAN_FRONTEND=noninteractive
# ============================================================
# 日志
# ============================================================
log() { echo -e "\n\033[1;32m===== $* =====\033[0m"; }
warn() { echo -e "\033[1;33m[WARN] $*\033[0m"; }
err() { echo -e "\033[1;31m[ERROR] $*\033[0m"; }
# ============================================================
# 1. ~/.bashrc
# ============================================================
setup_bashrc() {
log "1. 配置 ~/.bashrc"
if grep -qF "$BASHRC_MARKER" ~/.bashrc 2>/dev/null; then
echo "已配置,跳过。"
return
fi
cat >> ~/.bashrc << 'EOF'
# ===== init.sh custom config =====
export LS_OPTIONS='--color=auto'
eval "$(dircolors)"
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias l='ls $LS_OPTIONS -lA'
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# ===== init.sh custom config end =====
EOF
echo "已写入 ~/.bashrc。"
}
# ============================================================
# 2. ~/.vimrc
# ============================================================
setup_vimrc() {
log "2. 配置 ~/.vimrc"
cat > ~/.vimrc << 'EOF'
set number
set smartindent
set autoindent
set shiftwidth=4
set tabstop=4
set expandtab
set noswapfile
syntax on
EOF
echo "已写入 ~/.vimrc。"
}
# ============================================================
# 3. Swap
# ============================================================
setup_swap() {
log "3. 配置 ${SWAP_SIZE} Swap"
if [ -f "$SWAPFILE" ]; then
echo "$SWAPFILE 已存在,跳过创建。"
else
echo "创建 $SWAPFILE(${SWAP_SIZE})..."
local count=$(( ${SWAP_SIZE%G} * 1024 ))
if ! fallocate -l "$SWAP_SIZE" "$SWAPFILE" 2>/dev/null; then
warn "fallocate 失败,改用 dd。"
dd if=/dev/zero of="$SWAPFILE" bs=1M count="$count" status=progress
fi
chmod 600 "$SWAPFILE"
mkswap "$SWAPFILE"
fi
if swapon --show | grep -q "^$SWAPFILE"; then
echo "Swap 已启用。"
else
swapon "$SWAPFILE"
echo "Swap 已启用。"
fi
if grep -qF "$SWAPFILE" /etc/fstab; then
echo "/etc/fstab 已包含 Swap 条目。"
else
echo "$SWAPFILE none swap sw 0 0" >> /etc/fstab
echo "已写入 /etc/fstab。"
fi
free -h
swapon --show
}
# ============================================================
# 4. vm.swappiness(独立文件,覆盖写)
# ============================================================
setup_swappiness() {
log "4. 配置 vm.swappiness=${SWAPPINESS_VALUE}"
if grep -qE '^[[:space:]]*vm\.swappiness[[:space:]]*=' /etc/sysctl.conf 2>/dev/null; then
warn "检测到 /etc/sysctl.conf 中有 vm.swappiness,注释掉以免重复。"
sed -i -E 's|^[[:space:]]*(vm\.swappiness[[:space:]]*=.*)$|# \1 # moved to '"$SWAPPINESS_FILE"' by init.sh|' /etc/sysctl.conf
fi
echo "vm.swappiness=${SWAPPINESS_VALUE}" > "$SWAPPINESS_FILE"
sysctl --system >/dev/null 2>&1 || true
echo "当前值: $(cat /proc/sys/vm/swappiness)"
}
# ============================================================
# 5. 系统包更新(幂等;可用 SKIP_UPGRADE=1 跳过)
# ============================================================
setup_system_update() {
log "5. 系统包更新"
if [ "$SKIP_UPGRADE" = "1" ]; then
echo "SKIP_UPGRADE=1,跳过。"
return
fi
echo "--- apt update ---"
apt update
echo "--- apt upgrade(非交互,保留旧配置文件)---"
apt upgrade -y \
-o Dpkg::Options::="--force-confdef" \
-o Dpkg::Options::="--force-confold"
# autoremove:无残留时 apt 会输出 0 行,天然幂等
echo "--- apt autoremove ---"
apt autoremove -y
# 清理已下载的 .deb 缓存(幂等)
apt clean
# 是否需要重启
if [ -f /var/run/reboot-required ]; then
warn "系统建议重启以应用内核/核心库更新。"
warn "请手动执行: reboot"
[ -f /var/run/reboot-required.pkgs ] && \
warn "相关包: $(tr '\n' ' ' < /var/run/reboot-required.pkgs)"
else
echo "无需重启。"
fi
}
# ============================================================
# 6. Docker 安装
# ============================================================
setup_docker() {
log "6. 安装 Docker"
# 6.1 HTTPS 相关工具(已装则跳过)
if ! dpkg -s ca-certificates curl gnupg >/dev/null 2>&1; then
apt update
apt install -y ca-certificates curl gnupg
fi
install -m 0755 -d /etc/apt/keyrings
# 6.2 GPG key
if [ -s "$DOCKER_KEYRING" ] && gpg --show-keys "$DOCKER_KEYRING" >/dev/null 2>&1; then
echo "Docker GPG key 已存在且有效,跳过下载。"
else
echo "下载 Docker GPG key(腾讯云镜像)..."
rm -f "$DOCKER_KEYRING"
if ! curl -fsSL --retry 3 --retry-delay 2 --connect-timeout 10 \
"$TENCENT_DOCKER_BASE/gpg" \
| gpg --dearmor --yes -o "$DOCKER_KEYRING"; then
err "GPG key 下载失败。请检查网络后重试。"
exit 1
fi
chmod a+r "$DOCKER_KEYRING"
echo "GPG key 已就位。"
fi
# 6.3 仓库覆盖写
local codename arch
codename=$(. /etc/os-release && echo "$VERSION_CODENAME")
arch=$(dpkg --print-architecture)
echo "deb [arch=$arch signed-by=$DOCKER_KEYRING] $TENCENT_DOCKER_BASE $codename stable" \
> "$DOCKER_LIST"
# 6.4 安装
apt update
apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
systemctl enable --now docker
echo "Docker 已安装并启用。"
}
# ============================================================
# 7. Docker 镜像加速器
# ============================================================
setup_docker_mirrors() {
log "7. 配置 Docker 镜像加速器"
mkdir -p /etc/docker
if [ -f "$DOCKER_DAEMON_JSON" ]; then
if grep -q '"registry-mirrors"' "$DOCKER_DAEMON_JSON"; then
echo "$DOCKER_DAEMON_JSON 已包含 registry-mirrors,跳过。"
return
fi
warn "$DOCKER_DAEMON_JSON 已存在但无 registry-mirrors,备份后覆盖。"
cp "$DOCKER_DAEMON_JSON" "${DOCKER_DAEMON_JSON}.bak.$(date +%s)"
fi
{
echo '{'
echo ' "registry-mirrors": ['
local last=$((${#REGISTRY_MIRRORS[@]} - 1))
local i comma
for i in "${!REGISTRY_MIRRORS[@]}"; do
comma=","
[ "$i" -eq "$last" ] && comma=""
printf ' "%s"%s\n' "${REGISTRY_MIRRORS[$i]}" "$comma"
done
echo ' ]'
echo '}'
} > "$DOCKER_DAEMON_JSON"
echo "已写入 $DOCKER_DAEMON_JSON"
systemctl daemon-reload
systemctl restart docker
echo "Docker 已重启。"
}
# ============================================================
# 8. 验证
# ============================================================
verify() {
log "8. 验证"
docker --version
echo
echo "--- Registry Mirrors ---"
docker info 2>/dev/null | grep -A6 "Registry Mirrors" || true
echo
echo "--- hello-world 测试 ---"
if docker run --rm hello-world; then
echo "Docker 验证通过。"
else
warn "hello-world 拉取失败,请检查网络或镜像源。"
fi
}
# ============================================================
# main
# ============================================================
main() {
setup_bashrc
setup_vimrc
setup_swap
setup_swappiness
setup_system_update
setup_docker
setup_docker_mirrors
verify
log "初始化完成 ✅"
}
main "$@"
-177
View File
@@ -1,177 +0,0 @@
#!/bin/bash
# init_repos.sh — 从零把 agent + mcp 拉到本地并跑起来(幂等,可重复执行)。
#
# 位置:随便放哪都能跑(常驻位置是 agent/scripts/init_repos.sh;也支持拷到别处做首次引导)。
#
# 行为:
# 1) 先确定“仓库父目录”(agent/ 与 mcp/ 的上级),顺序见下面注释;
# 然后按 REPOS 表:目标目录里有 Git 仓库就只 pull 到最新;没有才 clone。
# 有未提交改动时跳过 pull(不覆盖你的东西),直接用当前代码继续。
# 2) 读部署配置(默认 <两个仓库的父目录>/deploy.conf,可用 CONF=... 覆盖),export 给 scripts/init.sh
# 3) 调 scripts/init.sh:生成 config.yml/.env → 起 agent + mcp
#
# 用法:
# ./scripts/init_repos.sh # 配置默认在 ../deploy.conf
# DEST_ROOT=/srv/tools ./init_repos.sh # 指定仓库父目录(脚本放哪都行)
# CONF=/root/deploy.conf ./scripts/init_repos.sh # 或显式指定配置
#
# 注:本脚本自己也在被 pull 的 agent 仓库里。bash 已打开的 FD 指向旧 inode,所以“边跑边被
# git pull 更新”不会错乱:本次仍按旧内容执行,下次运行才生效。
set -euo pipefail
# ============================================================
# 路径与配置
# ============================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOME_DIR="${HOME:-/root}"
# 仓库父目录(agent/ 与 mcp/ 的上级)的确定顺序 —— 保证脚本放在哪、从哪个目录跑都对:
# 1) 显式 DEST_ROOT=...(最高优先)
# 2) 脚本随 agent 仓库走(<父目录>/agent/scripts/init_repos.sh):父目录里就有 scripts/init.sh
# 3) 当前目录或 $HOME/workspace/tools 下已有 agent/ 或 mcp/ 仓库 → 就地更新(只 pull,不另建副本)
# 4) 都没有(首次引导)→ 落到 $HOME/workspace/tools
if [ -z "${DEST_ROOT:-}" ] && [ -f "$SCRIPT_DIR/../scripts/init.sh" ]; then
DEST_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
fi
if [ -z "${DEST_ROOT:-}" ]; then
for d in "$PWD" "$HOME_DIR/workspace/tools"; do
if [ -d "$d/agent/.git" ] || [ -d "$d/mcp/.git" ]; then DEST_ROOT="$d"; break; fi
done
fi
if [ -z "${DEST_ROOT:-}" ]; then
DEST_ROOT="$HOME_DIR/workspace/tools"
echo -e "\033[1;33m[WARN]\033[0m 没找到已有仓库,按默认位置新建:$DEST_ROOT(可用 DEST_ROOT=/path 指定)"
fi
AGENT_DIR="$DEST_ROOT/agent"
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=(
"agent|https://git.foresh.com/kicer/agent.git|"
"mcp|https://git.foresh.com/kicer/mcp.git|"
)
# ============================================================
# 日志
# ============================================================
log() { echo -e "\n\033[1;32m===== $* =====\033[0m"; }
info() { echo -e "\033[1;36m[INFO]\033[0m $*"; }
warn() { echo -e "\n\033[1;33m[WARN]\033[0m $*"; }
err() { echo -e "\033[1;31m[ERROR]\033[0m $*"; }
# ============================================================
# 依赖
# ============================================================
if ! command -v git >/dev/null 2>&1; then
log "安装 git"
apt update
apt install -y git
fi
# ============================================================
# 1. 拉仓库(不存在则 clone;存在则 pull 到最新后继续)
# ============================================================
log "准备仓库(父目录:$DEST_ROOT)"
fail=0
for item in "${REPOS[@]}"; do
IFS='|' read -r name url branch <<< "$item"
dest="$DEST_ROOT/$name"
log "处理 $name"
info "URL: $url"
info "目标: $dest"
if [ -d "$dest/.git" ]; then
# 已存在:只 pull(clone 过就不再另建副本)。有未提交改动就跳过 pull,直接用当前代码继续
if ! git -C "$dest" diff --quiet || \
! git -C "$dest" diff --cached --quiet; then
warn "$name 有未提交改动,跳过 pull,直接用当前代码继续。"
else
head_before="$(git -C "$dest" rev-parse HEAD)"
if git -C "$dest" pull --ff-only --quiet; then
if [ "$head_before" = "$(git -C "$dest" rev-parse HEAD)" ]; then
info "$name 已是最新"
else
info "$name 已更新到最新"
fi
else
warn "$name pull 失败(本地可能有未推送的提交,属正常;用当前代码继续)"
fi
fi
elif [ -e "$dest" ]; then
err "$name: $dest 已存在但不是 Git 仓库,跳过。"
fail=$((fail+1))
continue
else
if git clone "$url" "$dest"; then
info "$name 克隆完成"
else
err "$name 克隆失败"
fail=$((fail+1))
continue
fi
fi
if [ -n "$branch" ]; then
cur=$(git -C "$dest" rev-parse --abbrev-ref HEAD)
if [ "$cur" != "$branch" ]; then
info "切换分支: $cur → $branch"
git -C "$dest" checkout "$branch"
git -C "$dest" pull --ff-only || true
fi
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
# ============================================================
# 2. 读部署配置 → 传给 scripts/init.sh
# ============================================================
log "读取部署配置"
if [ ! -f "$AGENT_INIT" ]; then
err "找不到 $AGENT_INIT(agent 仓库不完整?)"
exit 1
fi
if [ ! -f "$CONF" ]; then
err "找不到部署配置:$CONF"
echo
echo "从模板复制一份填上即可:"
echo " cp $AGENT_DIR/scripts/deploy.conf.example $CONF"
echo " chmod 600 $CONF && vi $CONF"
exit 1
fi
# set -a 让下面 source 进来的变量全部 export 给子脚本(子脚本只认环境变量)
# shellcheck disable=SC1090
set -a
. "$CONF"
set +a
export CONF
log "调用 $AGENT_INIT"
info "配置来源: $CONF"
"$AGENT_INIT"
if [ "$fail" -gt 0 ]; then
warn "有 $fail 个仓库处理失败(见上面日志)。"
exit 1
fi
exit 0