Move env vars into a file that we dot-source instead of trying to write them all to the command line

This commit is contained in:
Megan Marsh 2017-09-14 16:39:35 -07:00
parent f86d45eaf6
commit 92e70757bb
1 changed files with 12 additions and 2 deletions

View File

@ -124,7 +124,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
} }
if p.config.ElevatedExecuteCommand == "" { if p.config.ElevatedExecuteCommand == "" {
p.config.ElevatedExecuteCommand = `if (Test-Path variable:global:ProgressPreference){$ProgressPreference='SilentlyContinue'};{{.Vars}}&'{{.Path}}';exit $LastExitCode` p.config.ElevatedExecuteCommand = `if (Test-Path variable:global:ProgressPreference){$ProgressPreference='SilentlyContinue'}; . {{.Vars}}; &'{{.Path}}'; exit $LastExitCode`
} }
if p.config.Inline != nil && len(p.config.Inline) == 0 { if p.config.Inline != nil && len(p.config.Inline) == 0 {
@ -413,10 +413,20 @@ func (p *Provisioner) generateCommandLineRunner(command string) (commandText str
func (p *Provisioner) createCommandTextPrivileged() (command string, err error) { func (p *Provisioner) createCommandTextPrivileged() (command string, err error) {
// Can't double escape the env vars, lets create shiny new ones // Can't double escape the env vars, lets create shiny new ones
flattenedEnvVars := p.createFlattenedEnvVars(true) flattenedEnvVars := p.createFlattenedEnvVars(true)
// Need to create a mini ps1 script containing all of the environment variables we want;
// we'll be dot-sourcing this later
envVarReader := strings.NewReader(flattenedEnvVars)
uuid := uuid.TimeOrderedUUID()
envVarPath := fmt.Sprintf(`${env:TEMP}\packer-env-vars-%s.ps1`, uuid)
log.Printf("Uploading env vars to %s", envVarPath)
err = p.communicator.Upload(envVarPath, envVarReader, nil)
if err != nil {
return "", fmt.Errorf("Error preparing elevated powershell script: %s", err)
}
p.config.ctx.Data = &ExecuteCommandTemplate{ p.config.ctx.Data = &ExecuteCommandTemplate{
Vars: flattenedEnvVars,
Path: p.config.RemotePath, Path: p.config.RemotePath,
Vars: envVarPath,
} }
command, err = interpolate.Render(p.config.ElevatedExecuteCommand, &p.config.ctx) command, err = interpolate.Render(p.config.ElevatedExecuteCommand, &p.config.ctx)
if err != nil { if err != nil {