diff --git a/cmd/agentop/cmd_update.go b/cmd/agentop/cmd_update.go index 6f04ba4bd..a736f4f11 100644 --- a/cmd/agentop/cmd_update.go +++ b/cmd/agentop/cmd_update.go @@ -43,10 +43,11 @@ install that runs the cpex build of cortex to the default build, and says so. --auto what the updater runs: install the newest release only when it is a plain release and update.wait (24h) old; never asks Downloads nothing when agentop and cortex in ~/.local/bin are already at the -release asked for, or when agentop is not a release build (make dev-install, ---ref=main) and there is no --ref. Run agentop doctor to check the rest of the install. +release asked for, or when there is no --ref and agentop is not a release build +(make dev-install, --ref=main) or agentop or cortex is a release candidate newer +than the newest release. Run agentop doctor to check the rest of the install. -Exit status: 0 already current or not a release build, 1 when a download or check fails, else setup's +Exit status: 0 already current, not a release build or a newer release candidate, 1 when a download or check fails, else setup's (or the installer's for --ref=main). ` @@ -140,14 +141,25 @@ func runUpdate(args []string, stdout, stderr io.Writer) int { _, _ = fmt.Fprintf(stdout, "Already at %s.\n", v) return 0 } + // An RC moves only forward, as in the automatic update: only --ref takes it back. + for _, b := range []string{"agentop", proxyBinary} { + if v := installedVersion(filepath.Join(env.binDir, b)); updatestatus.IsRC(v) && updatestatus.IsRelease(latest) && !updatestatus.Newer(latest, v) { + status(updatestatus.ResultCurrent, nil) + _, _ = fmt.Fprintf(stdout, rcAheadNotice+"\n", b, v, latest) + return 0 + } + } if err := cpexConfigBlocks(env); err != nil { status(updatestatus.ResultFailed, err) _, _ = fmt.Fprintf(stderr, "agentop: %v\n", err) return 1 } if err := installReleaseFn(env, *ref, updateFlags(env, *ref, yes), stdout, stderr); err != nil { - status(updatestatus.ResultFailed, err) var exit *exec.ExitError + if errors.As(err, &exit) && exit.ExitCode() == exitDeclined { + return exitDeclined // not a failure, so the status stays as it was + } + status(updatestatus.ResultFailed, err) if errors.As(err, &exit) { return exit.ExitCode() } @@ -163,6 +175,11 @@ func runUpdate(args []string, stdout, stderr io.Writer) int { const notReleaseNotice = "agentop %s is not a release build, and can be newer than the newest release. Not updated.\n" + "Run agentop update --ref=vX.Y.Z to install a release." +// rcAheadNotice is what update prints for an installed RC that is newer than the +// newest release. The arguments are the binary, its RC and that release. +const rcAheadNotice = "%s %s is a release candidate, newer than the newest release %s. Not updated.\n" + + "Run agentop update --ref=vX.Y.Z to install a release." + // installReleaseFn is what callers use, so tests can replace the installer. var installReleaseFn = installRelease diff --git a/cmd/agentop/cmd_update_test.go b/cmd/agentop/cmd_update_test.go index 0857354c3..db84a10fc 100644 --- a/cmd/agentop/cmd_update_test.go +++ b/cmd/agentop/cmd_update_test.go @@ -25,6 +25,8 @@ import ( "testing" "github.com/sigstore/sigstore-go/pkg/root" + + "github.com/rossoctl/cortex/cmd/agentop/updatestatus" ) // fakeInstaller puts a curl on PATH that serves an installer which prints its @@ -65,6 +67,7 @@ func TestUpdateSkipsTheInstallerWhenCurrent(t *testing.T) { args []string want string // stdout code int + result string // the status update.json records, if checked }{ {name: "newest", latest: "v9.9.9", want: "Already at v9.9.9.\n"}, {name: "pinned", latest: "v9.9.8", args: []string{"--ref=v9.9.9"}, want: "Already at v9.9.9.\n"}, @@ -74,6 +77,11 @@ func TestUpdateSkipsTheInstallerWhenCurrent(t *testing.T) { {name: "dev build", latest: "v9.9.9", agentop: "#!/bin/sh\necho agentop dev\n", want: fmt.Sprintf(notReleaseNotice+"\n", "dev")}, {name: "main build", latest: "v9.9.9", agentop: "#!/bin/sh\necho agentop main-abc1234\n", want: fmt.Sprintf(notReleaseNotice+"\n", "main-abc1234")}, {name: "dev build pinned", latest: "v9.9.9", release: "v9.9.9", agentop: "#!/bin/sh\necho agentop dev\n", args: []string{"--ref=v9.9.9"}, want: "staged setup\n", code: 7}, + // An RC moves only forward: to its release or a later one, or back by --ref. + {name: "rc ahead", latest: "v9.9.8", agentop: "#!/bin/sh\necho agentop v9.9.9-rc.1\n", want: fmt.Sprintf(rcAheadNotice+"\n", "agentop", "v9.9.9-rc.1", "v9.9.8"), result: updatestatus.ResultCurrent}, + {name: "cortex rc ahead", latest: "v9.9.9", cortex: "#!/bin/sh\necho cortex v9.9.10-rc.1\n", want: fmt.Sprintf(rcAheadNotice+"\n", "cortex", "v9.9.10-rc.1", "v9.9.9"), result: updatestatus.ResultCurrent}, + {name: "rc released", latest: "v9.9.9", release: "v9.9.9", agentop: "#!/bin/sh\necho agentop v9.9.9-rc.1\n", want: "staged setup\n", code: 7}, + {name: "rc pinned back", latest: "v9.9.8", release: "v9.9.8", agentop: "#!/bin/sh\necho agentop v9.9.9-rc.1\n", args: []string{"--ref=v9.9.8"}, want: "staged setup\n", code: 7}, {name: "main", latest: "v9.9.9", args: []string{"--ref=main"}, want: "installer: --ref=main\n", code: 7}, // setup repairs a pair that disagree. {name: "cortex behind", latest: "v9.9.9", release: "v9.9.9", cortex: "#!/bin/sh\necho cortex v9.9.8\n", want: "staged setup\n", code: 7}, @@ -98,6 +106,11 @@ func TestUpdateSkipsTheInstallerWhenCurrent(t *testing.T) { if code := runUpdate(c.args, &out, &errb); code != c.code || out.String() != c.want { t.Errorf("exit %d stdout %q, want %d %q\n%s", code, out.String(), c.code, c.want, errb.String()) } + if c.result != "" { + if s := readStatus(t, sc); s.Result != c.result || s.Latest != c.latest { + t.Errorf("status %q latest %q, want %q %q", s.Result, s.Latest, c.result, c.latest) + } + } }) } } @@ -172,6 +185,22 @@ func TestUpdateCpexNoticeAfterSetupSucceeds(t *testing.T) { } } +// A "no" at setup's prompt is not a failed update: the title bar must not say "failed". +func TestUpdateDeclinedIsNotAFailure(t *testing.T) { + sc := newDoctorScene(t) + sc.installed(t) + latestIs(t, "v9.9.10") + fakeStagedRelease(t, "v9.9.10", false) + t.Setenv("FAKE_SETUP_EXIT", fmt.Sprint(exitDeclined)) + var out, errb bytes.Buffer + if code := runUpdate(nil, &out, &errb); code != exitDeclined { + t.Fatalf("exit %d, want %d:\n%s", code, exitDeclined, errb.String()) + } + if _, err := os.Stat(filepath.Join(sc.home, ".cortex", updatestatus.FileName)); !errors.Is(err, fs.ErrNotExist) { + t.Errorf("a declined update wrote the status file (stat: %v)", err) + } +} + // addCpexEntry puts a cpex entry first in the outbound pipeline of the config setup wrote. func addCpexEntry(t *testing.T, home string) { t.Helper() diff --git a/docs/laptop-service.md b/docs/laptop-service.md index 2525e8f82..c8d4f5353 100644 --- a/docs/laptop-service.md +++ b/docs/laptop-service.md @@ -331,8 +331,9 @@ A service install (launchd or systemd) updates itself: A release-candidate install (`vX.Y.Z-rc.N`) moves only forward. It installs Latest when Latest is a later version, for example `v0.10.0` over `v0.10.0-rc.1`, after the -same wait. It never moves back to an older Latest. A plain release follows Latest in -both directions, so the maintainers can withdraw a bad release. +same wait. It never moves back to an older Latest. `agentop update` by hand follows +the same rule: to move back, give the release, for example `--ref=v0.9.5`. A plain +release follows Latest in both directions, so the maintainers can withdraw a bad release. It does not change a `dev` or `main-` install. That build has no version order against Latest, and is often ahead of it. `agentop doctor` says so, and the title bar