diff --git a/lib/vagrant/plugin/v1/config.rb b/lib/vagrant/plugin/v1/config.rb index 5a6e7f309..7d5e8b243 100644 --- a/lib/vagrant/plugin/v1/config.rb +++ b/lib/vagrant/plugin/v1/config.rb @@ -5,6 +5,13 @@ module Vagrant # V1. Any configuration key plugins for V1 should inherit from this # class. class Config + # This constant represents an unset value. This is useful so it is + # possible to know the difference between a configuration value that + # was never set, and a value that is nil (explicitly). Best practice + # is to initialize all variables to this value, then the {#merge} + # method below will "just work" in many cases. + UNSET_VALUE = Object.new + # This is called as a last-minute hook that allows the configuration # object to finalize itself before it will be put into use. This is # a useful place to do some defaults in the case the user didn't diff --git a/plugins/kernel_v1/config/nfs.rb b/plugins/kernel_v1/config/nfs.rb index 56041ba1a..2faae956a 100644 --- a/plugins/kernel_v1/config/nfs.rb +++ b/plugins/kernel_v1/config/nfs.rb @@ -5,6 +5,16 @@ module VagrantPlugins class NFSConfig < Vagrant.plugin("1", :config) attr_accessor :map_uid attr_accessor :map_gid + + def initialize + @map_uid = UNSET_VALUE + @map_gid = UNSET_VALUE + end + + def upgrade(new) + new.nfs.map_uid = @map_uid if @map_uid != UNSET_VALUE + new.nfs.map_gid = @map_gid if @map_gid != UNSET_VALUE + end end end end diff --git a/plugins/kernel_v1/config/package.rb b/plugins/kernel_v1/config/package.rb index 0f4c1d235..3b521d785 100644 --- a/plugins/kernel_v1/config/package.rb +++ b/plugins/kernel_v1/config/package.rb @@ -4,6 +4,14 @@ module VagrantPlugins module Kernel_V1 class PackageConfig < Vagrant.plugin("1", :config) attr_accessor :name + + def initialize + @name = UNSET_VALUE + end + + def upgrade(new) + new.package.name = @name if @name != UNSET_VALUE + end end end end diff --git a/plugins/kernel_v1/config/ssh.rb b/plugins/kernel_v1/config/ssh.rb index d6719a105..d6fdf159f 100644 --- a/plugins/kernel_v1/config/ssh.rb +++ b/plugins/kernel_v1/config/ssh.rb @@ -15,15 +15,32 @@ module VagrantPlugins attr_accessor :forward_x11 attr_accessor :shell - def validate(env, errors) - [:username, :host, :max_tries, :timeout].each do |field| - value = instance_variable_get("@#{field}".to_sym) - errors.add(I18n.t("vagrant.config.common.error_empty", :field => field)) if !value - end + def initialize + @username = UNSET_VALUE + @password = UNSET_VALUE + @host = UNSET_VALUE + @port = UNSET_VALUE + @guest_port = UNSET_VALUE + @max_tries = UNSET_VALUE + @timeout = UNSET_VALUE + @private_key_path = UNSET_VALUE + @forward_agent = UNSET_VALUE + @forward_x11 = UNSET_VALUE + @shell = UNSET_VALUE + end - if private_key_path && !File.file?(File.expand_path(private_key_path, env.root_path)) - errors.add(I18n.t("vagrant.config.ssh.private_key_missing", :path => private_key_path)) - end + def upgrade(new) + new.ssh.username = @username if @username != UNSET_VALUE + new.ssh.password = @password if @password != UNSET_VALUE + new.ssh.host = @host if @host != UNSET_VALUE + new.ssh.port = @port if @port != UNSET_VALUE + new.ssh.guest_port = @guest_port if @guest_port != UNSET_VALUE + new.ssh.max_tries = @max_tries if @max_tries != UNSET_VALUE + new.ssh.timeout = @timeout if @timeout != UNSET_VALUE + new.ssh.private_key_path = @private_key_path if @private_key_path != UNSET_VALUE + new.ssh.forward_agent = @forward_agent if @forward_agent != UNSET_VALUE + new.ssh.forward_x11 = @forward_x11 if @forward_x11 != UNSET_VALUE + new.ssh.shell = @shell if @shell != UNSET_VALUE end end end diff --git a/plugins/kernel_v1/config/vagrant.rb b/plugins/kernel_v1/config/vagrant.rb index ed3eee2c7..b9730a056 100644 --- a/plugins/kernel_v1/config/vagrant.rb +++ b/plugins/kernel_v1/config/vagrant.rb @@ -5,6 +5,16 @@ module VagrantPlugins class VagrantConfig < Vagrant.plugin("1", :config) attr_accessor :dotfile_name attr_accessor :host + + def initialize + @dotfile_name = UNSET_VALUE + @host = UNSET_VALUE + end + + def upgrade(new) + new.vagrant.dotfile_name = @dotfile_name if @dotfile_name != UNSET_VALUE + new.vagrant.host = @host if @host != UNSET_VALUE + end end end end diff --git a/test/unit/vagrant/plugin/v1/config_test.rb b/test/unit/vagrant/plugin/v1/config_test.rb index 32e08c518..425984794 100644 --- a/test/unit/vagrant/plugin/v1/config_test.rb +++ b/test/unit/vagrant/plugin/v1/config_test.rb @@ -10,6 +10,12 @@ describe Vagrant::Plugin::V1::Config do end end + it "has an UNSET_VALUE constant" do + value = described_class.const_get("UNSET_VALUE") + value.should be_kind_of Object + value.should eql(described_class.const_get("UNSET_VALUE")) + end + describe "merging" do it "should merge by default by simply copying each instance variable" do one = foo_class.new