diff options
author | Adam Edwards <adamed@opscode.com> | 2015-03-15 16:58:24 -0700 |
---|---|---|
committer | Kartik Null Cating-Subramanian <ksubramanian@chef.io> | 2015-05-29 16:58:55 -0400 |
commit | 48984933cca0d9d31cd667478f050f45029b08d5 (patch) | |
tree | f7820b0b40b5152c2d41f93d5b3b6115710078c9 | |
parent | 56eadf31275fa21f934d20a218cf63c0270d34be (diff) | |
download | chef-48984933cca0d9d31cd667478f050f45029b08d5.tar.gz |
powershell_script provider readability fixes
-rw-r--r-- | lib/chef/provider/powershell_script.rb | 84 |
1 files changed, 46 insertions, 38 deletions
diff --git a/lib/chef/provider/powershell_script.rb b/lib/chef/provider/powershell_script.rb index de06ddbe25..3b86e2a5de 100644 --- a/lib/chef/provider/powershell_script.rb +++ b/lib/chef/provider/powershell_script.rb @@ -24,8 +24,6 @@ class Chef provides :powershell_script, os: "windows" - public - def initialize (new_resource, run_context) super(new_resource, run_context, '.ps1') normalize_script_exit_status @@ -53,7 +51,50 @@ class Chef protected - # Process exit codes are strange with PowerShell. Unless you + # Process exit codes are strange with PowerShell and require + # special handling to cover common use cases. + def normalize_script_exit_status + self.code = wrapper_script + Chef::Log.debug("powershell_script provider called with script code:\n\n#{@new_resource.code}\n") + Chef::Log.debug("powershell_script provider will execute transformed code:\n\n#{self.code}\n") + end + + def validate_script_syntax! + interpreter_arguments = default_interpreter_flags.join(' ') + Tempfile.open(['chef_powershell_script-user-code', '.ps1']) do | user_script_file | + user_script_file.puts("{#{@new_resource.code}}") + user_script_file.close + + validation_command = "\"#{interpreter}\" #{interpreter_arguments} -Command #{user_script_file.path}" + + # For consistency with other script resources, allow even syntax errors + # to be suppressed if the returns attribute would have suppressed it + # at converge. + valid_returns = [0] + specified_returns = @new_resource.returns.is_a?(Integer) ? + [@new_resource.returns] : + @new_resource.returns + valid_returns.concat([1]) if specified_returns.include?(1) + + result = shell_out!(validation_command, {returns: valid_returns}) + result.exitstatus == 0 + end + end + + def default_interpreter_flags + [ + "-NoLogo", + "-NonInteractive", + "-NoProfile", + "-ExecutionPolicy Unrestricted", + # 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" + ] + end + + # A wrapper script is used to launch user-supplied script while + # still obtaining useful process exit codes. 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 @@ -62,8 +103,8 @@ class Chef # 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 normalize_script_exit_status - self.code = <<-EOH + def wrapper_script +<<-EOH # Chef Client wrapper for powershell_script resources # LASTEXITCODE can be uninitialized -- make it explictly 0 @@ -122,39 +163,6 @@ elseif ( $LASTEXITCODE -ne $null -and $LASTEXITCODE -ne 0 ) # 1 (i.e. failed) otherwise. exit $exitstatus EOH - Chef::Log.debug("powershell_script provider called with script code:\n\n#{@new_resource.code}\n") - Chef::Log.debug("powershell_script provider will execute transformed code:\n\n#{self.code}\n") - end - - def validate_script_syntax! - interpreter_arguments = default_interpreter_flags.join(' ') - Tempfile.open(['chef_powershell_script-user-code', '.ps1']) do | user_script_file | - user_script_file.puts("{#{@new_resource.code}}") - user_script_file.close - - validation_command = "\"#{interpreter}\" #{interpreter_arguments} -Command #{user_script_file.path}" - - valid_returns = [0] - specified_returns = @new_resource.returns.is_a?(Integer) ? - [@new_resource.returns] : - @new_resource.returns - valid_returns.concat([1]) if specified_returns.include?(1) - - result = shell_out!(validation_command, {returns: valid_returns}) - result.exitstatus == 0 - end - end - - def default_interpreter_flags - [ - "-NoLogo", - "-NonInteractive", - "-NoProfile", - "-ExecutionPolicy Unrestricted", - # 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" - ] end end |