#!/bin/bash # ============================================================ # AIOps 二进制部署脚本(embed 模式) # 一个二进制 = 前端 + 后端,无需 Docker/Nginx # 署名 Logan.Li # ============================================================ set -e # 颜色 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' info() { echo -e "${GREEN}[INFO]${NC} $1"; } warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } error() { echo -e "${RED}[ERROR]${NC} $1"; } # 默认路径 INSTALL_DIR="/opt/aiops" CONFIG_FILE="$INSTALL_DIR/config.yaml" SERVICE_FILE="/etc/systemd/system/aiops.service" BINARY_NAME="aiops" PID_FILE="/var/run/aiops.pid" # ============================================================ # 命令处理 # ============================================================ case "$1" in install) info "AIOps 二进制安装" echo "" if [ "$(id -u)" -ne 0 ]; then error "请使用 root 或 sudo 执行" exit 1 fi # 检查二进制 if [ ! -f "./${BINARY_NAME}" ]; then error "当前目录找不到 ./${BINARY_NAME} 二进制文件" echo " 请先在开发机执行: make build-binary" exit 1 fi # 创建安装目录 mkdir -p "$INSTALL_DIR/logs" "$INSTALL_DIR/uploads" # 复制二进制 cp "./${BINARY_NAME}" "$INSTALL_DIR/" chmod +x "$INSTALL_DIR/${BINARY_NAME}" info "二进制已安装到 $INSTALL_DIR/${BINARY_NAME}" # 生成默认配置(如果不存在) if [ ! -f "$CONFIG_FILE" ]; then cat > "$CONFIG_FILE" << 'EOF' mode: release port: "8080" log_level: info database: driver: mysql host: 127.0.0.1 port: 3306 user: aiops password: aiops_pass_2026 dbname: aiops charset: utf8mb4 max_conn: 100 max_idle: 10 redis: driver: redis host: 127.0.0.1 port: 6379 password: "" db: 0 jwt: secret: ezops-aiops-jwt-secret-2026 expire_hour: 24 EOF info "默认配置已生成: $CONFIG_FILE" warn "请根据实际环境修改数据库/Redis 配置" fi # 创建 systemd 服务 cat > "$SERVICE_FILE" << EOF [Unit] Description=AIOps Platform (Embed Mode) After=network-online.target mysql.service redis.service Wants=network-online.target [Service] Type=simple WorkingDirectory=${INSTALL_DIR} ExecStart=${INSTALL_DIR}/${BINARY_NAME} Restart=always RestartSec=5 LimitNOFILE=65536 Environment=GIN_MODE=release [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable aiops info "systemd 服务已创建并启用开机自启" echo "" info "安装完成!" echo "" echo " 启动: systemctl start aiops" echo " 状态: systemctl status aiops" echo " 日志: journalctl -u aiops -f" echo " 配置: vim $CONFIG_FILE" echo " 访问: http://:8080" echo "" ;; start) systemctl start aiops info "AIOps 已启动" ;; stop) systemctl stop aiops info "AIOps 已停止" ;; restart) systemctl restart aiops info "AIOps 已重启" ;; status) systemctl status aiops --no-pager ;; uninstall) warn "即将卸载 AIOps(保留数据/配置)" systemctl stop aiops 2>/dev/null systemctl disable aiops rm -f "$SERVICE_FILE" rm -f "$INSTALL_DIR/${BINARY_NAME}" systemctl daemon-reload info "已卸载(配置/日志/数据保留在 $INSTALL_DIR)" ;; *) echo "AIOps 二进制部署工具" echo "" echo "用法: $0 {install|start|stop|restart|status|uninstall}" echo "" echo " install - 安装二进制 + 生成配置 + 创建 systemd 服务" echo " start - 启动服务" echo " stop - 停止服务" echo " restart - 重启服务" echo " status - 查看服务状态" echo " uninstall - 卸载二进制(保留数据)" echo "" exit 1 ;; esac