2014-02-16 00:28:11 +00:00
|
|
|
require "json"
|
|
|
|
|
|
|
|
require "vagrant/util/powershell"
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2014-02-16 00:35:04 +00:00
|
|
|
require_relative "plugin"
|
|
|
|
|
2014-02-15 23:29:16 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module HyperV
|
2014-02-16 00:28:11 +00:00
|
|
|
class Driver
|
2014-02-16 00:51:25 +00:00
|
|
|
ERROR_REGEXP = /===Begin-Error===(.+?)===End-Error===/m
|
|
|
|
OUTPUT_REGEXP = /===Begin-Output===(.+?)===End-Output===/m
|
|
|
|
|
2014-02-16 00:28:11 +00:00
|
|
|
attr_reader :vmid
|
|
|
|
|
|
|
|
def initialize(id=nil)
|
|
|
|
@vmid = id
|
|
|
|
@output = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(path, options)
|
2014-02-16 00:51:25 +00:00
|
|
|
r = execute_powershell(path, options)
|
2014-02-16 00:35:04 +00:00
|
|
|
if r.exit_code != 0
|
|
|
|
raise Errors::PowerShellError,
|
|
|
|
script: path,
|
|
|
|
stderr: r.stderr
|
|
|
|
end
|
|
|
|
|
2014-02-16 00:51:25 +00:00
|
|
|
# We only want unix-style line endings within Vagrant
|
|
|
|
r.stdout.gsub!("\r\n", "\n")
|
|
|
|
r.stderr.gsub!("\r\n", "\n")
|
2014-02-16 00:28:11 +00:00
|
|
|
|
2014-02-16 00:51:25 +00:00
|
|
|
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 Errors::PowerShellError,
|
|
|
|
script: path,
|
|
|
|
stderr: data["error"]
|
2014-02-16 00:28:11 +00:00
|
|
|
end
|
2014-02-16 00:51:25 +00:00
|
|
|
|
|
|
|
# Nothing
|
|
|
|
return nil if !output_match
|
|
|
|
return JSON.parse(output_match[1])
|
2014-02-16 00:28:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def json_output
|
|
|
|
return @json_output if @json_output
|
|
|
|
json_success_begin = false
|
|
|
|
json_error_begin = false
|
|
|
|
success = []
|
|
|
|
error = []
|
|
|
|
@output.split("\n").each do |line|
|
|
|
|
json_error_begin = false if line.include?("===End-Error===")
|
|
|
|
json_success_begin = false if line.include?("===End-Output===")
|
|
|
|
message = ""
|
|
|
|
if json_error_begin || json_success_begin
|
|
|
|
message = line.gsub("\\'","\"")
|
|
|
|
end
|
|
|
|
success << message if json_success_begin
|
|
|
|
error << message if json_error_begin
|
|
|
|
json_success_begin = true if line.include?("===Begin-Output===")
|
|
|
|
json_error_begin = true if line.include?("===Begin-Error===")
|
|
|
|
end
|
|
|
|
@json_output = { :success => success, :error => error }
|
|
|
|
end
|
|
|
|
|
|
|
|
def success?
|
|
|
|
@error_messages.empty? && json_output[:error].empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_output(type, data)
|
|
|
|
if type == :stdout
|
|
|
|
@output = data.gsub("\r\n", "\n")
|
|
|
|
end
|
|
|
|
if type == :stdin
|
|
|
|
# $stdin.gets.chomp || ""
|
|
|
|
end
|
|
|
|
if type == :stderr
|
|
|
|
@error_messages = data.gsub("\r\n", "\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_output_buffer
|
|
|
|
@output = ""
|
|
|
|
@error_messages = ""
|
|
|
|
@json_output = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_powershell(path, options, &block)
|
2014-02-16 00:51:25 +00:00
|
|
|
lib_path = Pathname.new(File.expand_path("../scripts", __FILE__))
|
2014-02-16 00:28:11 +00:00
|
|
|
path = lib_path.join(path).to_s.gsub("/", "\\")
|
|
|
|
options = options || {}
|
|
|
|
ps_options = []
|
|
|
|
options.each do |key, value|
|
|
|
|
ps_options << "-#{key}"
|
|
|
|
ps_options << "'#{value}'"
|
|
|
|
end
|
|
|
|
clear_output_buffer
|
|
|
|
opts = { notify: [:stdout, :stderr, :stdin] }
|
|
|
|
Vagrant::Util::PowerShell.execute(path, *ps_options, **opts, &block)
|
|
|
|
end
|
2014-02-15 23:29:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|