From 10d959254ac2f99216cd9c5c55edfbcf73e57fc4 Mon Sep 17 00:00:00 2001 From: Tushar Aggarwal Date: Fri, 7 Aug 2020 08:29:53 -0700 Subject: [PATCH] Add support for passing environment variables to GOSS execution (#33) --- README.md | 4 ++++ packer-provisioner-goss.go | 23 +++++++++++++++++++---- packer-provisioner-goss.hcl2spec.go | 2 ++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index abe242e..cb9aea2 100644 --- a/README.md +++ b/README.md @@ -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`}}" diff --git a/packer-provisioner-goss.go b/packer-provisioner-goss.go index 01e14d5..53187f0 100644 --- a/packer-provisioner-goss.go +++ b/packer-provisioner-goss.go @@ -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"` @@ -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 { @@ -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)) @@ -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 { diff --git a/packer-provisioner-goss.hcl2spec.go b/packer-provisioner-goss.hcl2spec.go index 8ee147f..613ded1 100644 --- a/packer-provisioner-goss.hcl2spec.go +++ b/packer-provisioner-goss.hcl2spec.go @@ -24,6 +24,7 @@ type FlatGossConfig struct { GossFile *string `mapstructure:"goss_file" cty:"goss_file"` VarsFile *string `mapstructure:"vars_file" cty:"vars_file"` VarsInline map[string]string `mapstructure:"vars_inline" cty:"vars_inline"` + VarsEnv map[string]string `mapstructure:"vars_env" cty:"vars_env"` RemoteFolder *string `mapstructure:"remote_folder" cty:"remote_folder"` RemotePath *string `mapstructure:"remote_path" cty:"remote_path"` Format *string `mapstructure:"format" cty:"format"` @@ -58,6 +59,7 @@ func (*FlatGossConfig) HCL2Spec() map[string]hcldec.Spec { "goss_file": &hcldec.AttrSpec{Name: "goss_file", Type: cty.String, Required: false}, "vars_file": &hcldec.AttrSpec{Name: "vars_file", Type: cty.String, Required: false}, "vars_inline": &hcldec.AttrSpec{Name: "vars_inline", Type: cty.Map(cty.String), Required: false}, + "vars_env": &hcldec.AttrSpec{Name: "vars_env", Type: cty.Map(cty.String), Required: false}, "remote_folder": &hcldec.AttrSpec{Name: "remote_folder", Type: cty.String, Required: false}, "remote_path": &hcldec.AttrSpec{Name: "remote_path", Type: cty.String, Required: false}, "format": &hcldec.AttrSpec{Name: "format", Type: cty.String, Required: false},