Files
autoClaude/deploy.sh

171 lines
5.6 KiB
Bash
Raw Permalink 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.
#!/bin/bash
# ============================================================
# autoClaude-TGbot 一键部署脚本
# 用法: chmod +x deploy.sh && sudo ./deploy.sh
# ============================================================
set -e
# --- 颜色 ---
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
info() { echo -e "${CYAN}[INFO]${NC} $1"; }
ok() { echo -e "${GREEN}[✔]${NC} $1"; }
warn() { echo -e "${YELLOW}[!]${NC} $1"; }
err() { echo -e "${RED}[✘]${NC} $1"; exit 1; }
# --- 检查 root ---
if [ "$EUID" -ne 0 ]; then
err "请使用 sudo 运行: sudo ./deploy.sh"
fi
# --- 变量 ---
APP_NAME="autoclaude-tgbot"
APP_DIR="$(cd "$(dirname "$0")" && pwd)"
SERVICE_FILE="/etc/systemd/system/${APP_NAME}.service"
RUN_USER="${SUDO_USER:-$(whoami)}"
RUN_GROUP="$(id -gn "$RUN_USER")"
echo ""
echo -e "${CYAN}╔══════════════════════════════════════════╗${NC}"
echo -e "${CYAN}║ autoClaude-TGbot 一键部署 ║${NC}"
echo -e "${CYAN}╚══════════════════════════════════════════╝${NC}"
echo ""
info "项目目录: ${APP_DIR}"
info "运行用户: ${RUN_USER}"
echo ""
# ============================================================
# 1. 安装系统依赖
# ============================================================
info "检查系统依赖..."
# 安装 uv如果不存在
if ! command -v uv &> /dev/null; then
info "安装 uv..."
curl -LsSf https://astral.sh/uv/install.sh | sh
export PATH="$HOME/.local/bin:$PATH"
ok "uv 已安装"
else
ok "uv 已存在 ($(uv --version))"
fi
# ============================================================
# 2. 安装 Python 依赖
# ============================================================
info "安装 Python 依赖..."
cd "$APP_DIR"
sudo -u "$RUN_USER" uv sync 2>/dev/null || sudo -u "$RUN_USER" uv pip install -r pyproject.toml 2>/dev/null || true
ok "依赖安装完成"
# ============================================================
# 3. 检查配置文件
# ============================================================
if [ ! -f "${APP_DIR}/config.toml" ]; then
warn "config.toml 不存在,从模板复制..."
cp "${APP_DIR}/config.toml.example" "${APP_DIR}/config.toml"
chown "$RUN_USER:$RUN_GROUP" "${APP_DIR}/config.toml"
echo ""
echo -e "${YELLOW}════════════════════════════════════════════${NC}"
echo -e "${YELLOW} ⚠️ 请编辑 config.toml 填入实际配置:${NC}"
echo -e "${YELLOW} nano ${APP_DIR}/config.toml${NC}"
echo -e "${YELLOW}════════════════════════════════════════════${NC}"
echo ""
read -p "编辑完成后按 Enter 继续,或 Ctrl+C 退出..." _
else
ok "config.toml 已存在"
fi
# ============================================================
# 4. 获取 uv 和 python 路径
# ============================================================
UV_PATH="$(sudo -u "$RUN_USER" bash -c 'which uv')"
info "uv 路径: ${UV_PATH}"
# ============================================================
# 5. 创建 systemd 服务
# ============================================================
info "创建 systemd 服务: ${APP_NAME}"
cat > "$SERVICE_FILE" <<EOF
[Unit]
Description=autoClaude Telegram Bot
After=network.target
Wants=network-online.target
[Service]
Type=simple
User=${RUN_USER}
Group=${RUN_GROUP}
WorkingDirectory=${APP_DIR}
ExecStart=${UV_PATH} run python bot.py
Restart=on-failure
RestartSec=10
StartLimitIntervalSec=60
StartLimitBurst=3
# 环境
Environment=PYTHONUNBUFFERED=1
# 日志
StandardOutput=journal
StandardError=journal
SyslogIdentifier=${APP_NAME}
# 安全加固
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=read-only
ReadWritePaths=${APP_DIR}
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
ok "服务文件已创建: ${SERVICE_FILE}"
# ============================================================
# 6. 启用并启动服务
# ============================================================
info "重载 systemd 配置..."
systemctl daemon-reload
info "启用开机自启..."
systemctl enable "$APP_NAME"
info "启动服务..."
systemctl restart "$APP_NAME"
# 等一会检查状态
sleep 2
if systemctl is-active --quiet "$APP_NAME"; then
ok "服务已启动!"
else
warn "服务启动可能失败,请检查日志"
fi
# ============================================================
# 7. 完成
# ============================================================
echo ""
echo -e "${GREEN}╔══════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ ✅ 部署完成! ║${NC}"
echo -e "${GREEN}╚══════════════════════════════════════════╝${NC}"
echo ""
echo -e " ${CYAN}常用命令:${NC}"
echo ""
echo -e " 查看状态 ${GREEN}systemctl status ${APP_NAME}${NC}"
echo -e " 查看日志 ${GREEN}journalctl -u ${APP_NAME} -f${NC}"
echo -e " 重启服务 ${GREEN}systemctl restart ${APP_NAME}${NC}"
echo -e " 停止服务 ${GREEN}systemctl stop ${APP_NAME}${NC}"
echo -e " 编辑配置 ${GREEN}nano ${APP_DIR}/config.toml${NC}"
echo ""
echo -e " ${YELLOW}修改配置后记得重启: systemctl restart ${APP_NAME}${NC}"
echo ""