Module: Kitchen::Driver::VMMUtils

Included in:
Vmm
Defined in:
lib/kitchen/driver/vmm_utils.rb

Defined Under Namespace

Classes: StreamReader

Constant Summary collapse

ERROR_REGEXP =
/===Begin-Error===(.+?)===End-Error===/m
OUTPUT_REGEXP =
/===Begin-Output===(.+?)===End-Output===/m

Instance Method Summary collapse

Instance Method Details

#execute(path, options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kitchen/driver/vmm_utils.rb', line 8

def execute(path, options)
  r = execute_powershell(path, options)
  if r.error?
    raise ("Powershell failed, #{path}:#{r.stderr}")
  end

  # We only want unix-style line endings within Vagrant
  r.stdout.gsub!("\r\n", "\n")
  r.stderr.gsub!("\r\n", "\n")

  error_match  = ERROR_REGEXP.match(r.stdout)
  output_match = OUTPUT_REGEXP.match(r.stdout)

  if error_match
    data = JSON.parse(error_match[1])

    # We have some error data.
    raise "#{path}:#{data["error"]}"
  end

  # Nothing
  return nil if !output_match
  return JSON.parse(output_match[1])
end

#execute_powershell(path, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kitchen/driver/vmm_utils.rb', line 33

def execute_powershell(path, options = {})
  lib_path = Pathname.new(File.expand_path("../../../../support", __FILE__))
  script_path = lib_path.join(path).to_s.gsub("/", "\\")
  ps_options = ''
  options.each do |key, value|
    unless value.nil?
      ps_options += " -#{key} \"#{value}\""
    end
  end
  #
  stdout_stream_reader = StreamReader.new do |line|
    info(line)
  end
  ps_run = Mixlib::ShellOut.new("powershell.exe -File #{script_path} #{ps_options} -ErrorAction Stop")
  debug("Command: #{ps_run.command}")
  ps_run.live_stdout = stdout_stream_reader
  ps_run.run_command
  return ps_run
end