Re-upload un-versioned boxes based on modified time (#1382)

If no box version available, use the image modified time as part of the
final path in order to allow replacement anytime the box package is
altered.
This commit is contained in:
Darragh Bailey
2021-10-16 14:49:18 +01:00
committed by GitHub
parent 5b70a4a669
commit 23a23029a7
3 changed files with 53 additions and 10 deletions

View File

@@ -34,10 +34,11 @@ module VagrantPlugins
box_format = env[:machine].box.metadata['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 => HandleBoxImage.get_box_image_path(env[:machine].box, 'box.img'),
:name => HandleBoxImage.get_volume_name(env[:machine].box, 'box'),
: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,
}]
@@ -58,6 +59,8 @@ module VagrantPlugins
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
@@ -122,15 +125,21 @@ module VagrantPlugins
protected
def self.get_volume_name(box, name)
def self.get_volume_name(box, name, path, ui)
version = begin
box.version.to_s
rescue
''
end
if version.empty?
ui.warn(I18n.t('vagrant_libvirt.box_version_missing', name: box.name.to_s))
version = "0_#{File.mtime(path).to_i}"
end
vol_name = box.name.to_s.dup.gsub('/', '-VAGRANTSLASH-')
vol_name << "_vagrant_box_image_#{
begin
box.version.to_s
rescue
''
end
}_#{name.dup.gsub('/', '-SLASH-')}.img"
vol_name << "_vagrant_box_image_#{version}_#{name.dup.gsub('/', '-SLASH-')}.img"
end
def self.get_virtual_size(env)

View File

@@ -15,6 +15,10 @@ en:
manual_resize_required: |-
Created volume larger than box defaults, will require manual resizing of
filesystems to utilize.
box_version_missing: |-
No verison detected for %{name}, using timestamp to watch for modifications. Consider
generating a local metadata for the box with a version to allow better handling.
See https://www.vagrantup.com/docs/boxes/format#box-metadata for further details.
uploading_volume: |-
Uploading base box image as volume into Libvirt storage...
creating_domain_volume: |-

View File

@@ -98,6 +98,36 @@ describe VagrantPlugins::ProviderLibvirt::Action::HandleBoxImage do
)
end
context 'when no box version set' do
let(:box_mtime) { Time.now }
before do
expect(env[:machine]).to receive_message_chain("box.version") { nil }
expect(File).to receive(:mtime).and_return(box_mtime)
end
it 'should use the box file timestamp' do
expect(ui).to receive(:warn).with(
"No verison detected for test, using timestamp to watch for modifications. Consider\n" +
"generating a local metadata for the box with a version to allow better handling.\n" +
'See https://www.vagrantup.com/docs/boxes/format#box-metadata for further details.'
)
expect(subject.call(env)).to be_nil
expect(env[:box_volume_number]).to eq(1)
expect(env[:box_volumes]).to eq(
[
{
:path=>"/test/box.img",
:name=>"test_vagrant_box_image_0_#{box_mtime.to_i}_box.img",
:virtual_size=>byte_number_5G,
:format=>"qcow2"
}
]
)
end
end
context 'When config.machine_virtual_size is set and smaller than box_virtual_size' do
before do
allow(env[:machine]).to receive_message_chain("provider_config.machine_virtual_size").and_return(1)