From bdd2ce49ff58de49590180c8e9c567e3efe44de4 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Sat, 4 Jul 2026 12:35:11 +0800 Subject: [PATCH] feat: systemd service for agent daemon + one-click deploy script --- scripts/deploy-agent.sh | 47 ++++++++++++++++++++++++++++++++++++++ scripts/foms-agent.service | 21 +++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100755 scripts/deploy-agent.sh create mode 100644 scripts/foms-agent.service diff --git a/scripts/deploy-agent.sh b/scripts/deploy-agent.sh new file mode 100755 index 0000000..d686360 --- /dev/null +++ b/scripts/deploy-agent.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +# FOMS Agent 一键部署 — 在 A/B 机器上执行 +set -e + +INSTALL_DIR=${1:-/opt/foms-agent} +MANAGEMENT_URL=${2:-http://192.168.1.100:9080} +NODE_ID=${3:-} +AGENT_TOKEN=${4:-} + +if [ -z "$NODE_ID" ]; then + echo "用法: $0 <安装目录> <管理端URL> <节点ID> " + echo "示例: $0 /opt/foms-agent http://192.168.1.100:9080 node_a_001 mytoken123" + exit 1 +fi + +echo "=== FOMS Agent 部署 ===" +echo " 安装目录: $INSTALL_DIR" +echo " 管理端: $MANAGEMENT_URL" +echo " 节点ID: $NODE_ID" + +# 1. 创建目录 +mkdir -p "$INSTALL_DIR" + +# 2. 复制 agent 脚本 +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +cp "$SCRIPT_DIR/foms-agent.py" "$INSTALL_DIR/" + +# 3. 创建 .env +cat > "$INSTALL_DIR/.env" << EOF +FOMS_MANAGEMENT_URL=$MANAGEMENT_URL +FOMS_NODE_ID=$NODE_ID +FOMS_AGENT_TOKEN=$AGENT_TOKEN +FOMS_HEARTBEAT_INTERVAL=10 +EOF + +# 4. 安装 systemd 服务 +cp "$SCRIPT_DIR/foms-agent.service" /etc/systemd/system/foms-agent.service +sed -i "s|/opt/foms-agent|$INSTALL_DIR|g" /etc/systemd/system/foms-agent.service + +# 5. 启用并启动 +systemctl daemon-reload +systemctl enable foms-agent +systemctl restart foms-agent + +echo "=== 部署完成 ===" +echo " systemctl status foms-agent # 查看状态" +echo " journalctl -u foms-agent -f # 查看日志" diff --git a/scripts/foms-agent.service b/scripts/foms-agent.service new file mode 100644 index 0000000..29dca16 --- /dev/null +++ b/scripts/foms-agent.service @@ -0,0 +1,21 @@ +[Unit] +Description=FOMS Agent - Failover Management System Agent +Documentation=https://git.opencomputing.cn/yumoqing/foms +After=network.target mysqld.service + +[Service] +Type=simple +User=root +EnvironmentFile=/opt/foms-agent/.env +WorkingDirectory=/opt/foms-agent +ExecStart=/usr/bin/python3 /opt/foms-agent/foms-agent.py +Restart=always +RestartSec=10 +StandardOutput=append:/var/log/foms-agent.log +StandardError=append:/var/log/foms-agent.log +# 防止无限重启耗尽资源 +StartLimitInterval=300 +StartLimitBurst=5 + +[Install] +WantedBy=multi-user.target