From 2d5be72436ecb75ed7671302d6c2cf88750e5b01 Mon Sep 17 00:00:00 2001 From: mram Date: Mon, 21 Sep 2026 21:58:50 +0200 Subject: [PATCH] --force-update reports from/to versions; elevated parent no longer claims "update applied" when nothing changed --- cmd/gpu-turnstile/main.go | 51 ++++++++++++++++++++++++++-------- internal/update/update.go | 47 ++++++++++++++++--------------- internal/update/update_test.go | 22 +++++++++------ 3 files changed, 79 insertions(+), 41 deletions(-) diff --git a/cmd/gpu-turnstile/main.go b/cmd/gpu-turnstile/main.go index 147cd5e..ac420c6 100644 --- a/cmd/gpu-turnstile/main.go +++ b/cmd/gpu-turnstile/main.go @@ -39,6 +39,11 @@ var version = "dev" // process: a signed update has been staged and the GPU lock is idle. const exitCodeUpdate = 3 +// exitCodeStaged is returned by an elevated --force-update child when it +// staged a new binary, so the non-elevated parent can tell "updated" from +// "up to date" (it cannot see the child's console). +const exitCodeStaged = 4 + // stdoutIsTerminal reports whether stdout is a console (char device), as // opposed to a pipe or file — which is what Docker containers and services // see. @@ -348,11 +353,12 @@ func forceUpdateCommand(configPath string, elevatedChild bool) int { // instead of hanging in a TCP connect for minutes. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() - staged, err := u.Check(ctx, exePath) + staged, to, err := u.Check(ctx, exePath) if err != nil && isPermission(err) && !service.Elevated() { code, _ := elevateAndMirror("--force-update") - if code == 0 { - fmt.Println("update applied (elevated)") + reportElevatedUpdate(code, to) + if code == 0 || code == exitCodeStaged { + return 0 } return code } @@ -364,12 +370,13 @@ func forceUpdateCommand(configPath string, elevatedChild bool) int { fmt.Printf("%s is up to date\n", versionLine()) return 0 } - fmt.Printf("%s: update staged\n", versionLine()) + fmt.Printf("gpu-turnstile: updated from %s to %s\n", version, to) restarted, err := service.RestartIfRunning() if err != nil && isPermission(err) && !service.Elevated() { code, _ := elevateAndMirror("--force-update") - if code == 0 { - fmt.Println("update applied (elevated)") + reportElevatedUpdate(code, to) + if code == 0 || code == exitCodeStaged { + return 0 } return code } @@ -378,13 +385,35 @@ func forceUpdateCommand(configPath string, elevatedChild bool) int { return 1 } if restarted { - fmt.Println("service restarted on the new version") + fmt.Println("service restarted on " + to) } else { - fmt.Println("no running service; the new version applies on next start") + fmt.Println("no running service; " + to + " applies on the next start") + } + if elevatedChild { + // Tell the non-elevated parent (which cannot see this console) + // whether anything was staged, so its mirror message is honest. + return exitCodeStaged } return 0 } +// reportElevatedUpdate prints the parent's summary of an elevated +// --force-update child: exitCodeStaged means the child staged a new binary, +// 0 means it found nothing to do. to is the tag the parent's own check +// resolved before it hit the permission wall ("" when it never got that +// far). +func reportElevatedUpdate(code int, to string) { + switch { + case code == exitCodeStaged && to != "": + fmt.Printf("gpu-turnstile: updated from %s to %s (elevated)\n", version, to) + case code == exitCodeStaged: + fmt.Println("update applied (elevated)") + case code == 0: + fmt.Printf("%s is up to date\n", versionLine()) + } + // Non-zero, non-staged codes: elevateAndMirror already printed the failure. +} + // serviceCommand installs (copyBin = register the canonical-layout copy) // or removes the service and reports the result. On Windows, when the // shell is not elevated, the command relaunches itself through a UAC @@ -813,16 +842,16 @@ func updateLoop(ctx context.Context, cfg config.Config, log *slog.Logger, lk *lo } u := &update.Updater{Repo: cfg.UpdateRepo, Asset: cfg.UpdateAsset, Version: version, Desired: cfg.AppVersion, Log: log} for { - staged, err := u.Check(ctx, exePath) + staged, to, err := u.Check(ctx, exePath) if err != nil && ctx.Err() == nil { log.Warn("auto-update check failed", "err", err) } if staged { if !isService { - log.Warn("auto-update: new binary staged; restart gpu-turnstile to apply") + log.Warn("auto-update: new binary staged; restart gpu-turnstile to apply", "version", to) return } - log.Warn("auto-update: staged; restarting once the GPU is idle") + log.Warn("auto-update: staged; restarting once the GPU is idle", "version", to) if waitForIdle(ctx, lk, 24*time.Hour) { log.Warn("auto-update: restarting to apply update") os.Exit(exitCodeUpdate) diff --git a/internal/update/update.go b/internal/update/update.go index 08af61f..676719b 100644 --- a/internal/update/update.go +++ b/internal/update/update.go @@ -173,11 +173,13 @@ func CleanupOld(exePath string) { // Check performs a single update check. staged is true when a // signature-verified binary has been swapped into place at exePath; the -// caller should then restart the process. A nil error with staged=false -// means "no action" (up to date, APP_VER=dev, or no embedded public key); -// a non-nil error means the check failed and the running binary is -// untouched. -func (u *Updater) Check(ctx context.Context, exePath string) (staged bool, err error) { +// caller should then restart the process. to is the release tag the check +// resolved (the latest release or the pinned tag), set once the release +// fetch succeeded — even when staging afterwards fails. A nil error with +// staged=false means "no action" (up to date, APP_VER=dev, or no embedded +// public key); a non-nil error means the check failed and the running +// binary is untouched. +func (u *Updater) Check(ctx context.Context, exePath string) (staged bool, to string, err error) { log := u.logger() desired := u.Desired if desired == "" { @@ -185,15 +187,15 @@ func (u *Updater) Check(ctx context.Context, exePath string) (staged bool, err e } if desired == "dev" { log.Debug("auto-update: APP_VER=dev, skipping") - return false, nil + return false, "", nil } if publicKeyPEM == "" { log.Debug("auto-update: no public key embedded, skipping") - return false, nil + return false, "", nil } api, err := u.apiURL() if err != nil { - return false, err + return false, "", err } pinned := desired != "stable" @@ -203,29 +205,30 @@ func (u *Updater) Check(ctx context.Context, exePath string) (staged bool, err e } body, err := u.get(ctx, endpoint) if err != nil { - return false, fmt.Errorf("fetch release: %w", err) + return false, "", fmt.Errorf("fetch release: %w", err) } var rel release if err := json.Unmarshal(body, &rel); err != nil { - return false, fmt.Errorf("parse release: %w", err) + return false, "", fmt.Errorf("parse release: %w", err) } + to = rel.TagName if pinned { // Pin mode: any difference from the target tag means stage it — // including downgrades and replacing a dev binary. if u.Version == rel.TagName { log.Debug("auto-update: already on pinned version", "version", u.Version) - return false, nil + return false, to, nil } } else if u.Version != "" && u.Version != "dev" { // Stable mode: only strictly newer releases count; a dev binary // cannot be compared and is always replaced by the latest release. newer, err := newerVersion(u.Version, rel.TagName) if err != nil { - return false, err + return false, to, err } if !newer { log.Debug("auto-update: up to date", "version", u.Version, "latest", rel.TagName) - return false, nil + return false, to, nil } } @@ -235,42 +238,42 @@ func (u *Updater) Check(ctx context.Context, exePath string) (staged bool, err e } assetURL, ok := urls[u.Asset] if !ok { - return false, fmt.Errorf("release %s has no asset %q", rel.TagName, u.Asset) + return false, to, fmt.Errorf("release %s has no asset %q", rel.TagName, u.Asset) } sigURL, ok := urls[u.Asset+".sig"] if !ok { - return false, fmt.Errorf("release %s has no signature asset %q", rel.TagName, u.Asset+".sig") + return false, to, fmt.Errorf("release %s has no signature asset %q", rel.TagName, u.Asset+".sig") } data, err := u.get(ctx, assetURL) if err != nil { - return false, fmt.Errorf("download %s: %w", u.Asset, err) + return false, to, fmt.Errorf("download %s: %w", u.Asset, err) } sig, err := u.get(ctx, sigURL) if err != nil { - return false, fmt.Errorf("download signature: %w", err) + return false, to, fmt.Errorf("download signature: %w", err) } if sumURL, ok := urls[u.Asset+".sha256"]; ok { sumText, err := u.get(ctx, sumURL) if err != nil { - return false, fmt.Errorf("download checksum: %w", err) + return false, to, fmt.Errorf("download checksum: %w", err) } want := strings.Fields(string(sumText))[0] got := hex.EncodeToString(sha256Bytes(data)) if !strings.EqualFold(want, got) { - return false, fmt.Errorf("sha256 mismatch: got %s, want %s", got, want) + return false, to, fmt.Errorf("sha256 mismatch: got %s, want %s", got, want) } } if err := verifySignature(publicKeyPEM, data, sig); err != nil { - return false, err + return false, to, err } if err := stage(exePath, data); err != nil { - return false, fmt.Errorf("stage update: %w", err) + return false, to, fmt.Errorf("stage update: %w", err) } log.Info("auto-update: new version staged", "from", u.Version, "to", rel.TagName) - return true, nil + return true, to, nil } func sha256Bytes(data []byte) []byte { diff --git a/internal/update/update_test.go b/internal/update/update_test.go index 8588a17..cac19b3 100644 --- a/internal/update/update_test.go +++ b/internal/update/update_test.go @@ -102,13 +102,16 @@ func TestCheckStagesUpdate(t *testing.T) { withPublicKey(t, f.pubPEM) exe := fakeExe(t) - staged, err := f.updater("v0.1.2").Check(context.Background(), exe) + staged, to, err := f.updater("v0.1.2").Check(context.Background(), exe) if err != nil { t.Fatal(err) } if !staged { t.Fatal("expected staged update") } + if to != "v9.9.9" { + t.Fatalf("to = %q, want v9.9.9", to) + } content, _ := os.ReadFile(exe) if string(content) != "new-binary" { t.Fatalf("exe content = %q", content) @@ -125,7 +128,7 @@ func TestCheckRejectsTamperedSignature(t *testing.T) { withPublicKey(t, f.pubPEM) exe := fakeExe(t) - staged, err := f.updater("v0.1.2").Check(context.Background(), exe) + staged, _, err := f.updater("v0.1.2").Check(context.Background(), exe) if err == nil { t.Fatal("expected signature error") } @@ -142,7 +145,7 @@ func TestCheckSkipsOlderOrEqual(t *testing.T) { for _, tag := range []string{"v0.1.2", "v0.1.1", "v0.0.9"} { f := newFakeGitea(t, tag, []byte("new-binary")) withPublicKey(t, f.pubPEM) - staged, err := f.updater("v0.1.2").Check(context.Background(), fakeExe(t)) + staged, _, err := f.updater("v0.1.2").Check(context.Background(), fakeExe(t)) if err != nil { t.Fatal(err) } @@ -155,7 +158,7 @@ func TestCheckSkipsOlderOrEqual(t *testing.T) { func TestCheckSkipsWithoutPublicKey(t *testing.T) { f := newFakeGitea(t, "v9.9.9", []byte("new-binary")) withPublicKey(t, "") - staged, err := f.updater("v0.1.2").Check(context.Background(), fakeExe(t)) + staged, _, err := f.updater("v0.1.2").Check(context.Background(), fakeExe(t)) if err != nil || staged { t.Fatalf("staged=%v err=%v, want no action without key", staged, err) } @@ -167,7 +170,7 @@ func TestCheckDevBuildGetsStable(t *testing.T) { f := newFakeGitea(t, "v9.9.9", []byte("new-binary")) withPublicKey(t, f.pubPEM) exe := fakeExe(t) - staged, err := f.updater("dev").Check(context.Background(), exe) + staged, _, err := f.updater("dev").Check(context.Background(), exe) if err != nil { t.Fatal(err) } @@ -184,7 +187,7 @@ func TestCheckDesiredDevDisables(t *testing.T) { f := newFakeGitea(t, "v9.9.9", []byte("new-binary")) withPublicKey(t, f.pubPEM) for _, version := range []string{"dev", "v0.1.2"} { - staged, err := f.updaterDesired(version, "dev").Check(context.Background(), fakeExe(t)) + staged, _, err := f.updaterDesired(version, "dev").Check(context.Background(), fakeExe(t)) if err != nil || staged { t.Fatalf("version %s: staged=%v err=%v, want no action with APP_VER=dev", version, staged, err) } @@ -198,13 +201,16 @@ func TestCheckPinned(t *testing.T) { f := newFakeGitea(t, "v0.5.0", []byte("pinned-binary")) withPublicKey(t, f.pubPEM) exe := fakeExe(t) - staged, err := f.updaterDesired(version, "v0.5.0").Check(context.Background(), exe) + staged, to, err := f.updaterDesired(version, "v0.5.0").Check(context.Background(), exe) if err != nil { t.Fatalf("version %s: %v", version, err) } if !staged { t.Fatalf("version %s: expected pinned v0.5.0 to be staged", version) } + if to != "v0.5.0" { + t.Fatalf("version %s: to = %q, want v0.5.0", version, to) + } content, _ := os.ReadFile(exe) if string(content) != "pinned-binary" { t.Fatalf("version %s: exe content = %q", version, content) @@ -213,7 +219,7 @@ func TestCheckPinned(t *testing.T) { f := newFakeGitea(t, "v0.5.0", []byte("pinned-binary")) withPublicKey(t, f.pubPEM) - staged, err := f.updaterDesired("v0.5.0", "v0.5.0").Check(context.Background(), fakeExe(t)) + staged, _, err := f.updaterDesired("v0.5.0", "v0.5.0").Check(context.Background(), fakeExe(t)) if err != nil || staged { t.Fatalf("staged=%v err=%v, want no action when already on the pinned version", staged, err) }