uninstall.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Package uninstall: construcción del script que desinstala el agente
  2. // netpulse-agent de un router OpenWrt (#624). Es la operación inversa al
  3. // paquete reinstall: en lugar de instalar binario + env + init procd
  4. // (con self-heal), desinstala el agente standalone: detiene y deshabilita el
  5. // servicio, y borra binario/env/init. El watchdog cron se sigue limpiando
  6. // porque las instalaciones previas a #851 lo dejaron en el router.
  7. //
  8. // Vive en su propio paquete (como reinstall) porque httpapi importa
  9. // rearmer/reinstall y ninguno de esos paquetes puede importar a httpapi.
  10. package uninstall
  11. // Script construye el POSIX sh que se ejecuta en el router para quitar el
  12. // agente. Es idempotente: si el agente ya no está, no falla.
  13. //
  14. // Rutas canónicas (las mismas que reinstall.Script):
  15. // - INIT=/etc/init.d/netpulse-agent
  16. // - BIN=/usr/sbin/netpulse-agent
  17. // - ENV_FILE=/etc/netpulse-agent.env
  18. // - WATCHDOG=/usr/sbin/netpulse-watchdog
  19. // - cron */2 con netpulse-watchdog (desmontado por reinstall)
  20. func Script() string {
  21. return `#!/bin/sh
  22. # NetPulse agent uninstall (#624) — idempotente.
  23. INIT=/etc/init.d/netpulse-agent
  24. BIN=/usr/sbin/netpulse-agent
  25. ENV_FILE=/etc/netpulse-agent.env
  26. WATCHDOG=/usr/sbin/netpulse-watchdog
  27. # 1. Detener (si el init existe) y deshabilitar.
  28. if [ -f "$INIT" ]; then
  29. "$INIT" stop >/dev/null 2>&1 || true
  30. "$INIT" disable >/dev/null 2>&1 || true
  31. rm -f "$INIT"
  32. fi
  33. # 2. Matar cualquier proceso que quedara (self-heal puede relanzarlo).
  34. if pidof netpulse-agent >/dev/null 2>&1; then
  35. killall netpulse-agent >/dev/null 2>&1 || true
  36. fi
  37. # 3. Borrar binario, watchdog, env y artefactos temporales.
  38. rm -f "$BIN"
  39. rm -f "$WATCHDOG"
  40. rm -f "$ENV_FILE"
  41. rm -f /tmp/netpulse-agent /tmp/netpulse-agent.new /tmp/netpulse-agent.$$ /tmp/netpulse-agent.heartbeat
  42. # 4. Quitar la línea de cron del watchdog.
  43. ( crontab -l 2>/dev/null | grep -v netpulse-watchdog ) | crontab - 2>/dev/null || true
  44. /etc/init.d/cron restart >/dev/null 2>&1 || true
  45. # 5. Señal de éxito (el server solo comprueba exit 0).
  46. exit 0
  47. `
  48. }