Last active
May 12, 2016 03:09
-
-
Save tjsingleton/81eb9cf8c9440f53ab58437dda745864 to your computer and use it in GitHub Desktop.
For a list of instances returned from a chef search, terminate instances, release any elastic ips, and delete any attached volumes.
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
require 'time' | |
require 'json' | |
require 'pp' | |
require 'open3' | |
class DeleteShare | |
def initialize | |
@instances = Hash.new {|h,k| h[k] = [] } | |
@public_ips = Hash.new {|h,k| h[k] = [] } | |
@volumes = Hash.new {|h,k| h[k] = [] } | |
@eipallocs = Hash.new {|h,k| h[k] = [] } | |
end | |
def run | |
parse_chef_json_to_instance_ids | |
fetch_instance_details | |
fetch_address_details | |
terminate_instances | |
release_ips | |
delete_volumes | |
end | |
private | |
def parse_chef_json_to_instance_ids | |
# created with: | |
# knife search -F json '(chef_environment:prd OR chef_environment:prd-b) AND recipe:clearslide-screenshare AND fqdn:share*' -a ec2 > old_share.json | |
data = JSON.parse(File.read('old_share.json')) | |
data.fetch('rows').each do |row| | |
row.each do |host, wrapper| | |
attributes = wrapper.fetch('ec2') | |
parse_each_host(host, attributes) | |
end | |
end | |
end | |
def parse_each_host(host, attributes) | |
region = az_to_region attributes.fetch('placement_availability_zone') | |
instance_id = attributes.fetch('instance_id') | |
public_ipv4 = attributes.fetch('public_ipv4') | |
@instances[region] << instance_id | |
@public_ips[region] << public_ipv4 | |
end | |
def az_to_region(str) | |
str[0...-1] | |
end | |
def fetch_instance_details | |
@instances.each do |region, instances| | |
json = exec "aws --region=#{region} ec2 describe-instances --instance-ids #{instances.join(" ")}" | |
data = JSON.parse(json) | |
data.fetch("Reservations").each do |reservation| | |
reservation.fetch("Instances").each do |instance| | |
instance.fetch("BlockDeviceMappings").each do |device| | |
ebs = device.fetch("Ebs") | |
volume_id = ebs.fetch("VolumeId") | |
@volumes[region] << volume_id | |
end | |
end | |
end | |
end | |
end | |
def fetch_address_details | |
@public_ips.each do |region, ips| | |
json = exec "aws --region=#{region} ec2 describe-addresses --public-ips #{ips.join(" ")}" | |
data = JSON.parse(json) | |
data.fetch("Addresses").each do |address| | |
@eipallocs[region] << address["AllocationId"] | |
end | |
end | |
end | |
def terminate_instances | |
@instances.each do |region, instances| | |
exec "aws --region=#{region} ec2 terminate-instances --instance-ids #{instances.join(" ")}" | |
end | |
end | |
def release_ips | |
@eipallocs.each do |region, ids| | |
ids.each do |id| | |
exec "aws --region=#{region} ec2 release-address --allocation-id #{id}" | |
end | |
end | |
end | |
def delete_volumes | |
@volumes.each do |region, volumes| | |
volumes.each do |id| | |
exec "aws --region=#{region} ec2 delete-volume --volume-id #{id}" | |
end | |
end | |
end | |
def exec(command) | |
info "exec #{command}" | |
`#{command}` | |
end | |
def info(msg) | |
puts("#{Time.now.iso8601(3)} #{msg}") | |
end | |
end | |
DeleteShare.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment