vagrant box add --force works again [GH-1413]
This commit is contained in:
parent
e04efa155f
commit
dea881cad2
|
@ -31,7 +31,8 @@ module Vagrant
|
||||||
# Add the box
|
# Add the box
|
||||||
env[:ui].info I18n.t("vagrant.actions.box.add.adding", :name => env[:box_name])
|
env[:ui].info I18n.t("vagrant.actions.box.add.adding", :name => env[:box_name])
|
||||||
begin
|
begin
|
||||||
env[:box_collection].add(@temp_path, env[:box_name], env[:box_provider])
|
env[:box_collection].add(
|
||||||
|
@temp_path, env[:box_name], env[:box_provider], env[:box_force])
|
||||||
rescue Vagrant::Errors::BoxUpgradeRequired
|
rescue Vagrant::Errors::BoxUpgradeRequired
|
||||||
# Upgrade the box
|
# Upgrade the box
|
||||||
env[:box_collection].upgrade(env[:box_name])
|
env[:box_collection].upgrade(env[:box_name])
|
||||||
|
|
|
@ -63,14 +63,23 @@ module Vagrant
|
||||||
# will be verified with the `metadata.json` file in the box and is
|
# will be verified with the `metadata.json` file in the box and is
|
||||||
# meant as a basic check. If this isn't given, then whatever provider
|
# meant as a basic check. If this isn't given, then whatever provider
|
||||||
# the box represents will be added.
|
# the box represents will be added.
|
||||||
def add(path, name, provider=nil)
|
# @param [Boolean] force If true, any existing box with the same name
|
||||||
|
# and provider will be replaced.
|
||||||
|
def add(path, name, provider=nil, force=false)
|
||||||
# A helper to check if a box exists. We store this in a variable
|
# A helper to check if a box exists. We store this in a variable
|
||||||
# since we call it multiple times.
|
# since we call it multiple times.
|
||||||
check_box_exists = lambda do |box_provider|
|
check_box_exists = lambda do |box_provider|
|
||||||
if find(name, box_provider)
|
box = find(name, box_provider)
|
||||||
|
next if !box
|
||||||
|
|
||||||
|
if !force
|
||||||
@logger.error("Box already exists, can't add: #{name} #{box_provider}")
|
@logger.error("Box already exists, can't add: #{name} #{box_provider}")
|
||||||
raise Errors::BoxAlreadyExists, :name => name, :provider => box_provider
|
raise Errors::BoxAlreadyExists, :name => name, :provider => box_provider
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# We're forcing, so just delete the old box
|
||||||
|
@logger.info("Box already exists, but forcing so removing: #{name} #{box_provider}")
|
||||||
|
box.destroy!
|
||||||
end
|
end
|
||||||
|
|
||||||
log_provider = provider ? provider : "any provider"
|
log_provider = provider ? provider : "any provider"
|
||||||
|
|
|
@ -26,13 +26,6 @@ module VagrantPlugins
|
||||||
return if !argv
|
return if !argv
|
||||||
raise Vagrant::Errors::CLIInvalidUsage, :help => opts.help.chomp if argv.length < 2
|
raise Vagrant::Errors::CLIInvalidUsage, :help => opts.help.chomp if argv.length < 2
|
||||||
|
|
||||||
# If we're force adding, then be sure to destroy any existing box if it
|
|
||||||
# exists.
|
|
||||||
if options[:force]
|
|
||||||
existing = @env.boxes.find(argv[0], :virtualbox)
|
|
||||||
existing.destroy! if existing
|
|
||||||
end
|
|
||||||
|
|
||||||
# Get the provider if one was set
|
# Get the provider if one was set
|
||||||
provider = nil
|
provider = nil
|
||||||
provider = options[:provider].to_sym if options[:provider]
|
provider = options[:provider].to_sym if options[:provider]
|
||||||
|
@ -40,7 +33,8 @@ module VagrantPlugins
|
||||||
@env.action_runner.run(Vagrant::Action.action_box_add, {
|
@env.action_runner.run(Vagrant::Action.action_box_add, {
|
||||||
:box_name => argv[0],
|
:box_name => argv[0],
|
||||||
:box_provider => provider,
|
:box_provider => provider,
|
||||||
:box_url => argv[1]
|
:box_url => argv[1],
|
||||||
|
:box_force => options[:force]
|
||||||
})
|
})
|
||||||
|
|
||||||
# Success, exit status 0
|
# Success, exit status 0
|
||||||
|
|
|
@ -129,12 +129,14 @@ module Unit
|
||||||
#
|
#
|
||||||
# @param [Symbol] provider Provider for the box.
|
# @param [Symbol] provider Provider for the box.
|
||||||
# @return [Pathname] Path to the newly created box.
|
# @return [Pathname] Path to the newly created box.
|
||||||
def box2_file(provider)
|
def box2_file(provider, options=nil)
|
||||||
|
options ||= {}
|
||||||
|
|
||||||
# This is the metadata we want to store in our file
|
# This is the metadata we want to store in our file
|
||||||
metadata = {
|
metadata = {
|
||||||
"type" => "v2_box",
|
"type" => "v2_box",
|
||||||
"provider" => provider
|
"provider" => provider
|
||||||
}
|
}.merge(options[:metadata] || {})
|
||||||
|
|
||||||
# Create a temporary directory to store our data we will tar up
|
# Create a temporary directory to store our data we will tar up
|
||||||
td_source = Tempdir.new
|
td_source = Tempdir.new
|
||||||
|
|
|
@ -63,6 +63,19 @@ describe Vagrant::BoxCollection do
|
||||||
to raise_error(Vagrant::Errors::BoxAlreadyExists)
|
to raise_error(Vagrant::Errors::BoxAlreadyExists)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should replace the box if force is specified" do
|
||||||
|
prev_box_name = "foo"
|
||||||
|
prev_box_provider = :vmware
|
||||||
|
|
||||||
|
# Setup the environment with the box pre-added
|
||||||
|
environment.box2(prev_box_name, prev_box_provider)
|
||||||
|
|
||||||
|
# Attempt to add the box with the same name
|
||||||
|
box_path = environment.box2_file(prev_box_provider, metadata: { "replaced" => "yes" })
|
||||||
|
box = instance.add(box_path, prev_box_name, nil, true)
|
||||||
|
box.metadata["replaced"].should == "yes"
|
||||||
|
end
|
||||||
|
|
||||||
it "should raise an exception if the box already exists and no provider is given" do
|
it "should raise an exception if the box already exists and no provider is given" do
|
||||||
# Create some box file
|
# Create some box file
|
||||||
box_name = "foo"
|
box_name = "foo"
|
||||||
|
|
Loading…
Reference in New Issue