Class: VagrantPlugins::Cachier::Action::Clean

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-cachier/action/clean.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Clean

Returns a new instance of Clean.



7
8
9
10
# File 'lib/vagrant-cachier/action/clean.rb', line 7

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant::cachier::action::clean")
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vagrant-cachier/action/clean.rb', line 12

def call(env)
  @env     = env
  @machine = env[:machine]

  if symlinks.any?
    env[:ui].info I18n.t('vagrant_cachier.cleanup')
    if sshable?
      symlinks.each do |symlink|
        remove_symlink symlink
      end
    end

    File.delete @machine.data_dir.join('cache_dirs').to_s
  end

  @app.call env
end


57
58
59
60
61
62
# File 'lib/vagrant-cachier/action/clean.rb', line 57

def remove_symlink(symlink)
  if @machine.communicate.test("test -L #{symlink}")
    @logger.info "Removing symlink for '#{symlink}'"
    @machine.communicate.sudo("unlink #{symlink}")
  end
end

#sshable?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant-cachier/action/clean.rb', line 30

def sshable?
  return if @machine.state.id != :running

  # By default Vagrant will keep trying [1] to ssh connect to the VM for
  # a long and we've got to prevent that from happening, so we just wait
  # a few seconds and assume that the VM is halted / unresponsive and we
  # carry on if it times out.
  #   [1] - https://github.com/mitchellh/vagrant/blob/57e95323b6600b146167f0f14f83b22dd31dd03f/plugins/communicators/ssh/communicator.rb#L185-L200
  begin
    Timeout.timeout(35) do
      while true
        return true if @machine.communicate.ready?
        sleep 0.5
      end
    end
  rescue Timeout::Error
    @env[:ui].warn(I18n.t('vagrant_cachier.unable_to_ssh'))
  end

  return false
end


52
53
54
55
# File 'lib/vagrant-cachier/action/clean.rb', line 52

def symlinks
  # TODO: Check if file exists instead of a blank rescue
  @symlinks ||= @machine.data_dir.join('cache_dirs').read.split rescue []
end