Skip to content

Commit

Permalink
(puppetlabsGH-1846) Wrap all powershell commands in a script
Browse files Browse the repository at this point in the history
Powershell will [exit 1](PowerShell/PowerShell#11461) whenever the
$LASTEXITCODE is greater than 0, regardless of what the code is,
whenever running a bare command. Powershell exits with the correct exit
code when executing files, or when `exit $LASTEXITCODE` is appended to
the command being run (as in the [execute_process
snippet](https://github.com/puppetlabs/bolt/blob/0d44ce739d4dafcb6d27abd894e230894fdd50e0/lib/bolt/shell/powershell/snippets.rb#L19),
meaning this is only an issue when running commands using the local
transport through Bolt. This appends a Powershell snippet to exit with
the correct exit code to commands running on Windows using the local
transport.

Closes puppetlabs#1846

!bug

* **Return correct exit code when running commands in powershell** ([1846](puppetlabs#1846))

  Bolt will now display the correct exit code when running commands in
  powershell that exit with code > 1.
  • Loading branch information
lucywyman committed Jun 10, 2020
1 parent 0d44ce7 commit c791df8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/bolt/shell/powershell/snippets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def execute_process(command)
PS
end

def exit_with_code(command)
<<~PS
#{command}
if (-not $? -and ($LASTEXITCODE -eq $null)) { exit 1 }
exit $LASTEXITCODE
PS
end

def make_tmpdir(parent)
<<~PS
$parent = #{parent}
Expand Down
3 changes: 2 additions & 1 deletion lib/bolt/transport/local/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def execute(command)
# If it's already a powershell command then invoke it normally.
# Otherwise, wrap it in powershell.exe.
unless command.start_with?('powershell.exe')
command = ['powershell.exe', *Bolt::Shell::Powershell::PS_ARGS, '-Command', command]
cmd = Bolt::Shell::Powershell::Snippets.exit_with_code(command)
command = ['powershell.exe', *Bolt::Shell::Powershell::PS_ARGS, '-Command', cmd]
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/integration/local_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
expect(result[0]['_error']).to be
end

it 'returns correct exit code', winrm: true do
cmd = 'puppet apply --trace --detailed-exitcodes -e "notify {foo:}"'
result = run_failed_nodes(%W[command run #{cmd} -t #{uri}]).first
expect(result['_error']['msg']).to eq('The command failed with exit code 2')
expect(result['_error']['details']['exit_code']).to eq(2)
end

it 'runs a ruby task using bolt ruby', :reset_puppet_settings do
result = run_one_node(%w[task run sample::bolt_ruby message=somemessage] + config_flags)
expect(result['env']).to match(/somemessage/)
Expand Down

0 comments on commit c791df8

Please sign in to comment.