--force-update reports from/to versions; elevated parent no longer claims "update applied" when nothing changed
This commit is contained in:
+40
-11
@@ -39,6 +39,11 @@ var version = "dev"
|
|||||||
// process: a signed update has been staged and the GPU lock is idle.
|
// process: a signed update has been staged and the GPU lock is idle.
|
||||||
const exitCodeUpdate = 3
|
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
|
// stdoutIsTerminal reports whether stdout is a console (char device), as
|
||||||
// opposed to a pipe or file — which is what Docker containers and services
|
// opposed to a pipe or file — which is what Docker containers and services
|
||||||
// see.
|
// see.
|
||||||
@@ -348,11 +353,12 @@ func forceUpdateCommand(configPath string, elevatedChild bool) int {
|
|||||||
// instead of hanging in a TCP connect for minutes.
|
// instead of hanging in a TCP connect for minutes.
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
staged, err := u.Check(ctx, exePath)
|
staged, to, err := u.Check(ctx, exePath)
|
||||||
if err != nil && isPermission(err) && !service.Elevated() {
|
if err != nil && isPermission(err) && !service.Elevated() {
|
||||||
code, _ := elevateAndMirror("--force-update")
|
code, _ := elevateAndMirror("--force-update")
|
||||||
if code == 0 {
|
reportElevatedUpdate(code, to)
|
||||||
fmt.Println("update applied (elevated)")
|
if code == 0 || code == exitCodeStaged {
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
@@ -364,12 +370,13 @@ func forceUpdateCommand(configPath string, elevatedChild bool) int {
|
|||||||
fmt.Printf("%s is up to date\n", versionLine())
|
fmt.Printf("%s is up to date\n", versionLine())
|
||||||
return 0
|
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()
|
restarted, err := service.RestartIfRunning()
|
||||||
if err != nil && isPermission(err) && !service.Elevated() {
|
if err != nil && isPermission(err) && !service.Elevated() {
|
||||||
code, _ := elevateAndMirror("--force-update")
|
code, _ := elevateAndMirror("--force-update")
|
||||||
if code == 0 {
|
reportElevatedUpdate(code, to)
|
||||||
fmt.Println("update applied (elevated)")
|
if code == 0 || code == exitCodeStaged {
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
@@ -378,13 +385,35 @@ func forceUpdateCommand(configPath string, elevatedChild bool) int {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
if restarted {
|
if restarted {
|
||||||
fmt.Println("service restarted on the new version")
|
fmt.Println("service restarted on " + to)
|
||||||
} else {
|
} 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
|
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)
|
// serviceCommand installs (copyBin = register the canonical-layout copy)
|
||||||
// or removes the service and reports the result. On Windows, when the
|
// or removes the service and reports the result. On Windows, when the
|
||||||
// shell is not elevated, the command relaunches itself through a UAC
|
// 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}
|
u := &update.Updater{Repo: cfg.UpdateRepo, Asset: cfg.UpdateAsset, Version: version, Desired: cfg.AppVersion, Log: log}
|
||||||
for {
|
for {
|
||||||
staged, err := u.Check(ctx, exePath)
|
staged, to, err := u.Check(ctx, exePath)
|
||||||
if err != nil && ctx.Err() == nil {
|
if err != nil && ctx.Err() == nil {
|
||||||
log.Warn("auto-update check failed", "err", err)
|
log.Warn("auto-update check failed", "err", err)
|
||||||
}
|
}
|
||||||
if staged {
|
if staged {
|
||||||
if !isService {
|
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
|
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) {
|
if waitForIdle(ctx, lk, 24*time.Hour) {
|
||||||
log.Warn("auto-update: restarting to apply update")
|
log.Warn("auto-update: restarting to apply update")
|
||||||
os.Exit(exitCodeUpdate)
|
os.Exit(exitCodeUpdate)
|
||||||
|
|||||||
+25
-22
@@ -173,11 +173,13 @@ func CleanupOld(exePath string) {
|
|||||||
|
|
||||||
// Check performs a single update check. staged is true when a
|
// Check performs a single update check. staged is true when a
|
||||||
// signature-verified binary has been swapped into place at exePath; the
|
// signature-verified binary has been swapped into place at exePath; the
|
||||||
// caller should then restart the process. A nil error with staged=false
|
// caller should then restart the process. to is the release tag the check
|
||||||
// means "no action" (up to date, APP_VER=dev, or no embedded public key);
|
// resolved (the latest release or the pinned tag), set once the release
|
||||||
// a non-nil error means the check failed and the running binary is
|
// fetch succeeded — even when staging afterwards fails. A nil error with
|
||||||
// untouched.
|
// staged=false means "no action" (up to date, APP_VER=dev, or no embedded
|
||||||
func (u *Updater) Check(ctx context.Context, exePath string) (staged bool, err error) {
|
// 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()
|
log := u.logger()
|
||||||
desired := u.Desired
|
desired := u.Desired
|
||||||
if desired == "" {
|
if desired == "" {
|
||||||
@@ -185,15 +187,15 @@ func (u *Updater) Check(ctx context.Context, exePath string) (staged bool, err e
|
|||||||
}
|
}
|
||||||
if desired == "dev" {
|
if desired == "dev" {
|
||||||
log.Debug("auto-update: APP_VER=dev, skipping")
|
log.Debug("auto-update: APP_VER=dev, skipping")
|
||||||
return false, nil
|
return false, "", nil
|
||||||
}
|
}
|
||||||
if publicKeyPEM == "" {
|
if publicKeyPEM == "" {
|
||||||
log.Debug("auto-update: no public key embedded, skipping")
|
log.Debug("auto-update: no public key embedded, skipping")
|
||||||
return false, nil
|
return false, "", nil
|
||||||
}
|
}
|
||||||
api, err := u.apiURL()
|
api, err := u.apiURL()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
pinned := desired != "stable"
|
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)
|
body, err := u.get(ctx, endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("fetch release: %w", err)
|
return false, "", fmt.Errorf("fetch release: %w", err)
|
||||||
}
|
}
|
||||||
var rel release
|
var rel release
|
||||||
if err := json.Unmarshal(body, &rel); err != nil {
|
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 {
|
if pinned {
|
||||||
// Pin mode: any difference from the target tag means stage it —
|
// Pin mode: any difference from the target tag means stage it —
|
||||||
// including downgrades and replacing a dev binary.
|
// including downgrades and replacing a dev binary.
|
||||||
if u.Version == rel.TagName {
|
if u.Version == rel.TagName {
|
||||||
log.Debug("auto-update: already on pinned version", "version", u.Version)
|
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" {
|
} else if u.Version != "" && u.Version != "dev" {
|
||||||
// Stable mode: only strictly newer releases count; a dev binary
|
// Stable mode: only strictly newer releases count; a dev binary
|
||||||
// cannot be compared and is always replaced by the latest release.
|
// cannot be compared and is always replaced by the latest release.
|
||||||
newer, err := newerVersion(u.Version, rel.TagName)
|
newer, err := newerVersion(u.Version, rel.TagName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, to, err
|
||||||
}
|
}
|
||||||
if !newer {
|
if !newer {
|
||||||
log.Debug("auto-update: up to date", "version", u.Version, "latest", rel.TagName)
|
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]
|
assetURL, ok := urls[u.Asset]
|
||||||
if !ok {
|
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"]
|
sigURL, ok := urls[u.Asset+".sig"]
|
||||||
if !ok {
|
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)
|
data, err := u.get(ctx, assetURL)
|
||||||
if err != nil {
|
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)
|
sig, err := u.get(ctx, sigURL)
|
||||||
if err != nil {
|
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 {
|
if sumURL, ok := urls[u.Asset+".sha256"]; ok {
|
||||||
sumText, err := u.get(ctx, sumURL)
|
sumText, err := u.get(ctx, sumURL)
|
||||||
if err != nil {
|
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]
|
want := strings.Fields(string(sumText))[0]
|
||||||
got := hex.EncodeToString(sha256Bytes(data))
|
got := hex.EncodeToString(sha256Bytes(data))
|
||||||
if !strings.EqualFold(want, got) {
|
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 {
|
if err := verifySignature(publicKeyPEM, data, sig); err != nil {
|
||||||
return false, err
|
return false, to, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := stage(exePath, data); err != nil {
|
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)
|
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 {
|
func sha256Bytes(data []byte) []byte {
|
||||||
|
|||||||
@@ -102,13 +102,16 @@ func TestCheckStagesUpdate(t *testing.T) {
|
|||||||
withPublicKey(t, f.pubPEM)
|
withPublicKey(t, f.pubPEM)
|
||||||
exe := fakeExe(t)
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !staged {
|
if !staged {
|
||||||
t.Fatal("expected staged update")
|
t.Fatal("expected staged update")
|
||||||
}
|
}
|
||||||
|
if to != "v9.9.9" {
|
||||||
|
t.Fatalf("to = %q, want v9.9.9", to)
|
||||||
|
}
|
||||||
content, _ := os.ReadFile(exe)
|
content, _ := os.ReadFile(exe)
|
||||||
if string(content) != "new-binary" {
|
if string(content) != "new-binary" {
|
||||||
t.Fatalf("exe content = %q", content)
|
t.Fatalf("exe content = %q", content)
|
||||||
@@ -125,7 +128,7 @@ func TestCheckRejectsTamperedSignature(t *testing.T) {
|
|||||||
withPublicKey(t, f.pubPEM)
|
withPublicKey(t, f.pubPEM)
|
||||||
exe := fakeExe(t)
|
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 {
|
if err == nil {
|
||||||
t.Fatal("expected signature error")
|
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"} {
|
for _, tag := range []string{"v0.1.2", "v0.1.1", "v0.0.9"} {
|
||||||
f := newFakeGitea(t, tag, []byte("new-binary"))
|
f := newFakeGitea(t, tag, []byte("new-binary"))
|
||||||
withPublicKey(t, f.pubPEM)
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -155,7 +158,7 @@ func TestCheckSkipsOlderOrEqual(t *testing.T) {
|
|||||||
func TestCheckSkipsWithoutPublicKey(t *testing.T) {
|
func TestCheckSkipsWithoutPublicKey(t *testing.T) {
|
||||||
f := newFakeGitea(t, "v9.9.9", []byte("new-binary"))
|
f := newFakeGitea(t, "v9.9.9", []byte("new-binary"))
|
||||||
withPublicKey(t, "")
|
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 {
|
if err != nil || staged {
|
||||||
t.Fatalf("staged=%v err=%v, want no action without key", staged, err)
|
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"))
|
f := newFakeGitea(t, "v9.9.9", []byte("new-binary"))
|
||||||
withPublicKey(t, f.pubPEM)
|
withPublicKey(t, f.pubPEM)
|
||||||
exe := fakeExe(t)
|
exe := fakeExe(t)
|
||||||
staged, err := f.updater("dev").Check(context.Background(), exe)
|
staged, _, err := f.updater("dev").Check(context.Background(), exe)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -184,7 +187,7 @@ func TestCheckDesiredDevDisables(t *testing.T) {
|
|||||||
f := newFakeGitea(t, "v9.9.9", []byte("new-binary"))
|
f := newFakeGitea(t, "v9.9.9", []byte("new-binary"))
|
||||||
withPublicKey(t, f.pubPEM)
|
withPublicKey(t, f.pubPEM)
|
||||||
for _, version := range []string{"dev", "v0.1.2"} {
|
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 {
|
if err != nil || staged {
|
||||||
t.Fatalf("version %s: staged=%v err=%v, want no action with APP_VER=dev", version, staged, err)
|
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"))
|
f := newFakeGitea(t, "v0.5.0", []byte("pinned-binary"))
|
||||||
withPublicKey(t, f.pubPEM)
|
withPublicKey(t, f.pubPEM)
|
||||||
exe := fakeExe(t)
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("version %s: %v", version, err)
|
t.Fatalf("version %s: %v", version, err)
|
||||||
}
|
}
|
||||||
if !staged {
|
if !staged {
|
||||||
t.Fatalf("version %s: expected pinned v0.5.0 to be staged", version)
|
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)
|
content, _ := os.ReadFile(exe)
|
||||||
if string(content) != "pinned-binary" {
|
if string(content) != "pinned-binary" {
|
||||||
t.Fatalf("version %s: exe content = %q", version, content)
|
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"))
|
f := newFakeGitea(t, "v0.5.0", []byte("pinned-binary"))
|
||||||
withPublicKey(t, f.pubPEM)
|
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 {
|
if err != nil || staged {
|
||||||
t.Fatalf("staged=%v err=%v, want no action when already on the pinned version", staged, err)
|
t.Fatalf("staged=%v err=%v, want no action when already on the pinned version", staged, err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user