Skip to content

Instantly share code, notes, and snippets.

@c10l
Created December 6, 2012 11:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@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