Skip to content

Commit

Permalink
Don't emit rpm spec fields if empty in dalec spec
Browse files Browse the repository at this point in the history
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 Azure#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 <pmengelbert@gmail.com>
  • Loading branch information
pmengelbert committed Jun 26, 2024
1 parent 0bb8ba7 commit ec9a400
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions frontend/rpm/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,45 +293,66 @@ func (w *specWrapper) BuildSteps() fmt.Stringer {

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

if w.Spec.Artifacts.Systemd != nil {
keys := dalec.SortMapKeys(w.Spec.Artifacts.Systemd.Units)
for _, servicePath := range keys {
serviceName := filepath.Base(servicePath)
fmt.Fprintf(b, "%%systemd_preun %s\n", serviceName)
}
if w.Spec.Artifacts.Systemd == nil {
return b
}

if len(w.Spec.Artifacts.Systemd.Units) == 0 {
return b
}

b.WriteString("%preun\n")
keys := dalec.SortMapKeys(w.Spec.Artifacts.Systemd.Units)
for _, servicePath := range keys {
serviceName := filepath.Base(servicePath)
fmt.Fprintf(b, "%%systemd_preun %s\n", serviceName)
}

return b
}

func (w *specWrapper) Post() fmt.Stringer {
b := &strings.Builder{}

if w.Spec.Artifacts.Systemd == nil {
return b
}

if len(w.Spec.Artifacts.Systemd.Units) == 0 {
return b
}

b.WriteString("%post\n")
// TODO: can inject other post install steps here in the future

if w.Spec.Artifacts.Systemd != nil {
keys := dalec.SortMapKeys(w.Spec.Artifacts.Systemd.Units)
for _, servicePath := range keys {
unitConf := w.Spec.Artifacts.Systemd.Units[servicePath].Artifact()
fmt.Fprintf(b, "%%systemd_post %s\n", unitConf.ResolveName(servicePath))
}
keys := dalec.SortMapKeys(w.Spec.Artifacts.Systemd.Units)
for _, servicePath := range keys {
unitConf := w.Spec.Artifacts.Systemd.Units[servicePath].Artifact()
fmt.Fprintf(b, "%%systemd_post %s\n", unitConf.ResolveName(servicePath))
}

return b
}

func (w *specWrapper) PostUn() fmt.Stringer {
b := &strings.Builder{}

if w.Spec.Artifacts.Systemd == nil {
return b
}

if len(w.Spec.Artifacts.Systemd.Units) == 0 {
return b
}

b.WriteString("%postun\n")
if w.Spec.Artifacts.Systemd != nil {
keys := dalec.SortMapKeys(w.Spec.Artifacts.Systemd.Units)
for _, servicePath := range keys {
cfg := w.Spec.Artifacts.Systemd.Units[servicePath]
a := cfg.Artifact()
serviceName := a.ResolveName(servicePath)
fmt.Fprintf(b, "%%systemd_postun %s\n", serviceName)
}
keys := dalec.SortMapKeys(w.Spec.Artifacts.Systemd.Units)
for _, servicePath := range keys {
cfg := w.Spec.Artifacts.Systemd.Units[servicePath]
a := cfg.Artifact()
serviceName := a.ResolveName(servicePath)
fmt.Fprintf(b, "%%systemd_postun %s\n", serviceName)
}

return b
Expand Down

0 comments on commit ec9a400

Please sign in to comment.