Skip to content

Commit edfb67b

Browse files
committed
jammy: Remove cached data for local repos before install
We don't really need to cache these since they are locally anyway. It can also cause inconsistencies between builds if the repo config is different, for instance during tests when swithcing between signed and unsigned repos. Signed-off-by: Brian Goff <[email protected]>
1 parent 5f3ad94 commit edfb67b

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

frontend/jammy/handle_container.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,24 @@ func buildImageRootfs(worker llb.State, spec *dalec.Spec, sOpt dalec.SourceOpts,
104104
debug := llb.Scratch().File(llb.Mkfile("debug", 0o644, []byte(`debug=2`)), opts...)
105105
opts = append(opts, dalec.ProgressGroup("Install spec package"))
106106

107+
script := llb.Scratch().File(
108+
llb.Mkfile("install.sh", 0o755, []byte(`#!/usr/bin/env sh
109+
set -ex
110+
111+
rm -f /var/lib/apt/lists/_*
112+
apt autoclean -y
113+
114+
apt update
115+
apt install -y /tmp/pkg/*.deb
116+
`)),
117+
opts...,
118+
)
119+
120+
p := "/tmp/dalec/internal/install/container.sh"
121+
107122
return baseImg.Run(
108-
dalec.ShArgs("set -x; apt update && apt install -y /tmp/pkg/*.deb"),
123+
dalec.ShArgs(p),
124+
llb.AddMount(p, script, llb.SourcePath("install.sh")),
109125
customRepoOpts,
110126
llb.AddEnv("DEBIAN_FRONTEND", "noninteractive"),
111127
llb.AddMount("/tmp/pkg", deb, llb.Readonly),

frontend/jammy/handle_deb.go

+39-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package jammy
22

33
import (
44
"context"
5-
"fmt"
65
"io/fs"
76
"strings"
87

@@ -146,25 +145,60 @@ func customRepoMounts(worker llb.State, repos []dalec.PackageRepositoryConfig, s
146145
}
147146

148147
func installPackages(ls []string, rOpts ...llb.RunOption) llb.RunOption {
148+
script := llb.Scratch().File(
149+
llb.Mkfile("install.sh", 0o755, []byte(`#!/usr/bin/env sh
150+
set -ex
151+
152+
# Make sure any cached data from local repos is purged since this should not
153+
# be shared between builds.
154+
rm -f /var/lib/apt/lists/_*
155+
apt autoclean -y
156+
ls -lha /var/lib/apt/lists/
157+
158+
apt update
159+
apt install -y `+strings.Join(ls, " ")+`
160+
`,
161+
)),
162+
)
163+
164+
p := "/tmp/dalec/internal/deb/install.sh"
149165
return dalec.RunOptFunc(func(ei *llb.ExecInfo) {
150-
// This only runs apt-get update if the pkgcache is older than 10 minutes.
151-
dalec.ShArgs(`set -ex; apt update; apt install -y ` + strings.Join(ls, " ")).SetRunOption(ei)
166+
llb.AddMount(p, script, llb.SourcePath("install.sh")).SetRunOption(ei)
167+
dalec.ShArgs(p).SetRunOption(ei)
152168
dalec.WithMountedAptCache(AptCachePrefix).SetRunOption(ei)
153169
dalec.WithRunOptions(rOpts...).SetRunOption(ei)
154170
})
155171
}
156172

157173
func installWithConstraints(pkgPath string, pkgName string, rOpts ...llb.RunOption) llb.RunOption {
158174
return dalec.RunOptFunc(func(ei *llb.ExecInfo) {
175+
script := llb.Scratch().File(
176+
llb.Mkfile("install.sh", 0o755, []byte(`#!/usr/bin/env sh
177+
set -ex
178+
179+
# Make sure any cached data from local repos is purged since this should not
180+
# be shared between builds.
181+
rm -f /var/lib/apt/lists/_*
182+
apt autoclean -y
183+
ls -lha /var/lib/apt/lists/
184+
185+
dpkg -i --force-depends `+pkgPath+`
186+
187+
apt update
188+
aptitude install -y -f -o "Aptitude::ProblemResolver::Hints::=reject `+pkgName+` :UNINST"
189+
`),
190+
))
159191
// 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.
160192
// 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.
161193
// 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.
162194
// 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.
163195
// This forces aptitude to find a solution that will respect the constraints even if the solution involves pinning dependency packages to older versions.
164-
dalec.ShArgs(`set -ex; dpkg -i --force-depends ` + pkgPath +
165-
fmt.Sprintf(`; apt update; aptitude install -y -f -o "Aptitude::ProblemResolver::Hints::=reject %s :UNINST"`, pkgName)).SetRunOption(ei)
166196
dalec.WithMountedAptCache(AptCachePrefix).SetRunOption(ei)
167197
dalec.WithRunOptions(rOpts...).SetRunOption(ei)
198+
199+
p := "/tmp/dalec/internal/deb/install-with-constraints.sh"
200+
llb.AddMount(p, script, llb.SourcePath("install.sh")).SetRunOption(ei)
201+
dalec.ShArgs(p).SetRunOption(ei)
168202
})
169203
}
170204

0 commit comments

Comments
 (0)