Merge pull request #7370 from mitchellh/sethvargo/rm_custom_tempfile

Remove custom tempfile class
This commit is contained in:
Seth Vargo 2016-05-30 23:29:03 -04:00
commit 97f5ace2f2
21 changed files with 72 additions and 108 deletions

View File

@ -9,7 +9,6 @@ module Vagrant
autoload :SafeExec, 'vagrant/util/safe_exec'
autoload :StackedProcRunner, 'vagrant/util/stacked_proc_runner'
autoload :TemplateRenderer, 'vagrant/util/template_renderer'
autoload :Tempfile, 'vagrant/util/tempfile'
autoload :Subprocess, 'vagrant/util/subprocess'
end
end

View File

@ -1,58 +0,0 @@
require "tempfile"
module Vagrant
module Util
class Tempfile
# Utility function for creating a temporary file that will persist for
# the duration of the block and then be removed after the block finishes.
#
# @example
#
# Tempfile.create("arch-configure-networks") do |f|
# f.write("network-1")
# f.fsync
# f.close
# do_something_with_file(f.path)
# end
#
# @example
#
# Tempfile.create(["runner", "ps1"]) do |f|
# # f will have a suffix of ".ps1"
# # ...
# end
#
# @param [String, Array] name the prefix of the tempfile to create
# @param [Hash] options a list of options
# @param [Proc] block the block to yield the file object to
#
# @yield [File]
def self.create(name, options = {})
raise "No block given!" if !block_given?
options = {
binmode: true
}.merge(options)
# The user can specify an array where the first parameter is the prefix
# and the last parameter is the file suffix. We want to prefix the
# "prefix" with `vagrant-`, and this does that
if name.is_a?(Array)
basename = ["vagrant-#{name[0]}", name[1]]
else
basename = "vagrant-#{name}"
end
Dir::Tmpname.create(basename) do |path|
begin
f = File.open(path, "w+")
f.binmode if options[:binmode]
yield f
ensure
File.unlink(f.path) if File.file?(f.path)
end
end
end
end
end
end

View File

@ -1,11 +1,10 @@
require "timeout"
require "log4r"
require "tempfile"
require "timeout"
require_relative "helper"
require_relative "shell"
require_relative "command_filter"
require_relative "../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module CommunicatorWinRM
@ -205,7 +204,8 @@ module VagrantPlugins
interactive: interactive,
})
guest_script_path = "c:/tmp/vagrant-elevated-shell.ps1"
Tempfile.create(["vagrant-elevated-shell", "ps1"]) do |f|
Tempfile.open(["vagrant-elevated-shell", "ps1"]) do |f|
f.binmode
f.write(script)
f.fsync
f.close

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestArch
@ -24,7 +25,8 @@ module VagrantPlugins
remote_path = "/tmp/vagrant-network-#{Time.now.to_i}-#{i}"
Tempfile.create("arch-configure-networks") do |f|
Tempfile.open("arch-configure-networks") do |f|
f.binmode
f.write(entry)
f.fsync
f.close

View File

@ -1,5 +1,6 @@
require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestCoreOS
@ -53,7 +54,8 @@ module VagrantPlugins
})
end
Tempfile.create("coreos-configure-networks") do |f|
Tempfile.open("coreos-configure-networks") do |f|
f.binmode
f.write(entry)
f.fsync
f.close

View File

@ -1,7 +1,7 @@
require "set"
require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestDebian
@ -31,7 +31,8 @@ module VagrantPlugins
# Perform the careful dance necessary to reconfigure the network
# interfaces.
Tempfile.create("debian-configure-networks") do |f|
Tempfile.open("debian-configure-networks") do |f|
f.binmode
f.write(entries.join("\n"))
f.fsync
f.close

View File

@ -1,8 +1,8 @@
require "set"
require "tempfile"
require_relative "../../../../lib/vagrant/util/retryable"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestFedora
@ -96,7 +96,8 @@ module VagrantPlugins
entry = TemplateRenderer.render("guests/fedora/network_#{network[:type]}",
options: network)
Tempfile.create("fedora-configure-networks") do |f|
Tempfile.open("fedora-configure-networks") do |f|
f.binmode
f.write(entry)
f.fsync
f.close

View File

@ -1,5 +1,6 @@
require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestFreeBSD
@ -28,7 +29,8 @@ module VagrantPlugins
entry = TemplateRenderer.render("guests/freebsd/network_#{network[:type]}",
options: network, ifname: ifname)
Tempfile.create("freebsd-configure-networks") do |f|
Tempfile.open("freebsd-configure-networks") do |f|
f.binmode
f.write(entry)
f.fsync
f.close

View File

@ -1,5 +1,6 @@
require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestFuntoo
@ -24,7 +25,8 @@ module VagrantPlugins
options: network)
# Upload the entry to a temporary location
Tempfile.create("funtoo-configure-networks") do |f|
Tempfile.open("funtoo-configure-networks") do |f|
f.binmode
f.write(entry)
f.fsync
f.close

View File

@ -1,5 +1,6 @@
require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestGentoo
@ -20,7 +21,8 @@ module VagrantPlugins
options: network)
# Upload the entry to a temporary location
Tempfile.create("gentoo-configure-networks") do |f|
Tempfile.open("gentoo-configure-networks") do |f|
f.binmode
f.write(entry)
f.fsync
f.close

View File

@ -1,5 +1,6 @@
require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestNetBSD
@ -19,7 +20,8 @@ module VagrantPlugins
entry = TemplateRenderer.render("guests/netbsd/network_#{network[:type]}",
options: network)
Tempfile.create("netbsd-configure-networks") do |f|
Tempfile.open("netbsd-configure-networks") do |f|
f.binmode
f.write(entry)
f.fsync
f.close

View File

@ -1,5 +1,6 @@
require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestNixos
@ -17,7 +18,8 @@ module VagrantPlugins
def self.upload(machine, content, remote_path)
remote_temp = mktemp(machine)
Tempfile.create("nixos-change-host-name") do |f|
Tempfile.open("nixos-change-host-name") do |f|
f.binmode
f.write(content)
f.fsync
f.close

View File

@ -1,7 +1,7 @@
require "ipaddr"
require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestNixos
@ -67,7 +67,8 @@ module VagrantPlugins
def self.upload(machine, content, remote_path)
remote_temp = mktemp(machine)
Tempfile.create("nixos-configure-networks") do |f|
Tempfile.open("nixos-configure-networks") do |f|
f.binmode
f.write(content)
f.fsync
f.close

View File

@ -1,5 +1,6 @@
require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestOpenBSD
@ -12,7 +13,8 @@ module VagrantPlugins
entry = TemplateRenderer.render("guests/openbsd/network_#{network[:type]}",
options: network)
Tempfile.create("openbsd-configure-networks") do |f|
Tempfile.open("openbsd-configure-networks") do |f|
f.binmode
f.write(entry)
f.fsync
f.close

View File

@ -1,8 +1,8 @@
require "set"
require "tempfile"
require_relative "../../../../lib/vagrant/util/retryable"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestRedHat
@ -55,7 +55,8 @@ module VagrantPlugins
entry = TemplateRenderer.render("guests/redhat/network_#{network[:type]}",
options: network)
Tempfile.create("red-hat-configure-networks") do |f|
Tempfile.open("red-hat-configure-networks") do |f|
f.binmode
f.write(entry)
f.fsync
f.close

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestSlackware
@ -19,7 +20,8 @@ module VagrantPlugins
entry = TemplateRenderer.render("guests/slackware/network_#{network[:type]}", options: network)
Tempfile.create("slackware-configure-networks") do |f|
Tempfile.open("slackware-configure-networks") do |f|
f.binmode
f.write(entry)
f.fsync
f.close

