/
run.go
74 lines (59 loc) · 1.78 KB
/
run.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
package instruction
import (
"context"
"fmt"
"strings"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/werf/logboek"
"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/container_backend"
)
type Run struct {
*instructions.RunCommand
}
func NewRun(i *instructions.RunCommand) *Run {
return &Run{RunCommand: i}
}
func (i *Run) UsesBuildContext() bool {
for _, mount := range i.GetMounts() {
if mount.Type == instructions.MountTypeBind && mount.From == "" {
return true
}
}
return false
}
func (i *Run) GetMounts() []*instructions.Mount {
return instructions.GetMounts(i.RunCommand)
}
func (i *Run) GetSecurity() string {
return instructions.GetSecurity(i.RunCommand)
}
func (i *Run) GetNetwork() string {
return instructions.GetNetwork(i.RunCommand)
}
func (i *Run) Apply(ctx context.Context, containerName string, drv buildah.Buildah, drvOpts buildah.CommonOpts, buildContextArchive container_backend.BuildContextArchiver) error {
var contextDir string
if i.UsesBuildContext() {
var err error
contextDir, err = buildContextArchive.ExtractOrGetExtractedDir(ctx)
if err != nil {
return fmt.Errorf("unable to extract build context: %w", err)
}
}
var addCapabilities []string
if i.GetSecurity() == "insecure" {
addCapabilities = []string{"all"}
}
logboek.Context(ctx).Default().LogF("$ %s\n", strings.Join(i.CmdLine, " "))
if err := drv.RunCommand(ctx, containerName, i.CmdLine, buildah.RunCommandOpts{
CommonOpts: drvOpts,
ContextDir: contextDir,
PrependShell: i.PrependShell,
AddCapabilities: addCapabilities,
NetworkType: i.GetNetwork(),
RunMounts: i.GetMounts(),
}); err != nil {
return fmt.Errorf("error running command %v for container %s: %w", i.CmdLine, containerName, err)
}
return nil
}