2014-02-26 19:56:37 +00:00
|
|
|
require "fileutils"
|
|
|
|
|
2014-02-15 23:29:16 +00:00
|
|
|
require "log4r"
|
2014-02-15 23:38:11 +00:00
|
|
|
|
2014-02-15 23:29:16 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module HyperV
|
|
|
|
module Action
|
|
|
|
class Import
|
|
|
|
def initialize(app, env)
|
|
|
|
@app = app
|
2014-02-16 02:13:39 +00:00
|
|
|
@logger = Log4r::Logger.new("vagrant::hyperv::import")
|
2014-02-15 23:29:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2014-02-16 02:13:39 +00:00
|
|
|
vm_dir = env[:machine].box.directory.join("Virtual Machines")
|
|
|
|
hd_dir = env[:machine].box.directory.join("Virtual Hard Disks")
|
|
|
|
|
|
|
|
if !vm_dir.directory? || !hd_dir.directory?
|
|
|
|
raise Errors::BoxInvalid
|
|
|
|
end
|
|
|
|
|
|
|
|
config_path = nil
|
|
|
|
vm_dir.each_child do |f|
|
|
|
|
if f.extname.downcase == ".xml"
|
|
|
|
config_path = f
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
vhdx_path = nil
|
|
|
|
hd_dir.each_child do |f|
|
|
|
|
if f.extname.downcase == ".vhdx"
|
|
|
|
vhdx_path = f
|
|
|
|
break
|
|
|
|
end
|
2014-02-15 23:29:16 +00:00
|
|
|
end
|
|
|
|
|
2014-02-16 02:13:39 +00:00
|
|
|
if !config_path || !vhdx_path
|
|
|
|
raise Errors::BoxInvalid
|
2014-02-15 23:29:16 +00:00
|
|
|
end
|
|
|
|
|
2014-02-26 19:56:37 +00:00
|
|
|
env[:ui].output("Importing a Hyper-V instance")
|
2014-02-27 16:08:02 +00:00
|
|
|
|
|
|
|
switches = env[:machine].provider.driver.execute("get_switches.ps1", {})
|
|
|
|
raise Errors::NoSwitches if switches.empty?
|
|
|
|
|
|
|
|
switch = switches[0]["Name"]
|
|
|
|
if switches.length > 1
|
|
|
|
env[:ui].detail(I18n.t("vagrant_hyperv.choose_switch") + "\n ")
|
|
|
|
switches.each_index do |i|
|
|
|
|
switch = switches[i]
|
|
|
|
env[:ui].detail("#{i+1}) #{switch["Name"]}")
|
|
|
|
end
|
|
|
|
env[:ui].detail(" ")
|
|
|
|
|
|
|
|
switch = nil
|
|
|
|
while !switch
|
|
|
|
switch = env[:ui].ask("What switch would you like to use? ")
|
|
|
|
next if !switch
|
|
|
|
switch = switch.to_i - 1
|
|
|
|
switch = nil if switch < 0 || switch >= switches.length
|
|
|
|
end
|
|
|
|
switch = switches[switch]["Name"]
|
|
|
|
end
|
|
|
|
|
2014-02-26 19:56:37 +00:00
|
|
|
env[:ui].detail("Cloning virtual hard drive...")
|
|
|
|
source_path = vhdx_path.to_s
|
|
|
|
dest_path = env[:machine].data_dir.join("disk.vhdx").to_s
|
|
|
|
FileUtils.cp(source_path, dest_path)
|
|
|
|
vhdx_path = dest_path
|
|
|
|
|
2014-02-16 02:13:39 +00:00
|
|
|
# We have to normalize the paths to be Windows paths since
|
|
|
|
# we're executing PowerShell.
|
2014-02-15 23:29:16 +00:00
|
|
|
options = {
|
2014-02-16 02:13:39 +00:00
|
|
|
vm_xml_config: config_path.to_s.gsub("/", "\\"),
|
|
|
|
vhdx_path: vhdx_path.to_s.gsub("/", "\\")
|
2014-02-15 23:29:16 +00:00
|
|
|
}
|
2014-02-27 16:08:02 +00:00
|
|
|
options[:switchname] = switch if switch
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2014-02-26 19:56:37 +00:00
|
|
|
env[:ui].detail("Creating and registering the VM...")
|
2014-03-06 16:51:07 +00:00
|
|
|
server = env[:machine].provider.driver.import(options)
|
2014-02-26 19:12:24 +00:00
|
|
|
env[:ui].detail("Successfully imported a VM with name: #{server['name']}")
|
2014-02-15 23:29:16 +00:00
|
|
|
env[:machine].id = server["id"]
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|