Просмотр исходного кода

fix(ssh): report the router's stderr when a reinstall fails (#1737) (#1851)

Nacho 1 месяц назад
Родитель
Сommit
da172bbeca
3 измененных файлов с 60 добавлено и 6 удалено
  1. 1 0
      CHANGELOG.md
  2. 42 6
      server-go/internal/adapters/sshpool.go
  3. 17 0
      server-go/internal/adapters/sshpool_test.go

+ 1 - 0
CHANGELOG.md

@@ -42,6 +42,7 @@ y este proyecto se adhiere a [Versionado Semántico](https://semver.org/lang/es/
 
 ### Fixed
 
+- An SSH reinstall that fails now reports the last line the router printed on stderr, not just "Process exited with status 1", so the reason (a failing curl, a full filesystem, a missing command) reaches the operator (#1737).
 - Addresses that come and go within a minute are no longer clients (#1818). Randomised MACs, phones that probe once and mesh units that bounce an association all reached the device table, the counters and the new-client alert within a single poll. A MAC the database has never seen now has to stay online for a minute before it is stored, counted or alerted, and when it finally lands the row keeps the moment it was first seen instead of the moment it was promoted. The wait is kept in memory on purpose: it is a judgement about the current poll cycle, so a restart costs one extra minute at most, and a device that already exists in the table is never held back. Without a database the check steps aside and the old behaviour returns.
 - The neighbour scan survives a radio whose AP interface refuses to scan (#1839). On Asuswrt and other Broadcom firmwares every radio exposes an AP interface and a managed one, the AP one answers `Operation not supported (-95)` to a scan, and the picker preferred AP, so those units reported no neighbours at all while the managed interface on the same radio would have answered. The agent keeps the AP first and, when the scan fails, retries on another interface of the same radio within the same cycle, at most twice, which leaves the uplink alone on units where AP works and fixes the ones where it does not. Each radio also reports the outcome of its scan, the interface that answered and the reason when none did, so a refusal and an empty band no longer look the same on the server.
 - The channel plan stops moving a wide radio to the other half of the channel it already uses (#1844). The current block was matched by equality, so a 40 or 80 MHz radio on a channel that is not the block primary matched no block, kept the "this channel cannot be recommended" flag and fell into the DFS path, which suggests a channel without looking at the neighbour count. A radio on 48 at 80 MHz got 36 with every score at zero, and 36 to 48 is the same block, so the advice was not even a different channel. The block is now found by coverage and the plan will not suggest the primary of the block the radio is already on. The 2.4 GHz overlap rule and the DFS rule keep moving a radio without measurements, because there the move is the point.

+ 42 - 6
server-go/internal/adapters/sshpool.go

@@ -360,6 +360,30 @@ func (p *SSHPool) drop(host string) {
 	}
 }
 
+// sshErrDetail (#1737) saca el motivo real de un comando remoto fallido.
+// session.Output descarta el stderr salvo que se le de un destino, y antes se
+// tiraba: un reinstall fallido solo decia "Process exited with status 1" y el
+// motivo (curl, wget, uci) se quedaba en el router. Se devuelve la ULTIMA
+// linea no vacia y recortada, que es la que explica el fallo sin arrastrar el
+// ruido del script entero.
+func sshErrDetail(stderr string) string {
+	if stderr == "" {
+		return ""
+	}
+	lines := strings.Split(strings.TrimRight(stderr, "\n"), "\n")
+	for i := len(lines) - 1; i >= 0; i-- {
+		line := strings.TrimSpace(lines[i])
+		if line == "" {
+			continue
+		}
+		if len(line) > 300 {
+			line = line[:300] + "..."
+		}
+		return line
+	}
+	return ""
+}
+
 // Run ejecuta cmd en host con timeout (por defecto 5 s, como el JS).
 // Una sesión fallida por canal roto invalida la conexión (reconexión).
 func (p *SSHPool) Run(host, cmd string, timeout time.Duration) (string, error) {
@@ -378,13 +402,16 @@ func (p *SSHPool) Run(host, cmd string, timeout time.Duration) (string, error) {
 	defer session.Close()
 
 	type result struct {
-		out []byte
-		err error
+		out    []byte
+		err    error
+		stderr string
 	}
 	ch := make(chan result, 1)
 	go func() {
+		var errBuf strings.Builder
+		session.Stderr = &errBuf
 		out, err := session.Output(cmd)
-		ch <- result{out, err}
+		ch <- result{out, err, errBuf.String()}
 	}()
 	select {
 	case res := <-ch:
@@ -393,6 +420,9 @@ func (p *SSHPool) Run(host, cmd string, timeout time.Duration) (string, error) {
 			if !errors.As(res.err, &ee) {
 				p.drop(host) // canal roto, no exit status
 			}
+			if msg := sshErrDetail(res.stderr); msg != "" {
+				return "", fmt.Errorf("ssh %s: %w: %s", host, res.err, msg)
+			}
 			return "", fmt.Errorf("ssh %s: %w", host, res.err)
 		}
 		return string(res.out), nil
@@ -422,13 +452,16 @@ func (p *SSHPool) RunCtx(ctx context.Context, host, cmd string, timeout time.Dur
 	defer session.Close()
 
 	type result struct {
-		out []byte
-		err error
+		out    []byte
+		err    error
+		stderr string
 	}
 	ch := make(chan result, 1)
 	go func() {
+		var errBuf strings.Builder
+		session.Stderr = &errBuf
 		out, err := session.Output(cmd)
-		ch <- result{out, err}
+		ch <- result{out, err, errBuf.String()}
 	}()
 	select {
 	case res := <-ch:
@@ -437,6 +470,9 @@ func (p *SSHPool) RunCtx(ctx context.Context, host, cmd string, timeout time.Dur
 			if !errors.As(res.err, &ee) {
 				p.drop(host) // canal roto, no exit status
 			}
+			if msg := sshErrDetail(res.stderr); msg != "" {
+				return "", fmt.Errorf("ssh %s: %w: %s", host, res.err, msg)
+			}
 			return "", fmt.Errorf("ssh %s: %w", host, res.err)
 		}
 		return string(res.out), nil

+ 17 - 0
server-go/internal/adapters/sshpool_test.go

@@ -300,3 +300,20 @@ func TestSSHPoolCloseDuringDialReleasesClient(t *testing.T) {
 		t.Fatal("el cliente SSH no se cerró al detectar el pool cerrado")
 	}
 }
+
+// #1737: el stderr remoto es lo unico que explica un reinstall fallido; el
+// error de ssh solo trae el codigo de salida.
+func TestSSHErrDetail(t *testing.T) {
+	cases := []struct{ name, in, want string }{
+		{"sin stderr", "", ""},
+		{"una linea", "curl: (6) Could not resolve host\n", "curl: (6) Could not resolve host"},
+		{"ultima no vacia", "wget: descargando\n\nopkg: no space left on device\n\n", "opkg: no space left on device"},
+		{"solo espacios", " \n\t\n", ""},
+		{"recorta", strings.Repeat("x", 400), strings.Repeat("x", 300) + "..."},
+	}
+	for _, c := range cases {
+		if got := sshErrDetail(c.in); got != c.want {
+			t.Errorf("%s: got %q, want %q", c.name, got, c.want)
+		}
+	}
+}