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 Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#computeObject

lazy loaders



19
20
21
22
23
24
25
26
27
# File 'lib/easy_e/snapshotter.rb', line 19

def compute
  require 'fog/aws'
  Fog.mock! if options[:mock]
  @compute ||= Fog::Compute.new({
    :aws_access_key_id => access_key,
    :aws_secret_access_key => secret_key,
    :provider => "AWS"
  }) 
end

#instance_idObject



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

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

#storage=(value) ⇒ Object (writeonly)

Sets the attribute storage



7
8
9
# File 'lib/easy_e/snapshotter.rb', line 7

def storage=(value)
  @storage = value
end

Instance Method Details

#access_keyObject



33
34
35
# File 'lib/easy_e/snapshotter.rb', line 33

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

#attached_volumesObject



29
30
31
# File 'lib/easy_e/snapshotter.rb', line 29

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

#credentialsObject



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

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

#secret_keyObject



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

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

#take_snapshotsObject



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

def take_snapshots
  attached_volumes.collect do |vol|
    logger.debug "Snapping #{vol.volume_id}"
    snapshot = compute.snapshots.new
    snapshot.volume_id = vol.volume_id
    snapshot.save
    snapshot
  end
end