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

fix(updater): unblock apply on installs without a git clone, stop burning API quota (#963)

* fix(updater): skip the git readiness check on installs without a clone

Rolling mode is detected by the presence of deploy/update.sh, but that
script supports directories that are not git clones (it resolves main
HEAD via ls-remote and never runs git reset --hard, #897). The working
tree readiness check still ran git status there, failed with "could not
read git status" and blocked the apply button forever.

Skip the check (OK, not applicable) when repoRoot has no .git, the same
way stable mode skips it.

Ref #955

* fix(updater): probe connectivity against github.com, not the API

The readiness network check did GET api.github.com on every compute
(cached only 30 s). While the update UI is open the status endpoint is
polled every few seconds, so the probe alone could exceed the 60 req/h
anonymous API quota of the server's public IP; once exhausted, the apply
could not fetch the release metadata and failed with the rate-limit
error. The public web endpoint proves connectivity the same way (any
HTTP response counts) and does not consume API quota.

Ref #956

* fix(devices): stop showing AdGuard coverage bar as danger at 100%

MetricBar's metricColor() thresholds are load-oriented (>90 = danger),
which inverted the semantics of the AdGuard protection coverage stat:
at 100% the bar rendered red and read as an error.

Add an optional colorClass override to MetricBar (default behavior
unchanged for CPU/RAM usage) and render the coverage bar with bg-ok.

Ref #959

* feat(devices): redesign AdGuard stat card with full-coverage state and unprotected filter

When every client is protected the card now shows a large success
shield with the 100% caption instead of a number and a progress bar.
With partial coverage the card keeps the protected/total count (no bar)
and becomes clickable: it toggles a table filter showing only
unprotected clients, surfaced as a removable active-filter pill and
cleared by the global clear-filters action.

The MetricBar colorClass override from the previous commit is no longer
used by any caller, so it is reverted to keep the component API minimal.

Ref #959
Nacho 2 месяцев назад
Родитель
Сommit
0d1fa3b08a

+ 22 - 2
server-go/internal/updater/readiness.go

@@ -12,6 +12,7 @@ import (
 	"fmt"
 	"net/http"
 	"os/exec"
+	"path/filepath"
 	"strings"
 	"syscall"
 	"time"
@@ -25,6 +26,15 @@ const (
 	readinessNetTimeout = 5 * time.Second
 )
 
+// probeURL es el endpoint del check de conectividad. Es github.com (HTML) y
+// NO api.github.com a propósito (issue #956): la API anónima tiene cuota de
+// 60 req/h por IP y el readiness se recomputa cada readinessTTL mientras la
+// UI de actualización está abierta, lo que agotaba la cuota y luego impedía
+// al apply descargar los metadatos de la release. La web pública no cuenta
+// contra esa cuota y un 4xx también prueba conectividad.
+// Var de paquete para apuntarlo al httptest de los tests.
+var probeURL = "https://github.com"
+
 // minDiskFreeBytes es el espacio libre mínimo exigido en repoRoot (descarga
 // del binario de CI ~40 MB + backup del binario actual + margen de build).
 // Var de paquete para poder bajarlo en tests.
@@ -111,6 +121,15 @@ func (u *Updater) checkGit() CheckResult {
 	if u.mode != "rolling" {
 		return CheckResult{OK: true, Detail: "no aplica (layout estable)"}
 	}
+	// Rolling sin clone (.git ausente, p.ej. el layout estable actualizado
+	// in place con deploy/update.sh copiado): update.sh resuelve el SHA de
+	// main vía ls-remote y NO hace git reset --hard (issue #897), así que no
+	// hay working tree que proteger. Sin este skip el check fallaba con "no
+	// se pudo leer el estado de git" y bloqueaba el apply para siempre
+	// (issue #955).
+	if !fileExists(filepath.Join(u.repoRoot, ".git")) {
+		return CheckResult{OK: true, Detail: "no aplica (sin clone git)"}
+	}
 	out, err := exec.Command("git", "-C", u.repoRoot, "status", "--porcelain", "--untracked-files=no").Output()
 	if err != nil {
 		return CheckResult{OK: false, Detail: "no se pudo leer el estado de git"}
@@ -122,11 +141,12 @@ func (u *Updater) checkGit() CheckResult {
 }
 
 // checkNetwork comprueba que GitHub es alcanzable (cualquier respuesta HTTP
-// cuenta: un 4xx también significa que hay conectividad).
+// cuenta: un 4xx también significa que hay conectividad). Usa probeURL (web
+// pública, sin cuota de API; #956).
 func (u *Updater) checkNetwork() CheckResult {
 	ctx, cancel := context.WithTimeout(context.Background(), readinessNetTimeout)
 	defer cancel()
-	req, err := http.NewRequestWithContext(ctx, "GET", APIBase, nil)
+	req, err := http.NewRequestWithContext(ctx, "GET", probeURL, nil)
 	if err != nil {
 		return CheckResult{OK: false, Detail: "URL de API inválida"}
 	}

+ 24 - 3
server-go/internal/updater/updater_readiness_test.go

@@ -67,6 +67,27 @@ func TestReadinessGitDirty(t *testing.T) {
 	}
 }
 
+func TestReadinessGitSinClone(t *testing.T) {
+	root := t.TempDir()
+	writeDeployScript(t, root)
+	// SIN writeGitHead: deploy/update.sh presente → modo rolling, pero el
+	// directorio no es un clone git (layout estable actualizado in place,
+	// issue #897). update.sh resuelve main vía ls-remote y no hace reset
+	// --hard: el check de git no aplica y no debe bloquear el apply (#955).
+	withAPI(t, func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })
+	u := New(root, "owner/netpulse", "", "2.0.0", nil)
+	r := u.Readiness()
+	if !r.Git.OK {
+		t.Errorf("git NO debería fallar sin clone (.git ausente): %+v", r.Git)
+	}
+	if r.Git.Detail == "" {
+		t.Error("el check git skipado debería explicarse en detail")
+	}
+	if !r.Ready {
+		t.Errorf("ready debería ser true sin clone git: %+v", r)
+	}
+}
+
 func TestReadinessGitTrackedModificado(t *testing.T) {
 	if !gitAvailable() {
 		t.Skip("git no disponible")
@@ -122,9 +143,9 @@ func TestReadinessDiskInsufficient(t *testing.T) {
 func TestReadinessNetworkDown(t *testing.T) {
 	root := t.TempDir()
 	writeDeployScript(t, root)
-	old := APIBase
-	APIBase = "http://127.0.0.1:1" // puerto cerrado → conexión rechazada
-	t.Cleanup(func() { APIBase = old })
+	old := probeURL
+	probeURL = "http://127.0.0.1:1" // puerto cerrado → conexión rechazada
+	t.Cleanup(func() { probeURL = old })
 	u := New(root, "owner/netpulse", "", "2.0.0", nil)
 	r := u.Readiness()
 	if r.Network.OK {

+ 5 - 0
server-go/internal/updater/updater_test.go

@@ -24,6 +24,11 @@ func withAPI(t *testing.T, h http.HandlerFunc) {
 	old := APIBase
 	APIBase = srv.URL
 	t.Cleanup(func() { APIBase = old })
+	// El check de conectividad del readiness usa probeURL (web pública de
+	// GitHub, #956): apuntarlo también al httptest para tests herméticos.
+	oldProbe := probeURL
+	probeURL = srv.URL
+	t.Cleanup(func() { probeURL = oldProbe })
 }
 
 func TestCheckUpdateAvailable(t *testing.T) {