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-03-06 16:51:07 +00:00
|
|
|
|
2014-03-06 16:58:31 +00:00
|
|
|
attr_reader :vm_id
|
|
|
|
|
|
|
|
def initialize(id)
|
|
|
|
@vm_id = id
|
2014-03-06 16:51:07 +00:00
|
|
|
end
|
2014-02-16 00:51:25 +00:00
|
|
|
|
2014-02-16 00:28:11 +00:00
|
|
|
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
|
|
|
|
|
2014-03-06 16:51:07 +00:00
|
|
|
def get_current_state
|
|
|
|
execute('get_vm_status.ps1', { VmId: vm_id })
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_vm
|
|
|
|
execute('delete_vm.ps1', { VmId: vm_id })
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_guest_ip
|
|
|
|
execute('get_network_config.ps1', { VmId: vm_id })
|
|
|
|
end
|
|
|
|
|
|
|
|
def resume
|
|
|
|
execute('resume_vm.ps1', { VmId: vm_id })
|
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
execute('start_vm.ps1', { VmId: vm_id })
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
execute('stop_vm.ps1', { VmId: vm_id })
|
|
|
|
end
|
|
|
|
|
|
|
|
def suspend
|
|
|
|
execute("suspend_vm.ps1", { VmId: vm_id })
|
|
|
|
end
|
|
|
|
|
|
|
|
def import(options)
|
2016-10-02 14:10:58 +00:00
|
|
|
config_type = options.delete(:vm_config_type)
|
|
|
|
if config_type === "vmcx"
|
|
|
|
execute('import_vm_vmcx.ps1', options)
|
|
|
|
else
|
|
|
|
execute('import_vm_xml.ps1', options)
|
|
|
|
end
|
2014-03-06 16:51:07 +00:00
|
|
|
end
|
|
|
|
|
2015-03-30 14:37:29 +00:00
|
|
|
def net_set_vlan(vlan_id)
|
|
|
|
execute("set_network_vlan.ps1", { VmId: vm_id, VlanId: vlan_id })
|
|
|
|
end
|
|
|
|
|
2015-09-22 10:35:55 +00:00
|
|
|
def net_set_mac(mac_addr)
|
|
|
|
execute("set_network_mac.ps1", { VmId: vm_id, Mac: mac_addr })
|
|
|
|
end
|
2016-03-05 16:41:18 +00:00
|
|
|
|
|
|
|
def create_snapshot(snapshot_name)
|
|
|
|
execute("create_snapshot.ps1", { VmId: vm_id, SnapName: (snapshot_name) } )
|
|
|
|
end
|
|
|
|
|
|
|
|
def restore_snapshot(snapshot_name)
|
|
|
|
execute("restore_snapshot.ps1", { VmId: vm_id, SnapName: (snapshot_name) } )
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_snapshots()
|
|
|
|
snaps = execute("list_snapshots.ps1", { VmID: vm_id } )
|
|
|
|
snaps.map { |s| s['Name'] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_snapshot(snapshot_name)
|
|
|
|
execute("delete_snapshot.ps1", {VmID: vm_id, SnapName: snapshot_name})
|
|
|
|
end
|
|
|
|
|
2014-02-16 00:28:11 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
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
|
2014-02-16 19:42:20 +00:00
|
|
|
|
|
|
|
# Always have a stop error action for failures
|
|
|
|
ps_options << "-ErrorAction" << "Stop"
|
|
|
|
|
2014-02-16 00:28:11 +00:00
|
|
|
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
|