实现 LightOps 运维面板基础功能
This commit is contained in:
81
scripts/upgrade-agent.sh
Executable file
81
scripts/upgrade-agent.sh
Executable file
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env sh
|
||||
set -eu
|
||||
|
||||
SERVER=""
|
||||
BIN_URL=""
|
||||
VERSION=""
|
||||
SHA256=""
|
||||
|
||||
while [ "$#" -gt 0 ]; do
|
||||
case "$1" in
|
||||
--server) SERVER="$2"; shift 2 ;;
|
||||
--bin-url) BIN_URL="$2"; shift 2 ;;
|
||||
--version) VERSION="$2"; shift 2 ;;
|
||||
--sha256) SHA256="$2"; shift 2 ;;
|
||||
*) echo "未知参数:$1" >&2; exit 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
echo "请使用 root 用户运行" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "$BIN_URL" ]; then
|
||||
if [ -z "$SERVER" ]; then
|
||||
if [ -f /etc/lightops/agent.toml ]; then
|
||||
SERVER="$(awk -F= '/server_url/ { gsub(/[ "]/, "", $2); print $2; exit }' /etc/lightops/agent.toml)"
|
||||
fi
|
||||
fi
|
||||
if [ -z "$SERVER" ]; then
|
||||
echo "缺少 --server 或 --bin-url" >&2
|
||||
exit 1
|
||||
fi
|
||||
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
|
||||
|
||||
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"
|
||||
"$TMP_BIN" --version >/dev/null
|
||||
|
||||
systemctl stop lightops-agent 2>/dev/null || true
|
||||
if [ -x /usr/local/bin/lightops-agent ]; then
|
||||
BACKUP="/usr/local/bin/lightops-agent.bak.$(date +%Y%m%d%H%M%S)"
|
||||
cp /usr/local/bin/lightops-agent "$BACKUP"
|
||||
else
|
||||
BACKUP=""
|
||||
fi
|
||||
mv "$TMP_BIN" /usr/local/bin/lightops-agent
|
||||
|
||||
if ! systemctl start lightops-agent; then
|
||||
echo "新版本启动失败,正在回滚" >&2
|
||||
if [ -n "$BACKUP" ]; then
|
||||
cp "$BACKUP" /usr/local/bin/lightops-agent
|
||||
systemctl start lightops-agent || true
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
if ! systemctl is-active --quiet lightops-agent; then
|
||||
echo "新版本未保持运行,正在回滚" >&2
|
||||
if [ -n "$BACKUP" ]; then
|
||||
cp "$BACKUP" /usr/local/bin/lightops-agent
|
||||
systemctl restart lightops-agent || true
|
||||
fi
|
||||
journalctl -u lightops-agent -n 80 --no-pager >&2 || true
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "LightOps Agent 已升级"
|
||||
Reference in New Issue
Block a user