forked from terramate-io/terramate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
557 lines (457 loc) · 13 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright 2021 Mineiros GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package terramate
import (
"errors"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"sort"
"github.com/madlambda/spells/errutil"
"github.com/mineiros-io/terramate/git"
"github.com/mineiros-io/terramate/hcl"
"github.com/mineiros-io/terramate/project"
"github.com/mineiros-io/terramate/stack"
"github.com/rs/zerolog/log"
)
const ErrDirtyRepo errutil.Error = "the repository is not clean"
type (
// Manager is the terramate stacks manager.
Manager struct {
root string // root is the project's root directory
gitBaseRef string // gitBaseRef is the git ref where we compare changes.
stackLoader stack.Loader
}
// Entry is a stack entry result.
Entry struct {
Stack stack.S
Reason string // Reason why this entry was returned.
}
)
// NewManager creates a new stack manager. The rootdir is the project's
// directory and gitBaseRef is the git reference to compare against for changes.
func NewManager(rootdir string, gitBaseRef string) *Manager {
return &Manager{
root: rootdir,
gitBaseRef: gitBaseRef,
stackLoader: stack.NewLoader(rootdir),
}
}
// List walks the basedir directory looking for terraform stacks.
// It returns a lexicographic sorted list of stack directories.
func (m *Manager) List() ([]Entry, error) {
logger := log.With().
Str("action", "Manager.List()").
Logger()
logger.Debug().Msg("List stacks.")
entries, err := ListStacks(m.root)
if err != nil {
return nil, err
}
logger.Trace().Str("repo", m.root).Msg("Create git wrapper for repo.")
g, err := git.WithConfig(git.Config{
WorkingDir: m.root,
})
if err != nil {
return nil, err
}
logger.Trace().Msg("Check if path is git repo.")
if !g.IsRepository() {
return entries, nil
}
err = checkRepoIsClean(g)
if err == nil || errors.Is(err, ErrDirtyRepo) {
return entries, err
}
return nil, err
}
// ListChanged lists the stacks that have changed on the current branch,
// compared to the main branch. This method assumes a version control
// system in place and that you are working on a branch that is not main.
// It's an error to call this method in a directory that's not
// inside a repository or a repository with no commits in it.
func (m *Manager) ListChanged() ([]Entry, error) {
logger := log.With().
Str("action", "ListChanged()").
Logger()
logger.Debug().
Msg("List changed files.")
files, err := listChangedFiles(m.root, m.gitBaseRef)
if err != nil {
return nil, err
}
stackSet := map[string]Entry{}
logger.Trace().
Msg("Range over files.")
for _, path := range files {
logger.Trace().
Msg("Get dir name.")
dirname := filepath.Dir(filepath.Join(m.root, path))
if _, ok := stackSet[project.PrjAbsPath(m.root, dirname)]; ok {
continue
}
logger.Debug().
Str("path", dirname).
Msg("Try load changed.")
s, found, err := m.stackLoader.TryLoadChanged(m.root, dirname)
if err != nil {
return nil, fmt.Errorf("listing changed files: %w", err)
}
if !found {
logger.Debug().
Str("path", dirname).
Msg("Lookup parent stack.")
s, found, err = stack.LookupParent(m.root, dirname)
if err != nil {
return nil, fmt.Errorf("listing changed files: %w", err)
}
if !found {
continue
}
}
stackSet[s.PrjAbsPath()] = Entry{
Stack: s,
Reason: "stack has unmerged changes",
}
}
logger.Debug().
Msg("Get list of all stacks.")
allstacks, err := m.List()
if err != nil {
return nil, fmt.Errorf("searching for stacks: %v", err)
}
logger.Trace().
Msg("Range over all stacks.")
for _, stackEntry := range allstacks {
stack := stackEntry.Stack
if _, ok := stackSet[stack.PrjAbsPath()]; ok {
continue
}
logger.Debug().
Stringer("stack", stack).
Msg("Apply function to stack.")
err := m.filesApply(stack.AbsPath(), func(file fs.DirEntry) error {
if path.Ext(file.Name()) != ".tf" {
return nil
}
logger.Debug().
Stringer("stack", stack).
Msg("Get tf file path.")
tfpath := filepath.Join(stack.AbsPath(), file.Name())
logger.Trace().
Stringer("stack", stack).
Str("configFile", tfpath).
Msg("Parse modules.")
modules, err := hcl.ParseModules(tfpath)
if err != nil {
return fmt.Errorf("parsing modules at %q: %w",
file.Name(), err)
}
logger.Trace().
Stringer("stack", stack).
Str("configFile", tfpath).
Msg("Range over modules.")
for _, mod := range modules {
logger.Trace().
Stringer("stack", stack).
Str("configFile", tfpath).
Msg("Check if module changed.")
changed, why, err := m.moduleChanged(mod, stack.AbsPath(), make(map[string]bool))
if err != nil {
return fmt.Errorf("checking module %q: %w", mod.Source, err)
}
if changed {
logger.Debug().
Stringer("stack", stack).
Str("configFile", tfpath).
Msg("Module changed.")
stack.SetChanged(true)
stackSet[stack.PrjAbsPath()] = Entry{
Stack: stack,
Reason: fmt.Sprintf("stack changed because %q changed because %s", mod.Source, why),
}
return nil
}
}
return nil
})
if err != nil {
return nil, fmt.Errorf("checking module changes: %w", err)
}
}
logger.Trace().
Msg("Make set of changed stacks.")
changedStacks := make([]Entry, 0, len(stackSet))
for _, stack := range stackSet {
changedStacks = append(changedStacks, stack)
}
logger.Trace().
Msg("Sort changed stacks.")
sort.Sort(EntrySlice(changedStacks))
return changedStacks, nil
}
func (m *Manager) filesApply(dir string, apply func(file fs.DirEntry) error) error {
logger := log.With().
Str("action", "filesApply()").
Str("path", dir).
Logger()
logger.Debug().
Msg("Read dir.")
files, err := os.ReadDir(dir)
if err != nil {
return fmt.Errorf("listing files of directory %q: %w", dir, err)
}
logger.Trace().
Msg("Range files in dir.")
for _, file := range files {
if file.IsDir() {
continue
}
logger.Debug().
Msg("Apply function to file.")
err := apply(file)
if err != nil {
return fmt.Errorf("applying operation to file %q: %w", file, err)
}
}
return nil
}
// moduleChanged recursively check if the module mod or any of the modules it
// uses has changed. All .tf files of the module are parsed and this function is
// called recursively. The visited keep track of the modules already parsed to
// avoid infinite loops.
func (m *Manager) moduleChanged(
mod hcl.Module, basedir string, visited map[string]bool,
) (changed bool, why string, err error) {
logger := log.With().
Str("action", "moduleChanged()").
Logger()
if _, ok := visited[mod.Source]; ok {
return false, "", nil
}
logger.Trace().
Str("path", basedir).
Msg("Check if module source is local directory.")
if !mod.IsLocal() {
// if the source is a remote path (URL, VCS path, S3 bucket, etc) then
// we assume it's not changed.
return false, "", nil
}
logger.Trace().
Str("path", basedir).
Msg("Get module path.")
modPath := filepath.Join(basedir, mod.Source)
logger.Trace().
Str("path", modPath).
Msg("Get module path info.")
st, err := os.Stat(modPath)
// TODO(i4k): resolve symlinks
if err != nil || !st.IsDir() {
return false, "", fmt.Errorf("\"source\" path %q is not a directory", modPath)
}
logger.Debug().
Str("path", modPath).
Msg("Get list of changed files.")
changedFiles, err := listChangedFiles(modPath, m.gitBaseRef)
if err != nil {
return false, "", fmt.Errorf("listing changes in the module %q: %w",
mod.Source, err)
}
if len(changedFiles) > 0 {
return true, fmt.Sprintf("module %q has unmerged changes", mod.Source), nil
}
visited[mod.Source] = true
logger.Debug().
Str("path", modPath).
Msg("Apply function to files in path.")
err = m.filesApply(modPath, func(file fs.DirEntry) error {
if changed {
return nil
}
if path.Ext(file.Name()) != ".tf" {
return nil
}
logger.Trace().
Str("path", modPath).
Msg("Parse modules.")
modules, err := hcl.ParseModules(filepath.Join(modPath, file.Name()))
if err != nil {
return fmt.Errorf("parsing module %q: %w", mod.Source, err)
}
logger.Trace().
Str("path", modPath).
Msg("Range over modules.")
for _, mod2 := range modules {
var reason string
logger.Trace().
Str("path", modPath).
Msg("Get if module is changed.")
changed, reason, err = m.moduleChanged(mod2, modPath, visited)
if err != nil {
return err
}
if changed {
logger.Trace().
Str("path", modPath).
Msg("Module was changed.")
why = fmt.Sprintf("%s%s changed because %s ", why, mod.Source,
reason)
return nil
}
}
return nil
})
if err != nil {
return false, "", err
}
return changed, fmt.Sprintf("module %q changed because %s", mod.Source, why), nil
}
func (m *Manager) AddWantedOf(stacks []stack.S) ([]stack.S, error) {
wantedBy := map[string]stack.S{}
wanted := []stack.S{}
for _, s := range stacks {
wantedBy[s.PrjAbsPath()] = s
wanted = append(wanted, s)
}
visited := map[string]struct{}{}
for len(wantedBy) > 0 {
for _, s := range wantedBy {
logger := log.With().
Str("action", "AddWantedOf()").
Stringer("stack", s).
Logger()
logger.Debug().Msg("Loading \"wanted\" stacks.")
wantedStacks, err := m.stackLoader.LoadAll(m.root, s.AbsPath(), s.Wants()...)
if err != nil {
return nil, fmt.Errorf("calculating wanted stacks: %v", err)
}
logger.Debug().Msg("The \"wanted\" stacks were loaded successfully.")
for _, wantedStack := range wantedStacks {
if wantedStack.AbsPath() == s.AbsPath() {
logger.Warn().
Stringer("stack", s).
Msgf("stack %q wants itself.", s)
continue
}
if _, ok := visited[wantedStack.PrjAbsPath()]; !ok {
wanted = append(wanted, wantedStack)
visited[wantedStack.PrjAbsPath()] = struct{}{}
wantedBy[wantedStack.PrjAbsPath()] = wantedStack
}
}
delete(wantedBy, s.PrjAbsPath())
}
}
stack.Sort(wanted)
return wanted, nil
}
// listChangedFiles lists all changed files in the dir directory.
func listChangedFiles(dir string, gitBaseRef string) ([]string, error) {
logger := log.With().
Str("action", "listChangedFiles()").
Str("path", dir).
Logger()
logger.Trace().
Msg("Get dir info.")
st, err := os.Stat(dir)
if err != nil {
return nil, fmt.Errorf("stat failed on %q: %w", dir, err)
}
logger.Trace().
Msg("Check if path is dir.")
if !st.IsDir() {
return nil, fmt.Errorf("is not a directory")
}
logger.Trace().
Msg("Create git wrapper with dir.")
g, err := git.WithConfig(git.Config{
WorkingDir: dir,
})
if err != nil {
return nil, err
}
logger.Trace().
Msg("Check if path is git repo.")
if !g.IsRepository() {
return nil, fmt.Errorf("the path \"%s\" is not a git repository", dir)
}
err = checkRepoIsClean(g)
if err != nil {
return nil, err
}
logger.Trace().
Msg("Get commit id of git base ref.")
baseRef, err := g.RevParse(gitBaseRef)
if err != nil {
return nil, fmt.Errorf("getting revision %q: %w", gitBaseRef, err)
}
logger.Trace().
Msg("Get commit id of HEAD.")
headRef, err := g.RevParse("HEAD")
if err != nil {
return nil, fmt.Errorf("getting HEAD revision: %w", err)
}
if baseRef == headRef {
return []string{}, nil
}
logger.Trace().
Msg("Find common commit ancestor of HEAd and base ref.")
mergeBaseRef, err := g.MergeBase("HEAD", baseRef)
if err != nil {
return nil, fmt.Errorf("getting merge-base HEAD main: %w", err)
}
if baseRef != mergeBaseRef {
return nil, fmt.Errorf("main branch is not reachable: main ref %q can't reach %q",
baseRef, mergeBaseRef)
}
return g.DiffNames(baseRef, headRef)
}
func checkRepoIsClean(g *git.Git) error {
logger := log.With().
Str("action", "checkRepoIsClean()").
Logger()
logger.Debug().
Msg("Get list of untracked files.")
untracked, err := g.ListUntracked()
if err != nil {
return fmt.Errorf("listing untracked files: %v", err)
}
logger.Debug().
Msg("Get list of uncommitted files in dir.")
uncommitted, err := g.ListUncommitted()
if err != nil {
return fmt.Errorf("listing uncommitted files: %v", err)
}
if len(untracked) > 0 {
logger.Warn().
Strs("files", untracked).
Msg("repository has untracked files")
}
if len(uncommitted) > 0 {
log.Warn().
Strs("files", uncommitted).
Msg("repository has uncommitted files")
}
if len(uncommitted)+len(untracked) > 0 {
return ErrDirtyRepo
}
return nil
}
// EntrySlice implements the Sort interface.
type EntrySlice []Entry
func (x EntrySlice) Len() int { return len(x) }
func (x EntrySlice) Less(i, j int) bool { return x[i].Stack.PrjAbsPath() < x[j].Stack.PrjAbsPath() }
func (x EntrySlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }