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.



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

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

Instance Method Details

#call(env) ⇒ Object



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vagrant-qemu/action/import.rb', line 15

def call(env)
  image_path = Array.new
  if env[:machine].provider_config.image_path
    paths = env[:machine].provider_config.image_path
    paths = [paths] if !paths.kind_of?(Array)
    paths.each do |p|
      image_path.append(Pathname.new(p))
    end
  else
    disks = env[:machine].box..fetch('disks', [])
    if disks.empty?
      # box v1 format
      image_path.append(env[:machine].box.directory.join("box.img"))
    else
      # box v2 format
      disks.each_with_index do |d, i|
        if d['path'].nil?
          @logger.error("Missing box image path for disk #{i}")
          raise Errors::BoxInvalid, name: env[:machine].name, err: "Missing box image path for disk #{i}"
        end
        image_path.append(env[:machine].box.directory.join(d['path']))
      end
    end
  end

  if image_path.empty?
    @logger.error("Empty box image path")
    raise Errors::BoxInvalid, name: env[:machine].name, err: "Empty box image path"
  end
  image_path.each do |img|
    if !img.file?
      @logger.error("Invalid box image path: #{img}")
      raise Errors::BoxInvalid, name: env[:machine].name, err: "Invalid box image path: #{img}"
    end
    img_str = img.to_s
    stdout, stderr, status = Open3.capture3('qemu-img', 'info', '--output=json', img_str)
    if !status.success?
      @logger.error("Run qemu-img info failed, #{img_str}, out: #{stdout}, err: #{stderr}")
      raise Errors::BoxInvalid, name: env[:machine].name, err: "Run qemu-img info failed, #{img_str}, out: #{stdout}, err: #{stderr}"
    end
    img_info = JSON.parse(stdout)
    format = img_info['format']
    if format != 'qcow2'
      @logger.error("Invalid box image format, #{img_str}, format: #{format}")
      raise Errors::BoxInvalid, name: env[:machine].name, err: "Invalid box image format, #{img_str}, format: #{format}"
    end
    @logger.info("Found box image path: #{img_info}")
  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,
    :firmware_format => env[:machine].provider_config.firmware_format
  }

  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