Extract cachier's root bucket directory creation out to a separate action [GH-60]

This commit is contained in:
Fabio Rehm 2014-02-01 17:21:46 -02:00
parent e01a78138a
commit aa793367b7
3 changed files with 51 additions and 20 deletions

View file

@ -0,0 +1,46 @@
require 'timeout'
module VagrantPlugins
module Cachier
class Action
class ConfigureBucketRoot
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant::cachier::action::clean")
end
def call(env)
@env = env
if !env[:cache_buckets_folder_configured] && env[:machine].config.cache.enabled?
setup_buckets_folder
env[:cache_buckets_folder_configured] = true
end
@app.call env
end
def setup_buckets_folder
FileUtils.mkdir_p(cache_root.to_s) unless cache_root.exist?
synced_folder_opts = {id: "vagrant-cache"}
synced_folder_opts.merge!(@env[:machine].config.cache.synced_folder_opts)
@env[:machine].config.vm.synced_folder cache_root, '/tmp/vagrant-cache', synced_folder_opts
@env[:cache_dirs] = []
end
def cache_root
@cache_root ||= case @env[:machine].config.cache.scope.to_sym
when :box
@env[:home_path].join('cache', @env[:machine].box.name)
when :machine
@env[:machine].data_dir.parent.join('cache')
else
raise "Unknown cache scope: '#{@env[:machine].config.cache.scope}'"
end
end
end
end
end
end

View file

@ -82,6 +82,11 @@ module VagrantPlugins
end
action_hook 'remove-guest-symlinks-on-halt', :machine_action_halt, &clean_action_hook
action_hook 'remove-guest-symlinks-on-package', :machine_action_package, &clean_action_hook
action_hook ALL_ACTIONS do |hook|
require_relative 'action/configure_bucket_root'
hook.before Vagrant::Action::Builtin::Provision, Action::ConfigureBucketRoot
end
end
end
end

View file

@ -15,15 +15,6 @@ module VagrantPlugins
return old_call(env) unless env[:machine].config.cache.enabled?
FileUtils.mkdir_p(cache_root.to_s) unless cache_root.exist?
synced_folder_opts = {id: "vagrant-cache"}
synced_folder_opts.merge!(env[:machine].config.cache.synced_folder_opts)
env[:machine].config.vm.synced_folder cache_root, '/tmp/vagrant-cache', synced_folder_opts
env[:cache_dirs] = []
old_call(env)
configure_cache_buckets
@ -54,17 +45,6 @@ module VagrantPlugins
data_file = @env[:machine].data_dir.join('cache_dirs')
data_file.open('w') { |f| f.print @env[:cache_dirs].uniq.join("\n") }
end
def cache_root
@cache_root ||= case @env[:machine].config.cache.scope.to_sym
when :box
@env[:home_path].join('cache', @env[:machine].box.name)
when :machine
@env[:machine].data_dir.parent.join('cache')
else
raise "Unknown cache scope: '#{@env[:machine].config.cache.scope}'"
end
end
end
end
end