1
0
forked from Eeveid/lightOps
Files
lightOps/scripts/install-agent.sh

109 lines
2.7 KiB
Bash
Executable File
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.
#!/usr/bin/env sh
set -eu
SERVER=""
TOKEN=""
BIN_URL=""
VERSION=""
KEEP_CONFIG="0"
SHA256=""
while [ "$#" -gt 0 ]; do
case "$1" in
--server) SERVER="$2"; shift 2 ;;
--token) TOKEN="$2"; shift 2 ;;
--bin-url) BIN_URL="$2"; shift 2 ;;
--version) VERSION="$2"; shift 2 ;;
--sha256) SHA256="$2"; shift 2 ;;
--keep-config) KEEP_CONFIG="1"; shift 1 ;;
*) echo "未知参数:$1" >&2; exit 1 ;;
esac
done
if [ -z "$SERVER" ] || [ -z "$TOKEN" ]; then
echo "用法install-agent.sh --server https://panel.example.com --token TOKEN [--bin-url URL]" >&2
exit 1
fi
if [ "$(id -u)" -ne 0 ]; then
echo "请使用 root 用户运行" >&2
exit 1
fi
install -d -m 0755 /etc/lightops /usr/local/bin
if command -v systemctl >/dev/null 2>&1; then
systemctl stop lightops-agent 2>/dev/null || true
fi
if [ -z "$BIN_URL" ]; then
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
SUFFIX=""
if [ -n "$VERSION" ]; then
SUFFIX="-$VERSION"
fi
BIN_URL="$SERVER/downloads/lightops-agent-$OS-$ARCH$SUFFIX"
fi
if [ -n "$BIN_URL" ]; then
TMP_BIN="$(mktemp)"
echo "正在下载 Agent$BIN_URL"
curl -fL --retry 3 --connect-timeout 10 "$BIN_URL" -o "$TMP_BIN"
if [ -n "$SHA256" ]; then
echo "$SHA256 $TMP_BIN" | sha256sum -c -
fi
chmod +x "$TMP_BIN"
if /usr/local/bin/lightops-agent --version >/dev/null 2>&1; then
cp /usr/local/bin/lightops-agent "/usr/local/bin/lightops-agent.bak.$(date +%Y%m%d%H%M%S)" || true
fi
mv "$TMP_BIN" /usr/local/bin/lightops-agent
elif [ ! -x /usr/local/bin/lightops-agent ]; then
echo "未在 /usr/local/bin/lightops-agent 找到 lightops-agent 二进制文件"
echo "请先复制二进制文件,或传入 --bin-url https://.../lightops-agent"
exit 1
fi
if [ "$KEEP_CONFIG" != "1" ] || [ ! -f /etc/lightops/agent.toml ]; then
cat >/etc/lightops/agent.toml <<EOF
server_url = "$SERVER"
token = "$TOKEN"
heartbeat_interval = 30
EOF
chmod 0600 /etc/lightops/agent.toml
fi
cat >/etc/systemd/system/lightops-agent.service <<'EOF'
[Unit]
Description=LightOps Agent
After=network-online.target
Wants=network-online.target
StartLimitIntervalSec=300
StartLimitBurst=10
[Service]
Type=simple
ExecStart=/usr/local/bin/lightops-agent --config /etc/lightops/agent.toml
Restart=always
RestartSec=5
KillSignal=SIGINT
TimeoutStopSec=20
User=root
NoNewPrivileges=false
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now lightops-agent
sleep 2
if ! systemctl is-active --quiet lightops-agent; then
echo "Agent 启动失败,最近日志如下:" >&2
journalctl -u lightops-agent -n 80 --no-pager >&2 || true
exit 1
fi
echo "LightOps Agent 已安装并运行"
systemctl status lightops-agent --no-pager