View File

@ -1,8 +1,8 @@
require "set"
require "tempfile"
require_relative "../../../../lib/vagrant/util/retryable"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module GuestSUSE
@ -33,7 +33,8 @@ module VagrantPlugins
entry = TemplateRenderer.render("guests/suse/network_#{network[:type]}",
options: network)
Tempfile.create("suse-configure-networks") do |f|
Tempfile.open("suse-configure-networks") do |f|
f.binmode
f.write(entry)
f.fsync
f.close

View File

@ -1,5 +1,6 @@
require "tempfile"
require_relative "base"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module Ansible
@ -106,7 +107,8 @@ module VagrantPlugins
create_and_chown_remote_folder(inventory_basedir)
@machine.communicate.sudo("rm -f #{inventory_path}", error_check: false)
Tempfile.create("ansible-local-inventory-#{@machine.name}") do |f|
Tempfile.open("ansible-local-inventory-#{@machine.name}") do |f|
f.binmode
f.write(inventory_content)
f.fsync
f.close

View File

@ -1,6 +1,7 @@
require "tempfile"
require_relative "../../../../lib/vagrant/util/presence"
require_relative "../../../../lib/vagrant/util/template_renderer"
require_relative "../../../../lib/vagrant/util/tempfile"
require_relative "../installer"
@ -141,7 +142,8 @@ module VagrantPlugins
# Create a temporary file to store the data so we can upload it.
remote_file = File.join(guest_provisioning_path, filename)
@machine.communicate.sudo(remove_command(remote_file), error_check: false)
Tempfile.create("chef-provisioner-config") do |f|
Tempfile.open("chef-provisioner-config") do |f|
f.binmode
f.write(config_file)
f.fsync
f.close
@ -161,7 +163,8 @@ module VagrantPlugins
# Create a temporary file to store the data so we can upload it.
remote_file = File.join(guest_provisioning_path, "dna.json")
@machine.communicate.sudo(remove_command(remote_file), error_check: false)
Tempfile.create("chef-provisioner-config") do |f|
Tempfile.open("chef-provisioner-config") do |f|
f.binmode
f.write(json)
f.fsync
f.close

View File

@ -1,7 +1,7 @@
require "digest/md5"
require "tempfile"
require_relative "base"
require_relative "../../../../lib/vagrant/util/tempfile"
module VagrantPlugins
module Chef
@ -55,7 +55,8 @@ module VagrantPlugins
# machine.
def upload_recipe
# Write the raw recipe contents to a tempfile and upload
Tempfile.create(["chef-apply", ".rb"]) do |f|
Tempfile.open(["chef-apply", ".rb"]) do |f|
f.binmode
f.write(config.recipe)
f.fsync
f.close

View File

@ -39,12 +39,8 @@ describe Vagrant::BoxCollection, :skip_windows do
end
it 'does not raise an exception when a file appears in the boxes dir' do
t = Tempfile.new('a_file', environment.boxes_dir)
t.close
begin
Tempfile.open('a_file', environment.boxes_dir) do
expect { subject.all }.to_not raise_error
ensure
t.unlink
end
end
end
@ -348,8 +344,9 @@ describe Vagrant::BoxCollection, :skip_windows do
CHECKSUM_OFFSET = 148
CHECKSUM_LENGTH = 8
f = Tempfile.new(['vagrant_testing', '.tar'])
begin
Tempfile.open(['vagrant_testing', '.tar']) do |f|
f.binmode
# Corrupt the tar by writing over the checksum field
f.seek(CHECKSUM_OFFSET)
f.write("\0"*CHECKSUM_LENGTH)
@ -357,9 +354,6 @@ describe Vagrant::BoxCollection, :skip_windows do
expect { subject.add(f.path, "foo", "1.0") }.
to raise_error(Vagrant::Errors::BoxUnpackageFailure)
ensure
f.close
f.unlink
end
end
end