From b190f45f45c4397c9902916e00ebae6fdb40ec23 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Wed, 8 May 2013 20:13:17 -0300 Subject: [PATCH] Remove `/tmp` files after machine shutdown Fixes #68 --- CHANGELOG.md | 1 + lib/vagrant-lxc/action.rb | 4 +++- .../action/remove_temporary_files.rb | 23 +++++++++++++++++++ spec/acceptance/sanity_check_spec.rb | 6 +++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 lib/vagrant-lxc/action/remove_temporary_files.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e30d1a..65a4e65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ FEATURES: IMPROVEMENTS: + - Remove `/tmp` files after the machine has been successfully shut down [#68][] - Clean up base boxes files after they've been configured, resulting in smaller packages - Bump development dependency to Vagrant 1.2+ series diff --git a/lib/vagrant-lxc/action.rb b/lib/vagrant-lxc/action.rb index 145014c..a025936 100644 --- a/lib/vagrant-lxc/action.rb +++ b/lib/vagrant-lxc/action.rb @@ -13,6 +13,7 @@ require 'vagrant-lxc/action/forward_ports' require 'vagrant-lxc/action/handle_box_metadata' require 'vagrant-lxc/action/is_running' require 'vagrant-lxc/action/message' +require 'vagrant-lxc/action/remove_temporary_files' require 'vagrant-lxc/action/setup_package_files' require 'vagrant-lxc/action/share_folders' @@ -117,9 +118,10 @@ module Vagrant # b.use CheckDependencies b.use Vagrant::Action::Builtin::Call, Created do |env, b2| if env[:result] - # TODO: If vagrant >=... + # TODO: Remove this on / after 0.4 b2.use Disconnect b2.use ClearForwardedPorts + b2.use RemoveTemporaryFiles b2.use Vagrant::Action::Builtin::Call, Vagrant::Action::Builtin::GracefulHalt, :stopped, :running do |env2, b3| if !env2[:result] b3.use ForcedHalt diff --git a/lib/vagrant-lxc/action/remove_temporary_files.rb b/lib/vagrant-lxc/action/remove_temporary_files.rb new file mode 100644 index 0000000..62e487d --- /dev/null +++ b/lib/vagrant-lxc/action/remove_temporary_files.rb @@ -0,0 +1,23 @@ +module Vagrant + module LXC + module Action + class RemoveTemporaryFiles + def initialize(app, env) + @app = app + @logger = Log4r::Logger.new("vagrant::lxc::action::remove_tmp_files") + end + + def call(env) + # Continue execution, we need the container to be stopped + @app.call env + + if env[:machine].state.id == :stopped + @logger.debug 'Removing temporary files' + tmp_path = env[:machine].provider.driver.rootfs_path.join('tmp') + system "sudo rm -rf #{tmp_path}/*" + end + end + end + end + end +end diff --git a/spec/acceptance/sanity_check_spec.rb b/spec/acceptance/sanity_check_spec.rb index e214d45..4f2fd71 100644 --- a/spec/acceptance/sanity_check_spec.rb +++ b/spec/acceptance/sanity_check_spec.rb @@ -44,6 +44,7 @@ describe 'Sanity check' do before(:all) do destroy_container vagrant_up + vagrant_ssh 'touch /tmp/{some,files}' vagrant_halt end @@ -61,6 +62,11 @@ describe 'Sanity check' do processes = `pgrep redir` expect($?.exitstatus).to_not eq 0 end + + it 'removes files under `/tmp`' do + container_tmp = Pathname("/var/lib/lxc/#{vagrant_container_name}/rootfs/tmp") + expect(container_tmp.entries).to have(2).items # basically '.' and '..' + end end context '`vagrant destroy`' do