2017-07-27 02:30:48 +02:00
|
|
|
// Package snapcraft implements the Pipe interface providing Snapcraft bindings.
|
|
|
|
package snapcraft
|
|
|
|
|
|
|
|
import (
|
2017-08-02 14:06:28 +02:00
|
|
|
"fmt"
|
2017-07-27 02:30:48 +02:00
|
|
|
"io/ioutil"
|
2017-07-28 03:05:43 +02:00
|
|
|
"os"
|
2017-07-27 02:30:48 +02:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2018-06-25 02:12:53 +02:00
|
|
|
"strings"
|
2017-12-17 21:25:04 +02:00
|
|
|
|
2017-07-27 02:30:48 +02:00
|
|
|
"github.com/apex/log"
|
2019-12-27 22:31:03 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
2017-12-18 13:00:19 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2019-05-29 14:13:52 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/ids"
|
2017-08-27 18:18:23 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/linux"
|
2018-09-12 19:18:01 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
2018-07-10 07:08:22 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
2018-07-09 05:47:30 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
2019-05-27 17:47:05 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2017-07-27 02:30:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrNoSnapcraft is shown when snapcraft cannot be found in $PATH
|
|
|
|
var ErrNoSnapcraft = errors.New("snapcraft not present in $PATH")
|
|
|
|
|
2017-08-17 23:41:08 +02:00
|
|
|
// ErrNoDescription is shown when no description provided
|
|
|
|
var ErrNoDescription = errors.New("no description provided for snapcraft")
|
|
|
|
|
|
|
|
// ErrNoSummary is shown when no summary provided
|
|
|
|
var ErrNoSummary = errors.New("no summary provided for snapcraft")
|
|
|
|
|
2017-09-15 02:19:56 +02:00
|
|
|
// Metadata to generate the snap package
|
|
|
|
type Metadata struct {
|
2017-07-27 02:30:48 +02:00
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
Summary string
|
|
|
|
Description string
|
2019-04-18 22:21:40 +02:00
|
|
|
Base string `yaml:",omitempty"`
|
2019-04-17 22:26:30 +02:00
|
|
|
License string `yaml:",omitempty"`
|
2017-07-27 02:30:48 +02:00
|
|
|
Grade string `yaml:",omitempty"`
|
|
|
|
Confinement string `yaml:",omitempty"`
|
|
|
|
Architectures []string
|
2017-08-04 07:42:55 +02:00
|
|
|
Apps map[string]AppMetadata
|
2019-03-14 14:56:11 +02:00
|
|
|
Plugs map[string]interface{} `yaml:",omitempty"`
|
2017-07-27 02:30:48 +02:00
|
|
|
}
|
|
|
|
|
2017-08-04 07:42:55 +02:00
|
|
|
// AppMetadata for the binaries that will be in the snap package
|
|
|
|
type AppMetadata struct {
|
2019-06-23 03:37:55 +02:00
|
|
|
Command string
|
|
|
|
Plugs []string `yaml:",omitempty"`
|
|
|
|
Daemon string `yaml:",omitempty"`
|
|
|
|
Completer string `yaml:",omitempty"`
|
2017-07-27 02:30:48 +02:00
|
|
|
}
|
|
|
|
|
2020-02-06 03:08:18 +02:00
|
|
|
const defaultNameTemplate = "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if .Mips }}_{{ .Mips }}{{ end }}"
|
2017-12-27 01:44:11 +02:00
|
|
|
|
2017-07-27 02:30:48 +02:00
|
|
|
// Pipe for snapcraft packaging
|
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-12-02 23:53:19 +02:00
|
|
|
func (Pipe) String() string {
|
2018-10-20 22:41:31 +02:00
|
|
|
return "Snapcraft Packages"
|
2017-07-27 02:30:48 +02:00
|
|
|
}
|
|
|
|
|
2017-12-27 01:44:11 +02:00
|
|
|
// Default sets the pipe defaults
|
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
2019-07-27 15:55:56 +02:00
|
|
|
var ids = ids.New("snapcrafts")
|
2019-05-27 17:47:05 +02:00
|
|
|
for i := range ctx.Config.Snapcrafts {
|
|
|
|
var snap = &ctx.Config.Snapcrafts[i]
|
|
|
|
if snap.NameTemplate == "" {
|
|
|
|
snap.NameTemplate = defaultNameTemplate
|
|
|
|
}
|
|
|
|
if len(snap.Builds) == 0 {
|
|
|
|
for _, b := range ctx.Config.Builds {
|
|
|
|
snap.Builds = append(snap.Builds, b.ID)
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 14:13:52 +02:00
|
|
|
ids.Inc(snap.ID)
|
2017-12-27 01:44:11 +02:00
|
|
|
}
|
2019-05-29 14:13:52 +02:00
|
|
|
return ids.Validate()
|
2017-12-27 01:44:11 +02:00
|
|
|
}
|
|
|
|
|
2017-07-27 02:30:48 +02:00
|
|
|
// Run the pipe
|
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2019-05-27 17:47:05 +02:00
|
|
|
for _, snap := range ctx.Config.Snapcrafts {
|
|
|
|
// TODO: deal with pipe.skip?
|
|
|
|
if err := doRun(ctx, snap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func doRun(ctx *context.Context, snap config.Snapcraft) error {
|
|
|
|
if snap.Summary == "" && snap.Description == "" {
|
2018-09-12 19:18:01 +02:00
|
|
|
return pipe.Skip("no summary nor description were provided")
|
2017-07-27 02:30:48 +02:00
|
|
|
}
|
2019-05-27 17:47:05 +02:00
|
|
|
if snap.Summary == "" {
|
2017-08-17 23:41:08 +02:00
|
|
|
return ErrNoSummary
|
|
|
|
}
|
2019-05-27 17:47:05 +02:00
|
|
|
if snap.Description == "" {
|
2017-08-17 23:41:08 +02:00
|
|
|
return ErrNoDescription
|
2017-07-27 02:30:48 +02:00
|
|
|
}
|
|
|
|
_, err := exec.LookPath("snapcraft")
|
|
|
|
if err != nil {
|
|
|
|
return ErrNoSnapcraft
|
|
|
|
}
|
|
|
|
|
2018-07-10 07:08:22 +02:00
|
|
|
var g = semerrgroup.New(ctx.Parallelism)
|
2017-12-17 21:25:04 +02:00
|
|
|
for platform, binaries := range ctx.Artifacts.Filter(
|
|
|
|
artifact.And(
|
|
|
|
artifact.ByGoos("linux"),
|
|
|
|
artifact.ByType(artifact.Binary),
|
2019-05-27 17:47:05 +02:00
|
|
|
artifact.ByIDs(snap.Builds...),
|
2017-12-17 21:25:04 +02:00
|
|
|
),
|
|
|
|
).GroupByPlatform() {
|
2017-12-29 19:10:09 +02:00
|
|
|
arch := linux.Arch(platform)
|
2020-02-06 03:08:18 +02:00
|
|
|
if !isValidArch(arch) {
|
2018-06-20 14:39:10 +02:00
|
|
|
log.WithField("arch", arch).Warn("ignored unsupported arch")
|
|
|
|
continue
|
|
|
|
}
|
2017-12-17 21:25:04 +02:00
|
|
|
binaries := binaries
|
|
|
|
g.Go(func() error {
|
2019-05-27 17:47:05 +02:00
|
|
|
return create(ctx, snap, arch, binaries)
|
2017-12-17 21:25:04 +02:00
|
|
|
})
|
2017-07-27 02:30:48 +02:00
|
|
|
}
|
|
|
|
return g.Wait()
|
|
|
|
}
|
|
|
|
|
2020-02-06 03:08:18 +02:00
|
|
|
func isValidArch(arch string) bool {
|
|
|
|
// https://snapcraft.io/docs/architectures
|
|
|
|
for _, a := range []string{"s390x", "ppc64el", "arm64", "armhf", "amd64", "i386"} {
|
|
|
|
if arch == a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-10-20 19:25:46 +02:00
|
|
|
// Publish packages
|
|
|
|
func (Pipe) Publish(ctx *context.Context) error {
|
|
|
|
snaps := ctx.Artifacts.Filter(artifact.ByType(artifact.PublishableSnapcraft)).List()
|
|
|
|
var g = semerrgroup.New(ctx.Parallelism)
|
|
|
|
for _, snap := range snaps {
|
|
|
|
snap := snap
|
|
|
|
g.Go(func() error {
|
2018-10-20 19:26:49 +02:00
|
|
|
return push(ctx, snap)
|
2018-10-20 19:25:46 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return g.Wait()
|
|
|
|
}
|
|
|
|
|
2019-08-12 22:44:48 +02:00
|
|
|
func create(ctx *context.Context, snap config.Snapcraft, arch string, binaries []*artifact.Artifact) error {
|
2017-08-20 21:35:46 +02:00
|
|
|
var log = log.WithField("arch", arch)
|
2018-07-09 05:47:30 +02:00
|
|
|
folder, err := tmpl.New(ctx).
|
2019-05-27 17:47:05 +02:00
|
|
|
WithArtifact(binaries[0], snap.Replacements).
|
|
|
|
Apply(snap.NameTemplate)
|
2017-12-17 21:25:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-26 17:23:08 +02:00
|
|
|
|
2017-07-28 03:05:43 +02:00
|
|
|
// prime is the directory that then will be compressed to make the .snap package.
|
2017-08-27 18:47:41 +02:00
|
|
|
var folderDir = filepath.Join(ctx.Config.Dist, folder)
|
|
|
|
var primeDir = filepath.Join(folderDir, "prime")
|
|
|
|
var metaDir = filepath.Join(primeDir, "meta")
|
2017-11-26 16:09:12 +02:00
|
|
|
// #nosec
|
2017-12-18 02:31:27 +02:00
|
|
|
if err = os.MkdirAll(metaDir, 0755); err != nil {
|
2017-07-28 03:05:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var file = filepath.Join(primeDir, "meta", "snap.yaml")
|
2018-10-27 00:31:06 +02:00
|
|
|
log.WithField("file", file).Debug("creating snap metadata")
|
2017-07-27 02:30:48 +02:00
|
|
|
|
2017-09-15 02:19:56 +02:00
|
|
|
var metadata = &Metadata{
|
2017-07-27 02:30:48 +02:00
|
|
|
Version: ctx.Version,
|
2019-05-27 17:47:05 +02:00
|
|
|
Summary: snap.Summary,
|
|
|
|
Description: snap.Description,
|
|
|
|
Grade: snap.Grade,
|
|
|
|
Confinement: snap.Confinement,
|
2017-07-27 02:30:48 +02:00
|
|
|
Architectures: []string{arch},
|
2018-12-12 22:24:22 +02:00
|
|
|
Apps: map[string]AppMetadata{},
|
2017-07-27 02:30:48 +02:00
|
|
|
}
|
2018-02-16 14:35:44 +02:00
|
|
|
|
2019-05-27 17:47:05 +02:00
|
|
|
if snap.Base != "" {
|
|
|
|
metadata.Base = snap.Base
|
2019-04-18 22:21:40 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 17:47:05 +02:00
|
|
|
if snap.License != "" {
|
|
|
|
metadata.License = snap.License
|
2019-04-17 22:26:30 +02:00
|
|
|
}
|
|
|
|
|
2018-02-16 14:35:44 +02:00
|
|
|
metadata.Name = ctx.Config.ProjectName
|
2019-05-27 17:47:05 +02:00
|
|
|
if snap.Name != "" {
|
|
|
|
metadata.Name = snap.Name
|
2017-08-07 18:28:04 +02:00
|
|
|
}
|
2017-07-27 02:30:48 +02:00
|
|
|
|
2020-01-26 17:23:08 +02:00
|
|
|
// if the user didn't specify any apps then
|
|
|
|
// default to the main binary being the command:
|
|
|
|
if len(snap.Apps) == 0 {
|
2020-01-30 15:57:07 +02:00
|
|
|
var name = snap.Name
|
|
|
|
if name == "" {
|
|
|
|
name = binaries[0].Name
|
2017-08-20 21:35:46 +02:00
|
|
|
}
|
2020-01-30 15:57:07 +02:00
|
|
|
metadata.Apps[name] = AppMetadata{
|
2020-01-26 17:23:08 +02:00
|
|
|
Command: filepath.Base(binaries[0].Name),
|
2017-08-04 07:42:55 +02:00
|
|
|
}
|
2020-01-26 17:23:08 +02:00
|
|
|
}
|
2017-07-28 03:05:43 +02:00
|
|
|
|
2020-01-26 17:23:08 +02:00
|
|
|
for _, binary := range binaries {
|
|
|
|
// build the binaries and link resources
|
|
|
|
completerPath := ""
|
2017-07-28 03:05:43 +02:00
|
|
|
destBinaryPath := filepath.Join(primeDir, filepath.Base(binary.Path))
|
2018-12-13 14:09:36 +02:00
|
|
|
log.WithField("src", binary.Path).
|
|
|
|
WithField("dst", destBinaryPath).
|
|
|
|
Debug("linking")
|
2020-01-26 17:23:08 +02:00
|
|
|
|
2017-12-18 02:31:27 +02:00
|
|
|
if err = os.Link(binary.Path, destBinaryPath); err != nil {
|
2018-12-12 22:32:22 +02:00
|
|
|
return errors.Wrap(err, "failed to link binary")
|
|
|
|
}
|
|
|
|
if err := os.Chmod(destBinaryPath, 0555); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to change binary permissions")
|
2017-08-02 14:06:28 +02:00
|
|
|
}
|
2019-06-23 03:37:55 +02:00
|
|
|
|
|
|
|
if completerPath != "" {
|
|
|
|
destCompleterPath := filepath.Join(primeDir, filepath.Base(completerPath))
|
|
|
|
log.WithField("src", completerPath).
|
|
|
|
WithField("dst", destCompleterPath).
|
|
|
|
Debug("linking")
|
|
|
|
if err := os.Link(completerPath, destCompleterPath); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to link completer")
|
|
|
|
}
|
|
|
|
if err := os.Chmod(destCompleterPath, 0444); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to change completer permissions")
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 14:38:42 +02:00
|
|
|
|
2020-01-26 17:23:08 +02:00
|
|
|
// setup the apps: directive for each binary
|
2020-01-30 15:57:07 +02:00
|
|
|
for name, config := range snap.Apps {
|
2020-01-26 17:23:08 +02:00
|
|
|
log.WithField("path", binary.Path).
|
|
|
|
WithField("name", binary.Name).
|
|
|
|
Debug("passed binary to snapcraft")
|
|
|
|
|
|
|
|
appMetadata := AppMetadata{
|
2020-01-30 15:57:07 +02:00
|
|
|
Command: strings.TrimSpace(strings.Join([]string{
|
2020-01-26 17:23:08 +02:00
|
|
|
binary.Name,
|
2020-01-30 15:57:07 +02:00
|
|
|
config.Args,
|
|
|
|
}, " ")),
|
|
|
|
Plugs: config.Plugs,
|
|
|
|
Daemon: config.Daemon,
|
|
|
|
}
|
2020-01-26 17:23:08 +02:00
|
|
|
|
2020-01-30 15:57:07 +02:00
|
|
|
if config.Completer != "" {
|
|
|
|
appMetadata.Completer = filepath.Base(config.Completer)
|
2020-01-26 17:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
metadata.Apps[name] = appMetadata
|
|
|
|
metadata.Plugs = snap.Plugs
|
|
|
|
}
|
2018-08-15 14:38:42 +02:00
|
|
|
}
|
|
|
|
|
2017-07-27 02:30:48 +02:00
|
|
|
out, err := yaml.Marshal(metadata)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-20 02:45:08 +02:00
|
|
|
log.WithField("file", file).Debugf("writing metadata file")
|
2017-07-27 02:30:48 +02:00
|
|
|
if err = ioutil.WriteFile(file, out, 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-27 17:47:05 +02:00
|
|
|
var snapFile = filepath.Join(ctx.Config.Dist, folder+".snap")
|
|
|
|
log.WithField("snap", snapFile).Info("creating")
|
2017-11-26 16:09:12 +02:00
|
|
|
/* #nosec */
|
2019-05-27 17:47:05 +02:00
|
|
|
var cmd = exec.CommandContext(ctx, "snapcraft", "pack", primeDir, "--output", snapFile)
|
2017-07-27 05:43:53 +02:00
|
|
|
if out, err = cmd.CombinedOutput(); err != nil {
|
2017-08-02 14:06:28 +02:00
|
|
|
return fmt.Errorf("failed to generate snap package: %s", string(out))
|
2017-07-27 05:43:53 +02:00
|
|
|
}
|
2019-05-27 17:47:05 +02:00
|
|
|
if !snap.Publish {
|
2018-10-20 20:13:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-12 22:44:48 +02:00
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
2018-10-20 19:25:46 +02:00
|
|
|
Type: artifact.PublishableSnapcraft,
|
2017-12-17 21:25:04 +02:00
|
|
|
Name: folder + ".snap",
|
2019-05-27 17:47:05 +02:00
|
|
|
Path: snapFile,
|
2017-12-17 21:25:04 +02:00
|
|
|
Goos: binaries[0].Goos,
|
|
|
|
Goarch: binaries[0].Goarch,
|
|
|
|
Goarm: binaries[0].Goarm,
|
|
|
|
})
|
2017-07-27 02:30:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-10-20 19:26:49 +02:00
|
|
|
|
2019-07-20 23:31:04 +02:00
|
|
|
const reviewWaitMsg = `Waiting for previous upload(s) to complete their review process.`
|
|
|
|
|
2019-08-12 22:44:48 +02:00
|
|
|
func push(ctx *context.Context, snap *artifact.Artifact) error {
|
2019-07-20 23:31:04 +02:00
|
|
|
var log = log.WithField("snap", snap.Name)
|
|
|
|
log.Info("pushing snap")
|
2018-10-20 22:47:55 +02:00
|
|
|
// TODO: customize --release based on snap.Grade?
|
2018-10-20 20:16:14 +02:00
|
|
|
/* #nosec */
|
2018-10-20 19:26:49 +02:00
|
|
|
var cmd = exec.CommandContext(ctx, "snapcraft", "push", "--release=stable", snap.Path)
|
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
2019-07-20 23:31:04 +02:00
|
|
|
if strings.Contains(string(out), reviewWaitMsg) {
|
|
|
|
log.Warn(reviewWaitMsg)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("failed to push %s package: %s", snap.Path, string(out))
|
|
|
|
}
|
2018-10-20 19:26:49 +02:00
|
|
|
}
|
|
|
|
snap.Type = artifact.Snapcraft
|
|
|
|
ctx.Artifacts.Add(snap)
|
|
|
|
return nil
|
|
|
|
}
|