chef-powershell 18.0.2 → 18.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82b01268630b72a3f8fc851b1707238c9f90a6c9ce9edc98c30e8a8a94b47b91
4
- data.tar.gz: 4cb1814a6dc51d7307a1754bc952fb11b1173b5acc2a3c9dc08bc27bbc9d94e8
3
+ metadata.gz: f3943b91db500700d5fa545d1ac204c220f39f105ddffcfb3a9c1c7fa7d4a194
4
+ data.tar.gz: ee94470b0f02a82da5bb63aadefd4411b79398f6591f622d202264cf34a1f7ea
5
5
  SHA512:
6
- metadata.gz: f53a300229d6c94603b3ee47731e8868b457c5573f678920013754b56c49ff6d49fbf85c32139dab00a012a368c40f11a9a3784e01827a442aacdf0812f5b975
7
- data.tar.gz: b0e283175d51673660b9e10b371faa91a64352055ca499534453b1a0c618df746dc7032079e6bc4abf01a3df269b635e063acab667d4ac7d9d19b2d10bd06759
6
+ metadata.gz: dd20e5f7adb2d459a8f0006a919d220a1a73cb99d75d3c651166053378ffafc1bd2859ac1bd1dcdaba8074e3b84f648d4c786ea1f2789303f05562f78c7a45ab
7
+ data.tar.gz: 9c2419d28ea984490a4263588476a58726d94e1cf6c6d9da985ad649d2711a2a88484a78dd46e1170adf455b4eb4c747b664c6c1be3a0b302cac429e24a9fe8f
@@ -64,6 +64,10 @@ class ChefPowerShell
64
64
  end
65
65
 
66
66
  module PowerMod
67
+ class << self
68
+ attr_accessor :result_string, :retry_count, :exception, :result, :errors, :verbose, :hashed_outcome
69
+ end
70
+
67
71
  extend FFI::Library
68
72
  # FFI requires attaching to modules, not classes, so we need to
69
73
  # have a module here. The module level variables *could* be refactored
@@ -72,17 +76,29 @@ class ChefPowerShell
72
76
  @@ps_command = ""
73
77
  @@ps_timeout = -1
74
78
 
75
- AllocateCallback = FFI::Function.new(:pointer, [:size_t]) do |size|
76
- # Capture the pointer here so that Ruby knows that it is an
77
- # FFI::MemoryPointer that can receive &.free. If you try to
78
- # free the pointer from execute_powershell, Ruby will not have
79
- # access to the type information and will core dump spectacularly.
80
- @@pointer = FFI::MemoryPointer.new(:uchar, size)
81
- end
82
-
83
- def self.free_pointer
84
- @@pointer&.free
85
- @@pointer = nil
79
+ StoreResultCallback = FFI::Function.new(:bool, %i{pointer size_t}) do |data, size|
80
+ # try parsing the result *first* before returning from the function, and if it fails,
81
+ # return false so that the function can be retried from the C++ side.
82
+ @result_string = data.get_bytes(0, size).force_encoding("UTF-16LE").encode("UTF-8")
83
+ @hashed_outcome = FFI_Yajl::Parser.parse(@result_string)
84
+ @result = FFI_Yajl::Parser.parse(@hashed_outcome["result"])
85
+ @errors = @hashed_outcome["errors"]
86
+ @verbose = @hashed_outcome["verbose"]
87
+ true
88
+ rescue NoMethodError, FFI_Yajl::ParseError => e
89
+ @retry_count += 1
90
+ # capture exception so that it can be raised later, since otherwise
91
+ # we will be raising back to C++.
92
+ @exception = e
93
+ return true if @retry_count > 3
94
+
95
+ puts "Retrying PowerShell command execution #{@retry_count}"
96
+ sleep 1
97
+ false
98
+ rescue => e
99
+ # no retry for other exceptions
100
+ @exception = e
101
+ true
86
102
  end
87
103
 
88
104
  def self.set_ps_dll(value)
@@ -98,10 +114,12 @@ class ChefPowerShell
98
114
  end
99
115
 
100
116
  def self.do_work
117
+ @exception = nil
118
+ @retry_count = 0
101
119
  ffi_lib @@powershell_dll
102
120
  attach_function :execute_powershell, :ExecuteScript, %i{string int pointer}, :pointer
103
121
 
104
- execute_powershell(@@ps_command, @@ps_timeout, AllocateCallback)
122
+ execute_powershell(@@ps_command, @@ps_timeout, StoreResultCallback)
105
123
  end
106
124
  end
107
125
 
@@ -117,13 +135,13 @@ class ChefPowerShell
117
135
 
118
136
  PowerMod.set_ps_command(script)
119
137
  execution = PowerMod.do_work
120
- output = execution.read_utf16string
121
- hashed_outcome = FFI_Yajl::Parser.parse(output)
122
- @result = FFI_Yajl::Parser.parse(hashed_outcome["result"])
123
- @errors = hashed_outcome["errors"]
124
- @verbose = hashed_outcome["verbose"]
125
- ensure
126
- PowerMod.free_pointer
138
+ # we returned "true" to escape retry, but we still need to check the
139
+ # exception and raise it if it exists.
140
+ raise PowerMod.exception if PowerMod.exception
141
+
142
+ @result = PowerMod.result
143
+ @errors = PowerMod.errors
144
+ @verbose = PowerMod.verbose
127
145
  end
128
146
  end
129
147
  end
@@ -16,5 +16,5 @@
16
16
 
17
17
  module ChefPowerShellModule
18
18
  CHEFPOWERSHELL_ROOT = File.expand_path("..", __dir__)
19
- VERSION = "18.0.2".freeze
19
+ VERSION = "18.1.0"
20
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-powershell
3
3
  version: !ruby/object:Gem::Version
4
- version: 18.0.2
4
+ version: 18.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chef Software, Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-19 00:00:00.000000000 Z
11
+ date: 2023-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi