2013-07-21 22:46:22 +00:00
|
|
|
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)
|
2013-10-17 15:58:16 +00:00
|
|
|
@machine = env[:machine]
|
2013-07-21 22:46:22 +00:00
|
|
|
|
2013-10-17 15:58:16 +00:00
|
|
|
if should_remove_symlinks?
|
2013-08-03 17:28:22 +00:00
|
|
|
env[:ui].info I18n.t('vagrant_cachier.cleanup')
|
2013-07-21 22:46:22 +00:00
|
|
|
symlinks.each do |symlink|
|
|
|
|
remove_symlink symlink
|
|
|
|
end
|
|
|
|
|
2013-10-17 15:58:16 +00:00
|
|
|
File.delete @machine.data_dir.join('cache_dirs').to_s
|
2013-07-21 22:46:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@app.call env
|
|
|
|
end
|
|
|
|
|
2013-10-17 15:58:16 +00:00
|
|
|
def should_remove_symlinks?
|
|
|
|
@logger.info 'Checking if cache symlinks should be removed'
|
|
|
|
symlinks.any? && @machine.state.id == :running
|
|
|
|
end
|
|
|
|
|
2013-07-21 22:46:22 +00:00
|
|
|
def symlinks
|
|
|
|
# TODO: Check if file exists instead of a blank rescue
|
2013-10-17 15:58:16 +00:00
|
|
|
@symlinks ||= @machine.data_dir.join('cache_dirs').read.split rescue []
|
2013-07-21 22:46:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_symlink(symlink)
|
2013-10-17 15:58:16 +00:00
|
|
|
if @machine.communicate.test("test -L #{symlink}")
|
2013-07-21 22:46:22 +00:00
|
|
|
@logger.debug "Removing symlink for '#{symlink}'"
|
2013-10-17 15:58:16 +00:00
|
|
|
@machine.communicate.sudo("unlink #{symlink}")
|
2013-07-21 22:46:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|