Class: VagrantPlugins::HyperV::Action::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-windows-hyperv/monkey_patch/action/import.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Import

Returns a new instance of Import.



14
15
16
17
# File 'lib/vagrant-windows-hyperv/monkey_patch/action/import.rb', line 14

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant::hyperv::import")
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vagrant-windows-hyperv/monkey_patch/action/import.rb', line 19

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?
    raise Errors::BoxInvalid
  end

  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

  env[:ui].output("Importing a Hyper-V instance")

  env[:ui].detail("Cloning virtual hard drive...")
  source_path = vhdx_path.to_s
  dest_path   = env[:machine].data_dir.join("disk.vhdx").to_s
  FileUtils.cp(source_path, dest_path)
  vhdx_path = dest_path

  # We have to normalize the paths to be Windows paths since
  # we're executing PowerShell.
  options = {
    vm_xml_config:  config_path.to_s.gsub("/", "\\"),
    vhdx_path:      vhdx_path.to_s.gsub("/", "\\")
  }

  env[:ui].detail("Creating and registering the VM...")
  server = env[:machine].provider.driver.import(options)
  env[:ui].detail("Successfully imported a VM with name: #{server['name']}")
  env[:machine].id = server["id"]
  @app.call(env)
end