From: Antonio Terceiro Date: Wed, 22 Oct 2014 09:40:14 -0200 Subject: Use a private temporary dir Without this vagrant will clutter $TMPDIR with dozens of even hundreds of temporary files (~4 per vagrant invocation). --- lib/vagrant/box.rb | 3 ++- lib/vagrant/bundler.rb | 9 ++++++--- lib/vagrant/util.rb | 1 + lib/vagrant/util/tempfile.rb | 15 +++++++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 lib/vagrant/util/tempfile.rb --- a/lib/vagrant/box.rb +++ b/lib/vagrant/box.rb @@ -9,6 +9,7 @@ require "vagrant/util/downloader" require "vagrant/util/platform" require "vagrant/util/safe_chdir" require "vagrant/util/subprocess" +require "vagrant/util/tempfile" module Vagrant # Represents a "box," which is a package Vagrant environment that is used @@ -118,7 +119,7 @@ module Vagrant # @param [Hash] download_options Options to pass to the downloader. # @return [BoxMetadata] def load_metadata(**download_options) - tf = Tempfile.new("vagrant") + tf = Util::Tempfile.new("box") tf.close url = @metadata_url --- a/lib/vagrant/bundler.rb +++ b/lib/vagrant/bundler.rb @@ -9,6 +9,8 @@ require_relative "shared_helpers" require_relative "version" require_relative "util/safe_env" +require 'vagrant/util/tempfile' + module Vagrant # This class manages Vagrant's interaction with Bundler. Vagrant uses # Bundler as a way to properly resolve all dependencies of Vagrant and @@ -55,13 +57,13 @@ module Vagrant # Setup the "local" Bundler configuration. We need to set BUNDLE_PATH # because the existence of this actually suppresses `sudo`. - @appconfigpath = Dir.mktmpdir + @appconfigpath = Util::Tempfile.private_tmpdir File.open(File.join(@appconfigpath, "config"), "w+") do |f| f.write("BUNDLE_PATH: \"#{bundle_path}\"") end # Setup the Bundler configuration - @configfile = File.open(Tempfile.new("vagrant").path + "1", "w+") + @configfile = File.open(Util::Tempfile.new("vagrant").path + "1", "w+") @configfile.close # Build up the Gemfile for our Bundler context. We make sure to @@ -184,7 +186,8 @@ module Vagrant def build_gemfile(plugins) sources = plugins.values.map { |p| p["sources"] }.flatten.compact.uniq - f = File.open(Tempfile.new("vagrant").path + "2", "w+") + + f = File.open(Util::Tempfile.new("vagrant").path, "w+") f.tap do |gemfile| sources.each do |source| next if source == "" @@ -257,7 +260,7 @@ module Vagrant # native extensions because it causes all sorts of problems. old_rubyopt = ENV["RUBYOPT"] old_gemfile = ENV["BUNDLE_GEMFILE"] - ENV["BUNDLE_GEMFILE"] = Tempfile.new("vagrant-gemfile").path + ENV["BUNDLE_GEMFILE"] = Util::Tempfile.new("vagrant-gemfile").path ENV["RUBYOPT"] = (ENV["RUBYOPT"] || "").gsub(/-rbundler\/setup\s*/, "") # Set the GEM_HOME so gems are installed only to our local gem dir --- a/lib/vagrant/util.rb +++ b/lib/vagrant/util.rb @@ -8,6 +8,7 @@ module Vagrant autoload :Retryable, 'vagrant/util/retryable' autoload :SafeExec, 'vagrant/util/safe_exec' autoload :StackedProcRunner, 'vagrant/util/stacked_proc_runner' + autoload :Tempfile, 'vagrant/util/tempfile' autoload :TemplateRenderer, 'vagrant/util/template_renderer' autoload :Subprocess, 'vagrant/util/subprocess' end --- /dev/null +++ b/lib/vagrant/util/tempfile.rb @@ -0,0 +1,33 @@ +require 'fileutils' + +module Vagrant + module Util + class Tempfile < ::Tempfile + + def initialize(basename) + super(basename, private_tmpdir) + end + + def private_tmpdir + self.class.private_tmpdir + end + + def self.private_tmpdir + @private_tmpdir ||= + begin + user = Etc.getpwuid.name + pid = Process.pid + tmpdir = File.join(Dir.tmpdir, "vagrant-#{user}-#{pid}") + FileUtils.mkdir_p(tmpdir) + FileUtils.chmod(0700, tmpdir) + tmpdir + end + end + + end + end +end + +at_exit do + FileUtils.rm_rf(Vagrant::Util::Tempfile.private_tmpdir) +end