post-processor/shell-local: Align flattened env vars gen with provisioners

* Move code to generate quoted flattened env vars to separate function
* Fix: generate flattened env vars once per provisioner not once per script
This commit is contained in:
DanHam 2017-01-23 22:15:51 +00:00
parent 5a350f5699
commit 9e480eea49
No known key found for this signature in database
GPG Key ID: 58E79AEDD6AA987E
1 changed files with 35 additions and 15 deletions

View File

@ -7,6 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"sort"
"strings" "strings"
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
@ -112,17 +113,11 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
} }
// Do a check for bad environment variables, such as '=foo', 'foobar' // Do a check for bad environment variables, such as '=foo', 'foobar'
for idx, kv := range p.config.Vars { for _, kv := range p.config.Vars {
vs := strings.SplitN(kv, "=", 2) vs := strings.SplitN(kv, "=", 2)
if len(vs) != 2 || vs[0] == "" { if len(vs) != 2 || vs[0] == "" {
errs = packer.MultiErrorAppend(errs, errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Environment variable not in format 'key=value': %s", kv)) fmt.Errorf("Environment variable not in format 'key=value': %s", kv))
} else {
// Replace single quotes so they parse
vs[1] = strings.Replace(vs[1], "'", `'"'"'`, -1)
// Single quote env var values
p.config.Vars[idx] = fmt.Sprintf("%s='%s'", vs[0], vs[1])
} }
} }
@ -166,18 +161,13 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
tf.Close() tf.Close()
} }
// Build our variables up by adding in the build name and builder type // Create environment variables to set before executing the command
envVars := make([]string, len(p.config.Vars)+2) flattenedEnvVars := p.createFlattenedEnvVars()
envVars[0] = fmt.Sprintf("PACKER_BUILD_NAME='%s'", p.config.PackerBuildName)
envVars[1] = fmt.Sprintf("PACKER_BUILDER_TYPE='%s'", p.config.PackerBuilderType)
copy(envVars[2:], p.config.Vars)
for _, script := range scripts { for _, script := range scripts {
// Flatten the environment variables
flattendVars := strings.Join(envVars, " ")
p.config.ctx.Data = &ExecuteCommandTemplate{ p.config.ctx.Data = &ExecuteCommandTemplate{
Vars: flattendVars, Vars: flattenedEnvVars,
Script: script, Script: script,
} }
@ -210,3 +200,33 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
return artifact, true, nil return artifact, true, nil
} }
func (p *PostProcessor) createFlattenedEnvVars() (flattened string) {
flattened = ""
envVars := make(map[string]string)
// Always available Packer provided env vars
envVars["PACKER_BUILD_NAME"] = fmt.Sprintf("%s", p.config.PackerBuildName)
envVars["PACKER_BUILDER_TYPE"] = fmt.Sprintf("%s", p.config.PackerBuilderType)
// Split vars into key/value components
for _, envVar := range p.config.Vars {
keyValue := strings.SplitN(envVar, "=", 2)
// Store pair, replacing any single quotes in value so they parse
// correctly with required environment variable format
envVars[keyValue[0]] = strings.Replace(keyValue[1], "'", `'"'"'`, -1)
}
// Create a list of env var keys in sorted order
var keys []string
for k := range envVars {
keys = append(keys, k)
}
sort.Strings(keys)
// Re-assemble vars surrounding value with single quotes and flatten
for _, key := range keys {
flattened += fmt.Sprintf("%s='%s' ", key, envVars[key])
}
return
}