Skip to content

Commit

Permalink
Add support for passing inline vars to GOSS (#32)
Browse files Browse the repository at this point in the history
* Add support for passing inline vars to GOSS
* Add inspect mode to disable failing packer build
  • Loading branch information
EleanorRigby authored Jul 27, 2020
1 parent 617d576 commit 1c68d9a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 29 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,34 @@ There is an example packer build with goss tests in the `example/` directory.
```json
"provisioners" : [
{
# Packer Args
"type": "goss",
"version": "0.3.2",

# Packer Provisioner Args
"arch": "amd64",
"downloadPath": "/tmp/goss-VERSION-linux-ARCH",
"inspect": "{{user `inspect_mode`}}",
"password": "",
"skipInstall": false,
"url":"https://github.com/aelsabbahy/goss/releases/download/vVERSION/goss-linux-ARCH",
"username": "",
"version": "0.3.2",

# GOSS Args
"tests": [
"goss/goss.yaml"
],
"downloadPath": "/tmp/goss-VERSION-linux-ARCH",
"remote_folder": "/tmp",
"remote_path": "/tmp/goss",
"skipInstall": false,
"skip_ssl": false,
"use_sudo": false,
"format": "",
"goss_file": "",
"vars_file": "",
"username": "",
"password": "",
"vars_inline": {
"OS": "centos",
"version": "{{user `version`}}"
},
"retry_timeout": "0s",
"sleep": "1s"
}
Expand Down
43 changes: 37 additions & 6 deletions packer-provisioner-goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
Expand All @@ -28,6 +29,7 @@ type GossConfig struct {
Username string
Password string
SkipInstall bool
Inspect bool

// An array of tests to run.
Tests []string
Expand All @@ -51,6 +53,10 @@ type GossConfig struct {
// Can be YAML or JSON.
VarsFile string `mapstructure:"vars_file"`

// The --vars-inline flag
// Optional inline variables that overrides JSON file vars
VarsInline map[string]string `mapstructure:"vars_inline"`

// The remote folder where the goss tests will be uploaded to.
// This should be set to a pre-existing directory, it defaults to /tmp
RemoteFolder string `mapstructure:"remote_folder"`
Expand Down Expand Up @@ -227,6 +233,10 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C
}
}
}
if len(p.config.VarsInline) != 0 {
ui.Message(fmt.Sprintf("Inline variables are %v", p.config.VarsInline))
ui.Message(fmt.Sprintf("Inline variable string is %s", p.inline_vars()))
}

for _, src := range p.config.Tests {
s, err := os.Stat(src)
Expand Down Expand Up @@ -290,19 +300,28 @@ func (p *Provisioner) runGoss(ui packer.Ui, comm packer.Communicator) error {
goss := fmt.Sprintf("%s", p.config.DownloadPath)
ctx := context.TODO()

strcmd := fmt.Sprintf("cd %s && %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.inline_vars(), p.retryTimeout(), p.sleep(), p.format(), p.formatOptions())
ui.Message(fmt.Sprintf("Command : %s", strcmd))

cmd := &packer.RemoteCmd{
Command: fmt.Sprintf(
"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.retryTimeout(), p.sleep(), p.format(), p.formatOptions()),
Command: strcmd,
}
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
return err
}
if cmd.ExitStatus() != 0 {
return fmt.Errorf("goss non-zero exit status")
// Inspect mode is on. Report failure but don't fail.
if p.config.Inspect {
ui.Say(fmt.Sprintf("Goss tests failed"))
ui.Say(fmt.Sprintf("Inpect mode on : proceeding without failing Packer"))
} else {
return fmt.Errorf("goss non-zero exit status")
}
} else {
ui.Say(fmt.Sprintf("Goss tests ran successfully"))
}
ui.Say(fmt.Sprintf("Goss tests ran successfully"))
return nil
}

Expand Down Expand Up @@ -341,6 +360,18 @@ func (p *Provisioner) vars() string {
return ""
}

func (p *Provisioner) inline_vars() string {
if len(p.config.VarsInline) != 0 {
inlineVarsJson, err := json.Marshal(p.config.VarsInline)
if err == nil {
return fmt.Sprintf("--vars-inline '%s'", string(inlineVarsJson))
} else {
fmt.Errorf("Error converting inline vars to json string %v", err)
}
}
return ""
}

func (p *Provisioner) sslFlag(cmdType string) string {
if p.config.SkipSSLChk {
switch cmdType {
Expand Down
40 changes: 22 additions & 18 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 1c68d9a

Please sign in to comment.