From 7014aa3babf04a31af4ec37f40871ed3dcf43481 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 30 May 2016 23:17:02 -0400 Subject: [PATCH] Remove custom tempfile class This is not actually providing any additional utility and also causes namespace conflicts with people trying to use the real Tempfile class. --- lib/vagrant/util.rb | 1 - lib/vagrant/util/tempfile.rb | 58 ------------------- plugins/communicators/winrm/communicator.rb | 8 +-- plugins/guests/arch/cap/configure_networks.rb | 6 +- .../guests/coreos/cap/configure_networks.rb | 6 +- .../guests/debian/cap/configure_networks.rb | 5 +- .../guests/fedora/cap/configure_networks.rb | 5 +- .../guests/freebsd/cap/configure_networks.rb | 6 +- .../guests/funtoo/cap/configure_networks.rb | 6 +- .../guests/gentoo/cap/configure_networks.rb | 6 +- .../guests/netbsd/cap/configure_networks.rb | 6 +- plugins/guests/nixos/cap/change_host_name.rb | 6 +- .../guests/nixos/cap/configure_networks.rb | 5 +- .../guests/openbsd/cap/configure_networks.rb | 6 +- .../guests/redhat/cap/configure_networks.rb | 5 +- .../slackware/cap/configure_networks.rb | 6 +- plugins/guests/suse/cap/configure_networks.rb | 5 +- .../provisioners/ansible/provisioner/guest.rb | 6 +- plugins/provisioners/chef/provisioner/base.rb | 9 ++- .../chef/provisioner/chef_apply.rb | 5 +- test/unit/vagrant/box_collection_test.rb | 14 ++--- 21 files changed, 72 insertions(+), 108 deletions(-) delete mode 100644 lib/vagrant/util/tempfile.rb diff --git a/lib/vagrant/util.rb b/lib/vagrant/util.rb index f3017dea4..896b5d7e0 100644 --- a/lib/vagrant/util.rb +++ b/lib/vagrant/util.rb @@ -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 diff --git a/lib/vagrant/util/tempfile.rb b/lib/vagrant/util/tempfile.rb deleted file mode 100644 index 66006b788..000000000 --- a/lib/vagrant/util/tempfile.rb +++ /dev/null @@ -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 diff --git a/plugins/communicators/winrm/communicator.rb b/plugins/communicators/winrm/communicator.rb index bb04ee277..78ba73879 100644 --- a/plugins/communicators/winrm/communicator.rb +++ b/plugins/communicators/winrm/communicator.rb @@ -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 diff --git a/plugins/guests/arch/cap/configure_networks.rb b/plugins/guests/arch/cap/configure_networks.rb index 76de628dd..c61e8c6bc 100644 --- a/plugins/guests/arch/cap/configure_networks.rb +++ b/plugins/guests/arch/cap/configure_networks.rb @@ -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 diff --git a/plugins/guests/coreos/cap/configure_networks.rb b/plugins/guests/coreos/cap/configure_networks.rb index eeca1302c..04b1e3aac 100644 --- a/plugins/guests/coreos/cap/configure_networks.rb +++ b/plugins/guests/coreos/cap/configure_networks.rb @@ -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 diff --git a/plugins/guests/debian/cap/configure_networks.rb b/plugins/guests/debian/cap/configure_networks.rb index 15095968e..315a0ffc3 100644 --- a/plugins/guests/debian/cap/configure_networks.rb +++ b/plugins/guests/debian/cap/configure_networks.rb @@ -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 diff --git a/plugins/guests/fedora/cap/configure_networks.rb b/plugins/guests/fedora/cap/configure_networks.rb index 425d4c5db..23318b0ac 100644 --- a/plugins/guests/fedora/cap/configure_networks.rb +++ b/plugins/guests/fedora/cap/configure_networks.rb @@ -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 diff --git a/plugins/guests/freebsd/cap/configure_networks.rb b/plugins/guests/freebsd/cap/configure_networks.rb index 4744c641d..f0ebb3ea5 100644 --- a/plugins/guests/freebsd/cap/configure_networks.rb +++ b/plugins/guests/freebsd/cap/configure_networks.rb @@ -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 diff --git a/plugins/guests/funtoo/cap/configure_networks.rb b/plugins/guests/funtoo/cap/configure_networks.rb index d5a1a653a..9265171af 100644 --- a/plugins/guests/funtoo/cap/configure_networks.rb +++ b/plugins/guests/funtoo/cap/configure_networks.rb @@ -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 diff --git a/plugins/guests/gentoo/cap/configure_networks.rb b/plugins/guests/gentoo/cap/configure_networks.rb index 5d7796ef0..49553569b 100644 --- a/plugins/guests/gentoo/cap/configure_networks.rb +++ b/plugins/guests/gentoo/cap/configure_networks.rb @@ -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 diff --git a/plugins/guests/netbsd/cap/configure_networks.rb b/plugins/guests/netbsd/cap/configure_networks.rb index d52f48267..81d5ea366 100644 --- a/plugins/guests/netbsd/cap/configure_networks.rb +++ b/plugins/guests/netbsd/cap/configure_networks.rb @@ -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 diff --git a/plugins/guests/nixos/cap/change_host_name.rb b/plugins/guests/nixos/cap/change_host_name.rb index 5fbd5c38a..b838928c0 100644 --- a/plugins/guests/nixos/cap/change_host_name.rb +++ b/plugins/guests/nixos/cap/change_host_name.rb @@ -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 diff --git a/plugins/guests/nixos/cap/configure_networks.rb b/plugins/guests/nixos/cap/configure_networks.rb index 4161610b4..5a788781a 100644 --- a/plugins/guests/nixos/cap/configure_networks.rb +++ b/plugins/guests/nixos/cap/configure_networks.rb @@ -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 diff --git a/plugins/guests/openbsd/cap/configure_networks.rb b/plugins/guests/openbsd/cap/configure_networks.rb index 774fe2c79..5a123288b 100644 --- a/plugins/guests/openbsd/cap/configure_networks.rb +++ b/plugins/guests/openbsd/cap/configure_networks.rb @@ -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 diff --git a/plugins/guests/redhat/cap/configure_networks.rb b/plugins/guests/redhat/cap/configure_networks.rb index 7001bacc8..8c5dfef26 100644 --- a/plugins/guests/redhat/cap/configure_networks.rb +++ b/plugins/guests/redhat/cap/configure_networks.rb @@ -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 diff --git a/plugins/guests/slackware/cap/configure_networks.rb b/plugins/guests/slackware/cap/configure_networks.rb index d3b8e23fc..7708ac784 100644 --- a/plugins/guests/slackware/cap/configure_networks.rb +++ b/plugins/guests/slackware/cap/configure_networks.rb @@ -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 diff --git a/plugins/guests/suse/cap/configure_networks.rb b/plugins/guests/suse/cap/configure_networks.rb index 204ff5f69..d1060e425 100644 --- a/plugins/guests/suse/cap/configure_networks.rb +++ b/plugins/guests/suse/cap/configure_networks.rb @@ -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 diff --git a/plugins/provisioners/ansible/provisioner/guest.rb b/plugins/provisioners/ansible/provisioner/guest.rb index 3f9fd7163..395db8cae 100644 --- a/plugins/provisioners/ansible/provisioner/guest.rb +++ b/plugins/provisioners/ansible/provisioner/guest.rb @@ -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 diff --git a/plugins/provisioners/chef/provisioner/base.rb b/plugins/provisioners/chef/provisioner/base.rb index 936e4f455..eb4cfa153 100644 --- a/plugins/provisioners/chef/provisioner/base.rb +++ b/plugins/provisioners/chef/provisioner/base.rb @@ -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 diff --git a/plugins/provisioners/chef/provisioner/chef_apply.rb b/plugins/provisioners/chef/provisioner/chef_apply.rb index a5aab3d8a..77bbb0069 100644 --- a/plugins/provisioners/chef/provisioner/chef_apply.rb +++ b/plugins/provisioners/chef/provisioner/chef_apply.rb @@ -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 diff --git a/test/unit/vagrant/box_collection_test.rb b/test/unit/vagrant/box_collection_test.rb index 7f121f17d..45bd8e5b1 100644 --- a/test/unit/vagrant/box_collection_test.rb +++ b/test/unit/vagrant/box_collection_test.rb @@ -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