[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

This commit is contained in:
Fabio Rehm 2013-08-03 16:07:42 -03:00
parent 7ad195776d
commit f8d8b2f9cf
5 changed files with 69 additions and 4 deletions

View file

@ -1,3 +1,5 @@
require_relative '../errors'
module VagrantPlugins module VagrantPlugins
module Cachier module Cachier
class Action class Action
@ -7,9 +9,51 @@ module VagrantPlugins
end end
def call(env) 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) @app.call(env)
end 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/<NAME>/<PROVIDER>,
# 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 end
end end

View file

@ -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

View file

@ -50,12 +50,12 @@ module VagrantPlugins
# possible provider action class that Vagrant might have # possible provider action class that Vagrant might have
ensure_single_cache_root = lambda do |hook| ensure_single_cache_root = lambda do |hook|
require_relative 'action/ensure_single_cache_root' 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) if defined?(Vagrant::LXC)
# TODO: Require just the boot action file once its "require dependencies" are sorted out # TODO: Require just the boot action file once its "require dependencies" are sorted out
require 'vagrant-lxc/action' require 'vagrant-lxc/action'
hook.after Vagrant::LXC::Action::Boot, Action::EnsureSingleCacheRoot hook.before Vagrant::LXC::Action::Boot, Action::EnsureSingleCacheRoot
end end
end end
action_hook 'ensure-single-cache-root-exists-on-up', :machine_action_up, &ensure_single_cache_root action_hook 'ensure-single-cache-root-exists-on-up', :machine_action_up, &ensure_single_cache_root

View file

@ -56,7 +56,7 @@ module VagrantPlugins
when :box when :box
@env[:home_path].join('cache', @env[:machine].box.name) @env[:home_path].join('cache', @env[:machine].box.name)
when :machine when :machine
@env[:machine].data_dir.join('cache') @env[:machine].data_dir.parent.join('cache')
else else
raise "Unknown cache scope: '#{@env[:machine].config.cache.scope}'" raise "Unknown cache scope: '#{@env[:machine].config.cache.scope}'"
end end

View file

@ -6,3 +6,15 @@ en:
Skipping %{bucket} cache bucket as the guest machine does not support it Skipping %{bucket} cache bucket as the guest machine does not support it
unknown_cache_scope: |- unknown_cache_scope: |-
Unknown cache scope '%{cache_scope}' (allowed scopes: %{allowed}) 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.