Last active
August 29, 2015 14:13
-
-
Save EdHurtig/e8371a103abf90c6d5f7 to your computer and use it in GitHub Desktop.
Searches AWS for zombie test servers
This file contains 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
#!/usr/bin/env ruby | |
require 'aws-sdk' | |
require 'colorize' | |
AWS.config( | |
access_key_id: ENV['AWS_ACCESS_KEY_ID'], | |
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']) | |
if ARGV.any? | |
security_group = ARGV[0] | |
elsif ENV['AWS_TESTING_SECURITY_GROUP'] | |
security_group = ENV['AWS_TESTING_SECURITY_GROUP'] | |
else | |
security_group = 'ci-testing' | |
end | |
puts "Searching for Instances in the '#{security_group}' security group" | |
ec2 = AWS::EC2.new | |
instances = ec2.security_groups.filter('group-name', security_group).first.instances | |
puts "Discovered #{instances.count} Test Instances.\n" | |
i = 0 | |
stoppable = [] | |
instances.each do |instance| | |
i += 1 | |
line = " #{i}. #{instance.id} : #{instance.tags.Name} : #{instance.status}" | |
if instance.status == :terminated | |
puts line.red | |
elsif instance.status == :running | |
puts line.green | |
stoppable << instance | |
else | |
puts line.yellow | |
end | |
end | |
puts '' | |
if stoppable.any? | |
stoppable.each do |instance| | |
puts "Instance #{instance.id} (#{instance.tags.Name}) is #{instance.status}." | |
print 'Would you like to terminate it? [yN]' | |
if 'y' == gets.chomp | |
print "Are you absolutely sure you wish to terminate instance #{instance.id} (#{instance.tags.Name}) [yN]".red | |
if 'y' == gets.chomp | |
puts "Terminating Instance #{instance.id} (#{instance.tags.Name})" | |
instance.terminate | |
else | |
puts 'Ok, Won\'t Terminate Instance'.green | |
end | |
else | |
puts 'Ok, Won\'t Terminate Instance'.green | |
end | |
end | |
end | |
puts 'Zombie Checking Complete' | |
Updated to be significantly faster (Uses SecurityGroup.instances to get the list of just the testing instances instead of iterating through all instances)
Also added in ability to terminate instances right from the program.
@EdHurtig this is great. We've got evertrue/devops as a place for scripts like this; its got a scripts
folder. Please put this in there, chmod +x
, commit & push!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can run
instance.stop
in the last each block. Also can add a condition to line 18 to check created date/time