|
@@ -703,8 +703,17 @@ func (s *server) handleAgentReinstall(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
}
|
|
|
serverURL := scheme + "://" + r.Host
|
|
serverURL := scheme + "://" + r.Host
|
|
|
|
|
|
|
|
|
|
+ // Digests sha256 de los binarios embebidos (#463): el router verifica la
|
|
|
|
|
+ // descarga antes del swap. Arch sin binario (build dev sin agentes) queda
|
|
|
|
|
+ // sin verificar en lugar de bloquear la instalación.
|
|
|
|
|
+ digests := map[string]string{
|
|
|
|
|
+ "arm64": agentbin.Digest("arm64"),
|
|
|
|
|
+ "arm": agentbin.Digest("arm"),
|
|
|
|
|
+ "amd64": agentbin.Digest("amd64"),
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// Script de instalación (replica install-agent.sh vía SSH, sin scp).
|
|
// Script de instalación (replica install-agent.sh vía SSH, sin scp).
|
|
|
- script := reinstallScript(host, slug, token, serverURL)
|
|
|
|
|
|
|
+ script := reinstallScript(slug, token, serverURL, digests)
|
|
|
|
|
|
|
|
before := time.Now()
|
|
before := time.Now()
|
|
|
var prevSeen time.Time
|
|
var prevSeen time.Time
|
|
@@ -714,7 +723,8 @@ func (s *server) handleAgentReinstall(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if _, err := s.pool.Run(host, script, 60*time.Second); err != nil {
|
|
|
|
|
|
|
+ // Descarga + verificación + swap: margen generoso para enlaces lentos.
|
|
|
|
|
+ if _, err := s.pool.Run(host, script, 300*time.Second); err != nil {
|
|
|
writeError(w, http.StatusBadGateway, "ssh_failed", err.Error())
|
|
writeError(w, http.StatusBadGateway, "ssh_failed", err.Error())
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -739,37 +749,48 @@ func (s *server) handleAgentReinstall(w http.ResponseWriter, r *http.Request) {
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// reinstallScript construye el script POSIX sh que se ejecuta en el router.
|
|
|
|
|
-func reinstallScript(host, slug, token, serverURL string) string {
|
|
|
|
|
|
|
+// reinstallScript construye el script POSIX sh que se ejecuta en el router
|
|
|
|
|
+// (#463): instala el agente completo —binario verificado por sha256, config
|
|
|
|
|
+// .env, init procd con self-heal y watchdog con su cron— de forma idempotente.
|
|
|
|
|
+// digests mapa arch→sha256 del binario embebido; un arch sin digest queda sin
|
|
|
|
|
+// verificación (build dev) en lugar de bloquear.
|
|
|
|
|
+func reinstallScript(slug, token, serverURL string, digests map[string]string) string {
|
|
|
return `#!/bin/sh
|
|
return `#!/bin/sh
|
|
|
set -e
|
|
set -e
|
|
|
INIT=/etc/init.d/netpulse-agent
|
|
INIT=/etc/init.d/netpulse-agent
|
|
|
BIN=/usr/sbin/netpulse-agent
|
|
BIN=/usr/sbin/netpulse-agent
|
|
|
ENV_FILE=/etc/netpulse-agent.env
|
|
ENV_FILE=/etc/netpulse-agent.env
|
|
|
|
|
+WATCHDOG=/usr/sbin/netpulse-watchdog
|
|
|
SERVER="` + serverURL + `"
|
|
SERVER="` + serverURL + `"
|
|
|
SLUG="` + slug + `"
|
|
SLUG="` + slug + `"
|
|
|
TOKEN="` + token + `"
|
|
TOKEN="` + token + `"
|
|
|
|
|
|
|
|
-# Detectar arquitectura del router
|
|
|
|
|
|
|
+# Detectar arquitectura del router y el digest esperado del binario embebido
|
|
|
ARCH=$(uname -m)
|
|
ARCH=$(uname -m)
|
|
|
case "$ARCH" in
|
|
case "$ARCH" in
|
|
|
- aarch64|arm64) GOARCH=arm64 ;;
|
|
|
|
|
- armv7l|armv7) GOARCH=armv7 ;;
|
|
|
|
|
|
|
+ aarch64|arm64) GOARCH=arm64; SHA256="` + digests["arm64"] + `" ;;
|
|
|
|
|
+ armv7l|armv7|armhf|arm) GOARCH=arm; SHA256="` + digests["arm"] + `" ;;
|
|
|
|
|
+ x86_64|amd64) GOARCH=amd64; SHA256="` + digests["amd64"] + `" ;;
|
|
|
*) echo "arch no soportado: $ARCH"; exit 20 ;;
|
|
*) echo "arch no soportado: $ARCH"; exit 20 ;;
|
|
|
esac
|
|
esac
|
|
|
|
|
|
|
|
# Parar servicio previo si existe (el proceso vivo mantiene el binario abierto)
|
|
# Parar servicio previo si existe (el proceso vivo mantiene el binario abierto)
|
|
|
-[ -f "$INIT" ] && $INIT stop >/dev/null 2>&1 || true
|
|
|
|
|
|
|
+[ -f "$INIT" ] && "$INIT" stop >/dev/null 2>&1 || true
|
|
|
|
|
|
|
|
# Descargar el binario del propio server (auth por token)
|
|
# Descargar el binario del propio server (auth por token)
|
|
|
if command -v curl >/dev/null 2>&1; then
|
|
if command -v curl >/dev/null 2>&1; then
|
|
|
- curl -fsSL --connect-timeout 10 -H "Authorization: Bearer $TOKEN" "$SERVER/api/agents/$SLUG/binary?arch=$GOARCH" -o /tmp/netpulse-agent.new
|
|
|
|
|
|
|
+ curl -fsSL --connect-timeout 10 -m 600 -H "Authorization: Bearer $TOKEN" "$SERVER/api/agents/$SLUG/binary?arch=$GOARCH" -o /tmp/netpulse-agent.new
|
|
|
else
|
|
else
|
|
|
- wget -q -O /tmp/netpulse-agent.new --header="Authorization: Bearer $TOKEN" "$SERVER/api/agents/$SLUG/binary?arch=$GOARCH"
|
|
|
|
|
|
|
+ wget -q -T 60 -O /tmp/netpulse-agent.new --header="Authorization: Bearer $TOKEN" "$SERVER/api/agents/$SLUG/binary?arch=$GOARCH"
|
|
|
|
|
+fi
|
|
|
|
|
+
|
|
|
|
|
+# Verificación sha256 contra el digest embebido (#463); vacío = sin verificar
|
|
|
|
|
+if [ -n "$SHA256" ]; then
|
|
|
|
|
+ GOT=$(sha256sum /tmp/netpulse-agent.new | awk '{print $1}')
|
|
|
|
|
+ [ "$GOT" = "$SHA256" ] || { echo "sha256 no coincide (esperado $SHA256, obtenido $GOT)"; exit 21; }
|
|
|
fi
|
|
fi
|
|
|
chmod 0755 /tmp/netpulse-agent.new
|
|
chmod 0755 /tmp/netpulse-agent.new
|
|
|
-mv /tmp/netpulse-agent.new "$BIN"
|
|
|
|
|
-chmod 0755 "$BIN"
|
|
|
|
|
|
|
+mv -f /tmp/netpulse-agent.new "$BIN"
|
|
|
|
|
|
|
|
# Config (chmod 600)
|
|
# Config (chmod 600)
|
|
|
cat > "$ENV_FILE" <<EOF
|
|
cat > "$ENV_FILE" <<EOF
|
|
@@ -780,30 +801,91 @@ NETPULSE_TOKEN=$TOKEN
|
|
|
EOF
|
|
EOF
|
|
|
chmod 600 "$ENV_FILE"
|
|
chmod 600 "$ENV_FILE"
|
|
|
|
|
|
|
|
-# Init script procd (respawn)
|
|
|
|
|
|
|
+# Init procd con self-heal (#457): un sysupgrade solo conserva /etc, así que
|
|
|
|
|
+# si el binario falta al arrancar se descarga del server con este env.
|
|
|
cat > "$INIT" <<'INITEOF'
|
|
cat > "$INIT" <<'INITEOF'
|
|
|
#!/bin/sh /etc/rc.common
|
|
#!/bin/sh /etc/rc.common
|
|
|
-# netpulse-agent — agente nativo NetPulse para OpenWrt
|
|
|
|
|
|
|
+# netpulse-agent — agente nativo NetPulse para OpenWrt, con self-heal.
|
|
|
|
|
+# Config en /etc/netpulse-agent.env (chmod 600); procd reinicia si cambia.
|
|
|
START=99
|
|
START=99
|
|
|
STOP=10
|
|
STOP=10
|
|
|
USE_PROCD=1
|
|
USE_PROCD=1
|
|
|
|
|
|
|
|
|
|
+ENV_FILE=/etc/netpulse-agent.env
|
|
|
BIN=/usr/sbin/netpulse-agent
|
|
BIN=/usr/sbin/netpulse-agent
|
|
|
[ -x "$BIN" ] || BIN=/tmp/netpulse-agent
|
|
[ -x "$BIN" ] || BIN=/tmp/netpulse-agent
|
|
|
|
|
|
|
|
|
|
+selfheal_binary() {
|
|
|
|
|
+ [ -x "$BIN" ] && return 0
|
|
|
|
|
+ [ -f "$ENV_FILE" ] || return 1
|
|
|
|
|
+ . "$ENV_FILE" 2>/dev/null
|
|
|
|
|
+ [ -n "${NETPULSE_SERVER:-}" ] && [ -n "${NETPULSE_TOKEN:-}" ] && [ -n "${NETPULSE_SLUG:-}" ] || return 1
|
|
|
|
|
+ case "$(uname -m)" in
|
|
|
|
|
+ aarch64|arm64) local ARCH=arm64 ;;
|
|
|
|
|
+ armv7l|armv7|armhf|arm) local ARCH=arm ;;
|
|
|
|
|
+ x86_64|amd64) local ARCH=amd64 ;;
|
|
|
|
|
+ *) return 1 ;;
|
|
|
|
|
+ esac
|
|
|
|
|
+ local url tmp
|
|
|
|
|
+ url="${NETPULSE_SERVER%/}/api/agents/${NETPULSE_SLUG}/binary?arch=${ARCH}"
|
|
|
|
|
+ logger -t netpulse-agent "self-heal: binario ausente, descargando de $NETPULSE_SERVER"
|
|
|
|
|
+ tmp=/tmp/netpulse-agent.$$
|
|
|
|
|
+ if command -v curl >/dev/null 2>&1; then
|
|
|
|
|
+ curl -fsSL -m 120 -H "Authorization: Bearer $NETPULSE_TOKEN" -o "$tmp" "$url" || return 1
|
|
|
|
|
+ else
|
|
|
|
|
+ wget -q -T 60 -O "$tmp" --header="Authorization: Bearer $NETPULSE_TOKEN" "$url" || return 1
|
|
|
|
|
+ fi
|
|
|
|
|
+ chmod 0755 "$tmp" && mv "$tmp" /usr/sbin/netpulse-agent || { rm -f "$tmp"; return 1; }
|
|
|
|
|
+ logger -t netpulse-agent "self-heal: binario restaurado"
|
|
|
|
|
+ BIN=/usr/sbin/netpulse-agent
|
|
|
|
|
+ return 0
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
start_service() {
|
|
start_service() {
|
|
|
- procd_open_instance netpulse-agent
|
|
|
|
|
- procd_set_param command "$BIN"
|
|
|
|
|
- procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
|
|
|
|
|
- procd_set_param stdout 1
|
|
|
|
|
- procd_set_param stderr 1
|
|
|
|
|
- procd_set_param file /etc/netpulse-agent.env
|
|
|
|
|
- procd_close_instance
|
|
|
|
|
|
|
+ selfheal_binary || logger -t netpulse-agent "self-heal: no se pudo restaurar el binario"
|
|
|
|
|
+ procd_open_instance netpulse-agent
|
|
|
|
|
+ procd_set_param command "$BIN"
|
|
|
|
|
+ procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
|
|
|
|
|
+ procd_set_param stdout 1
|
|
|
|
|
+ procd_set_param stderr 1
|
|
|
|
|
+ procd_set_param file /etc/netpulse-agent.env
|
|
|
|
|
+ procd_close_instance
|
|
|
}
|
|
}
|
|
|
INITEOF
|
|
INITEOF
|
|
|
chmod 0755 "$INIT"
|
|
chmod 0755 "$INIT"
|
|
|
-$INIT enable
|
|
|
|
|
-$INIT restart
|
|
|
|
|
|
|
+
|
|
|
|
|
+# Watchdog + cron (mismo criterio que el paquete; detección por pidof,
|
|
|
|
|
+# compatible con BusyBox)
|
|
|
|
|
+cat > "$WATCHDOG" <<'WGEOF'
|
|
|
|
|
+#!/bin/sh
|
|
|
|
|
+# netpulse-watchdog — relanza el agente si el proceso murió o el heartbeat
|
|
|
|
|
+# lleva >300s sin latir. Cron cada 2 min.
|
|
|
|
|
+INIT=/etc/init.d/netpulse-agent
|
|
|
|
|
+HB=/tmp/netpulse-agent.heartbeat
|
|
|
|
|
+[ -x "$INIT" ] || exit 0
|
|
|
|
|
+if ! pidof netpulse-agent >/dev/null 2>&1; then
|
|
|
|
|
+ logger -t netpulse-watchdog "agente no está en marcha, reiniciando servicio"
|
|
|
|
|
+ "$INIT" restart >/dev/null 2>&1
|
|
|
|
|
+ exit 0
|
|
|
|
|
+fi
|
|
|
|
|
+if [ -f "$HB" ]; then
|
|
|
|
|
+ now=$(date +%s)
|
|
|
|
|
+ hb=$(cat "$HB" 2>/dev/null)
|
|
|
|
|
+ case "$hb" in ''|*[!0-9]*) exit 0 ;; esac
|
|
|
|
|
+ age=$((now - hb))
|
|
|
|
|
+ if [ "$age" -gt 300 ]; then
|
|
|
|
|
+ logger -t netpulse-watchdog "proceso vivo pero sin latido en ${age}s, reiniciando servicio"
|
|
|
|
|
+ "$INIT" restart >/dev/null 2>&1
|
|
|
|
|
+ fi
|
|
|
|
|
+fi
|
|
|
|
|
+exit 0
|
|
|
|
|
+WGEOF
|
|
|
|
|
+chmod 0755 "$WATCHDOG"
|
|
|
|
|
+( crontab -l 2>/dev/null | grep -v netpulse-watchdog ; echo '*/2 * * * * /usr/sbin/netpulse-watchdog' ) | crontab -
|
|
|
|
|
+/etc/init.d/cron restart >/dev/null 2>&1 || true
|
|
|
|
|
+
|
|
|
|
|
+"$INIT" enable
|
|
|
|
|
+"$INIT" restart
|
|
|
`
|
|
`
|
|
|
}
|
|
}
|
|
|
|
|
|