Răsfoiți Sursa

fix(agent): self-heal stale watchdog cron entry after sysupgrade (#879) (#880)

A sysupgrade wipes /usr/sbin but keeps /etc, so routers that had the old watchdog cron entry were left with a line pointing at a deleted file: cron logged a missing-command error every 2 minutes until something removed it. The package defaults and the console reinstall clean it, but neither ran on a plain sysupgrade.

The agent init script now self-heals at start: if the watchdog binary is gone and a netpulse-watchdog cron entry is still there, the entry is removed. Applied to all init copies (standalone deploy file, install-agent.sh embedded copy, the server reinstall script and the OpenWrt package init). Verified functionally in sh: entry removed when the binary is missing, kept when present, clean no-op otherwise.

Closes #879
Nacho 2 luni în urmă
părinte
comite
6d9a68f411

+ 12 - 0
agent/deploy/netpulse-agent.init

@@ -54,7 +54,19 @@ selfheal_binary() {
     return 0
 }
 
+
+# (#879) self-heal: tras un sysupgrade /etc sobrevive pero el binario del
+# watchdog no: si quedó la entrada de cron del antiguo watchdog, retirarla
+# (cron loguearía un comando inexistente cada 2 min).
+cleanup_stale_watchdog_cron() {
+    [ -f /usr/sbin/netpulse-watchdog ] && return 0
+    crontab -l 2>/dev/null | grep -q netpulse-watchdog || return 0
+    ( crontab -l 2>/dev/null | grep -v netpulse-watchdog ) | crontab - 2>/dev/null || true
+    logger -t netpulse-agent "watchdog cron huérfano retirado (#879)"
+}
+
 start_service() {
+    cleanup_stale_watchdog_cron
     selfheal_binary || logger -t netpulse-agent "self-heal: no se pudo restaurar el binario"
     procd_open_instance netpulse-agent
     procd_set_param command "$BIN"

+ 12 - 0
deploy/openwrt/netpulse-agent/files/netpulse-agent.init

@@ -60,7 +60,19 @@ load_config() {
 	[ -n "$server_fp" ] && export NETPULSE_SERVER_FP="$server_fp"
 }
 
+
+# (#879) self-heal: tras un sysupgrade /etc sobrevive pero el binario del
+# watchdog no: si quedó la entrada de cron del antiguo watchdog, retirarla
+# (cron loguearía un comando inexistente cada 2 min).
+cleanup_stale_watchdog_cron() {
+	[ -f /usr/sbin/netpulse-watchdog ] && return 0
+	crontab -l 2>/dev/null | grep -q netpulse-watchdog || return 0
+	( crontab -l 2>/dev/null | grep -v netpulse-watchdog ) | crontab - 2>/dev/null || true
+	logger -t netpulse-agent "watchdog cron huérfano retirado (#879)"
+}
+
 start_service() {
+	cleanup_stale_watchdog_cron
 	load_config
 
 	# Validación mínima

+ 12 - 0
install-agent.sh

@@ -402,7 +402,19 @@ selfheal_binary() {
     return 0
 }
 
+
+# (#879) self-heal: tras un sysupgrade /etc sobrevive pero el binario del
+# watchdog no: si quedó la entrada de cron del antiguo watchdog, retirarla
+# (cron loguearía un comando inexistente cada 2 min).
+cleanup_stale_watchdog_cron() {
+    [ -f /usr/sbin/netpulse-watchdog ] && return 0
+    crontab -l 2>/dev/null | grep -q netpulse-watchdog || return 0
+    ( crontab -l 2>/dev/null | grep -v netpulse-watchdog ) | crontab - 2>/dev/null || true
+    logger -t netpulse-agent "watchdog cron huérfano retirado (#879)"
+}
+
 start_service() {
+    cleanup_stale_watchdog_cron
     selfheal_binary || logger -t netpulse-agent "self-heal: no se pudo restaurar el binario"
     procd_open_instance netpulse-agent
     procd_set_param command "$BIN"

+ 13 - 1
server-go/internal/reinstall/reinstall.go

@@ -141,7 +141,19 @@ selfheal_binary() {
 	return 0
 }
 
-start_service() {
+
+	# (#879) self-heal: tras un sysupgrade /etc sobrevive pero el binario del
+	# watchdog no: si quedó la entrada de cron del antiguo watchdog, retirarla
+	# (cron loguearía un comando inexistente cada 2 min).
+	cleanup_stale_watchdog_cron() {
+	[ -f /usr/sbin/netpulse-watchdog ] && return 0
+	crontab -l 2>/dev/null | grep -q netpulse-watchdog || return 0
+	( crontab -l 2>/dev/null | grep -v netpulse-watchdog ) | crontab - 2>/dev/null || true
+	logger -t netpulse-agent "watchdog cron huérfano retirado (#879)"
+	}
+	
+	start_service() {
+	cleanup_stale_watchdog_cron
 	selfheal_binary || logger -t netpulse-agent "self-heal: no se pudo restaurar el binario"
 	procd_open_instance netpulse-agent
 	procd_set_param command "$BIN"

+ 17 - 0
server-go/internal/reinstall/script_test.go

@@ -129,6 +129,23 @@ func TestScriptSelfHealInit(t *testing.T) {
 	}
 }
 
+// #879: el init entregado por el reinstall auto-repara la entrada de cron del
+// antiguo watchdog (tras sysupgrade /etc sobrevive pero el binario no, y cron
+// loguearía un comando inexistente cada 2 min).
+func TestScriptInitSelfHealsStaleWatchdogCron(t *testing.T) {
+	s := scriptForTest()
+	for _, want := range []string{
+		"cleanup_stale_watchdog_cron()",
+		"[ -f /usr/sbin/netpulse-watchdog ] && return 0",
+		"crontab -l 2>/dev/null | grep -q netpulse-watchdog || return 0",
+		"cleanup_stale_watchdog_cron",
+	} {
+		if !strings.Contains(s, want) {
+			t.Errorf("init sin self-heal #879: falta %q", want)
+		}
+	}
+}
+
 // #851: el watchdog cron YA NO se instala; el script limpia los restos de
 // instalaciones previas que sí lo tenían.
 func TestScriptNoWatchdogCron(t *testing.T) {