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
|
2018-05-24 16:57:55 +00:00
|
|
|
|
|
|
|
VALID_HD_EXTENSIONS = [".vhd".freeze, ".vhdx".freeze].freeze
|
|
|
|
|
2014-02-15 23:29:16 +00:00
|
|
|
def initialize(app, env)
|
2016-10-08 14:44:24 +00:00
|
|
|
@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?
|
2018-05-24 16:57:55 +00:00
|
|
|
@logger.error("Required virtual machine directory not found!")
|
2018-05-25 23:43:17 +00:00
|
|
|
raise Errors::BoxInvalid, name: env[:machine].name
|
2014-02-16 02:13:39 +00:00
|
|
|
end
|
|
|
|
|
2018-05-24 16:57:55 +00:00
|
|
|
valid_config_ext = [".xml"]
|
|
|
|
if env[:machine].provider.driver.has_vmcx_support?
|
|
|
|
valid_config_ext << ".vmcx"
|
|
|
|
end
|
|
|
|
|
2014-02-16 02:13:39 +00:00
|
|
|
config_path = nil
|
2018-05-24 16:57:55 +00:00
|
|
|
vm_dir.each_child do |file|
|
|
|
|
if valid_config_ext.include?(file.extname.downcase)
|
|
|
|
config_path = file
|
2016-10-02 14:10:58 +00:00
|
|
|
break
|
|
|
|
end
|
2016-10-07 18:38:05 +00:00
|
|
|
end
|
|
|
|
|
2018-05-24 16:57:55 +00:00
|
|
|
if !config_path
|
|
|
|
@logger.error("Failed to locate box configuration path")
|
2018-05-25 23:43:17 +00:00
|
|
|
raise Errors::BoxInvalid, name: env[:machine].name
|
2018-05-24 16:57:55 +00:00
|
|
|
else
|
|
|
|
@logger.info("Found box configuration path: #{config_path}")
|
2014-02-16 02:13:39 +00:00
|
|
|
end
|
|
|
|
|
2014-07-17 09:50:51 +00:00
|
|
|
image_path = nil
|
2018-05-24 16:57:55 +00:00
|
|
|
hd_dir.each_child do |file|
|
|
|
|
if VALID_HD_EXTENSIONS.include?(file.extname.downcase)
|
|
|
|
image_path = file
|
2014-02-16 02:13:39 +00:00
|
|
|
break
|
|
|
|
end
|
2014-02-15 23:29:16 +00:00
|
|
|
end
|
|
|
|
|
2018-05-24 16:57:55 +00:00
|
|
|
if !image_path
|
|
|
|
@logger.error("Failed to locate box image path")
|
2018-05-25 23:43:17 +00:00
|
|
|
raise Errors::BoxInvalid, name: env[:machine].name
|
2018-05-24 16:57:55 +00:00
|
|
|
else
|
|
|
|
@logger.info("Found box image path: #{image_path}")
|
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")
|
2018-05-24 16:57:55 +00:00
|
|
|
dest_path = env[:machine].data_dir.join("Virtual Hard Disks").join(image_path.basename).to_s
|
2014-02-27 16:08:02 +00:00
|
|
|
|
2014-02-15 23:29:16 +00:00
|
|
|
options = {
|
2018-06-15 14:43:46 +00:00
|
|
|
"VMConfigFile" => Vagrant::Util::Platform.wsl_to_windows_path(config_path).gsub("/", "\\"),
|
|
|
|
"DestinationPath" => Vagrant::Util::Platform.wsl_to_windows_path(dest_path).gsub("/", "\\"),
|
|
|
|
"DataPath" => Vagrant::Util::Platform.wsl_to_windows_path(env[:machine].data_dir).gsub("/", "\\"),
|
2018-05-24 16:57:55 +00:00
|
|
|
"LinkedClone" => !!env[:machine].provider_config.linked_clone,
|
2018-06-15 14:43:46 +00:00
|
|
|
"SourcePath" => Vagrant::Util::Platform.wsl_to_windows_path(image_path).gsub("/", "\\"),
|
2018-05-24 16:57:55 +00:00
|
|
|
"VMName" => env[:machine].provider_config.vmname,
|
2014-02-15 23:29:16 +00:00
|
|
|
}
|
2018-05-24 16:57:55 +00:00
|
|
|
|
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)
|
2017-03-15 21:12:19 +00:00
|
|
|
|
2018-05-24 16:57:55 +00:00
|
|
|
env[:ui].detail("Successfully imported VM")
|
2014-02-15 23:29:16 +00:00
|
|
|
env[:machine].id = server["id"]
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|