diff --git a/lib/vagrant/action/general/package.rb b/lib/vagrant/action/general/package.rb index a04deb654..996368d3a 100644 --- a/lib/vagrant/action/general/package.rb +++ b/lib/vagrant/action/general/package.rb @@ -40,6 +40,8 @@ module Vagrant end def recover(env) + @env = env + # There are certain exceptions that we don't delete the file for. ignore_exc = [Errors::PackageOutputDirectory, Errors::PackageOutputExists] ignore_exc.each do |exc| diff --git a/plugins/providers/virtualbox/action/export.rb b/plugins/providers/virtualbox/action/export.rb index 8beb9869e..1a7df0620 100644 --- a/plugins/providers/virtualbox/action/export.rb +++ b/plugins/providers/virtualbox/action/export.rb @@ -4,8 +4,6 @@ module VagrantPlugins module ProviderVirtualBox module Action class Export - attr_reader :temp_dir - def initialize(app, env) @app = app end @@ -16,24 +14,9 @@ module VagrantPlugins raise Vagrant::Errors::VMPowerOffToPackage if \ @env[:machine].provider.state.id != :poweroff - setup_temp_dir export @app.call(env) - - recover(env) # called to cleanup temp directory - end - - def recover(env) - if temp_dir && File.exist?(temp_dir) - FileUtils.rm_rf(temp_dir) - end - end - - def setup_temp_dir - @env[:ui].info I18n.t("vagrant.actions.vm.export.create_dir") - @temp_dir = @env["export.temp_dir"] = @env[:tmp_path].join(Time.now.to_i.to_s) - FileUtils.mkpath(@env["export.temp_dir"]) end def export diff --git a/plugins/providers/virtualbox/action/package.rb b/plugins/providers/virtualbox/action/package.rb index 5a6c581ce..62a564ac9 100644 --- a/plugins/providers/virtualbox/action/package.rb +++ b/plugins/providers/virtualbox/action/package.rb @@ -1,3 +1,5 @@ +require 'fileutils' + require 'vagrant/action/general/package' module VagrantPlugins @@ -8,11 +10,33 @@ module VagrantPlugins # called in the unit tests. alias_method :general_call, :call def call(env) + # Setup the temporary directory + @temp_dir = env[:tmp_path].join(Time.now.to_i.to_s) + env["export.temp_dir"] = @temp_dir + FileUtils.mkpath(env["export.temp_dir"]) + # Just match up a couple environmental variables so that # the superclass will do the right thing. Then, call the # superclass env["package.directory"] = env["export.temp_dir"] + general_call(env) + + # Always call recover to clean up the temp dir + clean_temp_dir + end + + def recover(env) + clean_temp_dir + super + end + + protected + + def clean_temp_dir + if @temp_dir && File.exist?(@temp_dir) + FileUtils.rm_rf(@temp_dir) + end end end end