diff --git a/scripts/agent-update.sh b/scripts/agent-update.sh index e73dc97..f0f13da 100755 --- a/scripts/agent-update.sh +++ b/scripts/agent-update.sh @@ -13,15 +13,18 @@ command -v curl >/dev/null || die "缺少 curl" tmp_dir="$(mktemp -d /tmp/nexusgate-agent-update.XXXXXX)" trap 'rm -rf -- "$tmp_dir"' EXIT -# 按已安装的程序判断实现:Go 二进制优先,其次是旧版 Node agent.js +# 按已安装的程序判断当前实现:Go 二进制优先,其次是旧版 Node agent.js if [[ -x /opt/nexusgate-agent/nexusgate-agent ]]; then - AGENT_IMPL="go" + CURRENT_IMPL="go" elif [[ -f /opt/nexusgate-agent/agent.js ]]; then - AGENT_IMPL="node" + CURRENT_IMPL="node" else die "未找到 Agent 程序(/opt/nexusgate-agent/nexusgate-agent 或 agent.js)" fi -info "当前 Agent 实现:${AGENT_IMPL}" +# 目标实现:更新默认迁移到 Go;显式 NG_AGENT_IMPL=node 时保留 Node +TARGET_IMPL="go" +[[ "${NG_AGENT_IMPL:-}" == "node" ]] && TARGET_IMPL="node" +info "当前 Agent 实现:${CURRENT_IMPL};更新目标:${TARGET_IMPL}" raw_fetch() { curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/$1" -o "$2" @@ -56,7 +59,7 @@ raw_fetch scripts/agent-logrotate.conf "$tmp_dir/logrotate.conf" raw_fetch scripts/agent-logrotate-setup.sh "$tmp_dir/logrotate-setup.sh" [[ -s "$tmp_dir/update.sh" ]] || die 'Agent 升级脚本下载不完整' -if [[ "$AGENT_IMPL" == "go" ]]; then +if [[ "$TARGET_IMPL" == "go" ]]; then fetch_agent_binary "$tmp_dir/nexusgate-agent" else raw_fetch agent/agent.js "$tmp_dir/agent.js" @@ -68,14 +71,14 @@ fi # before the running process is replaced. ( set -a; source /etc/nexusgate/agent.env; set +a - if [[ "$AGENT_IMPL" == "go" ]]; then + if [[ "$CURRENT_IMPL" == "go" ]]; then /opt/nexusgate-agent/nexusgate-agent flush-usage else node /opt/nexusgate-agent/agent.js flush-usage fi ) || die '更新前流量统计上报失败,保留原 Agent;检查控制面连接和 ng-agent doctor 后重试' -if [[ "$AGENT_IMPL" == "go" ]]; then +if [[ "$TARGET_IMPL" == "go" ]]; then install -m 0755 "$tmp_dir/nexusgate-agent" /opt/nexusgate-agent/nexusgate-agent else install -m 0644 "$tmp_dir/agent.js" /opt/nexusgate-agent/agent.js @@ -112,7 +115,7 @@ if command -v systemctl >/dev/null && [[ -d /run/systemd/system ]]; then raw_fetch systemd/nexusgate-sing-box.service "$tmp_dir/nexusgate-sing-box.service" install -m 0644 "$tmp_dir/nexusgate-agent.service" /etc/systemd/system/nexusgate-agent.service install -m 0644 "$tmp_dir/nexusgate-sing-box.service" /etc/systemd/system/nexusgate-sing-box.service - if [[ "$AGENT_IMPL" == "node" ]]; then + if [[ "$TARGET_IMPL" == "node" ]]; then # 仓库里的 unit 默认指向 Go 二进制;Node 实现改为经 run.sh 启动 sed -i 's|^ExecStart=.*|ExecStart=/opt/nexusgate-agent/run.sh|' /etc/systemd/system/nexusgate-agent.service fi @@ -125,7 +128,7 @@ elif command -v rc-service >/dev/null; then raw_fetch openrc/nexusgate-sing-box "$tmp_dir/openrc-sing-box" install -m 0755 "$tmp_dir/openrc-agent" /etc/init.d/nexusgate-agent install -m 0755 "$tmp_dir/openrc-sing-box" /etc/init.d/nexusgate-sing-box - if [[ "$AGENT_IMPL" == "node" ]]; then + if [[ "$TARGET_IMPL" == "node" ]]; then # 仓库里的 OpenRC 服务默认指向 Go 二进制;Node 实现改为经 run.sh 启动 sed -i 's|^command=.*|command="/opt/nexusgate-agent/run.sh"|' /etc/init.d/nexusgate-agent fi