Skip to content

Commit

Permalink
feat: add %pretrans %posttrans scriptlets (#322)
Browse files Browse the repository at this point in the history
* create branch

* added rpm pretrans and posttrans scriptlets

* update rpmpack dependency

* updated acceptance testdata

* docs: updated documentation to include %pretrans and %posttrans rpm scripts

* cleanup

* docs: cleanup
  • Loading branch information
cristianciutea committed Apr 22, 2021
1 parent 501e743 commit 39f5bf3
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
7 changes: 7 additions & 0 deletions nfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
15 changes: 15 additions & 0 deletions rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
22 changes: 22 additions & 0 deletions rpm/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
})
}
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions testdata/acceptance/core.complex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions testdata/acceptance/rpm.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions testdata/acceptance/scripts/posttrans.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "Ok" > /tmp/posttrans-proof
3 changes: 3 additions & 0 deletions testdata/acceptance/scripts/pretrans.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "Ok" > /tmp/pretrans-proof
3 changes: 3 additions & 0 deletions testdata/scripts/posttrans.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "Posttrans" > /dev/null
3 changes: 3 additions & 0 deletions testdata/scripts/pretrans.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "Pretrans" > /dev/null
7 changes: 7 additions & 0 deletions www/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions www/docs/tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

1 comment on commit 39f5bf3

@vercel
Copy link

@vercel vercel bot commented on 39f5bf3 Apr 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.