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
- #access_key ⇒ Object
- #attached_volumes ⇒ Object
-
#compute ⇒ Object
lazy loaders.
- #credentials ⇒ Object
- #devices_to_snap ⇒ Object
- #instance_id ⇒ Object
- #instance_name ⇒ Object
- #region ⇒ Object
- #secret_key ⇒ Object
- #should_snap(vol) ⇒ Object
- #snapshot_name(vol) ⇒ Object
- #take_snapshots ⇒ Object
Instance Method Details
#access_key ⇒ Object
40 41 42 |
# File 'lib/easy_e/snapshotter.rb', line 40 def access_key @access_key ||= if [:credentials_file] then credentials.first["Access Key Id"] else [:access_key] end end |
#attached_volumes ⇒ Object
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 |
#compute ⇒ Object
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 [: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 ⇒ Object
48 49 50 |
# File 'lib/easy_e/snapshotter.rb', line 48 def credentials @credentials ||= CSV.parse(File.read([:credentials_file]), :headers => true) end |
#devices_to_snap ⇒ Object
75 76 77 78 79 |
# File 'lib/easy_e/snapshotter.rb', line 75 def devices_to_snap @devices_to_snap ||= .directory.split(',').map { |dir| `df --output=source #{dir} | grep dev`.strip } logger.debug @devices_to_snap @devices_to_snap end |
#instance_id ⇒ Object
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_name ⇒ Object
56 57 58 |
# File 'lib/easy_e/snapshotter.rb', line 56 def instance_name @instance_name ||= compute.servers.get(instance_id).['Name'] end |
#region ⇒ Object
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_key ⇒ Object
44 45 46 |
# File 'lib/easy_e/snapshotter.rb', line 44 def secret_key @secret_key ||= if [:credentials_file] then credentials.first["Secret Access Key"] else [: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 .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_snapshots ⇒ Object
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 |