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
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

View File

@ -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

View File

@ -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

View File

@ -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