providers/hyperv: more checks on machine import

This commit is contained in:
Mitchell Hashimoto 2014-02-15 18:13:39 -08:00
parent 784a5b2e32
commit fe93b0d2a5
4 changed files with 54 additions and 14 deletions

View File

@ -6,31 +6,48 @@ module VagrantPlugins
class Import class Import
def initialize(app, env) def initialize(app, env)
@app = app @app = app
@logger = Log4r::Logger.new("vagrant::hyperv::connection") @logger = Log4r::Logger.new("vagrant::hyperv::import")
end end
def call(env) def call(env)
box_directory = env[:machine].box.directory.to_s vm_dir = env[:machine].box.directory.join("Virtual Machines")
path = Pathname.new(box_directory.to_s + '/Virtual Machines') hd_dir = env[:machine].box.directory.join("Virtual Hard Disks")
config_path = ""
path.each_child do |f| if !vm_dir.directory? || !hd_dir.directory?
config_path = f.to_s if f.extname.downcase == ".xml" raise Errors::BoxInvalid
end end
path = Pathname.new(box_directory.to_s + '/Virtual Hard Disks') config_path = nil
vhdx_path = "" vm_dir.each_child do |f|
path.each_child do |f| if f.extname.downcase == ".xml"
vhdx_path = f.to_s if f.extname.downcase == ".vhdx" config_path = f
break
end
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 = { options = {
vm_xml_config: config_path.gsub("/", "\\"), vm_xml_config: config_path.to_s.gsub("/", "\\"),
vhdx_path: vhdx_path.gsub("/", "\\") vhdx_path: vhdx_path.to_s.gsub("/", "\\")
} }
env[:ui].info "Importing a Hyper-V instance" env[:ui].info "Importing a Hyper-V instance"
server = env[:machine].provider.driver.execute('import_vm.ps1', options) server = env[:machine].provider.driver.execute(
env[:ui].info "Successfully imported a VM with name #{server['name']}" 'import_vm.ps1', options)
env[:ui].info "Successfully imported a VM with name: #{server['name']}"
env[:machine].id = server["id"] env[:machine].id = server["id"]
@app.call(env) @app.call(env)
end end

View File

@ -10,6 +10,10 @@ module VagrantPlugins
error_key(:admin_required) error_key(:admin_required)
end end
class BoxInvalid < HyperVError
error_key(:box_invalid)
end
class PowerShellError < HyperVError class PowerShellError < HyperVError
error_key(:powershell_error) error_key(:powershell_error)
end end

View File

@ -7,6 +7,19 @@ en:
Hyper-V requires administrative privileges for management Hyper-V requires administrative privileges for management
commands. Please restart your console with administrative commands. Please restart your console with administrative
privileges and try again. 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: |- powershell_error: |-
An error occurred while executing a PowerShell script. This error An error occurred while executing a PowerShell script. This error
is shown below. Please read the error message and see if this is is shown below. Please read the error message and see if this is

View File

@ -39,4 +39,10 @@ describe VagrantPlugins::HyperV::Provider do
to raise_error(VagrantPlugins::HyperV::Errors::PowerShellRequired) to raise_error(VagrantPlugins::HyperV::Errors::PowerShellRequired)
end end
end end
describe "#driver" do
it "is initialized" do
expect(subject.driver).to be_kind_of(VagrantPlugins::HyperV::Driver)
end
end
end end