-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
exec.go
197 lines (173 loc) · 4.51 KB
/
exec.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
package solver
import (
"encoding/json"
"fmt"
"path"
"runtime"
"sort"
"strings"
"github.com/moby/buildkit/cache"
"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/progress/logs"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
const execCacheType = "buildkit.exec.v0"
type execOp struct {
op *pb.ExecOp
cm cache.Manager
exec executor.Executor
numInputs int
}
func newExecOp(v Vertex, op *pb.Op_Exec, cm cache.Manager, exec executor.Executor) (Op, error) {
return &execOp{
op: op.Exec,
cm: cm,
exec: exec,
numInputs: len(v.Inputs()),
}, nil
}
func (e *execOp) CacheKey(ctx context.Context) (digest.Digest, error) {
dt, err := json.Marshal(struct {
Type string
Exec *pb.ExecOp
OS string
Arch string
}{
Type: execCacheType,
Exec: e.op,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
})
if err != nil {
return "", err
}
return digest.FromBytes(dt), nil
}
func (e *execOp) Run(ctx context.Context, inputs []Reference) ([]Reference, error) {
var mounts []executor.Mount
var outputs []Reference
var root cache.Mountable
defer func() {
for _, o := range outputs {
if o != nil {
go o.Release(context.TODO())
}
}
}()
for _, m := range e.op.Mounts {
var mountable cache.Mountable
var ref cache.ImmutableRef
if m.Input != pb.Empty {
if int(m.Input) > len(inputs) {
return nil, errors.Errorf("missing input %d", m.Input)
}
inp := inputs[int(m.Input)]
var ok bool
ref, ok = toImmutableRef(inp)
if !ok {
return nil, errors.Errorf("invalid reference for exec %T", inputs[int(m.Input)])
}
mountable = ref
}
if m.Output != pb.SkipOutput {
if m.Readonly && ref != nil && m.Dest != pb.RootMount { // exclude read-only rootfs
outputs = append(outputs, newSharedRef(ref).Clone())
} else {
active, err := e.cm.New(ctx, ref, cache.WithDescription(fmt.Sprintf("mount %s from exec %s", m.Dest, strings.Join(e.op.Meta.Args, " ")))) // TODO: should be method
if err != nil {
return nil, err
}
outputs = append(outputs, active)
mountable = active
}
}
if m.Dest == pb.RootMount {
root = mountable
} else {
mounts = append(mounts, executor.Mount{Src: mountable, Dest: m.Dest, Readonly: m.Readonly, Selector: m.Selector})
}
}
sort.Slice(mounts, func(i, j int) bool {
return mounts[i].Dest < mounts[j].Dest
})
meta := executor.Meta{
Args: e.op.Meta.Args,
Env: e.op.Meta.Env,
Cwd: e.op.Meta.Cwd,
User: e.op.Meta.User,
}
stdout, stderr := logs.NewLogStreams(ctx)
defer stdout.Close()
defer stderr.Close()
if err := e.exec.Exec(ctx, meta, root, mounts, nil, stdout, stderr); err != nil {
return nil, errors.Wrapf(err, "executor failed running %v", meta.Args)
}
refs := []Reference{}
for i, o := range outputs {
if mutable, ok := o.(cache.MutableRef); ok {
ref, err := mutable.Commit(ctx)
if err != nil {
return nil, errors.Wrapf(err, "error committing %s", mutable.ID())
}
refs = append(refs, ref)
} else {
refs = append(refs, o)
}
outputs[i] = nil
}
return refs, nil
}
func (e *execOp) ContentMask(ctx context.Context) (digest.Digest, [][]string, error) {
// contentKey for exec uses content based checksum for mounts and definition
// based checksum for root
skipped := make(map[int]struct{}, 0)
type src struct {
index int
selector string
}
srcsMap := make(map[src]struct{})
mountsCopy := make([]*pb.Mount, len(e.op.Mounts))
for i, m := range e.op.Mounts {
copy := *m
mountsCopy[i] = ©
if m.Input != pb.Empty {
if m.Dest != pb.RootMount && m.Readonly { // could also include rw if they don't have a selector, but not sure if helps performance
srcsMap[src{int(m.Input), path.Join("/", m.Selector)}] = struct{}{}
mountsCopy[i].Selector = ""
} else {
skipped[int(m.Input)] = struct{}{}
}
}
}
if len(srcsMap) == 0 {
return "", nil, nil
}
contentInputs := make([][]string, e.numInputs)
for in := range srcsMap {
contentInputs[in.index] = append(contentInputs[in.index], in.selector)
}
// TODO: remove nested directories
for k := range contentInputs {
sort.Strings(contentInputs[k])
}
ecopy := *e.op
ecopy.Mounts = mountsCopy
dt, err := json.Marshal(struct {
Type string
Exec *pb.ExecOp
OS string
Arch string
}{
Type: execCacheType,
Exec: &ecopy,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
})
if err != nil {
return "", nil, err
}
return digest.FromBytes(dt), contentInputs, nil
}