From 0f533a9b307179604a0bd9b042b15ffd64d2bcab Mon Sep 17 00:00:00 2001
From: Fabio Rehm <fgrehm@gmail.com>
Date: Sat, 1 Feb 2014 17:50:12 -0200
Subject: [PATCH] :bomb: monkey patch on Builtin::Provision action \o/ (closes
 GH-60)

---
 lib/vagrant-cachier/action/install_buckets.rb | 40 ++++++++++++++
 lib/vagrant-cachier/plugin.rb                 | 15 ++++--
 lib/vagrant-cachier/provision_ext.rb          | 52 -------------------
 3 files changed, 50 insertions(+), 57 deletions(-)
 create mode 100644 lib/vagrant-cachier/action/install_buckets.rb
 delete mode 100644 lib/vagrant-cachier/provision_ext.rb

diff --git a/lib/vagrant-cachier/action/install_buckets.rb b/lib/vagrant-cachier/action/install_buckets.rb
new file mode 100644
index 0000000..f96d947
--- /dev/null
+++ b/lib/vagrant-cachier/action/install_buckets.rb
@@ -0,0 +1,40 @@
+require_relative '../bucket'
+
+module VagrantPlugins
+  module Cachier
+    class Action
+      class InstallBuckets
+        def initialize(app, env)
+          @app = app
+        end
+
+        def call(env)
+          @app.call(env)
+
+          @env = env
+          configure_cache_buckets
+        end
+
+        def configure_cache_buckets
+          return unless @env[:machine].config.cache.enabled?
+
+          if @env[:machine].config.cache.auto_detect
+            Bucket.auto_detect(@env)
+          end
+
+          return unless @env[:machine].config.cache.buckets.any?
+
+          @env[:ui].info 'Configuring cache buckets...'
+          cache_config = @env[:machine].config.cache
+          cache_config.buckets.each do |bucket_name, configs|
+            # cachier_debug "Installing #{bucket_name} with configs #{configs.inspect}"
+            Bucket.install(bucket_name, @env, configs)
+          end
+
+          data_file = @env[:machine].data_dir.join('cache_dirs')
+          data_file.open('w') { |f| f.print @env[:cache_dirs].uniq.join("\n") }
+        end
+      end
+    end
+  end
+end
diff --git a/lib/vagrant-cachier/plugin.rb b/lib/vagrant-cachier/plugin.rb
index 8d68e80..7b20598 100644
--- a/lib/vagrant-cachier/plugin.rb
+++ b/lib/vagrant-cachier/plugin.rb
@@ -4,11 +4,6 @@ unless Gem::Requirement.new('>= 1.4.0').satisfied_by?(Gem::Version.new(Vagrant::
   raise 'vagrant-cachier requires Vagrant >= 1.4.0 in order to work!'
 end
 
-require_relative 'provision_ext'
-Vagrant::Action::Builtin::Provision.class_eval do
-  include VagrantPlugins::Cachier::ProvisionExt
-end
-
 # Add our custom translations to the load path
 I18n.load_path << File.expand_path("../../../locales/en.yml", __FILE__)
 
@@ -85,7 +80,17 @@ module VagrantPlugins
 
       action_hook ALL_ACTIONS do |hook|
         require_relative 'action/configure_bucket_root'
+        require_relative 'action/install_buckets'
+
         hook.before Vagrant::Action::Builtin::Provision, Action::ConfigureBucketRoot
+        # This will do the initial buckets installation
+        hook.after Vagrant::Action::Builtin::Provision, Action::InstallBuckets
+      end
+
+      # This ensure buckets are reconfigured after provisioners runs
+      action_hook :provisioner_run do |hook|
+        require_relative 'action/install_buckets'
+        hook.after :run_provisioner, Action::InstallBuckets
       end
     end
   end
diff --git a/lib/vagrant-cachier/provision_ext.rb b/lib/vagrant-cachier/provision_ext.rb
deleted file mode 100644
index 4c0c384..0000000
--- a/lib/vagrant-cachier/provision_ext.rb
+++ /dev/null
@@ -1,52 +0,0 @@
-require_relative 'bucket'
-
-module VagrantPlugins
-  module Cachier
-    module ProvisionExt
-      def self.included(base)
-        base.class_eval do
-          def cachier_debug(msg)
-            @logger.debug "[CACHIER] #{msg}"
-          end
-
-          alias :old_call :call
-          def call(env)
-            @env = env
-
-            return old_call(env) unless env[:machine].config.cache.enabled?
-
-            old_call(env)
-
-            configure_cache_buckets
-          end
-
-          alias :old_run_provisioner :run_provisioner
-          def run_provisioner(*args)
-            configure_cache_buckets
-            old_run_provisioner(*args)
-          end
-
-          def configure_cache_buckets
-            return unless @env[:machine].config.cache.enabled?
-
-            if @env[:machine].config.cache.auto_detect
-              Bucket.auto_detect(@env)
-            end
-
-            return unless @env[:machine].config.cache.buckets.any?
-
-            @env[:ui].info 'Configuring cache buckets...'
-            cache_config = @env[:machine].config.cache
-            cache_config.buckets.each do |bucket_name, configs|
-              cachier_debug "Installing #{bucket_name} with configs #{configs.inspect}"
-              Bucket.install(bucket_name, @env, configs)
-            end
-
-            data_file = @env[:machine].data_dir.join('cache_dirs')
-            data_file.open('w') { |f| f.print @env[:cache_dirs].uniq.join("\n") }
-          end
-        end
-      end
-    end
-  end
-end