Split actions into separate files

This commit is contained in:
Fabio Rehm 2013-07-21 19:46:22 -03:00
parent 0ab8e98862
commit 410872230b
3 changed files with 42 additions and 38 deletions

View 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

View file

@ -1,5 +1,3 @@
require_relative 'bucket'
module VagrantPlugins
module Cachier
class Action
@ -51,40 +49,6 @@ module VagrantPlugins
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

View file

@ -39,14 +39,14 @@ module VagrantPlugins
end
install_action_hook = lambda do |hook|
require_relative 'action'
require_relative 'action/install'
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|
require_relative 'action'
require_relative 'action/clean'
hook.before Vagrant::Action::Builtin::GracefulHalt, VagrantPlugins::Cachier::Action::Clean
end
action_hook 'remove-guest-symlinks-on-machine-halt', :machine_action_halt, &clean_action_hook