From 24c1d3a8acf235849eed1a6d5865290f173cb198 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Thu, 17 Oct 2013 12:58:16 -0300 Subject: [PATCH 1/5] Clean up Action::Clean a bit and get ready to fix GH-8 --- lib/vagrant-cachier/action/clean.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/vagrant-cachier/action/clean.rb b/lib/vagrant-cachier/action/clean.rb index 84702d0..eb7341f 100644 --- a/lib/vagrant-cachier/action/clean.rb +++ b/lib/vagrant-cachier/action/clean.rb @@ -8,29 +8,34 @@ module VagrantPlugins end def call(env) - @env = env + @machine = env[:machine] - if env[:machine].state.id == :running && symlinks.any? + if should_remove_symlinks? env[:ui].info I18n.t('vagrant_cachier.cleanup') symlinks.each do |symlink| remove_symlink symlink end - File.delete env[:machine].data_dir.join('cache_dirs').to_s + File.delete @machine.data_dir.join('cache_dirs').to_s 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 + def symlinks # TODO: Check if file exists instead of a blank rescue - @symlinks ||= @env[:machine].data_dir.join('cache_dirs').read.split rescue [] + @symlinks ||= @machine.data_dir.join('cache_dirs').read.split rescue [] end def remove_symlink(symlink) - if @env[:machine].communicate.test("test -L #{symlink}") + if @machine.communicate.test("test -L #{symlink}") @logger.debug "Removing symlink for '#{symlink}'" - @env[:machine].communicate.sudo("unlink #{symlink}") + @machine.communicate.sudo("unlink #{symlink}") end end end From cfa2cc262f3d74a52cac0647aa9ab0b427705f8b Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Thu, 17 Oct 2013 13:07:43 -0300 Subject: [PATCH 2/5] Fix GH-8 by doing an extra check whether the VM is SSHable --- lib/vagrant-cachier/action/clean.rb | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/vagrant-cachier/action/clean.rb b/lib/vagrant-cachier/action/clean.rb index eb7341f..e25d64f 100644 --- a/lib/vagrant-cachier/action/clean.rb +++ b/lib/vagrant-cachier/action/clean.rb @@ -1,3 +1,5 @@ +require 'timeout' + module VagrantPlugins module Cachier class Action @@ -24,7 +26,27 @@ module VagrantPlugins def should_remove_symlinks? @logger.info 'Checking if cache symlinks should be removed' - symlinks.any? && @machine.state.id == :running + symlinks.any? && @machine.state.id == :running && sshable? + end + + def sshable? + # 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 + # We timed out, we failed. + end + + return false end def symlinks From 9ebdc24da4bc99d49a0f7b289b68a589c7f6b87b Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Thu, 17 Oct 2013 13:18:10 -0300 Subject: [PATCH 3/5] Bundle update --- Gemfile.lock | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 4f05fdf..ffc6056 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,26 +1,26 @@ GIT remote: git://github.com/fgrehm/vagrant-global-status.git - revision: 860a7ce70ff4596621af492e47eace5631f47324 + revision: a0295400a0e47756cbcb8f97ed9f4449b1fb6b56 specs: - vagrant-global-status (0.1.0) + vagrant-global-status (0.1.1) GIT remote: git://github.com/fgrehm/vagrant-lxc.git - revision: 5ae82681cdedaf2a6f778e005dd65014fc7c2cec + revision: 225af5622767059708278db29d42941e93d41994 specs: - vagrant-lxc (0.5.1.dev) + vagrant-lxc (0.6.4.dev) GIT remote: git://github.com/fgrehm/vagrant-pristine.git - revision: 5c400d7850fc5f98d9601b59f4c3bd74818650de + revision: 4638491786943bfbf6f115b1fc379f069963fe46 specs: - vagrant-pristine (0.2.0) + vagrant-pristine (0.3.0) GIT remote: git://github.com/mitchellh/vagrant.git - revision: 16002d03c07f842a23497543129fa99f40f2bbc0 + revision: 57e95323b6600b146167f0f14f83b22dd31dd03f specs: - vagrant (1.2.8.dev) + vagrant (1.3.6.dev) childprocess (~> 0.3.7) erubis (~> 2.7.0) i18n (~> 0.6.0) @@ -56,7 +56,7 @@ GEM erubis (2.7.0) ffi (1.9.0) highline (1.6.19) - i18n (0.6.4) + i18n (0.6.5) ipaddress (0.8.0) json (1.7.7) librarian (0.1.0) @@ -97,7 +97,7 @@ GEM thor (0.18.1) vagrant-librarian-chef (0.1.2) librarian-chef - vagrant-omnibus (1.1.0) + vagrant-omnibus (1.1.2) yajl-ruby (1.1.0) PLATFORMS From 199a14b61ffd11350bffee40b204a9e3eb560b73 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Thu, 17 Oct 2013 13:56:27 -0300 Subject: [PATCH 4/5] Let users know we were not able to SSH into the VM to remove symlinks --- lib/vagrant-cachier/action/clean.rb | 3 ++- locales/en.yml | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/vagrant-cachier/action/clean.rb b/lib/vagrant-cachier/action/clean.rb index e25d64f..9b7bf25 100644 --- a/lib/vagrant-cachier/action/clean.rb +++ b/lib/vagrant-cachier/action/clean.rb @@ -10,6 +10,7 @@ module VagrantPlugins end def call(env) + @env = env @machine = env[:machine] if should_remove_symlinks? @@ -43,7 +44,7 @@ module VagrantPlugins end end rescue Timeout::Error - # We timed out, we failed. + @env[:ui].warn(I18n.t('vagrant_cachier.unable_to_ssh')) end return false diff --git a/locales/en.yml b/locales/en.yml index b54004d..b8147ad 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -11,6 +11,8 @@ en: '%{current_path}' and it will be moved to '%{new_path}' as it is the new path for keeping machine scoped cache dirs starting with the 0.3.0 version of the plugin. + unable_to_ssh: |- + vagrant-cachier was unable to SSH into the VM to remove symlinks! vagrant: errors: multiple_provider_specific_cache_dirs_found: |- From cfabddc7c855fefd3adc9f27e7482b64c96c5c46 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Thu, 17 Oct 2013 14:04:09 -0300 Subject: [PATCH 5/5] Let users know we will attempt to remove symlinks, otherwise vagrant won't output anything prior to the warn --- lib/vagrant-cachier/action/clean.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/vagrant-cachier/action/clean.rb b/lib/vagrant-cachier/action/clean.rb index 9b7bf25..a2015bf 100644 --- a/lib/vagrant-cachier/action/clean.rb +++ b/lib/vagrant-cachier/action/clean.rb @@ -13,10 +13,12 @@ module VagrantPlugins @env = env @machine = env[:machine] - if should_remove_symlinks? + if symlinks.any? env[:ui].info I18n.t('vagrant_cachier.cleanup') - symlinks.each do |symlink| - remove_symlink symlink + if sshable? + symlinks.each do |symlink| + remove_symlink symlink + end end File.delete @machine.data_dir.join('cache_dirs').to_s @@ -25,12 +27,9 @@ module VagrantPlugins @app.call env end - def should_remove_symlinks? - @logger.info 'Checking if cache symlinks should be removed' - symlinks.any? && @machine.state.id == :running && sshable? - end - 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