Use Util::Tempfile in Chef provisioner
This also fixes some Windowsisms
This commit is contained in:
parent
5a4f345363
commit
f95eb124d5
|
@ -1,7 +1,6 @@
|
||||||
require 'tempfile'
|
require_relative "../../../../lib/vagrant/util/presence"
|
||||||
|
require_relative "../../../../lib/vagrant/util/template_renderer"
|
||||||
require "vagrant/util/presence"
|
require_relative "../../../../lib/vagrant/util/tempfile"
|
||||||
require "vagrant/util/template_renderer"
|
|
||||||
|
|
||||||
require_relative "../installer"
|
require_relative "../installer"
|
||||||
|
|
||||||
|
@ -12,6 +11,7 @@ module VagrantPlugins
|
||||||
# chef-solo and chef-client provisioning are stored. This is **not an actual
|
# chef-solo and chef-client provisioning are stored. This is **not an actual
|
||||||
# provisioner**. Instead, {ChefSolo} or {ChefServer} should be used.
|
# provisioner**. Instead, {ChefSolo} or {ChefServer} should be used.
|
||||||
class Base < Vagrant.plugin("2", :provisioner)
|
class Base < Vagrant.plugin("2", :provisioner)
|
||||||
|
include Vagrant::Util
|
||||||
include Vagrant::Util::Presence
|
include Vagrant::Util::Presence
|
||||||
|
|
||||||
class ChefError < Vagrant::Errors::VagrantError
|
class ChefError < Vagrant::Errors::VagrantError
|
||||||
|
@ -118,7 +118,7 @@ module VagrantPlugins
|
||||||
@machine.communicate.upload(expanded, remote_custom_config_path)
|
@machine.communicate.upload(expanded, remote_custom_config_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
config_file = Vagrant::Util::TemplateRenderer.render(template, {
|
config_file = TemplateRenderer.render(template, {
|
||||||
custom_configuration: remote_custom_config_path,
|
custom_configuration: remote_custom_config_path,
|
||||||
encrypted_data_bag_secret: guest_encrypted_data_bag_secret_key_path,
|
encrypted_data_bag_secret: guest_encrypted_data_bag_secret_key_path,
|
||||||
environment: @config.environment,
|
environment: @config.environment,
|
||||||
|
@ -138,16 +138,14 @@ module VagrantPlugins
|
||||||
formatter: @config.formatter
|
formatter: @config.formatter
|
||||||
}.merge(template_vars))
|
}.merge(template_vars))
|
||||||
|
|
||||||
# Create a temporary file to store the data so we
|
# Create a temporary file to store the data so we can upload it.
|
||||||
# can upload it
|
|
||||||
temp = Tempfile.new("vagrant")
|
|
||||||
temp.write(config_file)
|
|
||||||
temp.close
|
|
||||||
|
|
||||||
remote_file = File.join(guest_provisioning_path, filename)
|
remote_file = File.join(guest_provisioning_path, filename)
|
||||||
@machine.communicate.tap do |comm|
|
@machine.communicate.sudo(remove_command(remote_file), error_check: false)
|
||||||
comm.sudo("rm -f #{remote_file}", error_check: false)
|
Tempfile.create("chef-provisioner-config") do |f|
|
||||||
comm.upload(temp.path, remote_file)
|
f.write(config_file)
|
||||||
|
f.fsync
|
||||||
|
f.close
|
||||||
|
@machine.communicate.upload(f.path, remote_file)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -160,22 +158,14 @@ module VagrantPlugins
|
||||||
!@config.run_list.empty?
|
!@config.run_list.empty?
|
||||||
json = JSON.pretty_generate(json)
|
json = JSON.pretty_generate(json)
|
||||||
|
|
||||||
# Create a temporary file to store the data so we
|
# Create a temporary file to store the data so we can upload it.
|
||||||
# can upload it
|
|
||||||
temp = Tempfile.new("vagrant")
|
|
||||||
temp.write(json)
|
|
||||||
temp.close
|
|
||||||
|
|
||||||
remote_file = File.join(guest_provisioning_path, "dna.json")
|
remote_file = File.join(guest_provisioning_path, "dna.json")
|
||||||
@machine.communicate.tap do |comm|
|
@machine.communicate.sudo(remove_command(remote_file), error_check: false)
|
||||||
if windows?
|
Tempfile.create("chef-provisioner-config") do |f|
|
||||||
command = "if (test-path '#{remote_file}') {rm '#{remote_file}' -force -recurse}"
|
f.write(json)
|
||||||
else
|
f.fsync
|
||||||
command = "rm -f #{remote_file}"
|
f.close
|
||||||
end
|
@machine.communicate.upload(f.path, remote_file)
|
||||||
|
|
||||||
comm.sudo(command, error_check: false)
|
|
||||||
comm.upload(temp.path, remote_file)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -186,29 +176,15 @@ module VagrantPlugins
|
||||||
@machine.ui.info I18n.t(
|
@machine.ui.info I18n.t(
|
||||||
"vagrant.provisioners.chef.upload_encrypted_data_bag_secret_key")
|
"vagrant.provisioners.chef.upload_encrypted_data_bag_secret_key")
|
||||||
|
|
||||||
@machine.communicate.tap do |comm|
|
@machine.communicate.sudo(remove_command(remote_file), error_check: false)
|
||||||
if windows?
|
@machine.communicate.upload(encrypted_data_bag_secret_key_path, remote_file)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_encrypted_data_bag_secret
|
def delete_encrypted_data_bag_secret
|
||||||
remote_file = guest_encrypted_data_bag_secret_key_path
|
remote_file = guest_encrypted_data_bag_secret_key_path
|
||||||
if remote_file
|
return if remote_file.nil?
|
||||||
if windows?
|
|
||||||
command = "if (test-path ""#{remote_file}"") {rm ""#{remote_file}"" -force -recurse}"
|
|
||||||
else
|
|
||||||
command = "rm -f #{remote_file}"
|
|
||||||
end
|
|
||||||
|
|
||||||
@machine.communicate.sudo(command, error_check: false)
|
@machine.communicate.sudo(remove_command(remote_file), error_check: false)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def encrypted_data_bag_secret_key_path
|
def encrypted_data_bag_secret_key_path
|
||||||
|
@ -258,6 +234,14 @@ module VagrantPlugins
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def remove_command(path)
|
||||||
|
if windows?
|
||||||
|
"if (test-path ""#{path}"") {rm ""#{path}"" -force -recurse}"
|
||||||
|
else
|
||||||
|
"rm -f #{path}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def windows?
|
def windows?
|
||||||
@machine.config.vm.communicator == :winrm
|
@machine.config.vm.communicator == :winrm
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue