Skip to content

Commit 86aafc1

Browse files
committed
Add debhelper marker in postinst script
Without this we don't get any of the generated debhelper postinst scripts. Signed-off-by: Brian Goff <[email protected]>
1 parent 4b293ff commit 86aafc1

File tree

2 files changed

+98
-12
lines changed

2 files changed

+98
-12
lines changed

packaging/linux/deb/debroot.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,9 @@ func Debroot(ctx context.Context, sOpt dalec.SourceOpts, spec *dalec.Spec, worke
160160
states = append(states, dalecDir.File(llb.Mkfile(filepath.Join(dir, "dalec/"+customSystemdPostinstFile), 0o600, customEnable), opts...))
161161
}
162162

163-
postinst := bytes.NewBuffer(nil)
164-
artifacts := spec.GetArtifacts(target)
165-
writeUsersPostInst(postinst, artifacts.Users)
166-
writeGroupsPostInst(postinst, artifacts.Groups)
167-
setArtifactOwnershipPostInst(postinst, spec, target)
168-
setArtifactCapabilitiesPostInst(postinst, spec, target)
169-
170-
if postinst.Len() > 0 {
171-
dt := []byte("#!/usr/bin/env sh\nset -e\n")
172-
dt = append(dt, postinst.Bytes()...)
173-
174-
states = append(states, dalecDir.File(llb.Mkfile(filepath.Join(dir, "postinst"), 0o700, dt), opts...))
163+
postinst := generatePostinst(spec, target)
164+
if len(postinst) > 0 {
165+
states = append(states, dalecDir.File(llb.Mkfile(filepath.Join(dir, "postinst"), 0o700, postinst), opts...))
175166
}
176167

177168
patchDir := dalecDir.File(llb.Mkdir(filepath.Join(dir, "dalec/patches"), 0o755), opts...)
@@ -181,6 +172,7 @@ func Debroot(ctx context.Context, sOpt dalec.SourceOpts, spec *dalec.Spec, worke
181172
states = append(states, pls...)
182173
}
183174

175+
artifacts := spec.GetArtifacts(target)
184176
if len(artifacts.Links) > 0 {
185177
buf := bytes.NewBuffer(nil)
186178

@@ -196,6 +188,28 @@ func Debroot(ctx context.Context, sOpt dalec.SourceOpts, spec *dalec.Spec, worke
196188
return dalec.MergeAtPath(in, states, "/")
197189
}
198190

191+
func generatePostinst(spec *dalec.Spec, target string) []byte {
192+
buf := bytes.NewBuffer(nil)
193+
194+
artifacts := spec.GetArtifacts(target)
195+
writeUsersPostInst(buf, artifacts.Users)
196+
writeGroupsPostInst(buf, artifacts.Groups)
197+
setArtifactOwnershipPostInst(buf, spec, target)
198+
setArtifactCapabilitiesPostInst(buf, spec, target)
199+
200+
if buf.Len() == 0 {
201+
return nil
202+
}
203+
204+
// NOTE: `#DEBHELPER#` is a special marker that will be replaced by
205+
// debhelper when it generates the final postinst script.
206+
// Very important that this is included.
207+
dt := []byte("#!/usr/bin/env sh\nset -e\n\n#DEBHELPER#\n\n")
208+
dt = append(dt, buf.Bytes()...)
209+
210+
return dt
211+
}
212+
199213
func fixupArtifactPerms(spec *dalec.Spec, target string, cfg *SourcePkgConfig) []byte {
200214
buf := bytes.NewBuffer(nil)
201215
writeScriptHeader(buf, cfg)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package deb
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/moby/buildkit/client/llb"
10+
"github.com/moby/buildkit/solver/pb"
11+
"github.com/project-dalec/dalec"
12+
"gotest.tools/v3/assert"
13+
)
14+
15+
func TestDebrootPostinstIncludesDebhelperMarker(t *testing.T) {
16+
t.Parallel()
17+
18+
ctx := context.Background()
19+
spec := &dalec.Spec{
20+
Name: "example",
21+
Description: "Example package",
22+
Website: "https://example.invalid",
23+
Version: "1.0.0",
24+
Revision: "1",
25+
License: "Apache-2.0",
26+
Artifacts: dalec.Artifacts{
27+
Users: []dalec.AddUserConfig{
28+
{Name: "dalecuser"},
29+
},
30+
},
31+
}
32+
33+
st := Debroot(ctx, dalec.SourceOpts{}, spec, llb.Scratch(), llb.Scratch(), "", "", "", SourcePkgConfig{})
34+
def, err := st.Marshal(ctx)
35+
assert.NilError(t, err)
36+
37+
mkfile, err := findMkfile(t, def.ToPB(), filepath.Join("/debian", "postinst"))
38+
assert.NilError(t, err)
39+
assert.Assert(t, mkfile != nil)
40+
41+
assert.Equal(t, int32(0o700), mkfile.Mode)
42+
assert.Assert(t, bytes.Contains(mkfile.Data, []byte("#DEBHELPER#")))
43+
assert.Assert(t, bytes.Contains(mkfile.Data, []byte("useradd dalecuser")))
44+
}
45+
46+
func findMkfile(t *testing.T, def *pb.Definition, path string) (*pb.FileActionMkFile, error) {
47+
for _, dt := range def.Def {
48+
var op pb.Op
49+
if err := op.Unmarshal(dt); err != nil {
50+
return nil, err
51+
}
52+
53+
fileOp := op.GetFile()
54+
if fileOp == nil {
55+
continue
56+
}
57+
58+
for _, action := range fileOp.Actions {
59+
mkfile := action.GetMkfile()
60+
if mkfile == nil {
61+
continue
62+
}
63+
64+
t.Log(mkfile.Path)
65+
if filepath.Clean(mkfile.Path) == filepath.Clean(path) {
66+
return mkfile, nil
67+
}
68+
}
69+
}
70+
71+
return nil, nil
72+
}

0 commit comments

Comments
 (0)