From cee4a1fb8d5295023cb023d205969bd3a7983c1b Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Sun, 8 Nov 2026 20:31:06 +1100 Subject: [PATCH] Scope named deploys to their envs and complete env args --- internal/deploy/deploy.go | 53 ++++++++++++++++++++++------------ internal/deploy/deploy_test.go | 36 +++++++++++++++++++++++ main.go | 51 +++++++++++++++++++++----------- 3 files changed, 105 insertions(+), 35 deletions(-) diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index 6e3d49d..239e367 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -168,27 +168,29 @@ 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. func (d *Deployer) Deploy(ctx context.Context, only []string, modules bool) error { ctx, end := d.phase(ctx, "run") - want := map[string]bool{} - for _, e := range only { - want[e] = true - } d.mu.Lock() clear(d.skips) clear(d.counts) d.mu.Unlock() + found := map[string]bool{} var errs []error for _, s := range d.Sources() { - if err := d.deploySource(ctx, s, want, modules); err != nil { + if err := d.deploySource(ctx, s, only, found, modules); err != nil { errs = append(errs, fmt.Errorf("source %s: %w", s.Name, err)) } } - for e := range want { - errs = append(errs, fmt.Errorf("environment %s not found", e)) + for _, e := range only { + if !found[e] { + errs = append(errs, fmt.Errorf("environment %s not found", e)) + } } - if err := d.GC(ctx); err != nil { - errs = append(errs, err) + if len(only) == 0 { + if err := d.GC(ctx); err != nil { + errs = append(errs, err) + } } d.mu.Lock() for _, e := range slices.Sorted(maps.Keys(d.skips)) { @@ -209,7 +211,17 @@ func (d *Deployer) count(outcome string) { d.mu.Unlock() } -func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]string, []error) { +// Matches reports whether arg names env, either directly or as a branch that sanitises to it. +func (s *Source) Matches(arg, env string) bool { + n, _, err := s.EnvName(arg) + return arg == env || err == nil && n == env +} + +// Environments maps env name -> sha for every branch, or only the branches only names. +func (d *Deployer) Environments(ctx context.Context, s *Source, only ...string) (map[string]string, []error) { + wanted := func(branch, name string) bool { + return len(only) == 0 || slices.ContainsFunc(only, func(a string) bool { return a == branch || name != "" && s.Matches(a, name) }) + } all, err := d.branches(ctx, d.srcMirror(s)) if err != nil { return nil, []error{err} @@ -226,6 +238,9 @@ func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]stri continue } name, corrected, err := s.EnvName(b) + if !wanted(b, name) { + continue + } if err != nil { errs = append(errs, err) continue @@ -242,7 +257,7 @@ func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]stri return envs, errs } -func (d *Deployer) deploySource(ctx context.Context, s *Source, want map[string]bool, modules bool) error { +func (d *Deployer) deploySource(ctx context.Context, s *Source, only []string, found map[string]bool, modules bool) error { fctx, end := d.phase(ctx, "source.fetch", "source", s.Name) err := d.syncMirror(fctx, d.srcMirror(s), s.Remote, envRefspecs...) end() @@ -250,22 +265,24 @@ func (d *Deployer) deploySource(ctx context.Context, s *Source, want map[string] return err } bctx, end := d.phase(ctx, "source.branches", "source", s.Name) - envs, errs := d.Environments(bctx, s) + envs, errs := d.Environments(bctx, s, only...) end("envs", len(envs)) if envs == nil { return errors.Join(errs...) } - full := len(want) == 0 + for name := range envs { + for _, a := range only { + if s.Matches(a, name) { + found[a] = true + } + } + } if err := os.MkdirAll(s.Basedir, 0o755); err != nil { return err } var mu sync.Mutex var wg sync.WaitGroup for name, sha := range envs { - if !full && !want[name] { - continue - } - delete(want, name) wg.Add(1) go func() { defer wg.Done() @@ -277,7 +294,7 @@ func (d *Deployer) deploySource(ctx context.Context, s *Source, want map[string] }() } wg.Wait() - if full { + if len(only) == 0 { errs = append(errs, d.purge(s, envs)) } return errors.Join(errs...) diff --git a/internal/deploy/deploy_test.go b/internal/deploy/deploy_test.go index 77d8ba7..3c023e6 100644 --- a/internal/deploy/deploy_test.go +++ b/internal/deploy/deploy_test.go @@ -772,6 +772,42 @@ 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"}) + gitT(t, f.control, "branch", "gone") + 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") + f.mustDeploy(false) + f.clock = f.clock.Add(11 * time.Minute) + state := readFileT(t, f.cache(gcStateFile)) + + sha := commit(t, f.control, map[string]string{"manifests/site.pp": "node x {}\n"}) + gitT(t, f.control, "branch", "-f", "benvin/foo-bar", "main") + 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("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") + } + 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) { + t.Fatal("named deploys never GC") + } +} + func TestTimingsPhases(t *testing.T) { f := newFixture(t) f.cfg.W10k.GenerateTypes = true diff --git a/main.go b/main.go index 18f7850..6efec92 100644 --- a/main.go +++ b/main.go @@ -4,10 +4,11 @@ import ( "context" "fmt" "log/slog" + "maps" "os" "os/signal" "path/filepath" - "sort" + "slices" "strings" "syscall" "time" @@ -50,6 +51,24 @@ func rootCmd() *cobra.Command { } return d, nil } + live := func() ([][3]string, error) { + d, err := load() + if err != nil { + return nil, err + } + var rows [][3]string + for _, s := range d.Sources() { + live, err := d.Live(s) + if err != nil { + return nil, err + } + for _, e := range slices.Sorted(maps.Keys(live)) { + sha, _, _ := strings.Cut(filepath.Base(live[e]), "-") + rows = append(rows, [3]string{s.Name, e, sha}) + } + } + return rows, nil + } locked := func(fn func(*deploy.Deployer) error) error { d, err := load() if err != nil { @@ -77,10 +96,20 @@ func rootCmd() *cobra.Command { var modules bool deployCmd := &cobra.Command{ Use: "deploy [env...]", - Short: "Deploy all environments, or only the named ones", + Short: "Deploy all environments, or only the named ones (env or branch names)", RunE: func(cmd *cobra.Command, args []string) error { return locked(func(d *deploy.Deployer) error { return d.Deploy(cmd.Context(), args, modules) }) }, + ValidArgsFunction: func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { + rows, _ := live() + var envs []string + for _, r := range rows { + if !slices.Contains(args, r[1]) && !slices.Contains(envs, r[1]) { + envs = append(envs, r[1]) + } + } + return envs, cobra.ShellCompDirectiveNoFileComp + }, } deployCmd.Flags().BoolVarP(&modules, "modules", "m", false, "re-resolve Puppetfile modules for unchanged environments") @@ -89,24 +118,12 @@ func rootCmd() *cobra.Command { Short: "List deployed environments", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { - d, err := load() + rows, err := live() if err != nil { return err } - for _, s := range d.Sources() { - live, err := d.Live(s) - if err != nil { - return err - } - envs := make([]string, 0, len(live)) - for e := range live { - envs = append(envs, e) - } - sort.Strings(envs) - for _, e := range envs { - sha, _, _ := strings.Cut(filepath.Base(live[e]), "-") - _, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\t%s\t%s\n", s.Name, e, sha) - } + for _, r := range rows { + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\t%s\t%s\n", r[0], r[1], r[2]) } return nil },