Skip to content

Commit

Permalink
Add support for passing environment variables to GOSS execution (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
EleanorRigby authored Aug 7, 2020
1 parent 1c68d9a commit 10d9592
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ There is an example packer build with goss tests in the `example/` directory.
"format": "",
"goss_file": "",
"vars_file": "",
"vars_env": {
"ARCH": "amd64",
"PROVIDER": "{{user `cloud-provider`}}"
},
"vars_inline": {
"OS": "centos",
"version": "{{user `version`}}"
Expand Down
23 changes: 19 additions & 4 deletions packer-provisioner-goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type GossConfig struct {
// Optional inline variables that overrides JSON file vars
VarsInline map[string]string `mapstructure:"vars_inline"`

// Optional env variables
VarsEnv map[string]string `mapstructure:"vars_env"`

// 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 @@ -233,9 +236,13 @@ 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()))
ui.Message(fmt.Sprintf("Inline variables are %s", p.inline_vars()))
}

if len(p.config.VarsEnv) != 0 {
ui.Message(fmt.Sprintf("Env variables are %s", p.envVars()))
}

for _, src := range p.config.Tests {
Expand Down Expand Up @@ -300,8 +307,8 @@ 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,
strcmd := fmt.Sprintf("cd %s && %s %s %s %s %s %s validate --retry-timeout %s --sleep %s %s %s",
p.config.RemotePath, p.enableSudo(), p.envVars(), goss, p.config.GossFile,
p.vars(), p.inline_vars(), p.retryTimeout(), p.sleep(), p.format(), p.formatOptions())
ui.Message(fmt.Sprintf("Command : %s", strcmd))

Expand Down Expand Up @@ -372,6 +379,14 @@ func (p *Provisioner) inline_vars() string {
return ""
}

func (p *Provisioner) envVars() string {
var sb strings.Builder
for env_var, value := range p.config.VarsEnv {
sb.WriteString(fmt.Sprintf("%s=\"%s\" ", env_var, value))
}
return sb.String()
}

func (p *Provisioner) sslFlag(cmdType string) string {
if p.config.SkipSSLChk {
switch cmdType {
Expand Down
2 changes: 2 additions & 0 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 10d9592

Please sign in to comment.