forked from Azure/dalec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_deb.go
326 lines (271 loc) · 10.1 KB
/
handle_deb.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package jammy
import (
"context"
"io/fs"
"strings"
"github.com/Azure/dalec"
"github.com/Azure/dalec/frontend"
"github.com/Azure/dalec/frontend/deb"
"github.com/Azure/dalec/frontend/pkg/bkfs"
"github.com/containerd/platforms"
"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"
)
const JammyWorkerContextName = "dalec-jammy-worker"
func handleDeb(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
}
opt := dalec.ProgressGroup("Building Jammy deb package: " + spec.Name)
st, err := buildDeb(ctx, client, spec, sOpt, targetKey, opt)
if err != nil {
return nil, nil, err
}
def, err := st.Marshal(ctx)
if err != nil {
return nil, nil, err
}
res, err := client.Solve(ctx, gwclient.SolveRequest{
Definition: def.ToPB(),
})
if err != nil {
return nil, nil, err
}
ref, err := res.SingleRef()
if err != nil {
return nil, nil, err
}
if err := ref.Evaluate(ctx); err != nil {
return ref, nil, err
}
if ref, err := runTests(ctx, client, spec, sOpt, st, targetKey, opt); err != nil {
cfg, _ := buildImageConfig(ctx, client, spec, platform, targetKey)
return ref, cfg, err
}
if platform == nil {
p := platforms.DefaultSpec()
platform = &p
}
return ref, &dalec.DockerImageSpec{Image: ocispecs.Image{Platform: *platform}}, nil
})
}
func runTests(ctx context.Context, client gwclient.Client, spec *dalec.Spec, sOpt dalec.SourceOpts, deb llb.State, targetKey string, opts ...llb.ConstraintsOpt) (gwclient.Reference, error) {
worker, err := workerBase(sOpt, opts...)
if err != nil {
return nil, err
}
var includeTestRepo bool
workerFS, err := bkfs.FromState(ctx, &worker, client)
if err != nil {
return nil, err
}
// Check if there there is a test repo in the worker image.
// We'll mount that into the target container while installing packages.
_, repoErr := fs.Stat(workerFS, testRepoPath[1:])
_, listErr := fs.Stat(workerFS, testRepoSourceListPath[1:])
if listErr == nil && repoErr == nil {
// This is a test and we need to include the repo from the worker image
// into target container.
includeTestRepo = true
frontend.Warn(ctx, client, worker, "Including test repo from worker image")
}
st, err := buildImageRootfs(worker, spec, sOpt, deb, targetKey, includeTestRepo, opts...)
if err != nil {
return nil, err
}
def, err := st.Marshal(ctx, opts...)
if err != nil {
return nil, err
}
res, err := client.Solve(ctx, gwclient.SolveRequest{
Definition: def.ToPB(),
})
if err != nil {
return nil, err
}
ref, err := res.SingleRef()
if err != nil {
return nil, err
}
withTestDeps, err := installTestDeps(worker, spec, sOpt, targetKey, opts...)
if err != nil {
return nil, err
}
err = frontend.RunTests(ctx, client, spec, ref, withTestDeps, targetKey)
return ref, err
}
var jammyRepoPlatformCfg = dalec.RepoPlatformConfig{
ConfigRoot: "/etc/apt/sources.list.d",
GPGKeyRoot: "/usr/share/keyrings",
}
func customRepoMounts(worker llb.State, repos []dalec.PackageRepositoryConfig, sOpt dalec.SourceOpts, opts ...llb.ConstraintsOpt) (llb.RunOption, error) {
withRepos, err := dalec.WithRepoConfigs(repos, &jammyRepoPlatformCfg, sOpt, opts...)
if err != nil {
return nil, err
}
withData, err := dalec.WithRepoData(repos, sOpt, opts...)
if err != nil {
return nil, err
}
keyMounts, _, err := dalec.GetRepoKeys(worker, repos, &jammyRepoPlatformCfg, sOpt, opts...)
if err != nil {
return nil, err
}
return dalec.WithRunOptions(withRepos, withData, keyMounts), nil
}
func installPackages(ls []string, opts ...llb.ConstraintsOpt) llb.RunOption {
script := llb.Scratch().File(
llb.Mkfile("install.sh", 0o755, []byte(`#!/usr/bin/env sh
set -ex
# Make sure any cached data from local repos is purged since this should not
# be shared between builds.
rm -f /var/lib/apt/lists/_*
apt autoclean -y
apt update
apt install -y `+strings.Join(ls, " ")+`
`,
)),
opts...)
p := "/tmp/dalec/internal/deb/install.sh"
return dalec.RunOptFunc(func(ei *llb.ExecInfo) {
llb.AddMount(p, script, llb.SourcePath("install.sh")).SetRunOption(ei)
dalec.ShArgs(p).SetRunOption(ei)
dalec.WithMountedAptCache(AptCachePrefix).SetRunOption(ei)
})
}
func installWithConstraints(pkgPath string, pkgName string, opts ...llb.ConstraintsOpt) llb.RunOption {
return dalec.RunOptFunc(func(ei *llb.ExecInfo) {
// The apt solver always tries to select the latest package version even when constraints specify that an older version should be installed and that older version is available in a repo.
// This leads the solver to simply refuse to install our target package if the latest version of ANY dependency package is incompatible with the constraints.
// To work around this we first install the .deb for the package with dpkg, specifically ignoring any dependencies so that we can avoid the constraints issue.
// We then use aptitude to fix the (possibly broken) install of the package, and we pass the aptitude solver a hint to REJECT any solution that involves uninstalling the package.
// This forces aptitude to find a solution that will respect the constraints even if the solution involves pinning dependency packages to older versions.
script := llb.Scratch().File(
llb.Mkfile("install.sh", 0o755, []byte(`#!/usr/bin/env sh
set -ex
# Make sure any cached data from local repos is purged since this should not
# be shared between builds.
rm -f /var/lib/apt/lists/_*
apt autoclean -y
dpkg -i --force-depends `+pkgPath+`
apt update
aptitude install -y -f -o "Aptitude::ProblemResolver::Hints::=reject `+pkgName+` :UNINST"
`),
), opts...)
dalec.WithMountedAptCache(AptCachePrefix).SetRunOption(ei)
p := "/tmp/dalec/internal/deb/install-with-constraints.sh"
llb.AddMount(p, script, llb.SourcePath("install.sh")).SetRunOption(ei)
dalec.ShArgs(p).SetRunOption(ei)
})
}
func buildDeb(ctx context.Context, client gwclient.Client, spec *dalec.Spec, sOpt dalec.SourceOpts, targetKey string, opts ...llb.ConstraintsOpt) (llb.State, error) {
worker, err := workerBase(sOpt, opts...)
if err != nil {
return llb.Scratch(), err
}
versionID, err := deb.ReadDistroVersionID(ctx, client, worker)
if err != nil {
return llb.Scratch(), err
}
installBuildDeps, err := buildDepends(worker, sOpt, spec, targetKey, opts...)
if err != nil {
return llb.Scratch(), errors.Wrap(err, "error creating deb for build dependencies")
}
worker = worker.With(installBuildDeps)
st, err := deb.BuildDeb(worker, spec, sOpt, targetKey, versionID, opts...)
if err != nil {
return llb.Scratch(), err
}
signed, err := frontend.MaybeSign(ctx, client, st, spec, targetKey, sOpt)
if err != nil {
return llb.Scratch(), err
}
return signed, nil
}
func workerBase(sOpt dalec.SourceOpts, opts ...llb.ConstraintsOpt) (llb.State, error) {
base, err := sOpt.GetContext(jammyRef, dalec.WithConstraints(opts...))
if err != nil {
return llb.Scratch(), err
}
if base != nil {
return *base, nil
}
base, err = sOpt.GetContext(JammyWorkerContextName, dalec.WithConstraints(opts...))
if err != nil {
return llb.Scratch(), err
}
if base != nil {
return *base, nil
}
return llb.Image(jammyRef, llb.WithMetaResolver(sOpt.Resolver)).With(basePackages(opts...)).
// This file prevents installation of things like docs in ubuntu
// containers We don't want to exclude this because tests want to
// check things for docs in the build container. But we also don't
// want to remove this completely from the base worker image in the
// frontend because we usually don't want such things in the build
// environment. This is only needed because certain tests (which
// are using this customized builder image) are checking for files
// that are being excluded by this config file.
File(llb.Rm("/etc/dpkg/dpkg.cfg.d/excludes", llb.WithAllowNotFound(true))), nil
}
func basePackages(opts ...llb.ConstraintsOpt) llb.StateOption {
return func(in llb.State) llb.State {
opts = append(opts, dalec.ProgressGroup("Install base packages"))
return in.Run(
installPackages([]string{"aptitude", "dpkg-dev", "devscripts", "equivs", "fakeroot", "dh-make", "build-essential", "dh-apparmor", "dh-make", "dh-exec", "debhelper-compat=" + deb.DebHelperCompat}, opts...),
dalec.WithConstraints(opts...),
).Root()
}
}
func buildDepends(worker llb.State, sOpt dalec.SourceOpts, spec *dalec.Spec, targetKey string, opts ...llb.ConstraintsOpt) (llb.StateOption, error) {
deps := spec.Dependencies
if t, ok := spec.Targets[targetKey]; ok {
if t.Dependencies != nil {
deps = t.Dependencies
}
}
var buildDeps map[string]dalec.PackageConstraints
if deps != nil {
buildDeps = deps.Build
}
if len(buildDeps) == 0 {
return func(in llb.State) llb.State {
return in
}, nil
}
depsSpec := &dalec.Spec{
Name: spec.Name + "-deps",
Packager: "Dalec",
Version: spec.Version,
Revision: spec.Revision,
Dependencies: &dalec.PackageDependencies{
Runtime: buildDeps,
},
Description: "Build dependencies for " + spec.Name,
}
pg := dalec.ProgressGroup("Install build dependencies")
opts = append(opts, pg)
pkg, err := deb.BuildDeb(worker, depsSpec, sOpt, targetKey, "", append(opts, dalec.ProgressGroup("Create intermediate deb for build dependnencies"))...)
if err != nil {
return nil, errors.Wrap(err, "error creating intermediate package for installing build dependencies")
}
customRepoOpts, err := customRepoMounts(worker, spec.GetBuildRepos(targetKey), sOpt, opts...)
if err != nil {
return nil, err
}
return func(in llb.State) llb.State {
const (
debPath = "/tmp/dalec/internal/build/deps"
)
return in.Run(
installWithConstraints(debPath+"/*.deb", depsSpec.Name, opts...),
customRepoOpts,
llb.AddMount(debPath, pkg, llb.Readonly),
dalec.WithConstraints(opts...),
).Root()
}, nil
}