Class: VagrantPlugins::ProviderLibvirt::Action::HandleBoxImage

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-libvirt/action/handle_box_image.rb

Constant Summary collapse

@@lock =
Mutex.new

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ HandleBoxImage

Returns a new instance of HandleBoxImage.



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

def initialize(app, env)
  @logger = Log4r::Logger.new('vagrant_libvirt::action::handle_box_image')
  @app = app
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
88
89
90
# File 'lib/vagrant-libvirt/action/handle_box_image.rb', line 15

def call(env)

  # Verify box metadata for mandatory values.
  #
  # Virtual size has to be set for allocating space in storage pool.
  box_virtual_size = env[:machine].box.['virtual_size']
  if box_virtual_size == nil
    raise Errors::NoBoxVirtualSizeSet
  end

  # Support qcow2 format only for now, but other formats with backing
  # store capability should be usable.
  box_format = env[:machine].box.['format']
  if box_format == nil
    raise Errors::NoBoxFormatSet
  elsif box_format != 'qcow2'
    raise Errors::WrongBoxFormatSet
  end

  # Get config options
  config   = env[:machine].provider_config
  box_image_file = env[:machine].box.directory.join('box.img').to_s
  env[:box_volume_name] = env[:machine].box.name.to_s.dup.gsub("/", "-VAGRANTSLASH-")
  env[:box_volume_name] << "_vagrant_box_image_#{env[:machine].box.version.to_s rescue ''}.img"

  @@lock.synchronize do
    # Don't continue if image already exists in storage pool.
    return @app.call(env) if ProviderLibvirt::Util::Collection.find_matching(
      env[:libvirt_compute].volumes.all, env[:box_volume_name])

    # Box is not available as a storage pool volume. Create and upload
    # it as a copy of local box image.
    env[:ui].info(I18n.t('vagrant_libvirt.uploading_volume'))

    # Create new volume in storage pool
    raise Errors::BoxNotFound if !File.exists?(box_image_file)
    box_image_size = File.size(box_image_file) # B
    message = "Creating volume #{env[:box_volume_name]}"
    message << " in storage pool #{config.storage_pool_name}."
    @logger.info(message)
    begin
      fog_volume = env[:libvirt_compute].volumes.create(
        name:         env[:box_volume_name],
        allocation:   "#{box_image_size/1024/1024}M",
        capacity:     "#{box_virtual_size}G",
        format_type:  box_format,
        pool_name:    config.storage_pool_name)
    rescue Fog::Errors::Error => e
      raise Errors::FogCreateVolumeError,
        :error_message => e.message
    end

    # Upload box image to storage pool
    ret = upload_image(box_image_file, config.storage_pool_name,
      env[:box_volume_name], env) do |progress|
        env[:ui].clear_line
        env[:ui].report_progress(progress, box_image_size, false)
    end

    # Clear the line one last time since the progress meter doesn't
    # disappear immediately.
    env[:ui].clear_line

    # If upload failed or was interrupted, remove created volume from
    # storage pool.
    if env[:interrupted] || !ret
      begin
        fog_volume.destroy
      rescue
        nil
      end
    end
  end

  @app.call(env)
end