summaryrefslogtreecommitdiff
path: root/lib/chef/provider/powershell_script.rb
diff options
context:
space:
mode:
authoradamedx <adamed@opscode.com>2013-06-25 05:43:13 -0700
committeradamedx <adamed@opscode.com>2013-06-25 06:05:11 -0700
commitf35150c6f099180f4273a240664ec35668d66011 (patch)
tree799f968a62ced148dc1d18b945176e886b867d4c /lib/chef/provider/powershell_script.rb
parent5324bdd06ebaef013776950fb6ad97b78a8e9b1c (diff)
downloadchef-f35150c6f099180f4273a240664ec35668d66011.tar.gz
Add functional tests for exit code and flags, fix architecture test verification
Diffstat (limited to 'lib/chef/provider/powershell_script.rb')
-rw-r--r--lib/chef/provider/powershell_script.rb46
1 files changed, 44 insertions, 2 deletions
diff --git a/lib/chef/provider/powershell_script.rb b/lib/chef/provider/powershell_script.rb
index d158ebc4ef..98433203b9 100644
--- a/lib/chef/provider/powershell_script.rb
+++ b/lib/chef/provider/powershell_script.rb
@@ -22,14 +22,56 @@ class Chef
class Provider
class PowershellScript < Chef::Provider::WindowsScript
+ protected
+
+ EXIT_STATUS_NORMALIZATION_SCRIPT = "\nif ($? -eq $true) {exit 0} elseif ( $LASTEXITCODE -ne 0) {exit $LASTEXITCODE} else { exit 1 }"
+ EXIT_STATUS_RESET_SCRIPT = "$LASTEXITCODE=0\n"
+
+ # Process exit codes are strange with PowerShell. Unless you
+ # explicitly call exit in Powershell, the powershell.exe
+ # interpreter returns only 0 for success or 1 for failure. Since
+ # we'd like to get specific exit codes from executable tools run
+ # with Powershell, we do some work using the automatic variables
+ # $? and $LASTEXITCODE to return the process exit code of the
+ # last process run in the script if it is the last command
+ # executed, otherwise 0 or 1 based on whether $? is set to true
+ # (success, where we return 0) or false (where we return 1).
+ def NormalizeScriptExitStatus( code )
+ @code = EXIT_STATUS_RESET_SCRIPT + code + EXIT_STATUS_NORMALIZATION_SCRIPT
+ end
+
+ public
+
def initialize (new_resource, run_context)
super(new_resource, run_context, '.ps1')
+ NormalizeScriptExitStatus(new_resource.code)
end
def flags
- @new_resource.flags.nil? ? '-ExecutionPolicy RemoteSigned -Command' : @new_resource.flags + '-ExecutionPolicy RemoteSigned -Command'
+ default_flags = [
+ "-NoLogo",
+ "-NonInteractive",
+ "-NoProfile",
+ "-ExecutionPolicy RemoteSigned",
+ # Powershell will hang if STDIN is redirected
+ # http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected
+ "-InputFormat None",
+ # Must use -File rather than -Command to launch the script
+ # file created by the base class that contains the script
+ # code -- otherwise, powershell.exe does not propagate the
+ # error status of a failed Windows process that ran at the
+ # end of the script, it gets changed to '1'.
+ "-File"
+ ]
+
+ interpreter_flags = default_flags.join(' ')
+
+ if ! (@new_resource.flags.nil?)
+ interpreter_flags = [@new_resource.flags, interpreter_flags].join(' ')
+ end
+
+ interpreter_flags
end
-
end
end
end