Created
June 12, 2011 03:00
-
-
Save jbarciauskas/1021207 to your computer and use it in GitHub Desktop.
Script to manage ebs raid volumes with logical names and timestamps
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
require 'rubygems' | |
require 'fog' | |
require 'ap' | |
require 'time' | |
def print_usage() | |
puts "Assumes all devices from sdc to sdz are available" | |
puts "Usage: volume attach [aws instance id] ['raid-name' tag value] ['snap-time' tag value]" | |
puts "Usage: volume detach ['raid-name' tag value] ['snap-time' tag value]" | |
puts "Usage: volume create ['raid-name' tag value] ['snap-time' tag value]" | |
puts "Usage: snapshot create ['raid-name' tag value] [previous 'snap-time' tag value (optional)] [new raid tag (optional)]" | |
exit 1 | |
end | |
settings = YAML.load_file("#{ENV['HOME']}/.fog") | |
aws_access_key = settings['default']['aws_access_key_id'] | |
aws_secret_key = settings['default']['aws_secret_access_key'] | |
AWS = Fog::Compute.new(:provider => 'AWS', :aws_access_key_id => aws_access_key, :aws_secret_access_key => aws_secret_key) | |
def attach_volume(instance_id, raid_tag, snap_time) | |
ec2instance = AWS.servers.get(instance_id) | |
puts("Attaching volumes to: #{ec2instance.tags['Name']}") | |
volumes = get_vols_by_tags(raid_tag, snap_time) | |
devices = ('sdc'..'sdz').to_a | |
volumes.each_with_index do |vol, i| | |
puts("Attaching #{vol.id} => /dev/#{devices[i]}") | |
vol.device = "/dev/#{devices[i]}" | |
vol.server = ec2instance | |
end | |
end | |
def detach_volume(raid_tag, snap_time) | |
volumes = get_vols_by_tags(raid_tag, snap_time) | |
volumes.each do |vol| | |
puts("Detaching #{vol.id}...") | |
vol.device = nil | |
vol.server = nil | |
end | |
end | |
def create_volume(raid_tag, snap_time) | |
snapshots = AWS.snapshots.all({'tag:raid-name'=> raid_tag, 'tag:snap-time' => snap_time}) | |
new_snap_time = Time.now.to_i; | |
puts("Creating raid-name #{raid_tag} with snap-time #{new_snap_time}") | |
snapshots.each do |snap| | |
puts("Creating volume from snapshot id #{snap.id}") | |
volume = AWS.volumes.new :snapshot_id => snap.id, :size => snap.volume_size, :availability_zone => 'us-east-1c' | |
volume.save | |
add_tags(volume, raid_tag, new_snap_time) | |
end | |
end | |
def create_snapshot(raid_tag, prev_snap_time, new_raid_tag) | |
if(new_raid_tag == nil) | |
new_raid_tag = raid_tag | |
snap_time = Time.now.to_i; | |
puts("Creating snapshot collection with raid-name tag #{new_raid_tag} and snap-time of #{snap_time}") | |
volumes = get_vols_by_tags(raid_tag, prev_snap_time) | |
volumes.each_with_index do |vol, i| | |
puts("Snapshotting volume #{vol.id} with raid-name #{new_raid_tag} and snap-time #{snap_time}...") | |
snap = AWS.snapshots.new :volume_id => vol.id, :description => "Snapshot of raid-name #{new_raid_tag} from #{Time.now}" | |
snap.save | |
add_tags(snap, new_raid_tag, snap_time) | |
end | |
end | |
def get_vols_by_tags(raid_tag, snap_time) | |
if(snap_time == nil) | |
return AWS.volumes.all({'tag:raid-name'=> raid_tag}) | |
else | |
return AWS.volumes.all({'tag:raid-name'=> raid_tag, 'tag:snap-time' => snap_time}) | |
end | |
end | |
def add_tags(resource, raid_tag, snap_time) | |
snap_tag = AWS.tags.new :resource_id => resource.id, :key => 'snap-time', :value => snap_time | |
snap_tag.save | |
raid_tag_obj = AWS.tags.new :resource_id => resource.id, :key => 'raid-name', :value => raid_tag | |
raid_tag_obj.save | |
end | |
if ARGV.length >= 3 | |
category = ARGV.shift | |
action = ARGV.shift | |
if category == 'volume' | |
if action == 'attach' | |
if ARGV.length == 3 | |
attach_volume(ARGV.shift, ARGV.shift, ARGV.shift) | |
exit 0 | |
end | |
elsif action == 'detach' | |
if ARGV.length == 2 | |
detach_volume(ARGV.shift, ARGV.shift) | |
exit 0 | |
end | |
elsif action == 'create' | |
if ARGV.length == 2 | |
create_volume(ARGV.shift, ARGV.shift) | |
exit 0 | |
end | |
end | |
elsif category == 'snapshot' | |
if action == 'create' | |
create_snapshot(ARGV.shift, ARGV.shift, ARGV.shift) | |
exit 0 | |
end | |
end | |
end | |
print_usage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment