diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index 239e367..beb9dcc 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -46,6 +46,7 @@ type Deployer struct { mu sync.Mutex flights map[string]*flight skips map[string]int + revived []string counts map[string]int Timings *slog.Logger } @@ -168,12 +169,13 @@ func (d *Deployer) Sources() []*Source { return out } -// Deploy builds every environment, or only those named by env or branch; a named deploy skips purge and GC. +// Deploy builds every environment, or only those named by env or branch; a named deploy skips purge and GC but unmarks what it swaps in. func (d *Deployer) Deploy(ctx context.Context, only []string, modules bool) error { ctx, end := d.phase(ctx, "run") d.mu.Lock() clear(d.skips) clear(d.counts) + d.revived = nil d.mu.Unlock() found := map[string]bool{} var errs []error @@ -191,6 +193,8 @@ func (d *Deployer) Deploy(ctx context.Context, only []string, modules bool) erro if err := d.GC(ctx); err != nil { errs = append(errs, err) } + } else if err := d.unmark(d.revived); err != nil { + errs = append(errs, err) } d.mu.Lock() for _, e := range slices.Sorted(maps.Keys(d.skips)) { @@ -386,6 +390,12 @@ func (d *Deployer) deployEnv(ctx context.Context, s *Source, name, sha string, m if err != nil { return err } + d.mu.Lock() + d.revived = append(d.revived, target) + for _, m := range mods { + d.revived = append(d.revived, m.dir) + } + d.mu.Unlock() outcome = "updated" if cur == "" { outcome = "created" @@ -860,6 +870,32 @@ func (d *Deployer) GC(ctx context.Context) error { return errors.Join(append(g.errs, err)...) } +// unmark drops GC marks for paths a named deploy made live again, so they get a fresh grace period once unreferenced. +func (d *Deployer) unmark(paths []string) error { + statePath := d.path(gcStateFile) + b, err := os.ReadFile(statePath) + if os.IsNotExist(err) { + return nil + } else if err != nil { + return err + } + marks := map[string]time.Time{} + if err := json.Unmarshal(b, &marks); err != nil { + return fmt.Errorf("gc: %s: %w", statePath, err) + } + n := len(marks) + for _, p := range paths { + delete(marks, p) + } + if len(marks) == n { + return nil + } + if b, err = json.MarshalIndent(marks, "", " "); err != nil { + return err + } + return writeAtomic(statePath, b) +} + // moduleRefs returns every module dir referenced by any source's env worktree on disk or by any link in a configured basedir. func (d *Deployer) moduleRefs(liveEnv map[string]bool) (map[string]bool, error) { wts := map[string]bool{} diff --git a/internal/deploy/deploy_test.go b/internal/deploy/deploy_test.go index 3c023e6..95db334 100644 --- a/internal/deploy/deploy_test.go +++ b/internal/deploy/deploy_test.go @@ -7,6 +7,7 @@ import ( "fmt" "log" "log/slog" + "maps" "net" "net/http" "net/http/httptest" @@ -774,40 +775,72 @@ func TestGCKeepsModulesOfRemovedSource(t *testing.T) { func TestNamedDeployTouchesOnlyNamed(t *testing.T) { f := newFixture(t) - commit(t, f.control, map[string]string{"Puppetfile": f.puppetfile(), "manifests/site.pp": "node default {}\n"}) + old := commit(t, f.control, map[string]string{"Puppetfile": f.puppetfile(), "manifests/site.pp": "node default {}\n"}) gitT(t, f.control, "branch", "gone") + commit(t, f.control, map[string]string{"manifests/site.pp": "node other {}\n"}) + gitT(t, f.control, "branch", "gone2") commit(t, f.control, map[string]string{"manifests/site.pp": "node default { }\n"}) gitT(t, f.control, "branch", "benvin/foo-bar") f.mustDeploy(false) - gone := f.env("gone") - gitT(t, f.control, "branch", "-D", "gone") + gone, gone2 := f.env("gone"), f.env("gone2") + gitT(t, f.control, "branch", "-D", "gone", "gone2") f.mustDeploy(false) f.clock = f.clock.Add(11 * time.Minute) - state := readFileT(t, f.cache(gcStateFile)) + marks := func() map[string]time.Time { + m := map[string]time.Time{} + if err := json.Unmarshal([]byte(readFileT(t, f.cache(gcStateFile))), &m); err != nil { + t.Fatal(err) + } + return m + } + before := marks() + if _, ok := before[gone2]; !ok { + t.Fatalf("gone2 must be marked: %v", before) + } - sha := commit(t, f.control, map[string]string{"manifests/site.pp": "node x {}\n"}) - gitT(t, f.control, "branch", "-f", "benvin/foo-bar", "main") + gitT(t, f.control, "branch", "-f", "benvin/foo-bar", old) main := f.env("main") if err := f.deploy(false, "benvin/foo-bar"); err != nil { t.Fatal(err) } - if !strings.HasPrefix(filepath.Base(f.env("benvin_foo_bar")), sha+"-") { - t.Fatal("a raw branch name deploys its sanitised env") + if f.env("benvin_foo_bar") != gone { + t.Fatal("a raw branch name deploys its sanitised env, reviving the matching worktree") } - if f.env("main") != main || !exists(gone) || readFileT(t, f.cache(gcStateFile)) != state { - t.Fatal("a named deploy must leave other envs, their worktrees and GC state untouched") + delete(before, gone) + if f.env("main") != main || !exists(gone2) || !maps.Equal(marks(), before) { + t.Fatalf("a named deploy must clear only the marks of what it revives: %v", marks()) } + sha := commit(t, f.control, map[string]string{"manifests/site.pp": "node x {}\n"}) if err := f.deploy(false, "main"); err != nil || !strings.HasPrefix(filepath.Base(f.env("main")), sha+"-") { t.Fatalf("env name deploys: %v", err) } if err := f.deploy(false, "main", "nope"); err == nil || !strings.Contains(err.Error(), "nope not found") { t.Fatalf("unknown env must error: %v", err) } - if !exists(gone) { + if !exists(gone2) { t.Fatal("named deploys never GC") } } +func TestNamedDeployRevivedWorktreeKeepsGrace(t *testing.T) { + f := newFixture(t) + a := commit(t, f.control, map[string]string{"Puppetfile": f.puppetfile(), "manifests/site.pp": "node a {}\n"}) + f.mustDeploy(false) + wa := f.env("main") + commit(t, f.control, map[string]string{"manifests/site.pp": "node b {}\n"}) + f.mustDeploy(false) + f.clock = f.clock.Add(11 * time.Minute) + gitT(t, f.control, "reset", "-q", "--hard", a) + if err := f.deploy(false, "main"); err != nil || f.env("main") != wa { + t.Fatalf("named deploy must revive wa: %v", err) + } + commit(t, f.control, map[string]string{"manifests/site.pp": "node c {}\n"}) + f.mustDeploy(false) + if !exists(wa) { + t.Fatal("replaced worktree GC'd on a stale mark; grace skipped") + } +} + func TestTimingsPhases(t *testing.T) { f := newFixture(t) f.cfg.W10k.GenerateTypes = true