/
git_archive.go
100 lines (82 loc) · 3.27 KB
/
git_archive.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
package stage
import (
"context"
"fmt"
"sort"
"github.com/werf/werf/pkg/container_runtime"
"github.com/werf/werf/pkg/git_repo"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/util"
)
type NewGitArchiveStageOptions struct {
ArchivesDir string
ScriptsDir string
ContainerArchivesDir string
ContainerScriptsDir string
}
func NewGitArchiveStage(gitArchiveStageOptions *NewGitArchiveStageOptions, baseStageOptions *NewBaseStageOptions) *GitArchiveStage {
s := &GitArchiveStage{
ArchivesDir: gitArchiveStageOptions.ArchivesDir,
ScriptsDir: gitArchiveStageOptions.ScriptsDir,
ContainerArchivesDir: gitArchiveStageOptions.ContainerArchivesDir,
ContainerScriptsDir: gitArchiveStageOptions.ContainerScriptsDir,
}
s.GitStage = newGitStage(GitArchive, baseStageOptions)
return s
}
type GitArchiveStage struct {
*GitStage
ArchivesDir string
ScriptsDir string
ContainerArchivesDir string
ContainerScriptsDir string
}
func (s *GitArchiveStage) SelectSuitableStage(ctx context.Context, c Conveyor, stages []*image.StageDescription) (*image.StageDescription, error) {
ancestorsStages, err := s.selectStagesAncestorsByGitMappings(ctx, c, stages)
if err != nil {
return nil, fmt.Errorf("unable to select cache images ancestors by git mappings: %s", err)
}
return s.selectStageByOldestCreationTimestamp(ancestorsStages)
}
// TODO: 1.3 add git mapping type (dir, file, ...) to gitArchive stage digest
func (s *GitArchiveStage) GetDependencies(ctx context.Context, c Conveyor, _, _ container_runtime.LegacyImageInterface) (string, error) {
var args []string
for _, gitMapping := range s.gitMappings {
if gitMapping.IsLocal() {
if err := c.GiterminismManager().Inspector().InspectBuildContextFiles(ctx, gitMapping.getPathMatcher()); err != nil {
return "", err
}
}
args = append(args, gitMapping.GetParamshash())
}
sort.Strings(args)
return util.Sha256Hash(args...), nil
}
func (s *GitArchiveStage) GetNextStageDependencies(ctx context.Context, c Conveyor) (string, error) {
return s.BaseStage.getNextStageGitDependencies(ctx, c)
}
func (s *GitArchiveStage) PrepareImage(ctx context.Context, c Conveyor, prevBuiltImage, image container_runtime.LegacyImageInterface) error {
if err := s.GitStage.PrepareImage(ctx, c, prevBuiltImage, image); err != nil {
return err
}
for _, gitMapping := range s.gitMappings {
if err := gitMapping.ApplyArchiveCommand(ctx, c, image); err != nil {
return err
}
}
image.Container().RunOptions().AddVolume(fmt.Sprintf("%s:%s:ro", git_repo.CommonGitDataManager.GetArchivesCacheDir(), s.ContainerArchivesDir))
image.Container().RunOptions().AddVolume(fmt.Sprintf("%s:%s:ro", s.ScriptsDir, s.ContainerScriptsDir))
return nil
}
func (s *GitArchiveStage) IsEmpty(ctx context.Context, c Conveyor, img container_runtime.LegacyImageInterface) (bool, error) {
for _, gitMapping := range s.gitMappings {
isGitMappingEmpty, err := gitMapping.isEmpty(ctx, c)
if err != nil {
return false, fmt.Errorf("error checking git mapping emptiness: %s", err)
}
if isGitMappingEmpty {
return false, fmt.Errorf(`"git.add: /%s" in werf.yaml matches no files. git.add requires at least one file matched by it. Fix and retry`, gitMapping.Add)
}
}
return s.GitStage.IsEmpty(ctx, c, img)
}