From f95eb124d531259ce4e9fc2337fff3135064060b Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sat, 28 May 2016 23:20:36 -0400 Subject: [PATCH] Use Util::Tempfile in Chef provisioner This also fixes some Windowsisms --- plugins/provisioners/chef/provisioner/base.rb | 78 ++++++++----------- 1 file changed, 31 insertions(+), 47 deletions(-) diff --git a/plugins/provisioners/chef/provisioner/base.rb b/plugins/provisioners/chef/provisioner/base.rb index 3f5ec7106..936e4f455 100644 --- a/plugins/provisioners/chef/provisioner/base.rb +++ b/plugins/provisioners/chef/provisioner/base.rb @@ -1,7 +1,6 @@ -require 'tempfile' - -require "vagrant/util/presence" -require "vagrant/util/template_renderer" +require_relative "../../../../lib/vagrant/util/presence" +require_relative "../../../../lib/vagrant/util/template_renderer" +require_relative "../../../../lib/vagrant/util/tempfile" require_relative "../installer" @@ -12,6 +11,7 @@ module VagrantPlugins # chef-solo and chef-client provisioning are stored. This is **not an actual # provisioner**. Instead, {ChefSolo} or {ChefServer} should be used. class Base < Vagrant.plugin("2", :provisioner) + include Vagrant::Util include Vagrant::Util::Presence class ChefError < Vagrant::Errors::VagrantError @@ -118,7 +118,7 @@ module VagrantPlugins @machine.communicate.upload(expanded, remote_custom_config_path) end - config_file = Vagrant::Util::TemplateRenderer.render(template, { + config_file = TemplateRenderer.render(template, { custom_configuration: remote_custom_config_path, encrypted_data_bag_secret: guest_encrypted_data_bag_secret_key_path, environment: @config.environment, @@ -138,16 +138,14 @@ module VagrantPlugins formatter: @config.formatter }.merge(template_vars)) - # Create a temporary file to store the data so we - # can upload it - temp = Tempfile.new("vagrant") - temp.write(config_file) - temp.close - + # Create a temporary file to store the data so we can upload it. remote_file = File.join(guest_provisioning_path, filename) - @machine.communicate.tap do |comm| - comm.sudo("rm -f #{remote_file}", error_check: false) - comm.upload(temp.path, remote_file) + @machine.communicate.sudo(remove_command(remote_file), error_check: false) + Tempfile.create("chef-provisioner-config") do |f| + f.write(config_file) + f.fsync + f.close + @machine.communicate.upload(f.path, remote_file) end end @@ -160,22 +158,14 @@ module VagrantPlugins !@config.run_list.empty? json = JSON.pretty_generate(json) - # Create a temporary file to store the data so we - # can upload it - temp = Tempfile.new("vagrant") - temp.write(json) - temp.close - + # Create a temporary file to store the data so we can upload it. remote_file = File.join(guest_provisioning_path, "dna.json") - @machine.communicate.tap do |comm| - if windows? - command = "if (test-path '#{remote_file}') {rm '#{remote_file}' -force -recurse}" - else - command = "rm -f #{remote_file}" - end - - comm.sudo(command, error_check: false) - comm.upload(temp.path, remote_file) + @machine.communicate.sudo(remove_command(remote_file), error_check: false) + Tempfile.create("chef-provisioner-config") do |f| + f.write(json) + f.fsync + f.close + @machine.communicate.upload(f.path, remote_file) end end @@ -186,29 +176,15 @@ module VagrantPlugins @machine.ui.info I18n.t( "vagrant.provisioners.chef.upload_encrypted_data_bag_secret_key") - @machine.communicate.tap do |comm| - if windows? - command = "if (test-path ""#{remote_file}"") {rm ""#{remote_file}"" -force -recurse}" - else - command = "rm -f #{remote_file}" - end - - comm.sudo(command, error_check: false) - comm.upload(encrypted_data_bag_secret_key_path, remote_file) - end + @machine.communicate.sudo(remove_command(remote_file), error_check: false) + @machine.communicate.upload(encrypted_data_bag_secret_key_path, remote_file) end def delete_encrypted_data_bag_secret remote_file = guest_encrypted_data_bag_secret_key_path - if remote_file - if windows? - command = "if (test-path ""#{remote_file}"") {rm ""#{remote_file}"" -force -recurse}" - else - command = "rm -f #{remote_file}" - end + return if remote_file.nil? - @machine.communicate.sudo(command, error_check: false) - end + @machine.communicate.sudo(remove_command(remote_file), error_check: false) end def encrypted_data_bag_secret_key_path @@ -258,6 +234,14 @@ module VagrantPlugins end end + def remove_command(path) + if windows? + "if (test-path ""#{path}"") {rm ""#{path}"" -force -recurse}" + else + "rm -f #{path}" + end + end + def windows? @machine.config.vm.communicator == :winrm end