2013-08-03 17:30:39 +00:00
|
|
|
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
|
|
|
|
|
2013-08-14 01:41:25 +00:00
|
|
|
return old_call(env) unless env[:machine].config.cache.enabled?
|
|
|
|
|
2013-08-03 17:30:39 +00:00
|
|
|
FileUtils.mkdir_p(cache_root.to_s) unless cache_root.exist?
|
|
|
|
|
2013-12-18 05:19:15 +00:00
|
|
|
synced_folder_opts = {id: "vagrant-cache"}
|
2014-01-25 04:20:39 +00:00
|
|
|
synced_folder_opts.merge!(env[:machine].config.cache.sync_opts)
|
|
|
|
|
2013-12-18 05:19:15 +00:00
|
|
|
if env[:machine].config.cache.enable_nfs
|
|
|
|
# REFACTOR: Drop the `nfs: true` argument once we drop support for Vagrant < 1.4
|
|
|
|
synced_folder_opts.merge!({ nfs: true, type: 'nfs' })
|
|
|
|
end
|
|
|
|
env[:machine].config.vm.synced_folder cache_root, '/tmp/vagrant-cache', synced_folder_opts
|
2013-08-03 17:30:39 +00:00
|
|
|
|
|
|
|
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
|
2013-08-06 20:31:55 +00:00
|
|
|
return unless @env[:machine].config.cache.enabled?
|
|
|
|
|
2013-08-03 17:30:39 +00:00
|
|
|
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
|
2013-08-03 17:36:29 +00:00
|
|
|
@cache_root ||= case @env[:machine].config.cache.scope.to_sym
|
2013-08-03 17:30:39 +00:00
|
|
|
when :box
|
|
|
|
@env[:home_path].join('cache', @env[:machine].box.name)
|
|
|
|
when :machine
|
2013-08-03 19:07:42 +00:00
|
|
|
@env[:machine].data_dir.parent.join('cache')
|
2013-08-03 17:30:39 +00:00
|
|
|
else
|
|
|
|
raise "Unknown cache scope: '#{@env[:machine].config.cache.scope}'"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|