Class: EC2VolumeSnapshoter
- Inherits:
-
Object
- Object
- EC2VolumeSnapshoter
- Defined in:
- lib/ec2_volume_snapshoter.rb
Constant Summary collapse
- NAME_PREFIX =
'Volume Snapshot'- KINDS =
Kind of snapshot and their expiration in days
{ 'test' => 1, 'snapshot' => 0, 'daily' => 7, 'weekly' => 31, 'monthly' => 300, 'yearly' => 0}
Instance Attribute Summary collapse
-
#instance_id ⇒ Object
readonly
Returns the value of attribute instance_id.
Instance Method Summary collapse
- #find_volume_for_device(device) ⇒ Object
-
#initialize(aki, sak, instance_id = open("http://169.254.169.254/latest/meta-data/instance-id").read) ⇒ EC2VolumeSnapshoter
constructor
Need access_key_id, secret_access_key and instance_id If not provided, attempt to fetch current instance_id.
- #list_snapshots(devices, kind) ⇒ Object
-
#snapshot_devices(devices, name = "#{instance_id}", kind = "test", limit = KINDS[kind]) ⇒ Object
Snapshots the list of devices devices is an array of device attached to the instance (/dev/foo) name if the name of the snapshot.
Constructor Details
#initialize(aki, sak, instance_id = open("http://169.254.169.254/latest/meta-data/instance-id").read) ⇒ EC2VolumeSnapshoter
Need access_key_id, secret_access_key and instance_id If not provided, attempt to fetch current instance_id
48 49 50 51 52 53 |
# File 'lib/ec2_volume_snapshoter.rb', line 48 def initialize(aki, sak, instance_id = open("http://169.254.169.254/latest/meta-data/instance-id").read) @instance_id = instance_id @compute = Fog::Compute.new({:provider => 'AWS', :aws_access_key_id => aki, :aws_secret_access_key => sak}) end |
Instance Attribute Details
#instance_id ⇒ Object (readonly)
Returns the value of attribute instance_id.
45 46 47 |
# File 'lib/ec2_volume_snapshoter.rb', line 45 def instance_id @instance_id end |
Instance Method Details
#find_volume_for_device(device) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/ec2_volume_snapshoter.rb', line 134 def find_volume_for_device(device) my = [] @compute.volumes.all().each do |volume| if volume.server_id == @instance_id my << volume if volume.device == device return volume end end end raise NoSuchVolumeException.new(@instance_id, device, my) end |
#list_snapshots(devices, kind) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ec2_volume_snapshoter.rb', line 110 def list_snapshots(devices, kind) volume_map = [] snapshots = {} = @compute..all(:key => 'instance_id', :value => instance_id) .each do |tag| snap = @compute.snapshots.get(tag.resource_id) t = snap. if devices.include?(t['device']) && instance_id == t['instance_id'] && NAME_PREFIX == t['application'] && kind == t['kind'] snapshots[t['date']] ||= [] snapshots[t['date']] << snap end end # take out incomplete backups snapshots.delete_if{ |date, snaps| snaps.length != devices.length } snapshots end |
#snapshot_devices(devices, name = "#{instance_id}", kind = "test", limit = KINDS[kind]) ⇒ Object
Snapshots the list of devices devices is an array of device attached to the instance (/dev/foo) name if the name of the snapshot
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ec2_volume_snapshoter.rb', line 57 def snapshot_devices(devices, name = "#{instance_id}", kind = "test", limit = KINDS[kind]) log "Snapshot of kind #{kind}, limit set to #{limit} (0 means never purge)" ts = DateTime.now.to_s name = "#{NAME_PREFIX}:" + name volumes = {} devices.each do |device| volumes[device] = find_volume_for_device(device) end sn = [] volumes.each do |device, volume| log "Creating volume snapshot for #{device} on instance #{instance_id}" snapshot = volume.snapshots.new snapshot.description = name+" #{device}" snapshot.save sn << snapshot snapshot.reload @compute..create(:resource_id => snapshot.id, :key =>"application", :value => NAME_PREFIX) @compute..create(:resource_id => snapshot.id, :key =>"device", :value => device) @compute..create(:resource_id => snapshot.id, :key =>"instance_id", :value =>instance_id) @compute..create(:resource_id => snapshot.id, :key =>"date", :value => ts) @compute..create(:resource_id => snapshot.id, :key =>"kind", :value => kind) end log "Waiting for snapshots to complete." sn.each do |s| begin sleep(3) s.reload end while s.state == 'nil' || s.state == 'pending' end if limit != 0 # populate data structure with updated information snapshots = list_snapshots(devices, kind) nsnaps = snapshots.keys.length if nsnaps-limit > 0 dates = snapshots.keys.sort puts dates.inspect extra_snapshots = dates[0..-limit] remaining_snapshots = dates[-limit..-1] extra_snapshots.each do |date| snapshots[date].each do |snap| log "Destroying #{snap.description} #{snap.id}" snap.destroy end end end end end |