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

Inherits:
Object
  • Object
show all
Includes:
Util::StorageUtil, Util::Ui
Defined in:
lib/vagrant-libvirt/action/handle_box_image.rb

Constant Summary collapse

@@lock =
Mutex.new

Instance Method Summary collapse

Methods included from Util::Ui

#rewriting

Methods included from Util::StorageUtil

#storage_gid, #storage_pool_path, #storage_uid

Constructor Details

#initialize(app, _env) ⇒ HandleBoxImage

Returns a new instance of HandleBoxImage.



21
22
23
24
# File 'lib/vagrant-libvirt/action/handle_box_image.rb', line 21

def initialize(app, _env)
  @logger = Log4r::Logger.new('vagrant_libvirt::action::handle_box_image')
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/vagrant-libvirt/action/handle_box_image.rb', line 26

def call(env)
  # Handle box formats converting between v1 => v2 and ensuring
  # any obsolete settings are rejected.

  disks = env[:machine].box..fetch('disks', [])
  if disks.empty?
    # Handle box v1 format

    # Only qcow2 format is supported in v1, but other formats with backing
    # store capability should be usable.
    box_format = env[:machine].box.['format']
    HandleBoxImage.verify_box_format(box_format)

    image_path = HandleBoxImage.get_box_image_path(env[:machine].box, 'box.img')
    env[:box_volume_number] = 1
    env[:box_volumes] = [{
      :path => image_path,
      :name => HandleBoxImage.get_volume_name(env[:machine].box, 'box', image_path, env[:ui]),
      :virtual_size => HandleBoxImage.get_virtual_size(env),
      :format => box_format,
      :compat => "1.1",
    }]
  else
    # Handle box v2 format
    # {
    #   'path': '<path-of-file-box>',
    #   'name': '<name-to-use-in-storage>' # optional, will use index
    # }
    #
    env[:box_volume_number] = disks.length()
    target_volumes = Hash[]
    env[:box_volumes] = Array.new(env[:box_volume_number]) { |i|
      raise Errors::BoxFormatMissingAttribute, attribute: "disks[#{i}]['path']" if disks[i]['path'].nil?

      image_path = HandleBoxImage.get_box_image_path(env[:machine].box, disks[i]['path'])
      format, virtual_size, compat = HandleBoxImage.get_box_disk_settings(image_path)
      volume_name = HandleBoxImage.get_volume_name(
        env[:machine].box,
        disks[i].fetch('name', disks[i]['path'].sub(/#{File.extname(disks[i]['path'])}$/, '')),
        image_path,
        env[:ui],
      )

      # allowing name means needing to check that it doesn't cause a clash
      existing = target_volumes[volume_name]
      if !existing.nil?
        raise Errors::BoxFormatDuplicateVolume, volume: volume_name, new_disk: "disks[#{i}]", orig_disk: "disks[#{existing}]"
      end
      target_volumes[volume_name] = i

      {
        :path => image_path,
        :name => volume_name,
        :virtual_size => virtual_size,
        :format => HandleBoxImage.verify_box_format(format),
        :compat => compat,
      }
    }
  end

  # Get config options
  config = env[:machine].provider_config
  box_image_files = []
  env[:box_volumes].each do |d|
    box_image_files.push(d[:path])
  end

  # Override box_virtual_size
  box_virtual_size = env[:box_volumes][0][:virtual_size]
  if config.machine_virtual_size
    config_machine_virtual_size = ByteNumber.from_GB(config.machine_virtual_size)
    if config_machine_virtual_size < box_virtual_size
      # Warn that a virtual size less than the box metadata size
      # is not supported and will be ignored
      env[:ui].warn I18n.t(
        'vagrant_libvirt.warnings.ignoring_virtual_size_too_small',
          requested: config_machine_virtual_size.to_GB, minimum: box_virtual_size.to_GB
      )
    else
      env[:ui].info I18n.t('vagrant_libvirt.manual_resize_required')
      box_virtual_size = config_machine_virtual_size
    end
  end
  # save for use by later actions
  env[:box_volumes][0][:virtual_size] = box_virtual_size
  # special handling for domain volume
  env[:box_volumes][0][:device] ||= config.disk_device

  # while inside the synchronize block take care not to call the next
  # action in the chain, as must exit this block first to prevent
  # locking all subsequent actions as well.
  @@lock.synchronize do
    env[:box_volumes].each_index do |i|
      # Don't continue if image already exists in storage pool.
      box_volume = env[:machine].provider.driver.connection.volumes.all(
        name: env[:box_volumes][i][:name]
      ).first
      next if box_volume && box_volume.id

      send_box_image(env, config, box_image_files[i], env[:box_volumes][i])
    end
  end

  @app.call(env)
end