Add support for "upgrade safe" config classes to plugins.

These are classes that use NO core classes of Vagrant, and are therefore
safe to load for upgrades. i.e. a V2 core can load a V1 config class
that is deemed upgrade safe without crashing Vagrant.
This commit is contained in:
Mitchell Hashimoto 2012-11-03 21:39:06 -07:00
parent 68923ff556
commit 59d5c5ed92
3 changed files with 52 additions and 7 deletions

View File

@ -42,6 +42,25 @@ module Vagrant
result
end
# This returns all the registered configuration classes that were
# marked as "upgrade safe."
#
# @return [Hash]
def config_upgrade_safe
result = {}
@registered.each do |plugin|
configs = plugin.data[:config_upgrade_safe]
if configs
configs.each do |key|
result[key] = plugin.config.get(key)
end
end
end
result
end
# This returns all the registered guests.
#
# @return [Hash]

View File

@ -126,7 +126,16 @@ module Vagrant
data[:config] ||= Registry.new
# Register a new config class only if a name was given.
data[:config].register(name.to_sym, &block) if name != UNSET_VALUE
if name != UNSET_VALUE
data[:config].register(name.to_sym, &block)
# If we were told this is an upgrade safe configuration class
# then we add it to the set.
if upgrade_safe
data[:config_upgrade_safe] ||= Set.new
data[:config_upgrade_safe].add(name.to_sym)
end
end
# Return the registry
data[:config]
@ -218,18 +227,19 @@ module Vagrant
data[:provisioners]
end
protected
# Sentinel value denoting that a value has not been set.
UNSET_VALUE = Object.new
# Returns the internal data associated with this plugin.
# Returns the internal data associated with this plugin. This
# should NOT be called by the general public.
#
# @return [Hash]
def self.data
@data ||= {}
end
protected
# Sentinel value denoting that a value has not been set.
UNSET_VALUE = Object.new
# Helper method that will set a value if a value is given, or otherwise
# return the already set value.
#

View File

@ -45,6 +45,22 @@ describe Vagrant::Plugin::V1::Manager do
instance.config[:bar].should == "baz"
end
it "should enumerate registered upgrade safe config classes" do
pA = plugin do |p|
p.config("foo", true) { "bar" }
end
pB = plugin do |p|
p.config("bar") { "baz" }
end
instance.register(pA)
instance.register(pB)
instance.config_upgrade_safe.length.should == 1
instance.config_upgrade_safe[:foo].should == "bar"
end
it "should enumerate registered guest classes" do
pA = plugin do |p|
p.guest("foo") { "bar" }