Skip to content

Instantly share code, notes, and snippets.

@priestjim
Created March 29, 2013 11:54
Show Gist options
  • Save priestjim/5270411 to your computer and use it in GitHub Desktop.
Save priestjim/5270411 to your computer and use it in GitHub Desktop.
An easy way to cleanup stale nodes (left from autoscaling, Vagrant/Vagabond boxes etc) on a Chef server. Use this on your chef-server role cookbook. Warning: this requires the Chef server client to be an admin!
# Modify the stale time accordingly. Currently, this is set to 2 days.
# You can also modify the search query to include only development/QA environments like:
# chef_last_run:[* TO #{Time.now.to_i - (86400 *2)}] AND (chef_environment:dev OR chef_environment:test)
require 'time'
if Chef::ApiClient.load(node.name).admin
Chef::Log.info("This is an administrative node. Stale node cleanup will take place")
search(:node, "ohai_time:[* TO #{Time.now.to_i - (86400 *2)}]").each do |n|
next if n.name.eql?(node.name) # Skip ourselves!
Chef::Log.info("Node #{n.name} hasn't checked in for 2 days, cleaning up...")
Chef::ApiClient.load(n.name).destroy
n.destroy
end
end
@brenguy
Copy link

brenguy commented Jun 15, 2015

This will only work if your chef-client is daemonized. If you don't daemonize your chef-clients then you can use this cookbook:

https://supermarket.chef.io/cookbooks/scaledown_cleanup

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