Class: VagrantPlugins::QEMU::Action::Import

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Import

Returns a new instance of Import.



9
10
11
12
# File 'lib/vagrant-qemu/action/import.rb', line 9

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

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
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
# File 'lib/vagrant-qemu/action/import.rb', line 14

def call(env)
  image_path = nil
  if env[:machine].provider_config.image_path
    image_path = Pathname.new(env[:machine].provider_config.image_path)
  elsif env[:machine].box
    image_path = env[:machine].box.directory.join("box.img")
  end

  if !image_path || !image_path.file?
    @logger.error("Invalid box image path: #{image_path}")
    raise Errors::BoxInvalid, name: env[:machine].name, err: "Invalid box image path: #{image_path}"
  else
    @logger.info("Found box image path: #{image_path}")
  end

  qemu_dir = Pathname.new(env[:machine].provider_config.qemu_dir)
  if !qemu_dir.directory?
    @logger.error("Invalid qemu dir: #{qemu_dir}")
    raise Errors::ConfigError, err: "Invalid qemu dir: #{qemu_dir}"
  else
    @logger.info("Found qemu dir: #{qemu_dir}")
  end

  env[:ui].output("Importing a QEMU instance")

  options = {
    :image_path => image_path,
    :qemu_dir => qemu_dir,
    :arch => env[:machine].provider_config.arch,
  }

  env[:ui].detail("Creating and registering the VM...")
  server = env[:machine].provider.driver.import(options)

  env[:ui].detail("Successfully imported VM")
  env[:machine].id = server[:id]
  @app.call(env)
end