ソースを参照

feat(routers): asuswrt type with AP role and agent-driven lifecycle (#1481) (#1498)

Nacho 1 ヶ月 前
親
コミット
4cd5322967

+ 2 - 1
app/src/components/routers/AgentBadge.tsx

@@ -19,7 +19,8 @@ interface AgentBadgeProps {
 
 function isOpenWrtDevice(t?: string): boolean {
   if (!t) return true
-  return t === 'glinet' || t === 'openwrt'
+  // #1481: asuswrt lleva agente nativo como openwrt/glinet.
+  return t === 'glinet' || t === 'openwrt' || t === 'asuswrt'
 }
 
 /**

+ 9 - 4
app/src/components/routers/AgentsSection.tsx

@@ -39,7 +39,9 @@ import { cn, copyToClipboard } from '@/lib/utils'
 
 function isOpenWrtType(t: string | undefined): boolean {
   if (!t) return true
-  return t === 'glinet' || t === 'openwrt'
+  // #1481: asuswrt es agent-driven como openwrt/glinet (badge, tabla de
+  // agentes y botón Actualizar); lo que NO tiene es SSH para install/reinstall.
+  return t === 'glinet' || t === 'openwrt' || t === 'asuswrt'
 }
 
 // copyText: alias del helper compartido con fallback para orígenes no
@@ -83,16 +85,19 @@ function AgentRow({ agent, router, nowSec }: { agent?: AgentInfo; router: Router
   // #483: sin agente = TOD router nativo OpenWrt sin agente (antes solo los
   // agent-only): la fila ofrece Instalar, que registra y despliega.
   const isMissing = agent === undefined
-  const canRecover = isOpenWrt && (isStale || isMissing) && auth?.role === 'admin'
+  // #1481: en asuswrt no hay SSH/procd: ni install (copiar comando) ni
+  // reinstall ni uninstall; el agente se actualiza con el botón Actualizar.
+  const isAsuswrt = router?.type === 'asuswrt'
+  const canRecover = isOpenWrt && !isAsuswrt && (isStale || isMissing) && auth?.role === 'admin'
   // #443: reinstall disponible SIEMPRE para admins en agentes nativos OpenWrt
   // (sanos o caídos): antes solo aparecía cuando estaba stale o faltaba, y el
   // duplicado vivía en la tarjeta Info del detalle.
-  const canReinstall = auth?.role === 'admin' && isOpenWrt && (agent !== undefined || isMissing)
+  const canReinstall = auth?.role === 'admin' && isOpenWrt && !isAsuswrt && (agent !== undefined || isMissing)
   // #624: desinstalar el agente vía SSH (liberar espacio en routers con poco
   // room, p. ej. UniFi 6 Lite). Solo agentes nativos OpenWrt con agente
   // presente: no tiene sentido desinstalar algo que no está, ni un NetGrip
   // (que se gestiona desde su propio panel) ni un scraper externo.
-  const canUninstall = auth?.role === 'admin' && isOpenWrt && agent !== undefined && agent.kind !== 'external' && agent.kind !== 'netgrip'
+  const canUninstall = auth?.role === 'admin' && isOpenWrt && !isAsuswrt && agent !== undefined && agent.kind !== 'external' && agent.kind !== 'netgrip'
   // #1382: retirar el agente de la flota (revoca token + olvida estado) solo
   // se ofrece con el agente SIN pulso (equipo apagado o quitado de la red):
   // retirar uno vivo deja un proceso empujando contra un token revocado.

+ 1 - 1
app/src/components/routers/FleetCard.tsx

@@ -92,7 +92,7 @@ export function FleetCard({ router, index = 0, refreshKey = 0 }: FleetCardProps)
   const inStartupGrace = (serverUptimeSec ?? Number.MAX_SAFE_INTEGER) < STARTUP_GRACE_SEC
   const extras = isDemo ? getRouterExtras(router.id) : EMPTY_EXTRAS
   const warn = router.status === 'warn'
-  const isOpenWrt = !router.type || router.type === 'glinet' || router.type === 'openwrt'
+  const isOpenWrt = !router.type || router.type === 'glinet' || router.type === 'openwrt' || router.type === 'asuswrt'
   const agentDown = isOpenWrt && agent !== undefined && !agent.fresh
   const agentMissing = isOpenWrt && agent === undefined && router.agentOnly
   const reduce = useReducedMotion()

+ 3 - 1
app/src/pages/Settings.tsx

@@ -357,7 +357,7 @@ function Confetti({ burstKey, reduce }: { burstKey: number; reduce: boolean }) {
 // Gestión de routers (modo live): CRUD contra /api/config/routers
 // ---------------------------------------------------------------------------
 
-type RouterType = 'glinet' | 'openwrt' | 'routeros' | 'managed-switch' | 'external'
+type RouterType = 'glinet' | 'openwrt' | 'routeros' | 'managed-switch' | 'external' | 'asuswrt'
 
 interface ConfigRouter {
   id: string
@@ -994,6 +994,7 @@ function RoutersManager({ reduce, onSaved }: { reduce: boolean; onSaved: () => v
               { value: 'openwrt', label: 'OpenWrt' },
               { value: 'glinet', label: 'GL.iNet' },
               { value: 'routeros', label: 'RouterOS' },
+              { value: 'asuswrt', label: 'Asuswrt' },
               { value: 'managed-switch', label: t('settings.routers.typeManaged') },
               { value: 'external', label: t('settings.routers.typeExternal') },
             ]}
@@ -1164,6 +1165,7 @@ function RoutersManager({ reduce, onSaved }: { reduce: boolean; onSaved: () => v
                     { value: 'openwrt', label: 'OpenWrt' },
                     { value: 'glinet', label: 'GL.iNet' },
                     { value: 'routeros', label: 'RouterOS' },
+              { value: 'asuswrt', label: 'Asuswrt' },
                     { value: 'managed-switch', label: t('settings.routers.typeManaged') },
                     { value: 'external', label: t('settings.routers.typeExternal') },
                   ]}

+ 5 - 2
server-go/internal/adapters/live.go

@@ -707,8 +707,9 @@ func (l *Live) SetRouters(list []RouterConfig) {
 	l.clients = map[string]*OpenWrtClient{}
 	for _, c := range l.routers {
 		// RouterOS no habla SSH/ubus: se sondea por su REST API (routeros_live.go),
-		// sin cliente SSH.
-		if !c.AgentOnly && c.Type != "routeros" {
+		// sin cliente SSH. Asuswrt tampoco se sondea por SSH (no hay ubus): su
+		// única fuente es el agente (#1481); sin agente la unidad cae a offline.
+		if !c.AgentOnly && c.Type != "routeros" && c.Type != "asuswrt" {
 			l.clients[c.ID] = NewOpenWrtClient(c, l.pool, "root", "")
 		}
 	}
@@ -1691,6 +1692,8 @@ func (l *Live) offlineRouter(cfg RouterConfig) Router {
 		switch cfg.Type {
 		case "glinet":
 			model = "GL.iNet"
+		case "asuswrt":
+			model = "Asuswrt"
 		case "routeros":
 			model = "RouterOS"
 		case "managed-switch":

+ 26 - 2
server-go/internal/httpapi/agent.go

@@ -447,7 +447,8 @@ func (s *server) rotateTokenHot(slug, token string) string {
 			break
 		}
 	}
-	if host == "" || !s.routerUpgradeable(slug) {
+	// #1481: asuswrt no tiene SSH para rotar el token en caliente.
+	if host == "" || !s.routerUpgradeable(slug) || s.routerTypeOf(slug) == "asuswrt" {
 		return "no_router"
 	}
 	if _, err := s.pool.Run(host, reinstall.TokenPushScript(slug, token), 60*time.Second); err != nil {
@@ -576,7 +577,22 @@ type agentUpgradeInfo struct {
 // actualizable; los de type managed-switch/external NO (son scrapers que no
 // usan el agente nativo y no entienden el comando "upgrade").
 func agentUpgradeable(typ string) bool {
-	return typ == "" || typ == "openwrt" || typ == "glinet"
+	// #1481: asuswrt se actualiza con el comando "upgrade" del agente (baja el
+	// binario y se lo cambia él mismo, sin procd); el REINSTALL por SSH sigue
+	// prohibido (handleAgentReinstall lo excluye aparte).
+	return typ == "" || typ == "openwrt" || typ == "glinet" || typ == "asuswrt"
+}
+
+// routerTypeOf: tipo de la fila routers para el slug ("" si no existe).
+func (s *server) routerTypeOf(slug string) string {
+	if s.db == nil {
+		return ""
+	}
+	var typ string
+	if err := s.db.QueryRow("SELECT type FROM routers WHERE id = ?", slug).Scan(&typ); err != nil {
+		return ""
+	}
+	return typ
 }
 
 // routerUpgradeable: variante por slug que consulta la tabla routers. NO usar
@@ -874,6 +890,14 @@ func (s *server) handleAgentReinstall(w http.ResponseWriter, r *http.Request) {
 			"este dispositivo no usa el agente nativo OpenWrt (scraper); reinstálalo con su propio mecanismo")
 		return
 	}
+	// #1481: asuswrt no tiene SSH/procd para un reinstall por el servidor; el
+	// agente se actualiza con el comando "upgrade" (o copiando el binario a
+	// mano), no reinstalando.
+	if s.routerTypeOf(slug) == "asuswrt" {
+		writeError(w, http.StatusConflict, "no_reinstall_asuswrt",
+			"en Asuswrt el agente se actualiza con el botón Actualizar (o copiando el binario); el reinstall por SSH no aplica")
+		return
+	}
 
 	// #1204: el env del agente lleva la sonda de latencia ACTIVA (WAN en el
 	// gateway, ping al gateway en los APs): el server resuelve el rol desde

+ 4 - 4
server-go/internal/httpapi/config.go

@@ -198,8 +198,8 @@ func (s *server) handleAddConfigRouter(w http.ResponseWriter, r *http.Request) {
 	if typ == "" {
 		typ = "openwrt"
 	}
-	if typ != "glinet" && typ != "openwrt" && typ != "routeros" && typ != "managed-switch" && typ != "external" {
-		writeError(w, http.StatusBadRequest, "invalid_input", "Invalid enum value. Expected 'glinet' | 'openwrt' | 'routeros' | 'managed-switch' | 'external'")
+	if typ != "glinet" && typ != "openwrt" && typ != "routeros" && typ != "managed-switch" && typ != "external" && typ != "asuswrt" {
+		writeError(w, http.StatusBadRequest, "invalid_input", "Invalid enum value. Expected 'glinet' | 'openwrt' | 'routeros' | 'managed-switch' | 'external' | 'asuswrt'")
 		return
 	}
 	var firmwareTarget string
@@ -350,8 +350,8 @@ func (s *server) handleUpdateConfigRouter(w http.ResponseWriter, r *http.Request
 	var typ *string
 	if in.Type != "" {
 		t := in.Type
-		if t != "glinet" && t != "openwrt" && t != "routeros" && t != "managed-switch" && t != "external" {
-			writeError(w, http.StatusBadRequest, "invalid_input", "Invalid enum value. Expected 'glinet' | 'openwrt' | 'routeros' | 'managed-switch' | 'external'")
+		if t != "glinet" && t != "openwrt" && t != "routeros" && t != "managed-switch" && t != "external" && t != "asuswrt" {
+			writeError(w, http.StatusBadRequest, "invalid_input", "Invalid enum value. Expected 'glinet' | 'openwrt' | 'routeros' | 'managed-switch' | 'external' | 'asuswrt'")
 			return
 		}
 		typ = &t

+ 7 - 0
server-go/internal/httpapi/config_test.go

@@ -106,6 +106,13 @@ func TestConfigRoutersCRUD(t *testing.T) {
 		t.Fatalf("type inválido: %d", res.StatusCode)
 	}
 
+	// #1481: asuswrt es un type válido (unidad agent-only sin SSH/procd).
+	res = doReq(t, "POST", srv.URL+"/api/config/routers", cookie,
+		`{"name":"GT-BE98 Pro","host":"192.168.8.30","type":"asuswrt","agent_only":true}`)
+	if res.StatusCode != 201 {
+		t.Fatalf("type asuswrt: %d, want 201", res.StatusCode)
+	}
+
 	// POST JSON inválido → 400 invalid_json
 	res = doReq(t, "POST", srv.URL+"/api/config/routers", cookie, `{`)
 	if res.StatusCode != 400 {