netpulse-agent.defaults 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/sh
  2. # uci-defaults: ejecutado UNA vez tras instalar el .ipk.
  3. # Migra una instalación manual previa (si existe /etc/netpulse-agent.env)
  4. # al formato UCI, y retira el watchdog cron de versiones anteriores (#851).
  5. AGENT_ENV=/etc/netpulse-agent.env
  6. AGENT_UCI=netpulse-agent
  7. # NetGrip detectado (#363): el agente EMBEBIDO en el panel ya cubre este
  8. # router. Entregarle la config migrada (solo si aún no tiene) y NO instalar
  9. # el standalone (ni servicio ni watchdog ni cron).
  10. if [ -x /usr/sbin/netgrip ] || [ -f /etc/init.d/netgrip ]; then
  11. NG_ENV=/etc/netgrip/netpulse.env
  12. if [ ! -f "$NG_ENV" ] && [ -f "$AGENT_ENV" ]; then
  13. . "$AGENT_ENV"
  14. if [ -n "$NETPULSE_SERVER" ] && [ -n "$NETPULSE_SLUG" ] && [ -n "$NETPULSE_TOKEN" ]; then
  15. mkdir -p /etc/netgrip
  16. {
  17. echo "# managed by netgrip; netpulse embedded agent config"
  18. echo "NETPULSE_SERVER=$NETPULSE_SERVER"
  19. echo "NETPULSE_SLUG=$NETPULSE_SLUG"
  20. echo "NETPULSE_TOKEN=$NETPULSE_TOKEN"
  21. echo "NETPULSE_ENABLED=1"
  22. } > "$NG_ENV"
  23. chmod 600 "$NG_ENV"
  24. logger -t netpulse-agent "NetGrip detectado: config migrada a $NG_ENV"
  25. fi
  26. fi
  27. /etc/init.d/netgrip restart >/dev/null 2>&1 || true
  28. logger -t netpulse-agent "NetGrip detectado: agente standalone NO instalado"
  29. exit 0
  30. fi
  31. # Migrar instalación manual previa (.env) a UCI
  32. if [ -f "$AGENT_ENV" ]; then
  33. . "$AGENT_ENV"
  34. [ -n "$NETPULSE_SERVER" ] && uci set "$AGENT_UCI.main.server=$NETPULSE_SERVER"
  35. [ -n "$NETPULSE_TOKEN" ] && uci set "$AGENT_UCI.main.token=$NETPULSE_TOKEN"
  36. [ -n "$NETPULSE_SLUG" ] && uci set "$AGENT_UCI.main.slug=$NETPULSE_SLUG"
  37. [ -n "$NETPULSE_INTERVAL" ] && uci set "$AGENT_UCI.main.interval=$NETPULSE_INTERVAL"
  38. [ "$NETPULSE_INSECURE_TLS" = "1" ] && uci set "$AGENT_UCI.main.insecure_tls=1"
  39. [ -n "$NETPULSE_WAN_TARGET" ] && uci set "$AGENT_UCI.main.wan_target=$NETPULSE_WAN_TARGET"
  40. [ -n "$NETPULSE_GW_TARGET" ] && uci set "$AGENT_UCI.main.gw_target=$NETPULSE_GW_TARGET"
  41. uci commit "$AGENT_UCI"
  42. fi
  43. # Watchdog cron (#851): YA NO se programa. Si venimos de una versión que lo
  44. # instalaba, limpiarlo aquí: el reinicio a ciegas de un agente en bucle de
  45. # crash no lo arregla (solo ensucia el log); la detección efectiva es el
  46. # dead-man del servidor (alerta) y el crash puntual lo cubre el respawn de
  47. # procd. El binario tras sysupgrade lo restaura el self-heal del init.
  48. rm -f /usr/sbin/netpulse-watchdog
  49. crontab -l 2>/dev/null | grep -v netpulse-watchdog | crontab - 2>/dev/null || true
  50. # Habilitar y arrancar el servicio
  51. /etc/init.d/netpulse-agent enable
  52. /etc/init.d/netpulse-agent restart
  53. # Config vacía (#462): el servicio NO arrancará hasta definir server, token
  54. # y slug. Banner claro en consola y syslog en vez de un fallo silencioso.
  55. SERVER_CFG=$(uci -q get netpulse-agent.main.server)
  56. TOKEN_CFG=$(uci -q get netpulse-agent.main.token)
  57. SLUG_CFG=$(uci -q get netpulse-agent.main.slug)
  58. if [ -z "$SERVER_CFG" ] || [ -z "$TOKEN_CFG" ] || [ -z "$SLUG_CFG" ]; then
  59. logger -t netpulse-agent "config vacía: define server, token y slug; el servicio no arrancará hasta completarla"
  60. echo ""
  61. echo "netpulse-agent: PENDIENTE DE CONFIGURAR: el servicio no arrancará hasta completarla."
  62. echo " 1. Crea el agente en el servidor NetPulse (Ajustes → Agents) y copia su token (se muestra una sola vez)."
  63. echo " 2. En este router:"
  64. echo " uci set netpulse-agent.main.server='http://<netpulse-server-ip>:3000'"
  65. echo " uci set netpulse-agent.main.slug='<slug>'"
  66. echo " uci set netpulse-agent.main.token='<token-64-hex>'"
  67. echo " uci commit netpulse-agent"
  68. echo " /etc/init.d/netpulse-agent enable && /etc/init.d/netpulse-agent restart"
  69. echo " (Si el servidor ya puede entrar por SSH a este router, usa el botón Reinstalar de la web y se configura solo.)"
  70. echo ""
  71. fi
  72. exit 0