vagrant/plugins/providers/hyperv/action/import.rb

84 lines
2.8 KiB
Ruby
Raw Normal View History

2014-02-26 19:56:37 +00:00
require "fileutils"
require "log4r"
2014-02-15 23:38:11 +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
def initialize(app, env)
2016-10-08 14:44:24 +00:00
@app = app
@logger = Log4r::Logger.new("vagrant::hyperv::import")
end
def call(env)
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!")
raise Errors::BoxInvalid, name: env[:machine].name
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
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
break
end
end
2018-05-24 16:57:55 +00:00
if !config_path
@logger.error("Failed to locate box configuration path")
raise Errors::BoxInvalid, name: env[:machine].name
2018-05-24 16:57:55 +00:00
else
@logger.info("Found box configuration path: #{config_path}")
end
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
break
end
end
2018-05-24 16:57:55 +00:00
if !image_path
@logger.error("Failed to locate box image path")
raise Errors::BoxInvalid, name: env[:machine].name
2018-05-24 16:57:55 +00:00
else
@logger.info("Found box image path: #{image_path}")
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
options = {
"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,
"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,
}
2018-05-24 16:57:55 +00:00
2014-02-26 19:56:37 +00:00
env[:ui].detail("Creating and registering the VM...")
server = env[:machine].provider.driver.import(options)
2018-05-24 16:57:55 +00:00
env[:ui].detail("Successfully imported VM")
env[:machine].id = server["id"]
@app.call(env)
end
end
end
end
end