From f8d8b2f9cf5f94151f49cbfef321a136ce88a2fe Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sat, 3 Aug 2013 16:07:42 -0300 Subject: [PATCH] [GH-6] Error out if multiple provider specific cache dirs are found and prepare to move dirs around in case a single cache dir exists --- .../action/ensure_single_cache_root.rb | 46 ++++++++++++++++++- lib/vagrant-cachier/errors.rb | 9 ++++ lib/vagrant-cachier/plugin.rb | 4 +- lib/vagrant-cachier/provision_ext.rb | 2 +- locales/en.yml | 12 +++++ 5 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 lib/vagrant-cachier/errors.rb diff --git a/lib/vagrant-cachier/action/ensure_single_cache_root.rb b/lib/vagrant-cachier/action/ensure_single_cache_root.rb index e4aa8c6..6834780 100644 --- a/lib/vagrant-cachier/action/ensure_single_cache_root.rb +++ b/lib/vagrant-cachier/action/ensure_single_cache_root.rb @@ -1,3 +1,5 @@ +require_relative '../errors' + module VagrantPlugins module Cachier class Action @@ -7,9 +9,51 @@ module VagrantPlugins end def call(env) - raise 'Do our work' + @env = env + + # If the cache is scoped to boxes or the existing cache dirs are not + # provider specific, there's nothing we need to do + if cache_scoped_to_machine? && provider_specific_cache_dirs.any? + ensure_single_cache_root_exists! + end + @app.call(env) end + + def cache_scoped_to_machine? + @env[:machine].config.cache.scope.to_sym == :machine + end + + def ensure_single_cache_root_exists! + if provider_specific_cache_dirs.size > 1 + cache_dirs = provider_specific_cache_dirs.map do |dir| + " - #{dir.to_s.gsub(/^#{@env[:root_path]}\//, '')}" + end + machine_path = @env[:machine].data_dir.parent.to_s.gsub(/^#{@env[:root_path]}\//, '') + raise Cachier::Errors::MultipleProviderSpecificCacheDirsFound, + machine: @env[:machine].name, + machine_path: machine_path, + dirs: cache_dirs.join("\n") + else + current_path = provider_specific_cache_dirs.first.to_s.gsub(/^#{@env[:root_path]}\//, '') + new_path = @env[:machine].data_dir.parent.join('cache').to_s.gsub(/^#{@env[:root_path]}\//, '') + # If we got here there is a single provider specific cacher dir, so + # let's be nice with users and just fix it ;) + @env[:ui].warn I18n.t('vagrant_cachier.will_fix_machine_cache_dir', + current_path: current_path, + new_path: new_path) + raise 'Move cache dir around' + end + end + + def provider_specific_cache_dirs + return @provider_specific_cache_dirs if @provider_specific_cache_dirs + + # By default data_dir points to ./.vagrant/machines//, + # so we go one directory up + machine_dir = @env[:machine].data_dir.parent + @provider_specific_cache_dirs = Pathname.glob(machine_dir.join('*/cache')) + end end end end diff --git a/lib/vagrant-cachier/errors.rb b/lib/vagrant-cachier/errors.rb new file mode 100644 index 0000000..0f11696 --- /dev/null +++ b/lib/vagrant-cachier/errors.rb @@ -0,0 +1,9 @@ +module VagrantPlugins + module Cachier + module Errors + class MultipleProviderSpecificCacheDirsFound < Vagrant::Errors::VagrantError + error_key(:multiple_provider_specific_cache_dirs_found) + end + end + end +end diff --git a/lib/vagrant-cachier/plugin.rb b/lib/vagrant-cachier/plugin.rb index c6f2a10..b24df90 100644 --- a/lib/vagrant-cachier/plugin.rb +++ b/lib/vagrant-cachier/plugin.rb @@ -50,12 +50,12 @@ module VagrantPlugins # possible provider action class that Vagrant might have ensure_single_cache_root = lambda do |hook| require_relative 'action/ensure_single_cache_root' - hook.after VagrantPlugins::ProviderVirtualBox::Action::Boot, Action::EnsureSingleCacheRoot + hook.before VagrantPlugins::ProviderVirtualBox::Action::Boot, Action::EnsureSingleCacheRoot if defined?(Vagrant::LXC) # TODO: Require just the boot action file once its "require dependencies" are sorted out require 'vagrant-lxc/action' - hook.after Vagrant::LXC::Action::Boot, Action::EnsureSingleCacheRoot + hook.before Vagrant::LXC::Action::Boot, Action::EnsureSingleCacheRoot end end action_hook 'ensure-single-cache-root-exists-on-up', :machine_action_up, &ensure_single_cache_root diff --git a/lib/vagrant-cachier/provision_ext.rb b/lib/vagrant-cachier/provision_ext.rb index ee14d10..21e094e 100644 --- a/lib/vagrant-cachier/provision_ext.rb +++ b/lib/vagrant-cachier/provision_ext.rb @@ -56,7 +56,7 @@ module VagrantPlugins when :box @env[:home_path].join('cache', @env[:machine].box.name) when :machine - @env[:machine].data_dir.join('cache') + @env[:machine].data_dir.parent.join('cache') else raise "Unknown cache scope: '#{@env[:machine].config.cache.scope}'" end diff --git a/locales/en.yml b/locales/en.yml index ec6d39a..fe5dd5b 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -6,3 +6,15 @@ en: Skipping %{bucket} cache bucket as the guest machine does not support it unknown_cache_scope: |- Unknown cache scope '%{cache_scope}' (allowed scopes: %{allowed}) + will_fix_machine_cache_dir: |- + A vagrant-cachier provider specific cache dir was found under + '%{current_path}' and it will be + moved to '%{new_path}' as it is the new path for keeping machine + scoped cache dirs starting with the 0.3.0 version of the plugin. + vagrant: + errors: + multiple_provider_specific_cache_dirs_found: |- + There are multiple provider specific cache dirs for the '%{machine}' machine: + %{dirs} + Please move one of them up to `%{machine_path}/cache` and remove the others + before bringing the machine up again.