Merge remote-tracking branch 'origin/benvin/timings' into benvin/named-deploy
# Conflicts: # internal/deploy/deploy.go
This commit is contained in:
+25
-36
@@ -218,6 +218,10 @@ func (d *Deployer) Sources() []*Source {
|
||||
// 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")
|
||||
want := map[string]bool{}
|
||||
for _, e := range only {
|
||||
want[e] = true
|
||||
}
|
||||
d.mu.Lock()
|
||||
clear(d.skips)
|
||||
clear(d.counts)
|
||||
@@ -226,11 +230,11 @@ func (d *Deployer) Deploy(ctx context.Context, only []string, modules bool) erro
|
||||
found := map[string]bool{}
|
||||
var errs []error
|
||||
for _, s := range d.Sources() {
|
||||
if err := d.deploySource(ctx, s, only, found, modules); err != nil {
|
||||
if err := d.deploySource(ctx, s, want, found, modules); err != nil {
|
||||
errs = append(errs, fmt.Errorf("source %s: %w", s.Name, err))
|
||||
}
|
||||
}
|
||||
for _, e := range only {
|
||||
for e := range want {
|
||||
if !found[e] {
|
||||
errs = append(errs, fmt.Errorf("environment %s not found", e))
|
||||
}
|
||||
@@ -261,17 +265,10 @@ func (d *Deployer) count(outcome string) {
|
||||
d.mu.Unlock()
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
type envRef struct{ branch, sha string }
|
||||
|
||||
// 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) })
|
||||
}
|
||||
// Environments maps env name -> branch and sha for every branch, or only those want names by branch or env.
|
||||
func (d *Deployer) Environments(ctx context.Context, s *Source, want map[string]bool) (map[string]envRef, []error) {
|
||||
all, err := d.branches(ctx, d.srcMirror(s))
|
||||
if err != nil {
|
||||
return nil, []error{err}
|
||||
@@ -281,14 +278,14 @@ func (d *Deployer) Environments(ctx context.Context, s *Source, only ...string)
|
||||
names = append(names, b)
|
||||
}
|
||||
sort.Strings(names)
|
||||
envs := map[string]string{}
|
||||
envs := map[string]envRef{}
|
||||
var errs []error
|
||||
for _, b := range names {
|
||||
if s.Ignored(b) {
|
||||
continue
|
||||
}
|
||||
name, corrected, err := s.EnvName(b)
|
||||
if !wanted(b, name) {
|
||||
if len(want) > 0 && !want[b] && (name == "" || !want[name]) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
@@ -302,12 +299,12 @@ func (d *Deployer) Environments(ctx context.Context, s *Source, only ...string)
|
||||
errs = append(errs, fmt.Errorf("branch %s collides with environment %s, skipped", b, name))
|
||||
continue
|
||||
}
|
||||
envs[name] = all[b]
|
||||
envs[name] = envRef{b, all[b]}
|
||||
}
|
||||
return envs, errs
|
||||
}
|
||||
|
||||
func (d *Deployer) deploySource(ctx context.Context, s *Source, only []string, found map[string]bool, modules bool) error {
|
||||
func (d *Deployer) deploySource(ctx context.Context, s *Source, want, 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, nil, envRefspecs)
|
||||
end()
|
||||
@@ -315,30 +312,24 @@ func (d *Deployer) deploySource(ctx context.Context, s *Source, only []string, f
|
||||
return err
|
||||
}
|
||||
bctx, end := d.phase(ctx, "source.branches", "source", s.Name)
|
||||
envs, errs := d.Environments(bctx, s, only...)
|
||||
envs, errs := d.Environments(bctx, s, want)
|
||||
end("envs", len(envs))
|
||||
if envs == nil {
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
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 {
|
||||
for name, ref := range envs {
|
||||
found[name], found[ref.branch] = true, true
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := func() (err error) {
|
||||
defer recoverErr(&err, "environment "+name)
|
||||
return d.deployEnv(ctx, s, name, sha, modules)
|
||||
return d.deployEnv(ctx, s, name, ref, modules)
|
||||
}()
|
||||
if err != nil {
|
||||
mu.Lock()
|
||||
@@ -348,7 +339,7 @@ func (d *Deployer) deploySource(ctx context.Context, s *Source, only []string, f
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
if len(only) == 0 {
|
||||
if len(want) == 0 {
|
||||
errs = append(errs, d.purge(s, envs))
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
@@ -380,7 +371,8 @@ func (d *Deployer) worktreePrefix(sha string) string {
|
||||
return sha + "-"
|
||||
}
|
||||
|
||||
func (d *Deployer) deployEnv(ctx context.Context, s *Source, name, sha string, modules bool) (err error) {
|
||||
func (d *Deployer) deployEnv(ctx context.Context, s *Source, name string, ref envRef, modules bool) (err error) {
|
||||
sha := ref.sha
|
||||
start := time.Now()
|
||||
ctx, total := d.phase(ctx, "env.total", "source", s.Name, "env", name)
|
||||
outcome := "unchanged"
|
||||
@@ -429,7 +421,7 @@ func (d *Deployer) deployEnv(ctx context.Context, s *Source, name, sha string, m
|
||||
if err := d.checkout(ctx, s, sha, target, mods); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return d.prepare(ctx, s, name, sha, checksum, target, mods, started)
|
||||
return d.prepare(ctx, s, name, ref, checksum, target, mods, started)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -662,7 +654,7 @@ func linkModule(root *os.Root, rel, dir string) error {
|
||||
}
|
||||
|
||||
// prepare runs last before a worktree can be swapped in; its out-of-tree done file marks the worktree complete.
|
||||
func (d *Deployer) prepare(ctx context.Context, s *Source, name, sha, checksum, target string, mods []resolved, started time.Time) (string, error) {
|
||||
func (d *Deployer) prepare(ctx context.Context, s *Source, name string, ref envRef, checksum, target string, mods []resolved, started time.Time) (string, error) {
|
||||
var skipped []error
|
||||
if d.cfg.W10k.GenerateTypes {
|
||||
_, end := d.phase(ctx, "env.generate_types")
|
||||
@@ -679,8 +671,8 @@ func (d *Deployer) prepare(ctx context.Context, s *Source, name, sha, checksum,
|
||||
_, end := d.phase(ctx, "env.marker")
|
||||
defer end()
|
||||
r := deployResult{
|
||||
Name: name,
|
||||
Signature: sha,
|
||||
Name: ref.branch,
|
||||
Signature: ref.sha,
|
||||
StartedAt: started,
|
||||
FinishedAt: d.now().UTC(),
|
||||
DeploySuccess: true,
|
||||
@@ -756,7 +748,7 @@ func Swap(target, link string) error {
|
||||
return os.Rename(tmp, link)
|
||||
}
|
||||
|
||||
func (d *Deployer) purge(s *Source, envs map[string]string) error {
|
||||
func (d *Deployer) purge(s *Source, envs map[string]envRef) error {
|
||||
entries, err := os.ReadDir(s.Basedir)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -901,9 +893,6 @@ func (d *Deployer) GC(ctx context.Context) error {
|
||||
liveMod, err := d.moduleRefs(liveEnv)
|
||||
if err != nil {
|
||||
g.errs = append(g.errs, err)
|
||||
for p := range g.marks {
|
||||
g.seen[p] = true
|
||||
}
|
||||
} else {
|
||||
keys, _ := os.ReadDir(d.path("modules"))
|
||||
for _, k := range keys {
|
||||
|
||||
@@ -628,7 +628,7 @@ func TestGenerateTypesBeforeSwap(t *testing.T) {
|
||||
newRepo(t, typed)
|
||||
commit(t, typed, map[string]string{
|
||||
"lib/puppet/type/typed.rb": "Puppet::Type.newtype(:typed) do\n newparam(:name, namevar: true)\nend\n",
|
||||
"lib/puppet/provider/typed/posix.rb": "",
|
||||
"lib/puppet/provider/typed/posix.rb": "Puppet::Type.type(:typed).provide(:posix) do\nend\n",
|
||||
})
|
||||
commit(t, f.control, map[string]string{
|
||||
"Puppetfile": fmt.Sprintf("moduledir 'external_modules'\nmod 'typed', :git => '%s'\n", f.url(typed)),
|
||||
@@ -658,7 +658,7 @@ func TestGenerateTypesBeforeSwap(t *testing.T) {
|
||||
}
|
||||
live = skipped
|
||||
|
||||
commit(t, f.control, map[string]string{"site/local/lib/puppet/type/local.rb": "Puppet::Type.newtype(:local) do\n newparam(:name)\nend\n" + strings.Repeat("module A\n", 300) + strings.Repeat("end\n", 300)})
|
||||
commit(t, f.control, map[string]string{"site/local/lib/puppet/type/local.rb": "Puppet::Type.newtype(:local) do\n newparam(:name) do\n newvalues(" + strings.Repeat("[", 300) + strings.Repeat("]", 300) + ")\n end\nend\n"})
|
||||
if err := f.deploy(false); err == nil || !strings.Contains(err.Error(), "deeper than") || !strings.Contains(err.Error(), "site/local/lib/puppet/type/local.rb") {
|
||||
t.Fatalf("a parser limit must fail the deploy naming the file: %v", err)
|
||||
}
|
||||
@@ -1086,3 +1086,84 @@ func TestConcurrentWorktreeAddsOneMirror(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGCRefsErrorDropsMarkOfLiveWorktree(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
s := &Source{Name: "s", Basedir: filepath.Join(root, "envs")}
|
||||
cfg := &Config{Cachedir: filepath.Join(root, "cache"), Sources: map[string]*Source{"s": s}}
|
||||
clock := time.Unix(1_700_000_000, 0)
|
||||
gc := func() error {
|
||||
d := New(cfg, 1, 10*time.Minute)
|
||||
d.now = func() time.Time { return clock }
|
||||
return d.GC(context.Background())
|
||||
}
|
||||
d := New(cfg, 1, 0)
|
||||
old, cur, broken := filepath.Join(d.srcWorktrees(s), "old"), filepath.Join(d.srcWorktrees(s), "cur"), filepath.Join(d.srcWorktrees(s), "broken")
|
||||
mkdirs(t, old, cur, broken, s.Basedir, filepath.Dir(d.doneFile(old)))
|
||||
for _, p := range []string{old, cur} {
|
||||
if err := os.WriteFile(d.doneFile(p), []byte(`{"modules":[]}`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
link := filepath.Join(s.Basedir, "develop")
|
||||
if err := Swap(cur, link); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := gc(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
clock = clock.Add(time.Hour)
|
||||
if err := Swap(old, link); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Swap(broken, filepath.Join(s.Basedir, "other")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := gc(); err == nil {
|
||||
t.Fatal("want moduleRefs error from live worktree without done file")
|
||||
}
|
||||
if err := os.WriteFile(d.doneFile(broken), []byte(`{"modules":[]}`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Swap(cur, link); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = gc()
|
||||
if !exists(old) {
|
||||
t.Fatal("worktree unreferenced this run was deleted in the same run via a stale mark kept while it was live")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeployOnlyLeavesLaterSourceAlone(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
commit(t, f.control, map[string]string{"manifests/site.pp": "node default {}\n"})
|
||||
gitT(t, f.control, "branch", "production")
|
||||
other := filepath.Join(f.root, "other")
|
||||
newRepo(t, other)
|
||||
commit(t, other, map[string]string{"manifests/site.pp": "node default {}\n"})
|
||||
otherBase := filepath.Join(f.root, "other-envs")
|
||||
cfg, err := ParseConfig(fmt.Appendf(nil, "cachedir: %s\nsources:\n aaa:\n remote: %s\n basedir: %s\n zzz:\n remote: %s\n basedir: %s\n", f.cfg.Cachedir, f.control, f.basedir, other, otherBase))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.cfg = cfg
|
||||
if err := f.deploy(false, "production"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if exists(filepath.Join(otherBase, "main")) {
|
||||
t.Fatal("deploy production also deployed every env of the next source")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeployMarkerNameIsBranch(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
commit(t, f.control, map[string]string{"manifests/site.pp": "node default {}\n"})
|
||||
gitT(t, f.control, "checkout", "-q", "-b", "feat/x")
|
||||
commit(t, f.control, map[string]string{"manifests/site.pp": "node x {}\n"})
|
||||
if err := f.deploy(false, "feat_x"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m := readMarker(t, f.env("feat_x")); m.Name != "feat/x" {
|
||||
t.Fatalf("marker name %q, want branch feat/x", m.Name)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user