From f999218c5360be0a5ed4da1cd853376a660e3cf4 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 18 Nov 2015 18:28:33 -0800 Subject: [PATCH 1/2] Perform Chef delete operations on the guest instead of the host --- .../provisioners/chef/config/chef_client.rb | 6 ----- .../chef/provisioner/chef_client.rb | 26 +++++++------------ 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/plugins/provisioners/chef/config/chef_client.rb b/plugins/provisioners/chef/config/chef_client.rb index 331cc9b21..b525a3307 100644 --- a/plugins/provisioners/chef/config/chef_client.rb +++ b/plugins/provisioners/chef/config/chef_client.rb @@ -63,12 +63,6 @@ module VagrantPlugins errors << I18n.t("vagrant.config.chef.validation_key_path") end - if delete_client || delete_node - if !Vagrant::Util::Which.which("knife") - errors << I18n.t("vagrant.chef_config_knife_not_found") - end - end - { "chef client provisioner" => errors } end end diff --git a/plugins/provisioners/chef/provisioner/chef_client.rb b/plugins/provisioners/chef/provisioner/chef_client.rb index 056ab802c..9cda068f3 100644 --- a/plugins/provisioners/chef/provisioner/chef_client.rb +++ b/plugins/provisioners/chef/provisioner/chef_client.rb @@ -31,8 +31,8 @@ module VagrantPlugins end def cleanup - delete_from_chef_server('client') if @config.delete_client delete_from_chef_server('node') if @config.delete_node + delete_from_chef_server('client') if @config.delete_client end def create_client_key_folder @@ -116,6 +116,10 @@ module VagrantPlugins end end + def guest_client_rb_path + File.join(guest_provisioning_path, "client.rb") + end + def guest_validation_key_path File.join(guest_provisioning_path, "validation.pem") end @@ -130,23 +134,13 @@ module VagrantPlugins return end - @machine.ui.info(I18n.t( - "vagrant.provisioners.chef.deleting_from_server", + @machine.ui.info(I18n.t("vagrant.provisioners.chef.deleting_from_server", deletable: deletable, name: node_name)) - # Knife is not part of the current Vagrant bundle, so it needs to run - # in the context of the system. - Vagrant.global_lock do - command = ["knife", deletable, "delete", "--yes", node_name] - r = Vagrant::Util::Subprocess.execute(*command) - if r.exit_code != 0 - @machine.ui.error(I18n.t( - "vagrant.chef_client_cleanup_failed", - deletable: deletable, - stdout: r.stdout, - stderr: r.stderr)) - end - end + command = "knife #{deletable} delete #{node_name}" + command << " --config '#{guest_client_rb_path}'" + command << " --yes" + @machine.communicate.sudo(command, error_check: true) end end end From a0c049da000e0c904be87715419eb4958b33f79f Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 18 Nov 2015 18:32:46 -0800 Subject: [PATCH 2/2] Fix tests --- .../chef/config/chef_client_test.rb | 36 ------------------- 1 file changed, 36 deletions(-) diff --git a/test/unit/plugins/provisioners/chef/config/chef_client_test.rb b/test/unit/plugins/provisioners/chef/config/chef_client_test.rb index 25034d019..402663bf3 100644 --- a/test/unit/plugins/provisioners/chef/config/chef_client_test.rb +++ b/test/unit/plugins/provisioners/chef/config/chef_client_test.rb @@ -96,41 +96,5 @@ describe VagrantPlugins::Chef::Config::ChefClient do expect(errors).to eq([I18n.t("vagrant.config.chef.validation_key_path")]) end end - - context "when #delete_client is given" do - before { subject.delete_client = true } - - context "when knife does not exist" do - before do - allow(Vagrant::Util::Which) - .to receive(:which) - .with("knife") - .and_return(nil) - end - - it "returns an error" do - subject.finalize! - expect(errors).to eq([I18n.t("vagrant.chef_config_knife_not_found")]) - end - end - end - - context "when #delete_node is given" do - before { subject.delete_node = true } - - context "when knife does not exist" do - before do - allow(Vagrant::Util::Which) - .to receive(:which) - .with("knife") - .and_return(nil) - end - - it "returns an error" do - subject.finalize! - expect(errors).to eq([I18n.t("vagrant.chef_config_knife_not_found")]) - end - end - end end end