Skip to content

Instantly share code, notes, and snippets.

@c10l
Created December 6, 2012 11:57
Show Gist options
  • Save c10l/4223941 to your computer and use it in GitHub Desktop.
Save c10l/4223941 to your computer and use it in GitHub Desktop.
Vagrant delete Chef node and Client on destroy
module Vagrant
module Provisioners
class ChefClient
def cleanup
env[:ui].info "Attempting to remove client #{env[:vm].config.vm.host_name}"
`knife client show #{env[:vm].config.vm.host_name}`
if $?.to_i == 0
env[:ui].info "Removing client #{env[:vm].config.vm.host_name}"
`knife client delete #{env[:vm].config.vm.host_name} -y`
end
env[:ui].info "Attempting to remove node #{env[:vm].config.vm.host_name}"
`knife node show #{env[:vm].config.vm.host_name}`
if $?.to_i == 0
env[:ui].info "Removing node #{env[:vm].config.vm.host_name}"
`knife node delete #{env[:vm].config.vm.host_name} -y`
end
end
end
end
end
@c10l
Copy link
Author

c10l commented Dec 6, 2012

Stick this to your Vagrantfile and the Chef's node and client will be deleted when you destroy the VM.

This is not an original piece of code, it was adapted from these references:
https://gist.github.com/1010660
http://frank.be/articles/2011/12/16/vagrant-and-chef-auto-deregister-on-vm-destroy/

@jflowers
Copy link

I get this error if I just stick that at the end of my Vagrantfile:

$ vagrant aws destroy
/var/lib/jenkins/jobs/cloudss-deploy-master/workspace/infrastructure/vagrant/boxes/test-aws/Vagrantfile:52: uninitialized constant Vagrant::Provisioners::Chef (NameError)
from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/lib/vagrant/config/loader.rb:115:in load' from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/lib/vagrant/config/loader.rb:115:inprocs_for_source'
from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/lib/vagrant/config.rb:41:in capture_configures' from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/lib/vagrant/config.rb:36:insynchronize'
from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/lib/vagrant/config.rb:36:in capture_configures' from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/lib/vagrant/config/loader.rb:114:inprocs_for_source'
from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/lib/vagrant/config/loader.rb:51:in set' from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/lib/vagrant/config/loader.rb:45:ineach'
from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/lib/vagrant/config/loader.rb:45:in set' from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/lib/vagrant/environment.rb:377:inload_config!'
from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/lib/vagrant/environment.rb:392:in call' from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/lib/vagrant/environment.rb:392:inload_config!'
from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/lib/vagrant/environment.rb:327:in load!' from /usr/lib64/ruby/gems/1.8/gems/vagrant-1.0.6.dev/bin/vagrant:40 from /usr/bin/vagrant:19:inload'
from /usr/bin/vagrant:19

@rodriguezsergio
Copy link

I get the following:

$ vagrant destroy

Are you sure you want to destroy the 'default' VM? [Y/N] y
[default] Forcing shutdown of VM...
[default] Attempting to remove client dummyVM

/Users/sergio/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find chef (>= 0) amongst [archive-tar-minitar-0.5.2, childprocess-0.3.5, erubis-2.7.0, ffi-1.1.5, i18n-0.6.1, json-1.5.4, log4r-1.1.10, net-scp-1.0.4, net-ssh-2.2.2, vagrant-1.0.5] (Gem::LoadError)

Which is just a listing of gems from

/Applications/Vagrant/embedded/gems/gems/

Any idea on how to fix it?

@FractalizeR
Copy link

What if Vagrant is configured to take node name from somewhere? Mine is configured like this:

NODE_FILE = "./chef.node"

I think in this case this snippet will not work. Also it gives an error:

rubygems_integration.rb:252:in block in replace_gem': chef is not part of the bundle. Add it to Gemfile.`

@lukas0krupa
Copy link

@FractalizeR I was having similar issue, I read most of conversations and no luck with any workaround provided. You may got here as well (hashicorp/vagrant#2476)

Then I tried similar thing, I removed/uninstall vagrant and install it again. However, I got another error, which sounds better:

stderr: WARNING: No knife configuration file found ERROR: Your private key could not be loaded from /etc/chef/client.pem

It might help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment