From 370d6b0c9881753d0b266e69ba3e5ce16dc2c8d3 Mon Sep 17 00:00:00 2001 From: Nate Smith Date: Mon, 17 Dec 2012 11:49:27 -0500 Subject: [PATCH 1/3] Delete Chef client and node on cleanup This fixes #1253 by shelling out to `knife` on cleanup, as per @mitchellh's suggestion in #1255 --- .../provisioners/chef/provisioner/chef_client.rb | 16 ++++++++++++++++ templates/locales/en.yml | 1 + 2 files changed, 17 insertions(+) diff --git a/plugins/provisioners/chef/provisioner/chef_client.rb b/plugins/provisioners/chef/provisioner/chef_client.rb index ebbf673f1..d1b3e7461 100644 --- a/plugins/provisioners/chef/provisioner/chef_client.rb +++ b/plugins/provisioners/chef/provisioner/chef_client.rb @@ -18,6 +18,8 @@ module VagrantPlugins attr_accessor :environment attr_accessor :encrypted_data_bag_secret_key_path attr_accessor :encrypted_data_bag_secret + attr_accessor :delete_client + attr_accessor :delete_node # Provide defaults in such a way that they won't override the instance # variable. This is so merging continues to work properly. @@ -26,6 +28,8 @@ module VagrantPlugins def file_cache_path; @file_cache_path || "/srv/chef/file_store"; end def file_backup_path; @file_backup_path || "/srv/chef/cache"; end def encrypted_data_bag_secret; @encrypted_data_bag_secret || "/tmp/encrypted_data_bag_secret"; end + def delete_client; @delete_client || true; end + def delete_node; @delete_node || true; end def validate(env, errors) super @@ -129,6 +133,18 @@ module VagrantPlugins def guest_validation_key_path File.join(config.provisioning_path, "validation.pem") end + + def cleanup + delete_from_chef_server('client') if config.delete_client + delete_from_chef_server('node') if config.delete_node + end + + def delete_from_chef_server(deletable) + node_name = (config.node_name || env[:vm].config.vm.host_name) + env[:ui].info I18n.t("vagrant.provisioners.chef.deleting_from_server", + :deletable => deletable) + Kernel.system("knife #{deletable} delete --yes #{node_name}") + end end end end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 28f4600d1..005197198 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -721,6 +721,7 @@ en: server_validation_key_doesnt_exist: |- The validation key set for `config.chef.validation_key_path` does not exist! This file needs to exist so it can be uploaded to the virtual machine. + deleting_from_server: "Deleting %{deletable} from Chef server..." puppet: not_detected: |- From 94ce12683ed55d60a165dbe4e26e69b1b9d82b44 Mon Sep 17 00:00:00 2001 From: Nate Smith Date: Mon, 17 Dec 2012 11:58:15 -0500 Subject: [PATCH 2/3] Show node name in logging --- plugins/provisioners/chef/provisioner/chef_client.rb | 2 +- templates/locales/en.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/provisioners/chef/provisioner/chef_client.rb b/plugins/provisioners/chef/provisioner/chef_client.rb index d1b3e7461..32bb76350 100644 --- a/plugins/provisioners/chef/provisioner/chef_client.rb +++ b/plugins/provisioners/chef/provisioner/chef_client.rb @@ -142,7 +142,7 @@ module VagrantPlugins def delete_from_chef_server(deletable) node_name = (config.node_name || env[:vm].config.vm.host_name) env[:ui].info I18n.t("vagrant.provisioners.chef.deleting_from_server", - :deletable => deletable) + :deletable => deletable, :name => node_name) Kernel.system("knife #{deletable} delete --yes #{node_name}") end end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 005197198..442a3035f 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -721,7 +721,7 @@ en: server_validation_key_doesnt_exist: |- The validation key set for `config.chef.validation_key_path` does not exist! This file needs to exist so it can be uploaded to the virtual machine. - deleting_from_server: "Deleting %{deletable} from Chef server..." + deleting_from_server: "Deleting %{deletable} \"%{name}\" from Chef server..." puppet: not_detected: |- From 691cca7911e1e8412e4e8745c6b93424a1711477 Mon Sep 17 00:00:00 2001 From: Nate Smith Date: Mon, 17 Dec 2012 12:04:53 -0500 Subject: [PATCH 3/3] Suppress knife output --- plugins/provisioners/chef/provisioner/chef_client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/provisioners/chef/provisioner/chef_client.rb b/plugins/provisioners/chef/provisioner/chef_client.rb index 32bb76350..a43ad7518 100644 --- a/plugins/provisioners/chef/provisioner/chef_client.rb +++ b/plugins/provisioners/chef/provisioner/chef_client.rb @@ -143,7 +143,7 @@ module VagrantPlugins node_name = (config.node_name || env[:vm].config.vm.host_name) env[:ui].info I18n.t("vagrant.provisioners.chef.deleting_from_server", :deletable => deletable, :name => node_name) - Kernel.system("knife #{deletable} delete --yes #{node_name}") + Kernel.system("knife #{deletable} delete --yes #{node_name} > /dev/null 2>&1") end end end