Merge pull request #11052 from briancain/add-skip-used-boxes-with-force-prune

Fixes #10908: Preserve in-use boxes when force flag is used with prune
This commit is contained in:
Brian Cain 2019-09-06 08:49:40 -07:00 committed by GitHub
commit f9b60ba603
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 3 deletions

View File

@ -99,8 +99,11 @@ module Vagrant
b.use Confirm, message, force_key b.use Confirm, message, force_key
end end
# Keep used boxes, even if "force" is applied
keep_used_boxes = env[:keep_used_boxes]
result = env[:action_runner].run(stack, env) result = env[:action_runner].run(stack, env)
if !result[:result] if !result[:result] || keep_used_boxes
# They said "no", so continue with the next box # They said "no", so continue with the next box
next next
end end

View File

@ -30,6 +30,10 @@ module VagrantPlugins
o.on("-f", "--force", "Destroy without confirmation even when box is in use.") do |f| o.on("-f", "--force", "Destroy without confirmation even when box is in use.") do |f|
options[:force] = f options[:force] = f
end end
o.on("-k", "--keep-active-boxes", "When combined with `--force`, will keep boxes still actively in use.") do |k|
options[:keep] = k
end
end end
# Parse the options # Parse the options
@ -41,7 +45,7 @@ module VagrantPlugins
return @env.ui.warn(I18n.t("vagrant.commands.box.no_installed_boxes"), prefix: false) return @env.ui.warn(I18n.t("vagrant.commands.box.no_installed_boxes"), prefix: false)
end end
delete_oldest_boxes(boxes, options[:provider], options[:force], options[:name], options[:dry_run]) delete_oldest_boxes(boxes, options[:provider], options[:force], options[:name], options[:dry_run], options[:keep])
# Success, exit status 0 # Success, exit status 0
0 0
@ -49,7 +53,7 @@ module VagrantPlugins
private private
def delete_oldest_boxes(boxes, only_provider, skip_confirm, only_name, dry_run) def delete_oldest_boxes(boxes, only_provider, skip_confirm, only_name, dry_run, keep_used_boxes)
# Find the longest box name # Find the longest box name
longest_box = boxes.max_by { |x| x[0].length } longest_box = boxes.max_by { |x| x[0].length }
longest_box_length = longest_box[0].length longest_box_length = longest_box[0].length
@ -112,6 +116,7 @@ module VagrantPlugins
box_provider: provider, box_provider: provider,
box_version: version, box_version: version,
force_confirm_box_remove: skip_confirm, force_confirm_box_remove: skip_confirm,
keep_used_boxes: keep_used_boxes,
box_remove_all_versions: false, box_remove_all_versions: false,
}) })
end end

View File

@ -151,6 +151,37 @@ describe Vagrant::Action::Builtin::BoxRemove do
subject.call(env) subject.call(env)
end end
it "doesn't delete if the box is in use and keep_used_boxes is set" do
env[:keep_used_boxes] = true
machine_index << new_entry("foo", "virtualbox", "1.0")
result = { result: true }
expect(action_runner).to receive(:run).
with(anything, env).and_return(result)
expect(box_collection).to receive(:find).with(
"foo", :virtualbox, "1.0").and_return(box)
expect(box).to receive(:destroy!).never
subject.call(env)
end
it "deletes if the box is in use and force is used" do
machine_index << new_entry("foo", "virtualbox", "1.0")
result = { result: true }
expect(action_runner).to receive(:run).
with(anything, env).and_return(result)
expect(box_collection).to receive(:find).with(
"foo", :virtualbox, "1.0").and_return(box)
expect(box_collection).to receive(:clean).with(box.name)
.and_return(true)
expect(box).to receive(:destroy!)
subject.call(env)
end
end end
it "errors if the box doesn't exist" do it "errors if the box doesn't exist" do

View File

@ -142,6 +142,8 @@ This command removes old versions of installed boxes. If the box is currently in
* `--force` - Destroy without confirmation even when box is in use. * `--force` - Destroy without confirmation even when box is in use.
* `--keep-active-boxes` - When combined with `--force`, will keep boxes still actively in use.
# Box Remove # Box Remove
**Command: `vagrant box remove NAME`** **Command: `vagrant box remove NAME`**