Merge pull request #32 from fgrehm/26-reconfigure-buckets-before-each-provisioner
Reconfigure buckets before each provisioner
This commit is contained in:
commit
5f30e3c9da
6 changed files with 123 additions and 104 deletions
|
@ -3,6 +3,7 @@
|
||||||
FEATURES:
|
FEATURES:
|
||||||
|
|
||||||
- Add `file_cache_path` support for Chef. [GH-14]
|
- Add `file_cache_path` support for Chef. [GH-14]
|
||||||
|
- Reconfigure buckets before each provisioner. [GH-26] / [GH-32]
|
||||||
|
|
||||||
IMPROVEMENTS:
|
IMPROVEMENTS:
|
||||||
|
|
||||||
|
|
12
README.md
12
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
|
the [available cache buckets](#available-cache-buckets) section below for more
|
||||||
information.
|
information.
|
||||||
|
|
||||||
Under the hood, the plugin will hook into calls to `Vagrant::Builtin::Provision`
|
Under the hood, the plugin will monkey patch `Vagrant::Builtin::Provision` and
|
||||||
during `vagrant up` / `vagrant reload` and will set things up for each configured
|
will set things up for each configured cache bucket before running each defined
|
||||||
cache bucket. Before halting the machine, it will revert the changes required
|
provisioner and after all provisioners are done. Before halting the machine,
|
||||||
to set things up by hooking into calls to `Vagrant::Builtin::GracefulHalt` so
|
it will revert the changes required to set things up by hooking into calls to
|
||||||
that you can repackage the machine for others to use without requiring users to
|
`Vagrant::Builtin::GracefulHalt` so that you can repackage the machine for others
|
||||||
install the plugin as well.
|
to use without requiring users to install the plugin as well.
|
||||||
|
|
||||||
Cache buckets will be available from `/tmp/vagrant-cachier` on your guest and
|
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
|
the appropriate folders will get symlinked to the right path _after_ the machine is
|
||||||
|
|
|
@ -1,90 +0,0 @@
|
||||||
require_relative 'bucket'
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
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
|
|
40
lib/vagrant-cachier/action/clean.rb
Normal file
40
lib/vagrant-cachier/action/clean.rb
Normal file
|
@ -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
|
||||||
|
|
70
lib/vagrant-cachier/action/provision_ext.rb
Normal file
70
lib/vagrant-cachier/action/provision_ext.rb
Normal file
|
@ -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
|
|
@ -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 VagrantPlugins
|
||||||
module Cachier
|
module Cachier
|
||||||
class Plugin < Vagrant.plugin('2')
|
class Plugin < Vagrant.plugin('2')
|
||||||
|
@ -38,15 +43,8 @@ module VagrantPlugins
|
||||||
Cap::Arch::PacmanCacheDir
|
Cap::Arch::PacmanCacheDir
|
||||||
end
|
end
|
||||||
|
|
||||||
install_action_hook = lambda do |hook|
|
|
||||||
require_relative 'action'
|
|
||||||
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|
|
clean_action_hook = lambda do |hook|
|
||||||
require_relative 'action'
|
require_relative 'action/clean'
|
||||||
hook.before Vagrant::Action::Builtin::GracefulHalt, VagrantPlugins::Cachier::Action::Clean
|
hook.before Vagrant::Action::Builtin::GracefulHalt, VagrantPlugins::Cachier::Action::Clean
|
||||||
end
|
end
|
||||||
action_hook 'remove-guest-symlinks-on-machine-halt', :machine_action_halt, &clean_action_hook
|
action_hook 'remove-guest-symlinks-on-machine-halt', :machine_action_halt, &clean_action_hook
|
||||||
|
|
Loading…
Reference in a new issue