Class: Fallout::Restore
- Inherits:
-
Object
- Object
- Fallout::Restore
- Defined in:
- lib/fallout/restore.rb
Instance Method Summary collapse
- #attach_volume(volume, instance, device = '/dev/sda1') ⇒ Object
- #create_volume(snapshot, availability_zone = 'us-east-1a', volume_type: 'gp2') ⇒ Object
- #delete_volume(volume) ⇒ Object
- #detach_volume(instance, device = '/dev/sda1') ⇒ Object
- #get_latest_snapshot_for_volume(volume) ⇒ Object
- #get_volume ⇒ Object
-
#initialize(options) ⇒ Restore
constructor
A new instance of Restore.
- #shutdown_instance ⇒ Object
- #start_instance(instance) ⇒ Object
Constructor Details
#initialize(options) ⇒ Restore
Returns a new instance of Restore.
3 4 5 6 7 8 |
# File 'lib/fallout/restore.rb', line 3 def initialize() @instance_id = [:instance] @volume_id = [:volume] @ec2 = AWS::EC2.new @ec2_client = AWS::EC2::Client.new end |
Instance Method Details
#attach_volume(volume, instance, device = '/dev/sda1') ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/fallout/restore.rb', line 42 def attach_volume(volume, instance, device = '/dev/sda1') volume.attach_to(instance, device) while volume.status != :in_use sleep 1 end volume end |
#create_volume(snapshot, availability_zone = 'us-east-1a', volume_type: 'gp2') ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/fallout/restore.rb', line 65 def create_volume(snapshot, availability_zone = 'us-east-1a', volume_type: 'gp2') resp = @ec2_client.create_volume( snapshot_id: snapshot.id, availability_zone: availability_zone, volume_type: volume_type) volume = @ec2.volumes[resp[:volume_id]] while volume.status != :available sleep 1 end volume end |
#delete_volume(volume) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/fallout/restore.rb', line 50 def delete_volume(volume) volume.delete while volume.status != :deleted sleep 1 end volume end |
#detach_volume(instance, device = '/dev/sda1') ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/fallout/restore.rb', line 32 def detach_volume(instance, device = '/dev/sda1') volume = @ec2.volumes[@volume_id] raise "Volume does not exist: #{volume_id}" if volume.nil? || !volume.exists? = volume.detach_from(instance, device) while volume.status != :available sleep 1 end end |
#get_latest_snapshot_for_volume(volume) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/fallout/restore.rb', line 58 def get_latest_snapshot_for_volume(volume) snapshots = @ec2.snapshots.filter('volume-id', @volume_id) raise "No snapshots for volume #{volume.id} found, aborting restore process.\n Hint: have you created a snapshot for this volume at least once?" unless snapshots.any? snapshots.max_by{|ss| Date.parse(ss..to_h[EXPIRES_AFTER_KEY]) rescue Date.new} end |
#get_volume ⇒ Object
10 11 12 |
# File 'lib/fallout/restore.rb', line 10 def get_volume @ec2.volumes[@volume_id] end |
#shutdown_instance ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/fallout/restore.rb', line 14 def shutdown_instance instance = @ec2.instances[@instance_id] raise "Instance does not exist: #{@instance_id}" if instance.nil? || !instance.exists? instance.stop while instance.status != :stopped sleep 1 end instance end |
#start_instance(instance) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/fallout/restore.rb', line 24 def start_instance(instance) instance.start while instance.status != :running sleep 1 end instance end |