-
Notifications
You must be signed in to change notification settings - Fork 13
/
handle_zip.go
234 lines (187 loc) 路 6.28 KB
/
handle_zip.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
package windows
import (
"bytes"
"context"
"fmt"
"path/filepath"
"strings"
"github.com/Azure/dalec"
"github.com/Azure/dalec/frontend"
"github.com/moby/buildkit/client/llb"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
const (
workerImgRef = "mcr.microsoft.com/mirror/docker/library/ubuntu:jammy"
outputDir = "/tmp/output"
buildScriptName = "_build.sh"
aptCachePrefix = "jammy-windowscross"
)
func handleZip(ctx context.Context, client gwclient.Client) (*gwclient.Result, error) {
return frontend.BuildWithPlatform(ctx, client, func(ctx context.Context, client gwclient.Client, platform *ocispecs.Platform, spec *dalec.Spec, targetKey string) (gwclient.Reference, *dalec.DockerImageSpec, error) {
sOpt, err := frontend.SourceOptFromClient(ctx, client)
if err != nil {
return nil, nil, err
}
pg := dalec.ProgressGroup("Build windows container: " + spec.Name)
worker := workerImg(sOpt, pg)
bin, err := buildBinaries(spec, worker, sOpt, targetKey)
if err != nil {
return nil, nil, fmt.Errorf("unable to build binaries: %w", err)
}
if signer, ok := spec.GetSigner(targetKey); ok {
signed, err := frontend.ForwardToSigner(ctx, client, platform, signer, bin)
if err != nil {
return nil, nil, err
}
bin = signed
}
st := getZipLLB(worker, spec.Name, bin)
def, err := st.Marshal(ctx)
if err != nil {
return nil, nil, fmt.Errorf("error marshalling llb: %w", err)
}
res, err := client.Solve(ctx, gwclient.SolveRequest{
Definition: def.ToPB(),
})
if err != nil {
return nil, nil, err
}
ref, err := res.SingleRef()
return ref, nil, err
})
}
const gomodsName = "__gomods"
func specToSourcesLLB(worker llb.State, spec *dalec.Spec, sOpt dalec.SourceOpts, opts ...llb.ConstraintsOpt) (map[string]llb.State, error) {
out := make(map[string]llb.State, len(spec.Sources))
for k, src := range spec.Sources {
displayRef, err := src.GetDisplayRef()
if err != nil {
return nil, err
}
pg := dalec.ProgressGroup("Add spec source: " + k + " " + displayRef)
st, err := src.AsState(k, sOpt, append(opts, pg)...)
if err != nil {
return nil, errors.Wrapf(err, "error creating source state for %q", k)
}
out[k] = st
}
opts = append(opts, dalec.ProgressGroup("Add gomod sources"))
st, err := spec.GomodDeps(sOpt, worker, opts...)
if err != nil {
return nil, errors.Wrap(err, "error adding gomod sources")
}
if st != nil {
out[gomodsName] = *st
}
return out, nil
}
func installBuildDeps(deps []string) llb.StateOption {
return func(s llb.State) llb.State {
if len(deps) == 0 {
return s
}
sorted := slices.Clone(deps)
slices.Sort(sorted)
return s.Run(
shArgs("apt-get update && apt-get install -y "+strings.Join(sorted, " ")),
dalec.WithMountedAptCache(aptCachePrefix),
).Root()
}
}
func withSourcesMounted(dst string, states map[string]llb.State, sources map[string]dalec.Source) llb.RunOption {
opts := make([]llb.RunOption, 0, len(states))
sorted := dalec.SortMapKeys(states)
files := []llb.State{}
for _, k := range sorted {
state := states[k]
// In cases where we have a generated soruce (e.g. gomods) we don't have a [dalec.Source] in the `sources` map.
// So we need to check for this.
src, ok := sources[k]
if ok && !dalec.SourceIsDir(src) {
files = append(files, state)
continue
}
dirDst := filepath.Join(dst, k)
opts = append(opts, llb.AddMount(dirDst, state))
}
ordered := make([]llb.RunOption, 1, len(opts)+1)
ordered[0] = llb.AddMount(dst, dalec.MergeAtPath(llb.Scratch(), files, "/"))
ordered = append(ordered, opts...)
return dalec.WithRunOptions(ordered...)
}
func buildBinaries(spec *dalec.Spec, worker llb.State, sOpt dalec.SourceOpts, targetKey string) (llb.State, error) {
worker = worker.With(installBuildDeps(spec.GetBuildDeps(targetKey)))
sources, err := specToSourcesLLB(worker, spec, sOpt)
if err != nil {
return llb.Scratch(), err
}
patched := dalec.PatchSources(worker, spec, sources)
buildScript := createBuildScript(spec)
binaries := maps.Keys(spec.Artifacts.Binaries)
script := generateInvocationScript(binaries)
artifacts := worker.Run(
shArgs(script.String()),
llb.Dir("/build"),
withSourcesMounted("/build", patched, spec.Sources),
llb.AddMount("/tmp/scripts", buildScript),
llb.Network(llb.NetModeNone),
).AddMount(outputDir, llb.Scratch())
return artifacts, nil
}
func getZipLLB(worker llb.State, name string, artifacts llb.State) llb.State {
outName := filepath.Join(outputDir, name+".zip")
zipped := worker.Run(
shArgs("zip "+outName+" *"),
llb.Dir("/tmp/artifacts"),
llb.AddMount("/tmp/artifacts", artifacts),
).AddMount(outputDir, llb.Scratch())
return zipped
}
func generateInvocationScript(binaries []string) *strings.Builder {
script := &strings.Builder{}
fmt.Fprintln(script, "#!/usr/bin/env sh")
fmt.Fprintln(script, "set -ex")
fmt.Fprintf(script, "/tmp/scripts/%s\n", buildScriptName)
for _, bin := range binaries {
fmt.Fprintf(script, "mv '%s' '%s'\n", bin, outputDir)
}
return script
}
func workerImg(sOpt dalec.SourceOpts, opts ...llb.ConstraintsOpt) llb.State {
// TODO: support named context override... also this should possibly be its own image, maybe?
return llb.Image(workerImgRef, llb.WithMetaResolver(sOpt.Resolver), dalec.WithConstraints(opts...)).
Run(
shArgs("apt-get update && apt-get install -y build-essential binutils-mingw-w64 g++-mingw-w64-x86-64 gcc git make pkg-config quilt zip"),
dalec.WithMountedAptCache(aptCachePrefix),
).Root()
}
func shArgs(cmd string) llb.RunOption {
return llb.Args([]string{"sh", "-c", cmd})
}
func createBuildScript(spec *dalec.Spec) llb.State {
buf := bytes.NewBuffer(nil)
fmt.Fprintln(buf, "#!/usr/bin/env sh")
fmt.Fprintln(buf, "set -x")
if spec.HasGomods() {
fmt.Fprintln(buf, "export GOMODCACHE=\"$(pwd)/"+gomodsName+"\"")
}
for i, step := range spec.Build.Steps {
fmt.Fprintln(buf, "(")
for k, v := range step.Env {
fmt.Fprintf(buf, "export %s=\"%s\"", k, v)
}
fmt.Fprintln(buf, step.Command)
fmt.Fprintf(buf, ")")
if i < len(spec.Build.Steps)-1 {
fmt.Fprintln(buf, " && \\")
continue
}
fmt.Fprintf(buf, "\n")
}
return llb.Scratch().
File(llb.Mkfile(buildScriptName, 0o770, buf.Bytes()))
}