From fe93b0d2a53c09b32b7918b7e8138f8b0f7e4906 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 15 Feb 2014 18:13:39 -0800 Subject: [PATCH] providers/hyperv: more checks on machine import --- plugins/providers/hyperv/action/import.rb | 45 +++++++++++++------ plugins/providers/hyperv/errors.rb | 4 ++ templates/locales/providers_hyperv.yml | 13 ++++++ .../plugins/providers/hyperv/provider_test.rb | 6 +++ 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/plugins/providers/hyperv/action/import.rb b/plugins/providers/hyperv/action/import.rb index 6cc0b0b42..fad717ccc 100644 --- a/plugins/providers/hyperv/action/import.rb +++ b/plugins/providers/hyperv/action/import.rb @@ -6,31 +6,48 @@ module VagrantPlugins class Import def initialize(app, env) @app = app - @logger = Log4r::Logger.new("vagrant::hyperv::connection") + @logger = Log4r::Logger.new("vagrant::hyperv::import") end def call(env) - box_directory = env[:machine].box.directory.to_s - path = Pathname.new(box_directory.to_s + '/Virtual Machines') - config_path = "" - path.each_child do |f| - config_path = f.to_s if f.extname.downcase == ".xml" + 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 - path = Pathname.new(box_directory.to_s + '/Virtual Hard Disks') - vhdx_path = "" - path.each_child do |f| - vhdx_path = f.to_s if f.extname.downcase == ".vhdx" + 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 + end + + if !config_path || !vhdx_path + raise Errors::BoxInvalid + end + + # We have to normalize the paths to be Windows paths since + # we're executing PowerShell. options = { - vm_xml_config: config_path.gsub("/", "\\"), - vhdx_path: vhdx_path.gsub("/", "\\") + vm_xml_config: config_path.to_s.gsub("/", "\\"), + vhdx_path: vhdx_path.to_s.gsub("/", "\\") } env[:ui].info "Importing a Hyper-V instance" - server = env[:machine].provider.driver.execute('import_vm.ps1', options) - env[:ui].info "Successfully imported a VM with name #{server['name']}" + server = env[:machine].provider.driver.execute( + 'import_vm.ps1', options) + env[:ui].info "Successfully imported a VM with name: #{server['name']}" env[:machine].id = server["id"] @app.call(env) end diff --git a/plugins/providers/hyperv/errors.rb b/plugins/providers/hyperv/errors.rb index fa48b67d6..67e26b3ad 100644 --- a/plugins/providers/hyperv/errors.rb +++ b/plugins/providers/hyperv/errors.rb @@ -10,6 +10,10 @@ module VagrantPlugins error_key(:admin_required) end + class BoxInvalid < HyperVError + error_key(:box_invalid) + end + class PowerShellError < HyperVError error_key(:powershell_error) end diff --git a/templates/locales/providers_hyperv.yml b/templates/locales/providers_hyperv.yml index 26322a4c9..edd2e21ae 100644 --- a/templates/locales/providers_hyperv.yml +++ b/templates/locales/providers_hyperv.yml @@ -7,6 +7,19 @@ en: Hyper-V requires administrative privileges for management commands. Please restart your console with administrative privileges and try again. + box_invalid: |- + The box you're using with the Hyper-V provider ('%{name}') + is invalid. A Hyper-V box should contain both a + "Virtual Machines" and a "Virtual Hard Disks" folder that are + created as part of exporting a Hyper-V machine. + + Within these directories, Vagrant expects to find the + virtual machine configuration as well as the root hard disk. + + The box you're attempting to use is missing one or both of + these directories or does not contain the files expected. Verify + that you added the correct box. If this problem persists, + please contact the creator of the box for assistance. powershell_error: |- An error occurred while executing a PowerShell script. This error is shown below. Please read the error message and see if this is diff --git a/test/unit/plugins/providers/hyperv/provider_test.rb b/test/unit/plugins/providers/hyperv/provider_test.rb index c69301a22..d431a2d2f 100644 --- a/test/unit/plugins/providers/hyperv/provider_test.rb +++ b/test/unit/plugins/providers/hyperv/provider_test.rb @@ -39,4 +39,10 @@ describe VagrantPlugins::HyperV::Provider do to raise_error(VagrantPlugins::HyperV::Errors::PowerShellRequired) end end + + describe "#driver" do + it "is initialized" do + expect(subject.driver).to be_kind_of(VagrantPlugins::HyperV::Driver) + end + end end