lib/kitchen/driver/powershell.rb in kitchen-hyperv-0.6.0 vs lib/kitchen/driver/powershell.rb in kitchen-hyperv-0.7.0

- old
+ new

@@ -14,10 +14,11 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require "mixlib/shellout" unless defined?(Mixlib::ShellOut) +require "benchmark" unless defined?(Benchmark) require "fileutils" unless defined?(FileUtils) require "json" unless defined?(JSON) module Kitchen module Driver @@ -26,10 +27,12 @@ encoded_script = script.encode("UTF-16LE", "UTF-8") Base64.strict_encode64(encoded_script) end def is_64bit? + return true if remote_hyperv + os_arch = ENV["PROCESSOR_ARCHITEW6432"] || ENV["PROCESSOR_ARCHITECTURE"] ruby_arch = ["foo"].pack("p").size == 4 ? 32 : 64 os_arch == "AMD64" && ruby_arch == 64 end @@ -46,11 +49,10 @@ 'c:\windows\sysnative\windowspowershell\v1.0\powershell.exe' end end def wrap_command(script) - base_script_path = File.join(File.dirname(__FILE__), "/../../../support/hyperv.ps1") debug("Loading functions from #{base_script_path}") new_script = [ ". #{base_script_path}", "#{script}" ].join(";\n") debug("Wrapped script: #{new_script}") "#{powershell_64_bit} -noprofile -executionpolicy bypass" \ " -encodedcommand #{encode_command new_script} -outputformat Text" @@ -69,16 +71,20 @@ wrapped_command = wrap_command cmd execute_command wrapped_command, options end def execute_command(cmd, options = {}) - debug("#Local Command BEGIN (#{cmd})") - sh = Mixlib::ShellOut.new(cmd, options) - sh.run_command - debug("Local Command END #{Util.duration(sh.execution_time)}") - raise "Failed: #{sh.stderr}" if sh.error? + debug("#Command BEGIN (#{cmd})") + sh = nil + bm = Benchmark.measure do + sh = connection.run_command(cmd, options) + end + + debug("Command END #{Util.duration(bm.total)}") + raise "Failed: #{sh.stderr}" if sh.exit_status != 0 + stdout = sanitize_stdout(sh.stdout) JSON.parse(stdout) if stdout.length > 2 end def sanitize_stdout(stdout) @@ -111,11 +117,11 @@ $NewVMParams = @{ Generation = #{config[:vm_generation]} DisableSecureBoot = "#{config[:disable_secureboot]}" MemoryStartupBytes = #{config[:memory_startup_bytes]} - StaticMacAddress = "#{config[:static_mac_address]}" + StaticMacAddress = "#{config[:static_mac_address].to_s}" Name = "#{instance.name}" Path = "#{kitchen_vm_path}" VHDPath = "#{differencing_disk_path}" SwitchName = "#{config[:vm_switch]}" VlanId = #{config[:vm_vlan_id] || "$null"} @@ -137,9 +143,10 @@ <<-EOH AdditionalDisks = @("#{@additional_disk_objects.join('","')}") EOH end + # TODO: Report if VM has no IP address instead of silently waiting forever def vm_details_ps <<-DETAILS Get-VmDetail -id "#{@state[:id]}" | ConvertTo-Json DETAILS