Skip to content

Instantly share code, notes, and snippets.

@apsoto
Forked from Aslan/gist:328766
Created March 11, 2010 21:51
Show Gist options
  • Save apsoto/329710 to your computer and use it in GitHub Desktop.
Save apsoto/329710 to your computer and use it in GitHub Desktop.
Delete ec2 terminated nodes from the chef server
#!/usr/bin/ruby
instances = `ec2-describe-instances --show-empty-fields`
lines = instances.split(/\n/)
running_instances = []
lines.each do |lines|
if lines =~ /^INSTANCE/
fields = lines.split(/\t/)
if fields[5] == 'running'
running_instances << fields[1]
running_instances << fields[4]
end
end
end
nodes = `knife node list`
nodes_arr = eval(nodes.gsub!(/\n/,''))
puts "deleting the following nodes from chef server :"
(nodes_arr - running_instances).each do |n|
cmd = "knife node delete #{n} -y"
puts "\t #{cmd}"
puts `#{cmd}`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment