[#37] 优化client构建

This commit is contained in:
2025-11-03 14:53:12 +08:00
parent 629aa88550
commit 252461c642
3 changed files with 139 additions and 5 deletions

View File

@@ -48,6 +48,31 @@ BACKUPS_DIR="$INSTALL_DIR/backups" # 备份目录
CURRENT_LINK="$INSTALL_DIR/current" # 当前版本软链接
LATEST_VERSION_FILE="$INSTALL_DIR/LATEST_VERSION" # 当前版本记录文件
# 预检查:Agent 元数据与 hostname 约束
require_agent_metadata() {
local hn
hn="$(hostname)"
local ok=false
# 三元环境变量
if [[ -n "${AGENT_ENV:-}" && -n "${AGENT_USER:-}" && -n "${AGENT_INSTANCE:-}" ]]; then
ok=true
fi
# host 形如 env-user-instance-xxx
if [[ "$hn" =~ ^[^-]+-[^-]+-[^-]+-.*$ ]]; then
ok=true
fi
if [[ "$ok" == false ]]; then
log_error "检测到 hostname 与 Agent 元数据不完整:"
log_error " 当前 hostname: $hn"
log_error " AGENT_ENV='${AGENT_ENV:-}' AGENT_USER='${AGENT_USER:-}' AGENT_INSTANCE='${AGENT_INSTANCE:-}'"
echo
log_info "请满足以下其一后重试:"
log_info " 方式A:设置 hostname 为 env-user-instance-任意,例如 dev-alice-node001-pod-0"
log_info " 方式B:导出环境变量:export AGENT_ENV=dev AGENT_USER=alice AGENT_INSTANCE=node001"
exit 1
fi
}
# 检查必需的FTP参数
check_ftp_params() {
local missing_params=()
@@ -873,6 +898,47 @@ rollback_version() {
fi
}
# 自检实现:等待 node.json 就绪且健康,并验证 last_report 持续更新
selfcheck_post_install() {
local hn="$(hostname)"
local node_file="/private/argus/agent/${AGENT_HOSTNAME:-$hn}/node.json"
local deadline=$(( $(date +%s) + 300 ))
local t1="" t2=""
while :; do
if [[ -f "$node_file" ]]; then
if command -v jq >/dev/null 2>&1; then
local ok_health lr
ok_health=$(jq -er '(.health["metric-argus-agent"].status=="healthy") and (.health["metric-node-exporter"].status=="healthy") and (.health["metric-fluent-bit"].status=="healthy") and (.health["metric-dcgm-exporter"].status=="healthy")' "$node_file" 2>/dev/null || echo false)
lr=$(jq -r '.last_report // ""' "$node_file" 2>/dev/null)
if [[ "$ok_health" == true && -n "$lr" ]]; then
if [[ -z "$t1" ]]; then
t1="$lr"
# agent 默认 60s 上报,等待 70s 再校验一次
sleep 70
continue
fi
t2="$lr"
if [[ "$t2" != "$t1" ]]; then
return 0
fi
# 若未变化,再等待一会儿直到超时
sleep 10
fi
else
# 无 jq 时的宽松校验
if grep -q '"status"\s*:\s*"healthy"' "$node_file"; then
return 0
fi
fi
fi
if (( $(date +%s) >= deadline )); then
log_error "自检超时:未在 5 分钟内确认 last_report 持续更新 或 健康状态不满足(路径:$node_file)"
return 1
fi
sleep 5
done
}
# 主函数
main() {
echo "=========================================="
@@ -912,17 +978,26 @@ main() {
# return 0
# fi
check_ftp_params
check_system
check_ftp_params
check_system
require_agent_metadata
if [[ "$ACTION" == "uninstall" ]]; then
uninstall_argus_metric
else
install_argus_metric
fi
# 安装后自检:最多等待 5 分钟,确认 node.json 存在且健康
echo
log_info "操作完成!"
log_info "开始安装后自检(最多等待 5 分钟)..."
selfcheck_post_install || {
log_error "安装后自检未通过,请查看 /var/log/argus-agent.log 以及 /opt/argus-metric/versions/*/.install.log"
exit 1
}
echo
log_success "全部自检通过,安装完成!"
}
# 脚本入口