Skip to content

Commit eb7e9b9

Browse files
DannyBritocpuguy83
authored andcommitted
update deb and multi-src structure
1 parent bfe45f9 commit eb7e9b9

File tree

5 files changed

+71
-75
lines changed

5 files changed

+71
-75
lines changed

docker-bake.hcl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ target "runc-azlinux" {
6262
"DALEC_DISABLE_DIFF_MERGE" = DALEC_DISABLE_DIFF_MERGE
6363
}
6464
contexts = {
65-
"dalec_frontend" = "target:frontend"
65+
"dalec_frontend" = "target:frontend"
6666
}
6767
matrix = {
6868
distro = ["mariner2", "azlinux3"]
@@ -86,7 +86,7 @@ target "runc-jammy" {
8686
"DALEC_DISABLE_DIFF_MERGE" = DALEC_DISABLE_DIFF_MERGE
8787
}
8888
contexts = {
89-
"dalec_frontend" = "target:frontend"
89+
"dalec_frontend" = "target:frontend"
9090
}
9191
matrix = {
9292
tgt = ["deb", "container"]
@@ -124,9 +124,9 @@ target "test-fixture" {
124124
matrix = {
125125
f = DALEC_DISABLE_NESTED == "1" ? (
126126
["http-src", "frontend", "local-context", "cmd-src-ref"]
127-
) : (
127+
) : (
128128
["http-src", "frontend", "local-context", "cmd-src-ref", "nested"]
129-
)
129+
)
130130
tgt = ["mariner2/container"]
131131
}
132132
dockerfile = "test/fixtures/${f}.yml"
@@ -136,7 +136,7 @@ target "test-fixture" {
136136
"DALEC_DISABLE_DIFF_MERGE" = DALEC_DISABLE_DIFF_MERGE
137137
}
138138
contexts = {
139-
"dalec_frontend" = "target:frontend"
139+
"dalec_frontend" = "target:frontend"
140140
}
141141
target = tgt
142142
}
@@ -156,7 +156,7 @@ target "build" {
156156
"BUILDKIT_SYNTAX" = "dalec_frontend"
157157
}
158158
contexts = {
159-
"dalec_frontend" = "target:frontend"
159+
"dalec_frontend" = "target:frontend"
160160
}
161161
target = "${distro}/${tgt}"
162162
// only tag the container target
@@ -175,7 +175,7 @@ target "examples" {
175175
"BUILDKIT_SYNTAX" = "dalec_frontend"
176176
}
177177
contexts = {
178-
"dalec_frontend" = "target:frontend"
178+
"dalec_frontend" = "target:frontend"
179179
}
180180
target = "${distro}/container"
181181
dockerfile = "docs/examples/${f}.yml"
@@ -197,7 +197,7 @@ dependencies:
197197
"BUILDKIT_SYNTAX" = "dalec_frontend"
198198
}
199199
contexts = {
200-
"dalec_frontend" = "target:frontend"
200+
"dalec_frontend" = "target:frontend"
201201
}
202202
target = "${distro}/container/depsonly"
203203
tags = ["local/dalec/deps-only:${distro}"]
@@ -231,4 +231,3 @@ target "frontend-ci-full" {
231231
inherits = ["frontend-ci"]
232232
platforms = ["linux/amd64", "linux/arm64"]
233233
}
234-

packaging/linux/deb/debroot.go

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ var patchHeader []byte
4141
//go:embed templates/debian_install_header.sh
4242
var debianInstall []byte
4343

