diff --git a/plugins/provisioners/chef/cap/windows/chef_installed.rb b/plugins/provisioners/chef/cap/windows/chef_installed.rb new file mode 100644 index 000000000..c95ea8498 --- /dev/null +++ b/plugins/provisioners/chef/cap/windows/chef_installed.rb @@ -0,0 +1,20 @@ +module VagrantPlugins + module Chef + module Cap + module Windows + module ChefInstalled + # Check if Chef is installed at the given version. + # @return [true, false] + def self.chef_installed(machine, version) + if version != :latest + command = 'if ((&knife --version) -Match "Chef: "' + version + '"){ exit 0 } else { exit 1 }' + else + command = 'if ((&knife --version) -Match "Chef: *"){ exit 0 } else { exit 1 }' + end + machine.communicate.test(command, sudo: true) + end + end + end + end + end +end diff --git a/plugins/provisioners/chef/plugin.rb b/plugins/provisioners/chef/plugin.rb index 5a1b2876f..540429fad 100644 --- a/plugins/provisioners/chef/plugin.rb +++ b/plugins/provisioners/chef/plugin.rb @@ -58,6 +58,11 @@ module VagrantPlugins Cap::Linux::ChefInstalled end + guest_capability(:windows, :chef_installed) do + require_relative "cap/windows/chef_installed" + Cap::Windows::ChefInstalled + end + guest_capability(:debian, :chef_install) do require_relative "cap/debian/chef_install" Cap::Debian::ChefInstall diff --git a/plugins/provisioners/chef/provisioner/base.rb b/plugins/provisioners/chef/provisioner/base.rb index a5efb5be4..aed44eaf0 100644 --- a/plugins/provisioners/chef/provisioner/base.rb +++ b/plugins/provisioners/chef/provisioner/base.rb @@ -37,8 +37,14 @@ module VagrantPlugins def verify_binary(binary) # Checks for the existence of chef binary and error if it # doesn't exist. + if windows? + command = "if ((&'#{binary}' -v) -Match 'Chef: *'){ exit 0 } else { exit 1 }" + else + command = "sh -c 'command -v #{binary}'" + end + @machine.communicate.sudo( - "sh -c 'command -v #{binary}'", + command, error_class: ChefError, error_key: :chef_not_detected, binary: binary, @@ -66,8 +72,12 @@ module VagrantPlugins @machine.communicate.tap do |comm| paths.each do |path| - comm.sudo("mkdir -p #{path}") - comm.sudo("chown -h #{@machine.ssh_info[:username]} #{path}") + if windows? + comm.sudo("mkdir ""#{path}"" -f") + else + comm.sudo("mkdir -p #{path}") + comm.sudo("chown -h #{@machine.ssh_info[:username]} #{path}") + end end end end @@ -133,7 +143,13 @@ module VagrantPlugins remote_file = File.join(@config.provisioning_path, "dna.json") @machine.communicate.tap do |comm| - comm.sudo("rm -f #{remote_file}", error_check: false) + 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) end end @@ -146,7 +162,13 @@ module VagrantPlugins "vagrant.provisioners.chef.upload_encrypted_data_bag_secret_key") @machine.communicate.tap do |comm| - comm.sudo("rm -f #{remote_file}", error_check: false) + 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 end @@ -154,7 +176,13 @@ module VagrantPlugins def delete_encrypted_data_bag_secret remote_file = guest_encrypted_data_bag_secret_key_path if remote_file - @machine.communicate.sudo("rm -f #{remote_file}", error_check: false) + 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) end end diff --git a/plugins/provisioners/chef/provisioner/chef_apply.rb b/plugins/provisioners/chef/provisioner/chef_apply.rb index 1ff4790b6..602c81f3d 100644 --- a/plugins/provisioners/chef/provisioner/chef_apply.rb +++ b/plugins/provisioners/chef/provisioner/chef_apply.rb @@ -18,8 +18,12 @@ module VagrantPlugins user = @machine.ssh_info[:username] # Reset upload path permissions for the current ssh user - @machine.communicate.sudo("mkdir -p #{config.upload_path}") - @machine.communicate.sudo("chown -R #{user} #{config.upload_path}") + if windows? + @machine.communicate.sudo("mkdir ""#{config.upload_path}"" -f") + else + @machine.communicate.sudo("mkdir -p #{config.upload_path}") + @machine.communicate.sudo("chown -R #{user} #{config.upload_path}") + end # Upload the recipe upload_recipe diff --git a/plugins/provisioners/chef/provisioner/chef_client.rb b/plugins/provisioners/chef/provisioner/chef_client.rb index c9b0ab6c0..f0209d2a0 100644 --- a/plugins/provisioners/chef/provisioner/chef_client.rb +++ b/plugins/provisioners/chef/provisioner/chef_client.rb @@ -39,7 +39,11 @@ module VagrantPlugins @machine.ui.info I18n.t("vagrant.provisioners.chef.client_key_folder") path = Pathname.new(@config.client_key_path) - @machine.communicate.sudo("mkdir -p #{path.dirname}") + if windows? + @machine.communicate.sudo("mkdir ""#{path.dirname}"" -f") + else + @machine.communicate.sudo("mkdir -p #{path.dirname}") + end end def upload_validation_key