From aa793367b7319892ac16097d8a41f9f48ea75af6 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sat, 1 Feb 2014 17:21:46 -0200 Subject: [PATCH] Extract cachier's root bucket directory creation out to a separate action [GH-60] --- .../action/configure_bucket_root.rb | 46 +++++++++++++++++++ lib/vagrant-cachier/plugin.rb | 5 ++ lib/vagrant-cachier/provision_ext.rb | 20 -------- 3 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 lib/vagrant-cachier/action/configure_bucket_root.rb diff --git a/lib/vagrant-cachier/action/configure_bucket_root.rb b/lib/vagrant-cachier/action/configure_bucket_root.rb new file mode 100644 index 0000000..70f275a --- /dev/null +++ b/lib/vagrant-cachier/action/configure_bucket_root.rb @@ -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 diff --git a/lib/vagrant-cachier/plugin.rb b/lib/vagrant-cachier/plugin.rb index 275e8c3..8d68e80 100644 --- a/lib/vagrant-cachier/plugin.rb +++ b/lib/vagrant-cachier/plugin.rb @@ -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 diff --git a/lib/vagrant-cachier/provision_ext.rb b/lib/vagrant-cachier/provision_ext.rb index e3c446c..4c0c384 100644 --- a/lib/vagrant-cachier/provision_ext.rb +++ b/lib/vagrant-cachier/provision_ext.rb @@ -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