vagrant-cachier-ng/lib/vagrant-cachier/action/clean.rb

45 lines
1.2 KiB
Ruby
Raw Normal View History

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)
@machine = env[:machine]
2013-07-21 22:46:22 +00:00
if should_remove_symlinks?
env[:ui].info I18n.t('vagrant_cachier.cleanup')
2013-07-21 22:46:22 +00:00
symlinks.each do |symlink|
remove_symlink symlink
end
File.delete @machine.data_dir.join('cache_dirs').to_s
2013-07-21 22:46:22 +00:00
end
@app.call env
end
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
@symlinks ||= @machine.data_dir.join('cache_dirs').read.split rescue []
2013-07-21 22:46:22 +00:00
end
def remove_symlink(symlink)
if @machine.communicate.test("test -L #{symlink}")
2013-07-21 22:46:22 +00:00
@logger.debug "Removing symlink for '#{symlink}'"
@machine.communicate.sudo("unlink #{symlink}")
2013-07-21 22:46:22 +00:00
end
end
end
end
end
end