1
1
mirror of https://github.com/goreleaser/nfpm synced 2024-11-19 07:34:48 +01:00
nfpm/rpm/rpm.go

393 lines
8.5 KiB
Go
Raw Normal View History

// Package rpm implements nfpm.Packager providing .rpm bindings using
// google/rpmpack.
2018-02-03 20:42:56 +01:00
package rpm
import (
"bytes"
"fmt"
2018-02-03 20:42:56 +01:00
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
2018-02-03 20:42:56 +01:00
"github.com/google/rpmpack"
"github.com/pkg/errors"
"github.com/sassoftware/go-rpmutils/cpio"
"github.com/goreleaser/nfpm/internal/files"
"github.com/goreleaser/chglog"
2018-02-05 02:53:22 +01:00
"github.com/goreleaser/nfpm"
2018-02-03 20:42:56 +01:00
)
const (
// https://github.com/rpm-software-management/rpm/blob/master/lib/rpmtag.h#L152
tagChangelogTime = 1080
// https://github.com/rpm-software-management/rpm/blob/master/lib/rpmtag.h#L153
tagChangelogName = 1081
// https://github.com/rpm-software-management/rpm/blob/master/lib/rpmtag.h#L154
tagChangelogText = 1082
changelogNotesTemplate = `
{{- range .Changes }}{{$note := splitList "\n" .Note}}
- {{ first $note }}
{{- range $i,$n := (rest $note) }}{{- if ne (trim $n) ""}}
{{$n}}{{end}}
{{- end}}{{- end}}`
)
2019-03-04 14:14:05 +01:00
// nolint: gochecknoinits
2018-02-12 16:50:25 +01:00
func init() {
nfpm.Register("rpm", Default)
}
2018-02-03 20:42:56 +01:00
2019-08-30 03:05:25 +02:00
// Default RPM packager
2019-03-04 14:14:05 +01:00
// nolint: gochecknoglobals
2018-02-03 20:42:56 +01:00
var Default = &RPM{}
// RPM is a RPM packager implementation.
2018-02-03 20:42:56 +01:00
type RPM struct{}
2019-03-20 01:48:14 +01:00
// nolint: gochecknoglobals
2019-03-20 01:34:06 +01:00
var archToRPM = map[string]string{
"all": "noarch",
2019-03-20 01:34:06 +01:00
"amd64": "x86_64",
"386": "i386",
"arm64": "aarch64",
}
func ensureValidArch(info *nfpm.Info) *nfpm.Info {
2019-03-20 01:34:06 +01:00
arch, ok := archToRPM[info.Arch]
if ok {
info.Arch = arch
}
return info
}
// ConventionalFileName returns a file name according
// to the conventions for RPM packages. See:
// http://ftp.rpm.org/max-rpm/ch-rpm-file-format.html
func (*RPM) ConventionalFileName(info *nfpm.Info) string {
info = ensureValidArch(info)
version := formatVersion(info)
if info.Release != "" {
version += "-" + info.Release
}
// name-version-release.architecture.rpm
return fmt.Sprintf("%s-%s.%s.rpm", info.Name, version, info.Arch)
}
// Package writes a new RPM package to the given writer using the given info.
func (*RPM) Package(info *nfpm.Info, w io.Writer) error {
var (
err error
meta *rpmpack.RPMMetaData
rpm *rpmpack.RPM
)
info = ensureValidArch(info)
2018-02-03 20:42:56 +01:00
if meta, err = buildRPMMeta(info); err != nil {
return err
}
if rpm, err = rpmpack.NewRPM(*meta); err != nil {
return err
}
2018-03-06 01:02:26 +01:00
addEmptyDirsRPM(info, rpm)
if err = createFilesInsideRPM(info, rpm); err != nil {
return err
}
addSymlinksInsideRPM(info, rpm)
if err = addScriptFiles(info, rpm); err != nil {
return err
}
2018-04-08 23:37:25 +02:00
if info.Changelog != "" {
if err = addChangeLog(info, rpm); err != nil {
return err
}
}
if err = rpm.Write(w); err != nil {
2018-04-08 23:37:25 +02:00
return err
}
2018-02-18 21:13:47 +01:00
return nil
2018-02-06 03:28:30 +01:00
}
func addChangeLog(info *nfpm.Info, rpm *rpmpack.RPM) error {
changelog, err := info.GetChangeLog()
if err != nil {
return fmt.Errorf("reading changelog: %w", err)
}
if len(changelog.Entries) == 0 {
// no nothing because creating empty tags
// would result in an invalid package
return nil
}
tpl, err := chglog.LoadTemplateData(changelogNotesTemplate)
if err != nil {
return fmt.Errorf("parsing RPM changelog template: %w", err)
}
changes := make([]string, len(changelog.Entries))
titles := make([]string, len(changelog.Entries))
times := make([]uint32, len(changelog.Entries))
for idx, entry := range changelog.Entries {
var formattedNotes bytes.Buffer
err := tpl.Execute(&formattedNotes, entry)
if err != nil {
return fmt.Errorf("formatting changlog notes: %w", err)
}
changes[idx] = strings.TrimSpace(formattedNotes.String())
times[idx] = uint32(entry.Date.Unix())
titles[idx] = fmt.Sprintf("%s - %s", entry.Packager, entry.Semver)
}
rpm.AddCustomTag(tagChangelogTime, rpmpack.EntryUint32(times))
rpm.AddCustomTag(tagChangelogName, rpmpack.EntryStringSlice(titles))
rpm.AddCustomTag(tagChangelogText, rpmpack.EntryStringSlice(changes))
return nil
}
//nolint:funlen
func buildRPMMeta(info *nfpm.Info) (*rpmpack.RPMMetaData, error) {
var (
err error
epoch uint64
provides,
depends,
recommends,
replaces,
suggests,
conflicts rpmpack.Relations
)
if epoch, err = strconv.ParseUint(defaultTo(info.Epoch, "0"), 10, 32); err != nil {
return nil, err
}
if provides, err = toRelation(info.Provides); err != nil {
return nil, err
}
if depends, err = toRelation(info.Depends); err != nil {
return nil, err
}
if recommends, err = toRelation(info.Recommends); err != nil {
return nil, err
}
if replaces, err = toRelation(info.Replaces); err != nil {
return nil, err
}
if suggests, err = toRelation(info.Suggests); err != nil {
return nil, err
}
if conflicts, err = toRelation(info.Conflicts); err != nil {
return nil, err
}
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
return &rpmpack.RPMMetaData{
Name: info.Name,
Summary: strings.Split(info.Description, "\n")[0],
Description: info.Description,
Version: formatVersion(info),
Release: defaultTo(info.Release, "1"),
Epoch: uint32(epoch),
Arch: info.Arch,
OS: info.Platform,
Licence: info.License,
URL: info.Homepage,
Vendor: info.Vendor,
Packager: info.Maintainer,
2020-08-04 20:41:54 +02:00
Group: info.RPM.Group,
Provides: provides,
Recommends: recommends,
Requires: depends,
Obsoletes: replaces,
Suggests: suggests,
Conflicts: conflicts,
Compressor: info.RPM.Compression,
BuildTime: time.Now(),
BuildHost: hostname,
}, nil
}
func formatVersion(info *nfpm.Info) string {
if info.Prerelease == "" {
return info.Version
}
return info.Version + "~" + info.Prerelease
}
func defaultTo(in, def string) string {
if in == "" {
return def
}
return in
}
func toRelation(items []string) (rpmpack.Relations, error) {
relations := make(rpmpack.Relations, 0)
for idx := range items {
if err := relations.Set(items[idx]); err != nil {
return nil, err
}
}
return relations, nil
}
func addScriptFiles(info *nfpm.Info, rpm *rpmpack.RPM) error {
if info.Scripts.PreInstall != "" {
data, err := ioutil.ReadFile(info.Scripts.PreInstall)
if err != nil {
return err
2018-04-08 23:37:25 +02:00
}
rpm.AddPrein(string(data))
2018-04-08 23:37:25 +02:00
}
if info.Scripts.PreRemove != "" {
data, err := ioutil.ReadFile(info.Scripts.PreRemove)
if err != nil {
return err
}
rpm.AddPreun(string(data))
2018-02-16 02:58:37 +01:00
}
if info.Scripts.PostInstall != "" {
data, err := ioutil.ReadFile(info.Scripts.PostInstall)
if err != nil {
return err
}
rpm.AddPostin(string(data))
2018-02-16 02:58:37 +01:00
}
if info.Scripts.PostRemove != "" {
data, err := ioutil.ReadFile(info.Scripts.PostRemove)
if err != nil {
return err
2018-02-06 03:28:30 +01:00
}
rpm.AddPostun(string(data))
2018-02-06 03:28:30 +01:00
}
2018-02-06 03:28:30 +01:00
return nil
}
func addEmptyDirsRPM(info *nfpm.Info, rpm *rpmpack.RPM) {
for _, dir := range info.EmptyFolders {
rpm.AddFile(
rpmpack.RPMFile{
Name: dir,
Mode: uint(040755),
MTime: uint32(time.Now().Unix()),
Owner: "root",
Group: "root",
},
)
}
}
func createFilesInsideRPM(info *nfpm.Info, rpm *rpmpack.RPM) error {
regularFiles, err := files.Expand(info.Files)
if err != nil {
return err
}
for _, file := range regularFiles {
err = copyToRPM(rpm, file.Source, file.Destination, rpmpack.GenericFile)
if err != nil {
return err
}
}
configFiles, err := files.Expand(info.ConfigFiles)
if err != nil {
return err
}
for _, file := range configFiles {
err = copyToRPM(rpm, file.Source, file.Destination, rpmpack.ConfigFile)
if err != nil {
return err
}
}
configNoReplaceFiles, err := files.Expand(info.RPM.ConfigNoReplaceFiles)
if err != nil {
return err
}
for _, file := range configNoReplaceFiles {
err = copyToRPM(rpm, file.Source, file.Destination, rpmpack.ConfigFile|rpmpack.NoReplaceFile)
if err != nil {
return err
}
2018-02-06 03:28:30 +01:00
}
2018-02-16 02:58:37 +01:00
return nil
2018-02-06 03:28:30 +01:00
}
func addSymlinksInsideRPM(info *nfpm.Info, rpm *rpmpack.RPM) {
for src, dst := range info.Symlinks {
rpm.AddFile(rpmpack.RPMFile{
Name: src,
Body: []byte(dst),
Mode: uint(cpio.S_ISLNK),
MTime: uint32(time.Now().UTC().Unix()),
Owner: "root",
Group: "root",
})
}
}
func copyToRPM(rpm *rpmpack.RPM, src, dst string, fileType rpmpack.FileType) error {
file, err := os.OpenFile(src, os.O_RDONLY, 0600) //nolint:gosec
2018-02-16 03:12:54 +01:00
if err != nil {
return errors.Wrap(err, "could not add file to the archive")
}
// don't care if it errs while closing...
defer file.Close() // nolint: errcheck,gosec
2018-02-16 03:12:54 +01:00
info, err := file.Stat()
if err != nil {
return err
}
if info.IsDir() {
// TODO: this should probably return an error
2018-02-16 03:12:54 +01:00
return nil
}
data, err := ioutil.ReadAll(file)
if err != nil {
return err
2018-02-16 03:12:54 +01:00
}
2018-02-03 20:42:56 +01:00
rpmFile := rpmpack.RPMFile{
Name: dst,
Body: data,
Mode: uint(info.Mode()),
MTime: uint32(info.ModTime().Unix()),
Owner: "root",
Group: "root",
Type: fileType,
}
rpm.AddFile(rpmFile)
2018-02-03 20:42:56 +01:00
return nil
}