44-
func sanitizeSourceKey(key string) string {
45-
replacer := strings.NewReplacer("_", "", "-", "", ".", "")
46-
return replacer.Replace(key)
47-
}
48-
4944
// This creates a directory in the debian root directory for each patch, and copies the patch files into it.
5045
// The format for each patch dir matches what would normally be under `debian/patches`, just that this is a separate dir for every source we are patching
5146
// This is purely for documenting in the source package how patches are applied in a more readable way than the big merged patch file.
@@ -136,20 +131,12 @@ func Debroot(ctx context.Context, sOpt dalec.SourceOpts, spec *dalec.Spec, worke
136131
installers := createInstallScripts(worker, spec, dir, target)
137132

138133
const (
139-
sourceFormat = "3.0 (quilt)"
140-
sourceOptions = "create-empty-orig"
134+
sourceFormat = "3.0 (quilt)"
141135
)
142136

143137
debian := base.
144138
File(llb.Mkdir(filepath.Join(dir, "source"), 0o755), opts...).
145-
With(func(in llb.State) llb.State {
146-
if len(spec.Sources) == 0 {
147-
return in
148-
}
149-
return in.
150-
File(llb.Mkfile(filepath.Join(dir, "source/format"), 0o640, []byte(sourceFormat)), opts...).
151-
File(llb.Mkfile(filepath.Join(dir, "source/options"), 0o640, []byte(sourceOptions)), opts...)
152-
}).
139+
File(llb.Mkfile(filepath.Join(dir, "source/format"), 0o640, []byte(sourceFormat)), opts...).
153140
File(llb.Mkdir(filepath.Join(dir, "dalec"), 0o755), opts...).
154141
File(llb.Mkfile(filepath.Join(dir, "source/include-binaries"), 0o640, append([]byte("dalec"), '\n')), opts...)
155142

@@ -161,7 +148,7 @@ func Debroot(ctx context.Context, sOpt dalec.SourceOpts, spec *dalec.Spec, worke
161148

162149
states = append(states, dalecDir.File(llb.Mkfile(filepath.Join(dir, "dalec/build.sh"), 0o700, createBuildScript(spec, &cfg)), opts...))
163150
states = append(states, dalecDir.File(llb.Mkfile(filepath.Join(dir, "dalec/patch.sh"), 0o700, createPatchScript(spec, &cfg)), opts...))
164-
states = append(states, dalecDir.File(llb.Mkfile(filepath.Join(dir, "dalec/fix_sources.sh"), 0o700, fixupSources(spec, &cfg)), opts...))
151+
states = append(states, dalecDir.File(llb.Mkfile(filepath.Join(dir, "dalec/fix_generators.sh"), 0o700, fixupGenerators(spec, &cfg)), opts...))
165152
states = append(states, dalecDir.File(llb.Mkfile(filepath.Join(dir, "dalec/fix_perms.sh"), 0o700, fixupArtifactPerms(spec, target, &cfg)), opts...))
166153

167154
customEnable, err := customDHInstallSystemdPostinst(spec, target)
@@ -294,33 +281,10 @@ func fixupArtifactPerms(spec *dalec.Spec, target string, cfg *SourcePkgConfig) [
294281
}
295282

296283
// For debian sources
297-
// 1. File backed sources are not in the correct format as expected by dalec.
298-
// 2. Sources with certain characters in the name had to be changed, so we need
299-
// to bring those back.
300-
//
301284
// This is called from `debian/rules` after the source tarball has been extracted.
302-
func fixupSources(spec *dalec.Spec, cfg *SourcePkgConfig) []byte {
285+
func fixupGenerators(spec *dalec.Spec, cfg *SourcePkgConfig) []byte {
303286
buf := bytes.NewBuffer(nil)
304287
writeScriptHeader(buf, cfg)
305-
306-
// now, we need to find all the sources that are file-backed and fix them up
307-
for name, src := range spec.Sources {
308-
dirName := sanitizeSourceKey(name)
309-
310-
if dalec.SourceIsDir(src) {
311-
if dirName == name {
312-
continue
313-
}
314-
fmt.Fprintf(buf, "mv '%s' '%s'\n", dirName, name)
315-
continue
316-
}
317-
318-
fmt.Fprintf(buf, "mv '%s/%s' '%s.dalec.tmp' || (ls -lh %q; exit 2)\n", dirName, name, name, dirName)
319-
fmt.Fprintf(buf, "rm -rf '%s'\n", dirName)
320-
fmt.Fprintf(buf, "mv '%s.dalec.tmp' '%s'\n", name, name)
321-
fmt.Fprintln(buf)
322-
}
323-
324288
if spec.HasGomods() {
325289
// Older go versions did not have support for the `GOMODCACHE` var
326290
// This is a hack to try and make the build work by linking the go modules

packaging/linux/deb/pkg.go

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ func SourcePackage(ctx context.Context, sOpt dalec.SourceOpts, worker llb.State,
9393
}
9494

9595
patches := createPatches(spec, sources, worker, dr, opts...)
96-
debSources := debSources(worker, sources)
9796

9897
work := worker.Run(
9998
dalec.ShArgs("set -e; ls -lh; dpkg-buildpackage -S -us -uc; mkdir -p /tmp/out; ls -lh; cp -r /work/"+spec.Name+"_"+spec.Version+"* /tmp/out; ls -lh /tmp/out"),
@@ -102,18 +101,29 @@ func SourcePackage(ctx context.Context, sOpt dalec.SourceOpts, worker llb.State,
102101
llb.AddMount("/work/pkg/debian/patches", patches, llb.Readonly),
103102
llb.AddEnv("DH_VERBOSE", "1"),
104103
dalec.RunOptFunc(func(ei *llb.ExecInfo) {
105-
// Mount all the tar+gz'd sources into the build which will get picked p by debbuild
106-
for key, src := range debSources {
107-
tarName := fmt.Sprintf("%s_%s.orig-%s.tar.gz", spec.Name, spec.Version, sanitizeSourceKey(key))
108-
llb.AddMount(filepath.Join("/work", tarName), src, llb.SourcePath("src.tar.gz"), llb.Readonly).SetRunOption(ei)
109-
}
104+
debSources := TarDebSources(worker, spec, sources, "src.tar.gz", sOpt, opts...)
105+
llb.AddMount("/work/"+spec.Name+"_"+spec.Version+".orig.tar.gz", debSources, llb.SourcePath("src.tar.gz")).SetRunOption(ei)
110106
}),
111107
dalec.WithConstraints(opts...),
112108
)
113109

114110
return work.AddMount("/tmp/out", llb.Scratch()), nil
115111
}
116112

113+
func BuildDebBinaryOnly(worker llb.State, spec *dalec.Spec, debroot llb.State, distroVersionID string, opts ...llb.ConstraintsOpt) (llb.State, error) {
114+
dirName := filepath.Join("/work", spec.Name+"_"+spec.Version+"-"+spec.Revision)
115+
st := worker.
116+
Run(
117+
dalec.ShArgs("set -e; ls -lh; dpkg-buildpackage -b -uc -us; mkdir -p /tmp/out; cp ../*.deb /tmp/out; ls -lh /tmp/out"),
118+
llb.Dir(dirName),
119+
llb.AddEnv("DH_VERBOSE", "1"),
120+
llb.AddMount(dirName, debroot),
121+
dalec.WithConstraints(opts...),
122+
).AddMount("/tmp/out", llb.Scratch())
123+
124+
return dalec.MergeAtPath(llb.Scratch(), []llb.State{st}, "/"), nil
125+
}
126+
117127
func BuildDeb(worker llb.State, spec *dalec.Spec, srcPkg llb.State, distroVersionID string, opts ...llb.ConstraintsOpt) (llb.State, error) {
118128

119129
dirName := filepath.Join("/work", spec.Name+"_"+spec.Version+"-"+spec.Revision)
@@ -129,13 +139,43 @@ func BuildDeb(worker llb.State, spec *dalec.Spec, srcPkg llb.State, distroVersio
129139
return dalec.MergeAtPath(llb.Scratch(), []llb.State{st, srcPkg}, "/"), nil
130140
}
131141

132-
func debSources(worker llb.State, sources map[string]llb.State, opts ...llb.ConstraintsOpt) map[string]llb.State {
133-
out := make(map[string]llb.State, len(sources))
142+
func TarDebSources(work llb.State, spec *dalec.Spec, srcStates map[string]llb.State, dest string, sOpts dalec.SourceOpts, opts ...llb.ConstraintsOpt) llb.State {
143+
outBase := "/tmp/out"
144+
out := filepath.Join(outBase, filepath.Dir(dest))
134145

135-
for key, src := range sources {
136-
st := dalec.Tar(worker, src, "src.tar.gz", opts...)
137-
out[key] = st
138-
}
146+
worker := work.Run(
147+
llb.AddMount("/src", llb.Scratch()),
148+
dalec.RunOptFunc(func(ei *llb.ExecInfo) {
149+
for key, state := range srcStates {
150+
151+
mountOpts := []llb.MountOption{}
152+
src, ok := spec.Sources[key]
153+
154+
// If the source is not explicitly listed in the spec sources, assume it is a directory (e.g., for gomod dependencies)
155+
isDir := true
156+
if ok {
157+
isDir = dalec.SourceIsDir(src)
158+
}
159+
160+
if !isDir {
161+
mountOpts = append(mountOpts, llb.SourcePath(filepath.Join("/", key)))
162+
}
163+
164+
// If the tar contains only a single directory, dpkg will extract its contents directly into the root directory.
165+
mounthPath := filepath.Join("/src", key)
166+
if len(srcStates) == 1 && isDir {
167+
mounthPath = filepath.Join("/src", key, key)
168+
}
169+
170+
llb.AddMount(mounthPath, state, mountOpts...).SetRunOption(ei)
171+
}
172+
}),
173+
dalec.ShArgs("tar -C /src -cvzf /tmp/st ."),
174+
dalec.WithConstraints(opts...),
175+
).Run(
176+
llb.Args([]string{"/bin/sh", "-c", "mkdir -p " + out + " && mv /tmp/st " + filepath.Join(out, filepath.Base(dest))}),
177+
dalec.WithConstraints(opts...),
178+
)
139179

140-
return out
180+
return worker.AddMount(outBase, llb.Scratch())
141181
}

packaging/linux/deb/templates/debian_rules.tmpl

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,12 @@ override_dh_auto_install:
1212
override_dh_auto_build:
1313
./debian/dalec/build.sh
1414

15+
.PHONY: fix_generators
1516

16-
.PHONY: fix_sources
17+
fix_generators:
18+
./debian/dalec/fix_generators.sh
1719

18-
# Move file-backed (Dalec) sources into the correct location
19-
# Mainly this is needed because with the way the debian sources are setup, our file-backed sources
20-
# end up getting placed in a sub-directory, e.g. for source "foo", we end up with "foo/foo".
21-
# For the purposes of making dalec builds work across different platforms, we need to move these so it is only a file, "foo".
22-
fix_sources:
23-
./debian/dalec/fix_sources.sh
24-
25-
26-
override_dh_auto_configure: fix_sources
20+
override_dh_auto_configure: fix_generators
2721
@# Stop debhelper from running auto configure, which should be part of the build steps
2822

2923
# dh_dwz is problematic for (some) go builds and causes the build to error out.
@@ -38,4 +32,3 @@ override_dh_dwz:
3832

3933
%:
4034
dh $@ -v
41-

targets/linux/deb/distro/install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ func (d *Config) InstallBuildDeps(sOpt dalec.SourceOpts, spec *dalec.Spec, targe
139139
opts := append(opts, dalec.ProgressGroup("Install build dependencies"))
140140
opts = append([]llb.ConstraintsOpt{dalec.WithConstraint(c)}, opts...)
141141

142-
srcPkg, err := deb.SourcePackage(ctx, sOpt, in, depsSpec, targetKey, "", deb.SourcePkgConfig{}, opts...)
142+
debRoot, err := deb.Debroot(ctx, sOpt, depsSpec, in, llb.Scratch(), targetKey, "", d.VersionID, deb.SourcePkgConfig{}, opts...)
143143
if err != nil {
144144
return in, err
145145
}
146146

147-
pkg, err := deb.BuildDeb(in, depsSpec, srcPkg, "", opts...)
147+
pkg, err := deb.BuildDebBinaryOnly(in, depsSpec, debRoot, "", opts...)
148148
if err != nil {
149149
return in, errors.Wrap(err, "error creating intermediate package for installing build dependencies")
150150
}

0 commit comments

Comments
 (0)