Skip to content

Commit

Permalink
Make hiding of progress backward compatible with old versions of powe…
Browse files Browse the repository at this point in the history
…rshell
  • Loading branch information
taliesins committed Dec 12, 2016
1 parent 7c754bf commit 5d5809b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion powershell/powershell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestOutput(t *testing.T) {

func TestRunFile(t *testing.T) {
var blockBuffer bytes.Buffer
blockBuffer.WriteString(`param([string]$a, [string]$b, [int]$x, [int]$y) $ProgressPreference='SilentlyContinue'; $n = $x + $y; Write-Output "$a $b $n";`)
blockBuffer.WriteString(`param([string]$a, [string]$b, [int]$x, [int]$y) if (Test-Path variable:global:ProgressPreference){$ProgressPreference="SilentlyContinue"}; $n = $x + $y; Write-Output "$a $b $n";`)

var ps PowerShellCmd
cmdOut, err := ps.Output(blockBuffer.String(), "a", "b", "5", "10")
Expand Down
2 changes: 1 addition & 1 deletion provisioner/powershell/elevated.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ $t.XmlText = @'
</Actions>
</Task>
'@
$ProgressPreference="SilentlyContinue";
if (Test-Path variable:global:ProgressPreference){$ProgressPreference="SilentlyContinue"}
$f = $s.GetFolder("\")
$f.RegisterTaskDefinition($name, $t, 6, "{{.User}}", "{{.Password}}", 1, $null) | Out-Null
$t = $f.GetTask("\$name")
Expand Down
6 changes: 3 additions & 3 deletions provisioner/powershell/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}

if p.config.ExecuteCommand == "" {
p.config.ExecuteCommand = `powershell '& {$ProgressPreference=\"SilentlyContinue\"; {{.Vars}}{{.Path}}; exit $LastExitCode}'`
p.config.ExecuteCommand = `powershell '& {if (Test-Path variable:global:ProgressPreference){$ProgressPreference=\"SilentlyContinue\"}; {{.Vars}}{{.Path}}; exit $LastExitCode}'`
}

if p.config.ElevatedExecuteCommand == "" {
p.config.ElevatedExecuteCommand = `powershell '& {$ProgressPreference=\"SilentlyContinue\"; {{.Vars}}{{.Path}}; exit $LastExitCode}'`
p.config.ElevatedExecuteCommand = `powershell '& {if (Test-Path variable:global:ProgressPreference){$ProgressPreference=\"SilentlyContinue\"}; {{.Vars}}{{.Path}}; exit $LastExitCode}'`
}

if p.config.Inline != nil && len(p.config.Inline) == 0 {
Expand Down Expand Up @@ -425,7 +425,7 @@ func (p *Provisioner) generateElevatedRunner(command string) (uploadedPath strin
Password: p.config.ElevatedPassword,
TaskDescription: "Packer elevated task",
TaskName: fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID()),
EncodedCommand: powershellEncode([]byte("$ProgressPreference=\"SilentlyContinue\"; " + command + "; exit $LASTEXITCODE")),
EncodedCommand: powershellEncode([]byte("if (Test-Path variable:global:ProgressPreference){$ProgressPreference=\"SilentlyContinue\"}; " + command + "; exit $LASTEXITCODE")),
})

if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions provisioner/powershell/provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func TestProvisionerPrepare_Defaults(t *testing.T) {
t.Error("expected elevated_password to be empty")
}

if p.config.ExecuteCommand != `powershell '& {$ProgressPreference=\"SilentlyContinue\"; {{.Vars}}{{.Path}}; exit $LastExitCode}'` {
t.Fatalf("Default command should be powershell '& {$ProgressPreference=\"SilentlyContinue\"; {{.Vars}}{{.Path}}; exit $LastExitCode}', but got %s", p.config.ExecuteCommand)
if p.config.ExecuteCommand != `powershell '& {if (Test-Path variable:global:ProgressPreference){$ProgressPreference=\"SilentlyContinue\"}; {{.Vars}}{{.Path}}; exit $LastExitCode}'` {
t.Fatalf("Default command should be powershell '& {if (Test-Path variable:global:ProgressPreference){$ProgressPreference=\"SilentlyContinue\"}; {{.Vars}}{{.Path}}; exit $LastExitCode}', but got %s", p.config.ExecuteCommand)
}

if p.config.ElevatedExecuteCommand != `powershell '& {$ProgressPreference=\"SilentlyContinue\"; {{.Vars}}{{.Path}}; exit $LastExitCode}'` {
t.Fatalf("Default command should be powershell powershell '& {$ProgressPreference=\"SilentlyContinue\"; {{.Vars}}{{.Path}}; exit $LastExitCode}', but got %s", p.config.ElevatedExecuteCommand)
if p.config.ElevatedExecuteCommand != `powershell '& {if (Test-Path variable:global:ProgressPreference){$ProgressPreference=\"SilentlyContinue\"}; {{.Vars}}{{.Path}}; exit $LastExitCode}'` {
t.Fatalf("Default command should be powershell powershell '& {if (Test-Path variable:global:ProgressPreference){$ProgressPreference=\"SilentlyContinue\"}; {{.Vars}}{{.Path}}; exit $LastExitCode}', but got %s", p.config.ElevatedExecuteCommand)
}

if p.config.ValidExitCodes == nil {
Expand Down Expand Up @@ -389,7 +389,7 @@ func TestProvisionerProvision_Inline(t *testing.T) {
t.Fatal("should not have error")
}

expectedCommand := `powershell '& {$ProgressPreference=\"SilentlyContinue\"; $env:PACKER_BUILDER_TYPE=\"iso\"; $env:PACKER_BUILD_NAME=\"vmware\"; c:/Windows/Temp/inlineScript.bat; exit $LastExitCode}'`
expectedCommand := `powershell '& {if (Test-Path variable:global:ProgressPreference){$ProgressPreference=\"SilentlyContinue\"}; $env:PACKER_BUILDER_TYPE=\"iso\"; $env:PACKER_BUILD_NAME=\"vmware\"; c:/Windows/Temp/inlineScript.bat; exit $LastExitCode}'`

// Should run the command without alteration
if comm.StartCmd.Command != expectedCommand {
Expand All @@ -408,7 +408,7 @@ func TestProvisionerProvision_Inline(t *testing.T) {
t.Fatal("should not have error")
}

expectedCommand = `powershell '& {$ProgressPreference=\"SilentlyContinue\"; $env:BAR=\"BAZ\"; $env:FOO=\"BAR\"; $env:PACKER_BUILDER_TYPE=\"iso\"; $env:PACKER_BUILD_NAME=\"vmware\"; c:/Windows/Temp/inlineScript.bat; exit $LastExitCode}'`
expectedCommand = `powershell '& {if (Test-Path variable:global:ProgressPreference){$ProgressPreference=\"SilentlyContinue\"}; $env:BAR=\"BAZ\"; $env:FOO=\"BAR\"; $env:PACKER_BUILDER_TYPE=\"iso\"; $env:PACKER_BUILD_NAME=\"vmware\"; c:/Windows/Temp/inlineScript.bat; exit $LastExitCode}'`

// Should run the command without alteration
if comm.StartCmd.Command != expectedCommand {
Expand All @@ -435,7 +435,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) {
}

//powershell -Command "$env:PACKER_BUILDER_TYPE=''"; powershell -Command "$env:PACKER_BUILD_NAME='foobuild'"; powershell -Command c:/Windows/Temp/script.ps1
expectedCommand := `powershell '& {$ProgressPreference=\"SilentlyContinue\"; $env:PACKER_BUILDER_TYPE=\"footype\"; $env:PACKER_BUILD_NAME=\"foobuild\"; c:/Windows/Temp/script.ps1; exit $LastExitCode}'`
expectedCommand := `powershell '& {if (Test-Path variable:global:ProgressPreference){$ProgressPreference=\"SilentlyContinue\"}; $env:PACKER_BUILDER_TYPE=\"footype\"; $env:PACKER_BUILD_NAME=\"foobuild\"; c:/Windows/Temp/script.ps1; exit $LastExitCode}'`

// Should run the command without alteration
if comm.StartCmd.Command != expectedCommand {
Expand Down Expand Up @@ -468,7 +468,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
t.Fatal("should not have error")
}

expectedCommand := `powershell '& {$ProgressPreference=\"SilentlyContinue\"; $env:BAR=\"BAZ\"; $env:FOO=\"BAR\"; $env:PACKER_BUILDER_TYPE=\"footype\"; $env:PACKER_BUILD_NAME=\"foobuild\"; c:/Windows/Temp/script.ps1; exit $LastExitCode}'`
expectedCommand := `powershell '& {if (Test-Path variable:global:ProgressPreference){$ProgressPreference=\"SilentlyContinue\"}; $env:BAR=\"BAZ\"; $env:FOO=\"BAR\"; $env:PACKER_BUILDER_TYPE=\"footype\"; $env:PACKER_BUILD_NAME=\"foobuild\"; c:/Windows/Temp/script.ps1; exit $LastExitCode}'`

// Should run the command without alteration
if comm.StartCmd.Command != expectedCommand {
Expand Down Expand Up @@ -582,7 +582,7 @@ func TestProvision_createCommandText(t *testing.T) {

// Non-elevated
cmd, _ := p.createCommandText()
if cmd != `powershell '& {$ProgressPreference=\"SilentlyContinue\"; $env:PACKER_BUILDER_TYPE=\"\"; $env:PACKER_BUILD_NAME=\"\"; c:/Windows/Temp/script.ps1; exit $LastExitCode}'` {
if cmd != `powershell '& {if (Test-Path variable:global:ProgressPreference){$ProgressPreference=\"SilentlyContinue\"}; $env:PACKER_BUILDER_TYPE=\"\"; $env:PACKER_BUILD_NAME=\"\"; c:/Windows/Temp/script.ps1; exit $LastExitCode}'` {
t.Fatalf("Got unexpected non-elevated command: %s", cmd)
}

Expand Down

0 comments on commit 5d5809b

Please sign in to comment.