Module: EasyE::Snapshotter

Included in:
EasyE
Defined in:
lib/easy_e/snapshotter.rb

Constant Summary collapse

AWS_INSTANCE_ID_URL =
'http://169.254.169.254/latest/dynamic/instance-identity/document'

Instance Method Summary collapse

Instance Method Details

#access_keyObject



40
41
42
# File 'lib/easy_e/snapshotter.rb', line 40

def access_key
  @access_key ||= if options[:credentials_file] then credentials.first["Access Key Id"] else options[:access_key] end
end

#attached_volumesObject



36
37
38
# File 'lib/easy_e/snapshotter.rb', line 36

def attached_volumes
  @attached_volumes ||= compute.volumes.select { |vol| vol.server_id == instance_id }
end

#computeObject

lazy loaders



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/easy_e/snapshotter.rb', line 19

def compute
  require 'fog/aws'
  if options[:mock]
    Fog.mock!
    @region = 'us-east-1'
    @instance_id = 'i-deadbeef'
    @instance_name = 'totally-not-the-cia'
  end

  @compute ||= Fog::Compute.new({
    :aws_access_key_id => access_key,
    :aws_secret_access_key => secret_key,
    :region => region,
    :provider => "AWS"
  }) 
end

#credentialsObject



48
49
50
# File 'lib/easy_e/snapshotter.rb', line 48

def credentials
  @credentials ||= CSV.parse(File.read(options[:credentials_file]), :headers => true)
end

#devices_to_snapObject



75
76
77
78
79
# File 'lib/easy_e/snapshotter.rb', line 75

def devices_to_snap
  @devices_to_snap ||= options.directory.split(',').map { |dir| `df --output=source #{dir} | grep dev`.strip }
  logger.debug @devices_to_snap
  @devices_to_snap
end

#instance_idObject



52
53
54
# File 'lib/easy_e/snapshotter.rb', line 52

def instance_id
  @instance_id ||= JSON.parse(HTTParty.get(AWS_INSTANCE_ID_URL))["instanceId"]
end

#instance_nameObject



56
57
58
# File 'lib/easy_e/snapshotter.rb', line 56

def instance_name
  @instance_name ||= compute.servers.get(instance_id).tags['Name']
end

#regionObject



60
61
62
# File 'lib/easy_e/snapshotter.rb', line 60

def region
  @region ||= JSON.parse(HTTParty.get(AWS_INSTANCE_ID_URL))["region"]
end

#secret_keyObject



44
45
46
# File 'lib/easy_e/snapshotter.rb', line 44

def secret_key
  @secret_key ||= if options[:credentials_file] then credentials.first["Secret Access Key"] else options[:secret_key] end
end

#should_snap(vol) ⇒ Object



70
71
72
73
# File 'lib/easy_e/snapshotter.rb', line 70

def should_snap vol
  normalized_device = vol.device.gsub('/dev/s', '/dev/xv') rescue vol.device
  options.directory.nil? or devices_to_snap.include?(normalized_device)
end

#snapshot_name(vol) ⇒ Object



64
65
66
67
68
# File 'lib/easy_e/snapshotter.rb', line 64

def snapshot_name vol
  id = instance_name
  id = instance_id if id.nil? or id.empty?
  "#{Time.now.strftime "%Y%m%d%H%M%S"}-#{id}-#{vol.device}"
end

#take_snapshotsObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/easy_e/snapshotter.rb', line 6

def take_snapshots
  attached_volumes.collect do |vol|
    next unless should_snap vol
    logger.debug "Snapping #{vol.id}"
    snapshot = compute.snapshots.new
    snapshot.volume_id = vol.id
    snapshot.description = snapshot_name(vol)
    snapshot.save
    snapshot
  end
end