module EasyE::Snapshotter

Constants

AWS_INSTANCE_ID_URL

Public Instance Methods

access_key() click to toggle source
# 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_volumes() click to toggle source
# File lib/easy_e/snapshotter.rb, line 36
def attached_volumes
  @attached_volumes ||= compute.volumes.select { |vol| vol.server_id == instance_id }
end
compute() click to toggle source

lazy loaders

# 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
credentials() click to toggle source
# File lib/easy_e/snapshotter.rb, line 48
def credentials
  @credentials ||= CSV.parse(File.read(options[:credentials_file]), :headers => true)
end
devices_to_snap() click to toggle source
# 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_id() click to toggle source
# File lib/easy_e/snapshotter.rb, line 52
def instance_id
  @instance_id ||= JSON.parse(HTTParty.get(AWS_INSTANCE_ID_URL))["instanceId"]
end
instance_name() click to toggle source
# File lib/easy_e/snapshotter.rb, line 56
def instance_name
  @instance_name ||= compute.servers.get(instance_id).tags['Name']
end
region() click to toggle source
# File lib/easy_e/snapshotter.rb, line 60
def region
  @region ||= JSON.parse(HTTParty.get(AWS_INSTANCE_ID_URL))["region"]
end
secret_key() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_snapshots() click to toggle source
# 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