Skip to content

Instantly share code, notes, and snippets.

@mikeverbeck
Created September 24, 2013 19:05
Show Gist options
  • Save mikeverbeck/6689659 to your computer and use it in GitHub Desktop.
Save mikeverbeck/6689659 to your computer and use it in GitHub Desktop.
This script finds dead/zombied nodes and clients on your chef server and removes them. Note: I pulled this from a larger script so it probably needs some bandaids.
require 'fog'
require 'json'
require 'pp'
require 'chef'
connection = Fog::Compute.new(
:aws_access_key_id => #insert id here,
:aws_secret_access_key => #insert secret here,
:provider => 'AWS'
)
env = Chef::Config
env[:node_name] = "<username>"
env[:client_key] = "<user.pem>"
env[:validation_key] = "<validation.pem>"
env[:validation_client_name] = "chef-validator"
env[:chef_server_url] = "http://<chef server>:4000"
aws_instance_ids = []
instance_data = connection.describe_instances
instance_data[:body]["reservationSet"].each do |instance|
id = instance["instancesSet"].first["instanceId"]
aws_instance_ids.push(id)
end
nodes = Chef::Node.list(true)
puts "Chef Node start count: #{nodes.count}"
nodes.each do |node_name, node_obj|
chef_instance_id = node_obj.attributes.automatic["ec2"]["instance_id"]
if !aws_instance_ids.include?(chef_instance_id)
print "#{node_name} does not exist in EC2 would you like to delete (y[es],n[o]) "
response = STDIN.gets
while(!["yes", "no", "y", "n"].include?(response.strip.downcase))
print "Invalid yes or no "
response = STDIN.gets
end
if ["yes", "y"].include?(response.strip.downcase)
client = Chef::ApiClient.load(node_name)
node_obj.destroy #delete node
client.destroy #delete client
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment