Module: Kitchen::Driver::HypervHelpers

Included in:
Vagrant
Defined in:
lib/kitchen/driver/helpers.rb

Instance Method Summary collapse

Instance Method Details

#encode_command(script) ⇒ Object



25
26
27
28
# File 'lib/kitchen/driver/helpers.rb', line 25

def encode_command(script)
  encoded_script = script.encode("UTF-16LE", "UTF-8")
  Base64.strict_encode64(encoded_script)
end

#execute_command(cmd, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/kitchen/driver/helpers.rb', line 73

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?
  stdout = sanitize_stdout(sh.stdout)
  JSON.parse(stdout) if stdout.length > 2
end

#hyperv_default_switch_psObject



97
98
99
100
101
# File 'lib/kitchen/driver/helpers.rb', line 97

def hyperv_default_switch_ps
  <<-VMSWITCH
    Get-DefaultVMSwitch #{ENV['KITCHEN_HYPERV_SWITCH']} | ConvertTo-Json
  VMSWITCH
end

#hyperv_switchObject



87
88
89
90
91
92
93
94
95
# File 'lib/kitchen/driver/helpers.rb', line 87

def hyperv_switch
  default_switch_object = run_ps hyperv_default_switch_ps
  if default_switch_object.nil? ||
      !default_switch_object.key?("Name") ||
      default_switch_object["Name"].empty?
    raise "Failed to find a default VM Switch."
  end
  default_switch_object["Name"]
end

#is_32bit?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/kitchen/driver/helpers.rb', line 36

def is_32bit?
  os_arch = ENV["PROCESSOR_ARCHITEW6432"] || ENV["PROCESSOR_ARCHITECTURE"]
  ruby_arch = ["foo"].pack("p").size == 4 ? 32 : 64
  os_arch != "AMD64" && ruby_arch == 32
end

#is_64bit?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/kitchen/driver/helpers.rb', line 30

def is_64bit?
  os_arch = ENV["PROCESSOR_ARCHITEW6432"] || ENV["PROCESSOR_ARCHITECTURE"]
  ruby_arch = ["foo"].pack("p").size == 4 ? 32 : 64
  os_arch == "AMD64" && ruby_arch == 64
end

#powershell_64_bitObject



42
43
44
45
46
47
48
# File 'lib/kitchen/driver/helpers.rb', line 42

def powershell_64_bit
  if is_64bit? || is_32bit?
    'c:\windows\system32\windowspowershell\v1.0\powershell.exe'
  else
    'c:\windows\sysnative\windowspowershell\v1.0\powershell.exe'
  end
end

#run_ps(cmd, options = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convenience method to run a powershell command locally.

Parameters:

  • cmd (String)

    command to run locally

  • options (Hash) (defaults to: {})

    options hash

See Also:

  • ShellOut.run_command


65
66
67
68
69
70
71
# File 'lib/kitchen/driver/helpers.rb', line 65

def run_ps(cmd, options = {})
  cmd = "echo #{cmd}" if config[:dry_run]
  debug("Preparing to run: ")
  debug("  #{cmd}")
  wrapped_command = wrap_command cmd
  execute_command wrapped_command, options
end

#sanitize_stdout(stdout) ⇒ Object



83
84
85
# File 'lib/kitchen/driver/helpers.rb', line 83

def sanitize_stdout(stdout)
  stdout.split("\n").select { |s| !s.start_with?("PS") }.join("\n")
end

#wrap_command(script) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/kitchen/driver/helpers.rb', line 50

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"
end