Created
March 29, 2013 11:54
-
-
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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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