Class: Fallout::Restore

Inherits:
Object
  • Object
show all
Defined in:
lib/fallout/restore.rb

Instance Method Summary collapse

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(options)
  @instance_id = options[:instance]
  @volume_id = options[: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?
  attachment = volume.detach_from(instance, device)
  while volume.status != :available
    sleep 1
  end
  attachment
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.tags.to_h[EXPIRES_AFTER_KEY]) rescue Date.new}
end

#get_volumeObject



10
11
12
# File 'lib/fallout/restore.rb', line 10

def get_volume
  @ec2.volumes[@volume_id]
end

#shutdown_instanceObject



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