Repository: goreleaser/nfpm
Pull request #322 (2021-04-20 15:26:22)

feat: add %pretrans %posttrans scriptlets
This PR adds %pretrans %posttrans scriptlets for rpm packages. https://github.com/google/rpmpack/pull/58
pretrans and posttrans are run at start and end of a transaction.

On upgrade, the scripts are run in the following order:

%pretrans of new package
%pre of new package
(package install)
%post of new package
%preun of old package
(removal of old package)
%postun of old package
%posttrans of new package

https://docs.fedoraproject.org/en-US/packaging-guidelines/Scriptlets/#ordering

Those scriptlets are useful in some scenarios, for e.g. on upgrade, when something needs to be run after preremove and postremove, which are the versions of the scripts from the old version of package and are already installed on customer machine.

vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).  
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/goreleaser/nfpm/CQtGgdctuDjkHdTdqxn27Xa1eYyQ](https://vercel.com/goreleaser/nfpm/CQtGgdctuDjkHdTdqxn27Xa1eYyQ)  
✅ Preview: *In Progress*


codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/nfpm/pull/322?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#322](https://codecov.io/gh/goreleaser/nfpm/pull/322?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (913addb) into [master](https://codecov.io/gh/goreleaser/nfpm/commit/1fb26910a00fb1e7625f2763db819521f87f1134?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (1fb2691) will **decrease** coverage by `0.08%`.
> The diff coverage is `60.00%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/nfpm/pull/322/graphs/tree.svg?width=650&height=150&src=pr&token=9QKGIxn7wg&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)](https://codecov.io/gh/goreleaser/nfpm/pull/322?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

```diff
@@            Coverage Diff             @@
##           master     #322      +/-   ##
==========================================
- Coverage   69.37%   69.28%   -0.09%     
==========================================
  Files           9        9              
  Lines        1159     1169      +10     
==========================================
+ Hits          804      810       +6     
- Misses        223      225       +2     
- Partials      132      134       +2     
```


| [Impacted Files](https://codecov.io/gh/goreleaser/nfpm/pull/322?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [nfpm.go](https://codecov.io/gh/goreleaser/nfpm/pull/322/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-bmZwbS5nbw==) | `85.32% <ø> (ø)` | |
| [rpm/rpm.go](https://codecov.io/gh/goreleaser/nfpm/pull/322/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-cnBtL3JwbS5nbw==) | `73.20% <60.00%> (-0.67%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/nfpm/pull/322?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/nfpm/pull/322?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [fb9b6f4...913addb](https://codecov.io/gh/goreleaser/nfpm/pull/322?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).

djgilcrease: Also, would you mind updating https://github.com/goreleaser/nfpm/blob/master/www/docs/tips.md#deb--rpm with the full script order and just note that the `pretrans` and `posttrans`  only apply to RPM
caarlos0: LGTM, thanks for the PR 🤘

Patch:
diff --git a/go.mod b/go.mod
index 4c2a6a5..abddf1e 100644
--- a/go.mod
+++ b/go.mod
@@ -14,7 +14,7 @@ require (
 	github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb
 	github.com/go-git/go-git/v5 v5.3.0 // indirect
 	github.com/google/go-cmp v0.5.4 // indirect
-	github.com/google/rpmpack v0.0.0-20201225075926-0a97c2c4b688
+	github.com/google/rpmpack v0.0.0-20210410105602-e20c988a6f5a
 	github.com/google/uuid v1.2.0 // indirect
 	github.com/goreleaser/chglog v0.1.2
 	github.com/goreleaser/fileglob v1.2.0
diff --git a/go.sum b/go.sum
index bd615f6..642a930 100644
--- a/go.sum
+++ b/go.sum
@@ -128,6 +128,8 @@ github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OI
 github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
 github.com/google/rpmpack v0.0.0-20201225075926-0a97c2c4b688 h1:jyGRCFMyDK/gKe6QRZQWci5+wEUBFElvxLHs3iwO3hY=
 github.com/google/rpmpack v0.0.0-20201225075926-0a97c2c4b688/go.mod h1:+y9lKiqDhR4zkLl+V9h4q0rdyrYVsWWm6LLCQP33DIk=
+github.com/google/rpmpack v0.0.0-20210410105602-e20c988a6f5a h1:XC048Fc/OB2rUl/BxruopEl2u/EP6cJNFveVxI1cvdk=
+github.com/google/rpmpack v0.0.0-20210410105602-e20c988a6f5a/go.mod h1:+y9lKiqDhR4zkLl+V9h4q0rdyrYVsWWm6LLCQP33DIk=
 github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
 github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
diff --git a/nfpm.go b/nfpm.go
index 7da6283..8c25078 100644
--- a/nfpm.go
+++ b/nfpm.go
@@ -248,12 +248,19 @@ type Overridables struct {
 
 // RPM is custom configs that are only available on RPM packages.
 type RPM struct {
+	Scripts     RPMScripts   `yaml:"scripts,omitempty"`
 	Group       string       `yaml:"group,omitempty"`
 	Summary     string       `yaml:"summary,omitempty"`
 	Compression string       `yaml:"compression,omitempty"`
 	Signature   RPMSignature `yaml:"signature,omitempty"`
 }
 
+// RPMScripts represents scripts only available on RPM packages.
+type RPMScripts struct {
+	PreTrans  string `yaml:"pretrans,omitempty"`
+	PostTrans string `yaml:"posttrans,omitempty"`
+}
+
 type PackageSignature struct {
 	// PGP secret key, can be ASCII-armored
 	KeyFile       string  `yaml:"key_file,omitempty"`
diff --git a/rpm/rpm.go b/rpm/rpm.go
index 93f6eec..4d3a8a2 100644
--- a/rpm/rpm.go
+++ b/rpm/rpm.go
@@ -266,6 +266,13 @@ func toRelation(items []string) (rpmpack.Relations, error) {
 }
 
 func addScriptFiles(info *nfpm.Info, rpm *rpmpack.RPM) error {
+	if info.RPM.Scripts.PreTrans != "" {
+		data, err := ioutil.ReadFile(info.RPM.Scripts.PreTrans)
+		if err != nil {
+			return err
+		}
+		rpm.AddPretrans(string(data))
+	}
 	if info.Scripts.PreInstall != "" {
 		data, err := ioutil.ReadFile(info.Scripts.PreInstall)
 		if err != nil {
@@ -298,6 +305,14 @@ func addScriptFiles(info *nfpm.Info, rpm *rpmpack.RPM) error {
 		rpm.AddPostun(string(data))
 	}
 
+	if info.RPM.Scripts.PostTrans != "" {
+		data, err := ioutil.ReadFile(info.RPM.Scripts.PostTrans)
+		if err != nil {
+			return err
+		}
+		rpm.AddPosttrans(string(data))
+	}
+
 	return nil
 }
 
diff --git a/www/docs/configuration.md b/www/docs/configuration.md
index 2fed241..16abb6c 100644
--- a/www/docs/configuration.md
+++ b/www/docs/configuration.md
@@ -190,6 +190,13 @@ overrides:
 
 # Custom configuration applied only to the RPM packager.
 rpm:
+  # RPM specific scripts.
+  scripts:
+    # The pretrans script runs before all RPM package transactions / stages.
+    pretrans: ./scripts/pretrans.sh
+    # The posttrans script runs after all RPM package transactions / stages.
+    posttrans: ./scripts/posttrans.sh
+
   # The package group. This option is deprecated by most distros
   # but required by old distros like CentOS 5 / EL 5 and earlier.
   group: Unspecified
diff --git a/www/docs/tips.md b/www/docs/tips.md
index 09757f3..45b3306 100644
--- a/www/docs/tips.md
+++ b/www/docs/tips.md
@@ -138,8 +138,13 @@ esac
 ```
 
 ### Deb & RPM
-* `postremove` runs **AFTER** `postinstall` when you are upgrading a package. 
-   * So you need to be careful if you are deleting files in `postremove`
+* On upgrade, the scripts are being executed in the following order:
+    1. `pretrans` of new package (only applies to RPM)
+    2. `preinstall` of new package
+    3. `postinstall` of new package
+    4. `preremove` of old package
+    5. `postremove` of old package
+    6. `posttrans` of new package (only applies to RPM)
 
 ## Systemd and upstart/init
 ### upstart / init


Test patch:
diff --git a/rpm/rpm_test.go b/rpm/rpm_test.go
index 94a0bb6..1ded46f 100644
--- a/rpm/rpm_test.go
+++ b/rpm/rpm_test.go
@@ -76,6 +76,12 @@ func exampleInfo() *nfpm.Info {
 				PreRemove:   "../testdata/scripts/preremove.sh",
 				PostRemove:  "../testdata/scripts/postremove.sh",
 			},
+			RPM: nfpm.RPM{
+				Scripts: nfpm.RPMScripts{
+					PreTrans:  "../testdata/scripts/pretrans.sh",
+					PostTrans: "../testdata/scripts/posttrans.sh",
+				},
+			},
 		},
 	})
 }
@@ -366,6 +372,22 @@ echo "Postinstall" > /dev/null
 
 echo "Postremove" > /dev/null
 `, data, "Postremove script does not match")
+
+	rpmPreTransTag := 1151
+	data, err = rpm.Header.GetString(rpmPreTransTag)
+	require.NoError(t, err)
+	assert.Equal(t, `#!/bin/bash
+
+echo "Pretrans" > /dev/null
+`, data, "Pretrans script does not match")
+
+	rpmPostTransTag := 1152
+	data, err = rpm.Header.GetString(rpmPostTransTag)
+	require.NoError(t, err)
+	assert.Equal(t, `#!/bin/bash
+
+echo "Posttrans" > /dev/null
+`, data, "Posttrans script does not match")
 }
 
 func TestRPMFileDoesNotExist(t *testing.T) {
diff --git a/testdata/acceptance/core.complex.yaml b/testdata/acceptance/core.complex.yaml
index f76405e..2afaccb 100644
--- a/testdata/acceptance/core.complex.yaml
+++ b/testdata/acceptance/core.complex.yaml
@@ -35,6 +35,10 @@ scripts:
   postinstall: ./testdata/acceptance/scripts/postinstall.sh
   preremove: ./testdata/acceptance/scripts/preremove.sh
   postremove: ./testdata/acceptance/scripts/postremove.sh
+rpm:
+  scripts:
+    pretrans: ./testdata/acceptance/scripts/pretrans.sh
+    posttrans: ./testdata/acceptance/scripts/posttrans.sh
 apk:
   scripts:
     preupgrade: ./testdata/acceptance/scripts/preupgrade.sh
diff --git a/testdata/acceptance/rpm.dockerfile b/testdata/acceptance/rpm.dockerfile
index 102a2ee..23badf7 100644
--- a/testdata/acceptance/rpm.dockerfile
+++ b/testdata/acceptance/rpm.dockerfile
@@ -51,6 +51,8 @@ RUN test -d /var/log/whatever
 RUN test -d /usr/share/foo
 RUN test -f /tmp/preinstall-proof
 RUN test -f /tmp/postinstall-proof
+RUN test -f /tmp/pretrans-proof
+RUN test -f /tmp/posttrans-proof
 RUN test ! -f /tmp/preremove-proof
 RUN test ! -f /tmp/postremove-proof
 RUN echo wat >> /etc/foo/whatever.conf
diff --git a/testdata/acceptance/scripts/posttrans.sh b/testdata/acceptance/scripts/posttrans.sh
new file mode 100644
index 0000000..c3bd5e1
--- /dev/null
+++ b/testdata/acceptance/scripts/posttrans.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+echo "Ok" > /tmp/posttrans-proof
diff --git a/testdata/acceptance/scripts/pretrans.sh b/testdata/acceptance/scripts/pretrans.sh
new file mode 100644
index 0000000..18be39c
--- /dev/null
+++ b/testdata/acceptance/scripts/pretrans.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+echo "Ok" > /tmp/pretrans-proof
diff --git a/testdata/scripts/posttrans.sh b/testdata/scripts/posttrans.sh
new file mode 100644
index 0000000..faa697c
--- /dev/null
+++ b/testdata/scripts/posttrans.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+echo "Posttrans" > /dev/null
diff --git a/testdata/scripts/pretrans.sh b/testdata/scripts/pretrans.sh
new file mode 100644
index 0000000..03182d1
--- /dev/null
+++ b/testdata/scripts/pretrans.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+echo "Pretrans" > /dev/null