vagrant-cachier-ng/lib/vagrant-cachier/action/configure_bucket_root.rb
Aneesh Agrawal 0733ad5afe Add config.cache.root option to specify a custom cache root.
Only available for single box environments.
Requires that the custom cache root already exists - because it is
custom, it is your responsbility instead of vagrant-cachier's.
Fixes #87.
2015-05-11 05:51:53 -04:00

82 lines
2.6 KiB
Ruby

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
custom_root = @env[:machine].config.cache.root
if custom_root != nil
case @env[:machine].config.cache.scope.to_sym
when :box
unless File.directory? custom_root
raise "Custom cache root must already exist and be a directory"
end
else
raise "Custom cache root only supported in single box environments"
end
else
FileUtils.mkdir_p(cache_root.to_s) unless cache_root.exist?
end
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
custom_root = @env[:machine].config.cache.root
return custom_root unless custom_root == nil
@box_name = box_name
# Box is optional with docker provider
if @box_name.nil? && @env[:machine].provider_name.to_sym == :docker
@image_name = image_name
# Use the image name if it's set
if @image_name
bucket_name = @image_name.gsub(':', '-')
else
raise "Cachier plugin only supported with docker provider when image is used"
end
else
bucket_name = @box_name
end
@env[:home_path].join('cache', bucket_name)
when :machine
@env[:machine].data_dir.parent.join('cache')
else
raise "Unknown cache scope: '#{@env[:machine].config.cache.scope}'"
end
end
def box_name
@env[:machine].config.vm.box
end
def image_name
@env[:machine].provider_config.image
end
end
end
end
end