Files
aiops/agent/deploy/install.sh
T

228 lines
6.7 KiB
Bash

#!/bin/bash
# AIOps Agent 一键安装脚本 — 支持 Linux(systemd) 和 macOS(launchd)
# 用法: curl -fsSL http://YOUR_SERVER/agent/install.sh | sudo bash -s -- --server http://YOUR_SERVER:8080 --token TOKEN
# 署名 Logan.Li
set -e
# 默认参数
AIOPS_SERVER=""
AGENT_TOKEN=""
INSTALL_DIR="/usr/local/bin"
SERVICE_NAME="aiops-agent"
PLIST_LABEL="com.ezops.aiops-agent"
ARCH=$(uname -m)
KERNEL=$(uname -s) # Linux 或 Darwin
# 检测 OS 显示名
detect_os() {
if [ "$KERNEL" = "Darwin" ]; then
echo "$(sw_vers -productName 2>/dev/null || echo 'macOS') $(sw_vers -productVersion 2>/dev/null || echo '')"
elif [ -f /etc/os-release ]; then
. /etc/os-release 2>/dev/null && echo "${PRETTY_NAME:-${NAME:-Linux}}" || echo "Linux"
else
echo "Linux"
fi
}
OS_DISPLAY=$(detect_os)
# OS 小写 (linux / darwin)
OS_LOWER=$(echo "$KERNEL" | tr '[:upper:]' '[:lower:]')
# 解析命令行参数
while [[ $# -gt 0 ]]; do
case $1 in
--server) AIOPS_SERVER="$2"; shift 2;;
--token) AGENT_TOKEN="$2"; shift 2;;
--version) AGENT_VERSION="$2"; shift 2;;
*) echo "未知参数: $1"; exit 1;;
esac
done
AGENT_VERSION="${AGENT_VERSION:-v1.0.0}"
echo ""
echo " ╔══════════════════════════════════════════╗"
echo " ║ AIOps Agent 一键安装 ║"
echo " ╚══════════════════════════════════════════╝"
echo ""
echo " 系统: ${OS_DISPLAY} (${ARCH})"
echo " 内核: ${KERNEL}"
echo " 版本: ${AGENT_VERSION}"
echo " 后端: ${AIOPS_SERVER:-未指定}"
echo ""
# 检查 root 权限
if [ "$(id -u)" -ne 0 ]; then
echo "❌ 请使用 root 或 sudo 执行此脚本"
echo " 推荐用法: curl -fsSL <url> | sudo bash -s -- --server <url> --token <token>"
echo " (sudo 放在 bash 前,不是 curl 前)"
exit 1
fi
# 确定下载架构
case "$ARCH" in
x86_64|amd64) DOWNLOAD_ARCH="amd64";;
aarch64|arm64) DOWNLOAD_ARCH="arm64";;
*)
echo "❌ 不支持的 CPU 架构: $ARCH (仅支持 x86_64/amd64 和 aarch64/arm64)"
exit 1
;;
esac
echo " 匹配: ${OS_LOWER}/${DOWNLOAD_ARCH}"
echo ""
# 安装依赖工具
if command -v curl &> /dev/null; then
echo "✓ curl 已安装"
else
echo "→ 安装 curl..."
if command -v yum &> /dev/null; then
yum install -y curl
elif command -v dnf &> /dev/null; then
dnf install -y curl
elif command -v apt-get &> /dev/null; then
apt-get update -qq && apt-get install -y curl
elif command -v brew &> /dev/null; then
brew install curl
else
echo "❌ 无法确定包管理器,请手动安装 curl"
exit 1
fi
fi
# 下载二进制
if [ -z "$AIOPS_SERVER" ]; then
echo "❌ 请通过 --server 指定 AIOps 后端地址"
exit 1
fi
BINARY_URL="${AIOPS_SERVER}/agent/aiops-agent-${OS_LOWER}-${DOWNLOAD_ARCH}"
echo "→ 下载 Agent: ${BINARY_URL}"
curl -fSL -o /tmp/aiops-agent "${BINARY_URL}" || {
echo "❌ 下载失败: ${BINARY_URL}"
echo " 请检查: 1) 后端地址是否正确 2) 该平台是否有对应二进制"
if [ "$OS_LOWER" = "darwin" ]; then
echo " 提示: macOS 二进制需要后端编译 darwin 版本 (make build-darwin-arm64)"
fi
exit 1
}
chmod +x /tmp/aiops-agent
mv /tmp/aiops-agent "${INSTALL_DIR}/aiops-agent"
echo "✓ 安装到 ${INSTALL_DIR}/aiops-agent"
if [ -z "$AGENT_TOKEN" ]; then
echo "⚠ 未提供 --token,使用空令牌"
AGENT_TOKEN="none"
fi
# ─── 平台分支: Linux(systemd) / macOS(launchd) ───
if [ "$OS_LOWER" = "darwin" ]; then
# ===== macOS: launchd 服务 =====
PLIST_PATH="/Library/LaunchDaemons/${PLIST_LABEL}.plist"
mkdir -p "$(dirname "$PLIST_PATH")"
cat > "$PLIST_PATH" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>${PLIST_LABEL}</string>
<key>ProgramArguments</key>
<array>
<string>${INSTALL_DIR}/aiops-agent</string>
<string>-server</string>
<string>${AIOPS_SERVER}</string>
<string>-interval</string>
<string>10</string>
<string>-token</string>
<string>${AGENT_TOKEN}</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/var/log/aiops-agent.log</string>
<key>StandardErrorPath</key>
<string>/var/log/aiops-agent.err.log</string>
</dict>
</plist>
EOF
echo "✓ 创建 launchd 服务 ($PLIST_PATH)"
# 停止旧实例(如果存在)
launchctl unload "$PLIST_PATH" 2>/dev/null || true
# 加载并启动
launchctl load -w "$PLIST_PATH"
sleep 2
# 检查运行状态
if launchctl list | grep -q "$PLIST_LABEL"; then
echo ""
echo "✅ 安装成功!Agent 已启动并设置为开机自启"
echo ""
echo " 查看状态: launchctl list | grep aiops"
echo " 查看日志: tail -f /var/log/aiops-agent.log"
echo " 手动停止: launchctl unload $PLIST_PATH"
echo " 手动启动: launchctl load -w $PLIST_PATH"
echo " 卸载: launchctl unload $PLIST_PATH && rm -f ${INSTALL_DIR}/aiops-agent $PLIST_PATH"
echo ""
else
echo "❌ Agent 启动失败,请检查日志:"
echo " cat /var/log/aiops-agent.err.log"
exit 1
fi
else
# ===== Linux: systemd 服务 =====
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
cat > "$SERVICE_FILE" << EOF
[Unit]
Description=AIOps Monitoring Agent
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=${INSTALL_DIR}/aiops-agent -server ${AIOPS_SERVER} -interval 10 -token ${AGENT_TOKEN}
Restart=always
RestartSec=5
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
echo "✓ 创建 systemd 服务"
# 启动服务
systemctl daemon-reload
systemctl enable ${SERVICE_NAME}
systemctl restart ${SERVICE_NAME}
sleep 2
if systemctl is-active --quiet ${SERVICE_NAME}; then
echo ""
echo "✅ 安装成功!Agent 已启动并设置为开机自启"
echo ""
echo " 查看状态: systemctl status ${SERVICE_NAME}"
echo " 查看日志: journalctl -u ${SERVICE_NAME} -f"
echo " 卸载: systemctl stop ${SERVICE_NAME} && rm -f ${INSTALL_DIR}/aiops-agent ${SERVICE_FILE} && systemctl daemon-reload"
echo ""
else
echo "❌ Agent 启动失败,请检查日志:"
echo " journalctl -u ${SERVICE_NAME} -n 20"
exit 1
fi
fi