Skip to content

Commit

Permalink
Fix & Enhance Goss packer provisioner (#30)
Browse files Browse the repository at this point in the history
* Add format options as a config param to goss-prov.

  - Correctly form download path when URL is given

* Remove invalid debug optiom

  - `goss validate` has no debug option. It is only
  for `goss generate`
  • Loading branch information
EleanorRigby authored Jul 14, 2020
1 parent 2c65c16 commit 06e3573
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 50 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ There is an example packer build with goss tests in the `example/` directory.
"vars_file": "",
"username": "",
"password": "",
"debug": false,
"retry_timeout": "0s",
"sleep": "1s"
}
Expand Down
51 changes: 38 additions & 13 deletions packer-provisioner-goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/hashicorp/hcl/v2/hcldec"

Expand All @@ -28,9 +29,6 @@ type GossConfig struct {
Password string
SkipInstall bool

// Enable debug for goss (defaults to false)
Debug bool

// An array of tests to run.
Tests []string

Expand Down Expand Up @@ -66,10 +64,16 @@ type GossConfig struct {
// Default: rspecish
Format string `mapstructure:"format"`

// The format options to use for printing test output
// Available: [perfdata verbose pretty]
// Default: verbose
FormatOptions string `mapstructure:"format_options"`

ctx interpolate.Context
}

var validFormats = []string{"documentation", "json", "json_oneline", "junit", "nagios", "nagios_verbose", "rspecish", "silent", "tap"}
var validFormatOptions = []string{"perfdata", "verbose", "pretty"}

// Provisioner implements a packer Provisioner
type Provisioner struct {
Expand Down Expand Up @@ -117,7 +121,14 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}

if p.config.DownloadPath == "" {
p.config.DownloadPath = fmt.Sprintf("/tmp/goss-%s-linux-%s", p.config.Version, p.config.Arch)
if p.config.URL == "" {
p.config.DownloadPath = fmt.Sprintf("/tmp/goss-%s-linux-%s", p.config.Version, p.config.Arch)
} else {
list := strings.Split(p.config.URL, "/")
arch := strings.Split(list[len(list)-1], "-")[2]
version := strings.TrimPrefix(list[len(list)-2], "v")
p.config.DownloadPath = fmt.Sprintf("/tmp/goss-%s-linux-%s", version, arch)
}
}

if p.config.RemoteFolder == "" {
Expand Down Expand Up @@ -152,6 +163,21 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}
}

if p.config.FormatOptions != "" {
valid := false
for _, candidate := range validFormatOptions {
if p.config.FormatOptions == candidate {
valid = true
break
}
}
if !valid {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Invalid format options choice %s. Valid options: %v",
p.config.FormatOptions, validFormatOptions))
}
}

if len(p.config.Tests) == 0 {
errs = packer.MultiErrorAppend(errs,
errors.New("tests must be specified"))
Expand Down Expand Up @@ -266,9 +292,9 @@ func (p *Provisioner) runGoss(ui packer.Ui, comm packer.Communicator) error {

cmd := &packer.RemoteCmd{
Command: fmt.Sprintf(
"cd %s && %s %s %s %s %s validate --retry-timeout %s --sleep %s %s",
"cd %s && %s %s %s %s validate --retry-timeout %s --sleep %s %s %s",
p.config.RemotePath, p.enableSudo(), goss, p.config.GossFile,
p.vars(), p.debug(), p.retryTimeout(), p.sleep(), p.format()),
p.vars(), p.retryTimeout(), p.sleep(), p.format(), p.formatOptions()),
}
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
return err
Expand All @@ -294,17 +320,16 @@ func (p *Provisioner) sleep() string {
return p.config.Sleep
}

// debug returns the debug flag if debug is configured
func (p *Provisioner) debug() string {
if p.config.Debug {
return "-d"
func (p *Provisioner) format() string {
if p.config.Format != "" {
return fmt.Sprintf("-f %s", p.config.Format)
}
return ""
}

func (p *Provisioner) format() string {
if p.config.Format != "" {
return fmt.Sprintf("-f %s", p.config.Format)
func (p *Provisioner) formatOptions() string {
if p.config.FormatOptions != "" {
return fmt.Sprintf("-o %s", p.config.FormatOptions)
}
return ""
}
Expand Down
72 changes: 36 additions & 36 deletions packer-provisioner-goss.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 06e3573

Please sign in to comment.