/
copy.go
62 lines (50 loc) · 2.62 KB
/
copy.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
package instruction
import (
"context"
"fmt"
"github.com/werf/werf/pkg/build/stage"
"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/container_backend"
backend_instruction "github.com/werf/werf/pkg/container_backend/instruction"
"github.com/werf/werf/pkg/dockerfile"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
"github.com/werf/werf/pkg/util"
)
type Copy struct {
*Base[*dockerfile_instruction.Copy, *backend_instruction.Copy]
}
func NewCopy(name stage.StageName, i *dockerfile.DockerfileStageInstruction[*dockerfile_instruction.Copy], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Copy {
return &Copy{Base: NewBase(name, i, backend_instruction.NewCopy(*i.Data), dependencies, hasPrevStage, opts)}
}
func (stg *Copy) ExpandInstruction(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevBuiltImage, stageImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) error {
if stg.instruction.Data.From != "" {
if ds := stg.instruction.GetDependencyByStageRef(stg.instruction.Data.From); ds != nil {
depStageImageName := c.GetImageNameForLastImageStage(ds.WerfImageName())
stg.backendInstruction.From = depStageImageName
}
}
return nil
}
func (stg *Copy) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) {
args, err := stg.getDependencies(ctx, c, cb, prevImage, prevBuiltImage, buildContextArchive, stg)
if err != nil {
return "", err
}
args = append(args, "Instruction", stg.instruction.Data.Name())
args = append(args, "From", stg.instruction.Data.From)
args = append(args, append([]string{"Src"}, stg.instruction.Data.Src...)...)
args = append(args, "Dst", stg.instruction.Data.Dst)
args = append(args, "Chown", stg.instruction.Data.Chown)
args = append(args, "Chmod", stg.instruction.Data.Chmod)
args = append(args, "ExpandedFrom", stg.backendInstruction.From)
if stg.UsesBuildContext() {
if srcChecksum, err := calculateBuildContextGlobsChecksum(ctx, stg.instruction.Data.Src, false, buildContextArchive); err != nil {
return "", fmt.Errorf("unable to calculate build context globs checksum: %w", err)
} else {
args = append(args, "SrcChecksum", srcChecksum)
}
}
// TODO(ilya-lesikov): should checksum of files from other image be calculated if --from specified?
// TODO(staged-dockerfile): support --link option: https://docs.docker.com/engine/reference/builder/#copy---link
return util.Sha256Hash(args...), nil
}