From 410872230b4271de6adcd396ef849eb4ed6f5b6b Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sun, 21 Jul 2013 19:46:22 -0300 Subject: [PATCH 1/4] Split actions into separate files --- lib/vagrant-cachier/action/clean.rb | 40 +++++++++++++++++++ .../{action.rb => action/install.rb} | 36 ----------------- lib/vagrant-cachier/plugin.rb | 4 +- 3 files changed, 42 insertions(+), 38 deletions(-) create mode 100644 lib/vagrant-cachier/action/clean.rb rename lib/vagrant-cachier/{action.rb => action/install.rb} (62%) diff --git a/lib/vagrant-cachier/action/clean.rb b/lib/vagrant-cachier/action/clean.rb new file mode 100644 index 0000000..cd1fc0b --- /dev/null +++ b/lib/vagrant-cachier/action/clean.rb @@ -0,0 +1,40 @@ +module VagrantPlugins + module Cachier + class Action + class Clean + def initialize(app, env) + @app = app + @logger = Log4r::Logger.new("vagrant::cachier::action::clean") + end + + def call(env) + @env = env + + if env[:machine].state.id == :running && symlinks.any? + env[:ui].info 'Removing cache buckets symlinks...' + symlinks.each do |symlink| + remove_symlink symlink + end + + File.delete env[:machine].data_dir.join('cache_dirs').to_s + end + + @app.call env + end + + def symlinks + # TODO: Check if file exists instead of a blank rescue + @symlinks ||= @env[:machine].data_dir.join('cache_dirs').read.split rescue [] + end + + def remove_symlink(symlink) + if @env[:machine].communicate.test("test -L #{symlink}") + @logger.debug "Removing symlink for '#{symlink}'" + @env[:machine].communicate.sudo("unlink #{symlink}") + end + end + end + end + end +end + diff --git a/lib/vagrant-cachier/action.rb b/lib/vagrant-cachier/action/install.rb similarity index 62% rename from lib/vagrant-cachier/action.rb rename to lib/vagrant-cachier/action/install.rb index 631da9b..2106146 100644 --- a/lib/vagrant-cachier/action.rb +++ b/lib/vagrant-cachier/action/install.rb @@ -1,5 +1,3 @@ -require_relative 'bucket' - module VagrantPlugins module Cachier class Action @@ -51,40 +49,6 @@ module VagrantPlugins end end end - - class Clean - def initialize(app, env) - @app = app - @logger = Log4r::Logger.new("vagrant::cachier::action::clean") - end - - def call(env) - @env = env - - if env[:machine].state.id == :running && symlinks.any? - env[:ui].info 'Removing cache buckets symlinks...' - symlinks.each do |symlink| - remove_symlink symlink - end - - File.delete env[:machine].data_dir.join('cache_dirs').to_s - end - - @app.call env - end - - def symlinks - # TODO: Check if file exists instead of a blank rescue - @symlinks ||= @env[:machine].data_dir.join('cache_dirs').read.split rescue [] - end - - def remove_symlink(symlink) - if @env[:machine].communicate.test("test -L #{symlink}") - @logger.debug "Removing symlink for '#{symlink}'" - @env[:machine].communicate.sudo("unlink #{symlink}") - end - end - end end end end diff --git a/lib/vagrant-cachier/plugin.rb b/lib/vagrant-cachier/plugin.rb index 83f879a..11b7ad7 100644 --- a/lib/vagrant-cachier/plugin.rb +++ b/lib/vagrant-cachier/plugin.rb @@ -39,14 +39,14 @@ module VagrantPlugins end install_action_hook = lambda do |hook| - require_relative 'action' + require_relative 'action/install' hook.after Vagrant::Action::Builtin::Provision, VagrantPlugins::Cachier::Action::Install end action_hook 'set-shared-cache-on-machine-up', :machine_action_up, &install_action_hook action_hook 'set-shared-cache-on-machine-reload', :machine_action_reload, &install_action_hook clean_action_hook = lambda do |hook| - require_relative 'action' + require_relative 'action/clean' hook.before Vagrant::Action::Builtin::GracefulHalt, VagrantPlugins::Cachier::Action::Clean end action_hook 'remove-guest-symlinks-on-machine-halt', :machine_action_halt, &clean_action_hook From e7912fa14a634759bf97e9a63cc2d2523b6deae6 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sun, 21 Jul 2013 20:46:20 -0300 Subject: [PATCH 2/4] Reconfigure buckets around after each provisioner Unfortunately I couldn't think of another way of doing this apart from monkey patching, there is no way to replace a previously defined action neither we can hook into calls to provisioners themselves. --- lib/vagrant-cachier/action/install.rb | 54 ---------------- lib/vagrant-cachier/action/provision_ext.rb | 70 +++++++++++++++++++++ lib/vagrant-cachier/plugin.rb | 12 ++-- 3 files changed, 75 insertions(+), 61 deletions(-) delete mode 100644 lib/vagrant-cachier/action/install.rb create mode 100644 lib/vagrant-cachier/action/provision_ext.rb diff --git a/lib/vagrant-cachier/action/install.rb b/lib/vagrant-cachier/action/install.rb deleted file mode 100644 index 2106146..0000000 --- a/lib/vagrant-cachier/action/install.rb +++ /dev/null @@ -1,54 +0,0 @@ -module VagrantPlugins - module Cachier - class Action - class Install - def initialize(app, env) - @app = app - @logger = Log4r::Logger.new("vagrant::cachier::action::install") - end - - def call(env) - return @app.call(env) unless env[:machine].config.cache.enabled? - - @env = env - - FileUtils.mkdir_p(cache_root.to_s) unless cache_root.exist? - - nfs_flag = env[:machine].config.cache.enable_nfs - env[:machine].config.vm.synced_folder cache_root, '/tmp/vagrant-cache', id: "vagrant-cache", nfs: nfs_flag - - @app.call env - - env[:cache_dirs] = [] - - if env[:machine].config.cache.auto_detect - Bucket.auto_detect(env) - end - - if 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| - @logger.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].join("\n") } - end - end - - def cache_root - @cache_root ||= case @env[:machine].config.cache.scope - when :box - @env[:home_path].join('cache', @env[:machine].box.name) - when :machine - @env[:machine].data_dir.join('cache') - else - raise "Unknown cache scope: '#{@env[:machine].config.cache.scope}'" - end - end - end - end - end -end diff --git a/lib/vagrant-cachier/action/provision_ext.rb b/lib/vagrant-cachier/action/provision_ext.rb new file mode 100644 index 0000000..d03a307 --- /dev/null +++ b/lib/vagrant-cachier/action/provision_ext.rb @@ -0,0 +1,70 @@ +require_relative '../bucket' + +module VagrantPlugins + module Cachier + module Action + 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) + return old_call(env) unless env[:machine].config.cache.enabled? + + @env = env + + FileUtils.mkdir_p(cache_root.to_s) unless cache_root.exist? + + nfs_flag = env[:machine].config.cache.enable_nfs + env[:machine].config.vm.synced_folder cache_root, '/tmp/vagrant-cache', id: "vagrant-cache", nfs: nfs_flag + + env[:cache_dirs] = [] + + 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 + 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 + + def cache_root + @cache_root ||= case @env[:machine].config.cache.scope + when :box + @env[:home_path].join('cache', @env[:machine].box.name) + when :machine + @env[:machine].data_dir.join('cache') + else + raise "Unknown cache scope: '#{@env[:machine].config.cache.scope}'" + end + end + end + end + end + end + end +end diff --git a/lib/vagrant-cachier/plugin.rb b/lib/vagrant-cachier/plugin.rb index 11b7ad7..c337090 100644 --- a/lib/vagrant-cachier/plugin.rb +++ b/lib/vagrant-cachier/plugin.rb @@ -1,3 +1,8 @@ +require_relative 'action/provision_ext' +Vagrant::Action::Builtin::Provision.class_eval do + include VagrantPlugins::Cachier::Action::ProvisionExt +end + module VagrantPlugins module Cachier class Plugin < Vagrant.plugin('2') @@ -38,13 +43,6 @@ module VagrantPlugins Cap::Arch::PacmanCacheDir end - install_action_hook = lambda do |hook| - require_relative 'action/install' - hook.after Vagrant::Action::Builtin::Provision, VagrantPlugins::Cachier::Action::Install - end - action_hook 'set-shared-cache-on-machine-up', :machine_action_up, &install_action_hook - action_hook 'set-shared-cache-on-machine-reload', :machine_action_reload, &install_action_hook - clean_action_hook = lambda do |hook| require_relative 'action/clean' hook.before Vagrant::Action::Builtin::GracefulHalt, VagrantPlugins::Cachier::Action::Clean From 175aba8b439198e736c3be24b718abfe6cc2d9b3 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sun, 21 Jul 2013 20:51:26 -0300 Subject: [PATCH 3/4] Update docs --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e14b574..4aad8b2 100644 --- a/README.md +++ b/README.md @@ -48,12 +48,12 @@ configure things properly from your `Vagrantfile`. Please have a look at the [available cache buckets](#available-cache-buckets) section below for more information. -Under the hood, the plugin will hook into calls to `Vagrant::Builtin::Provision` -during `vagrant up` / `vagrant reload` and will set things up for each configured -cache bucket. Before halting the machine, it will revert the changes required -to set things up by hooking into calls to `Vagrant::Builtin::GracefulHalt` so -that you can repackage the machine for others to use without requiring users to -install the plugin as well. +Under the hood, the plugin will monkey patch `Vagrant::Builtin::Provision` and +will set things up for each configured cache bucket before running each defined +provisioner and after all provisioners are done. Before halting the machine, +it will revert the changes required to set things up by hooking into calls to +`Vagrant::Builtin::GracefulHalt` so that you can repackage the machine for others +to use without requiring users to install the plugin as well. Cache buckets will be available from `/tmp/vagrant-cachier` on your guest and the appropriate folders will get symlinked to the right path _after_ the machine is From b07956ee3ede5643beac6ad6f885f820fdc47281 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sat, 3 Aug 2013 13:37:02 -0300 Subject: [PATCH 4/4] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b98fe8..c10ccc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ FEATURES: - Add `file_cache_path` support for Chef. [GH-14] + - Reconfigure buckets before each provisioner. [GH-26] / [GH-32] IMPROVEMENTS: