Skip to content

Commit 020f312

Browse files
pmengelbertcpuguy83
authored andcommitted
Don't emit rpm spec fields if empty in dalec spec
This PR prevents the fields `%post`, `%preun`, and `%postun` from being written to the rpm SPEC unless they are specified in the dalec spec. This is a short-term solution to the problem specified in #298. Please see that issue for more details on the long-term solution. A short summary of the problem follows: What is happening is that the presence of the `%post`, `%preun`, or `%postun` causes `/bin/sh` to be baked into the dependencies of the rpm. This makes sense because a shell is needed to execute the postinstall scripts, and would be needed to run pre- or post- uninstall scripts. _without %post_: ``` $ rpm -q --requires /tmp/out/RPMS/x86_64/oras-v1.2.0-1.cm2.x86_64.rpm rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 rpmlib(PayloadIsZstd) <= 5.4.18-1 ``` _with %post_: ``` $ rpm -q --requires /tmp/out/RPMS/x86_64/oras-v1.2.0-1.cm2.x86_64.rpm /bin/sh /bin/sh /bin/sh rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 rpmlib(PayloadIsZstd) <= 5.4.18-1 ``` The `bash` package supplies `/bin/sh`, and all of its dependencies are installed into the container as well. So the distroless minimal image is used, but it has a bunch of extra stuff installed. Signed-off-by: Peter Engelbert <[email protected]>
1 parent 1faeedf commit 020f312

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

frontend/rpm/template.go

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -293,45 +293,66 @@ func (w *specWrapper) BuildSteps() fmt.Stringer {
293293

294294
func (w *specWrapper) PreUn() fmt.Stringer {
295295
b := &strings.Builder{}
296-
b.WriteString("%preun\n")
297296

298-
if w.Spec.Artifacts.Systemd != nil {
299-
keys := dalec.SortMapKeys(w.Spec.Artifacts.Systemd.Units)
300-
for _, servicePath := range keys {
301-
serviceName := filepath.Base(servicePath)
302-
fmt.Fprintf(b, "%%systemd_preun %s\n", serviceName)
303-
}
297+
if w.Spec.Artifacts.Systemd == nil {
298+
return b
299+
}
300+
301+
if len(w.Spec.Artifacts.Systemd.Units) == 0 {
302+
return b
303+
}
304+
305+
b.WriteString("%preun\n")
306+
keys := dalec.SortMapKeys(w.Spec.Artifacts.Systemd.Units)
307+
for _, servicePath := range keys {
308+
serviceName := filepath.Base(servicePath)
309+
fmt.Fprintf(b, "%%systemd_preun %s\n", serviceName)
304310
}
305311

306312
return b
307313
}
308314

309315
func (w *specWrapper) Post() fmt.Stringer {
310316
b := &strings.Builder{}
317+
318+
if w.Spec.Artifacts.Systemd == nil {
319+
return b
320+
}
321+
322+
if len(w.Spec.Artifacts.Systemd.Units) == 0 {
323+
return b
324+
}
325+
311326
b.WriteString("%post\n")
312327
// TODO: can inject other post install steps here in the future
313328

314-
if w.Spec.Artifacts.Systemd != nil {
315-
keys := dalec.SortMapKeys(w.Spec.Artifacts.Systemd.Units)
316-
for _, servicePath := range keys {
317-
unitConf := w.Spec.Artifacts.Systemd.Units[servicePath].Artifact()
318-
fmt.Fprintf(b, "%%systemd_post %s\n", unitConf.ResolveName(servicePath))
319-
}
329+
keys := dalec.SortMapKeys(w.Spec.Artifacts.Systemd.Units)
330+
for _, servicePath := range keys {
331+
unitConf := w.Spec.Artifacts.Systemd.Units[servicePath].Artifact()
332+
fmt.Fprintf(b, "%%systemd_post %s\n", unitConf.ResolveName(servicePath))
320333
}
334+
321335
return b
322336
}
323337

324338
func (w *specWrapper) PostUn() fmt.Stringer {
325339
b := &strings.Builder{}
340+
341+
if w.Spec.Artifacts.Systemd == nil {
342+
return b
343+
}
344+
345+
if len(w.Spec.Artifacts.Systemd.Units) == 0 {
346+
return b
347+
}
348+
326349
b.WriteString("%postun\n")
327-
if w.Spec.Artifacts.Systemd != nil {
328-
keys := dalec.SortMapKeys(w.Spec.Artifacts.Systemd.Units)
329-
for _, servicePath := range keys {
330-
cfg := w.Spec.Artifacts.Systemd.Units[servicePath]
331-
a := cfg.Artifact()
332-
serviceName := a.ResolveName(servicePath)
333-
fmt.Fprintf(b, "%%systemd_postun %s\n", serviceName)
334-
}
350+
keys := dalec.SortMapKeys(w.Spec.Artifacts.Systemd.Units)
351+
for _, servicePath := range keys {
352+
cfg := w.Spec.Artifacts.Systemd.Units[servicePath]
353+
a := cfg.Artifact()
354+
serviceName := a.ResolveName(servicePath)
355+
fmt.Fprintf(b, "%%systemd_postun %s\n", serviceName)
335356
}
336357

337358
return b

0 commit comments

Comments
 (0)