diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b803fdb5..6191648f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ - `package` won't delete previously existing file. [GH-408] - Vagrantfile can be lowercase now. [GH-399] - Only one copy of Vagrant may be running at any given time. [GH-364] + - Default home directory for Vagrant moved to `~/.vagrant.d` [GH-333] ## 0.7.6 (July 2, 2011) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index b3126a269..0687db185 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -8,7 +8,7 @@ module Vagrant class Environment HOME_SUBDIRS = ["tmp", "boxes", "logs"] DEFAULT_VM = :default - DEFAULT_HOME = "~/.vagrant" + DEFAULT_HOME = "~/.vagrant.d" # Parent environment (in the case of multi-VMs) attr_reader :parent @@ -97,7 +97,29 @@ module Vagrant # # @return [Pathname] def home_path + return @_home_path if defined?(@_home_path) + @_home_path ||= Pathname.new(File.expand_path(ENV["VAGRANT_HOME"] || DEFAULT_HOME)) + + # This is the old default that Vagrant used to be put things into + # up until Vagrant 0.8.0. We keep around an automatic migration + # script here in case any old users upgrade. + old_home = File.expand_path("~/.vagrant") + if File.exists?(old_home) && File.directory?(old_home) + # We can't migrate if the home directory already exists + if File.exists?(@_home_path) + ui.warn I18n.t("vagrant.general.home_dir_migration_failed", + :old => old_home, + :new => @_home_path.to_s) + else + # If the new home path doesn't exist, simply transition to it + ui.info I18n.t("vagrant.general.moving_home_dir", :directory => @_home_path) + FileUtils.mv(old_home, @_home_path) + end + end + + # Return the home path + @_home_path end # The path to the Vagrant tmp directory diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index decc6d1af..59b0afef5 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -158,6 +158,11 @@ module Vagrant error_key(:environment_locked) end + class HomeDirectoryMigrationFailed < VagrantError + status_code(53) + error_key(:home_dir_migration_failed) + end + class ForwardPortAutolistEmpty < VagrantError status_code(27) error_key(:auto_empty, "vagrant.actions.vm.forward_ports") diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 5efb6bc73..cf4f29f9d 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -2,6 +2,14 @@ en: vagrant: general: creating_home_dir: "Creating home directory since it doesn't exist: %{directory}" + moving_home_dir: "Moving old Vagrant home directory to new location: %{directory}" + home_dir_migration_failed: |- + Both an old and new Vagrant home directory exist. Only the new one will + be used. Please merge the old directory into the new directory if you'd + like to use the old data as well. + + Old: %{old} + New: %{new} #------------------------------------------------------------------------------- # Translations for exception classes diff --git a/test/test_helper.rb b/test/test_helper.rb index c50dee490..93bb8fc62 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,6 +13,9 @@ begin rescue LoadError end +# Set the home directory to some temporary directory +ENV["HOME"] = Vagrant.source_root.join("test", "tmp", "home").to_s + # Add the I18n locale for tests I18n.load_path << File.expand_path("../locales/en.yml", __FILE__) diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index 1cef71619..5113f9c53 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -58,6 +58,12 @@ class EnvironmentTest < Test::Unit::TestCase context "home path" do setup do @env = @klass.new + + # Make a fake home directory for helping with tests + @home_path = tmp_path.join("home") + ENV["HOME"] = @home_path.to_s + FileUtils.rm_rf(@home_path) + FileUtils.mkdir_p(@home_path) end should "return the home path if it loaded" do @@ -73,6 +79,36 @@ class EnvironmentTest < Test::Unit::TestCase expected = Pathname.new(File.expand_path(ENV["VAGRANT_HOME"])) assert_equal expected, @env.home_path end + + should "move the old home directory to the new location" do + new_path = @home_path.join(".vagrant.d") + old_path = @home_path.join(".vagrant") + old_path.mkdir + + # Get the home path + ENV["VAGRANT_HOME"] = new_path.to_s + + assert !new_path.exist? + assert_equal new_path, @env.home_path + assert !old_path.exist? + assert new_path.exist? + end + + should "not move the old home directory if the new one already exists" do + new_path = @home_path.join(".vagrant.d") + new_path.mkdir + + old_path = @home_path.join(".vagrant") + old_path.mkdir + + # Get the home path + ENV["VAGRANT_HOME"] = new_path.to_s + + assert new_path.exist? + assert_equal new_path, @env.home_path + assert old_path.exist? + assert new_path.exist? + end end context "temp path" do