Class: SnapshotOptimization::IdentifyDuplicateSnapshots

Inherits:
SnapshotOptimizationState show all
Defined in:
lib/scripts/ec2/snapshot_optimization.rb

Overview

All snapshots retrieved. Group them by volume and identify duplicates

Instance Attribute Summary

Attributes inherited from ScriptExecutionState

#context, #logger

Instance Method Summary collapse

Methods inherited from SnapshotOptimizationState

load_state

Methods inherited from ScriptExecutionState

#done?, #end_state, #failed?, #initialize, #register_state_change_listener, #start_state_machine, #to_s

Methods included from StateTransitionHelper

#attach_volume, #check_string_alnum, #connect, #copy_distribution, #create_fs, #create_image_from_instance, #create_labeled_fs, #create_snapshot, #create_volume, #create_volume_from_snapshot, #delete_snapshot, #delete_volume, #describe_instance, #detach_volume, #determine_file, #disable_ssh_tty, #disconnect, #ec2_handler, #ec2_handler=, #enable_ssh_tty, #get_aws_kernel_image_aki, #get_aws_region_from_endpoint, #get_partition_fs_type, #get_partition_fs_type_and_label, #get_partition_label, #get_root_device_name, #get_root_partition_fs_type, #get_root_partition_fs_type_and_label, #get_root_partition_label, #get_root_volume_id, #launch_instance, #local_decompress_and_dump_file_to_device, #local_dump_and_compress_device_to_file, #local_dump_device_to_file, #local_dump_file_to_device, #mount_fs, #mount_fs_old, #register_snapshot, #remote_copy, #remote_copy_old, #remote_handler, #remote_handler=, #retrieve_instances, #retrieve_security_groups, #shut_down_instance, #snapshot_accessible, #start_instance, #stop_instance, #unmount_fs, #upload_file, #zip_volume

Methods included from VCloudTransitionHelper

#retrieve_ip_services

Constructor Details

This class inherits a constructor from ScriptExecutionState

Instance Method Details

#enterObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/scripts/ec2/snapshot_optimization.rb', line 64

def enter
  post_message("Going to check for duplicates among snapshots")
  volume_map = {}
  unless @context[:snapshots]['snapshotSet'] == nil
    @context[:snapshots]['snapshotSet']['item'].each() do |snapshot|
      next unless snapshot['progress'] == "100%"
      next if snapshot['ownerAlias'] == "amazon"
      snaps = volume_map[snapshot['volumeId']]
      if snaps == nil
        snaps = []
        volume_map[snapshot['volumeId']] = snaps
      end
      snaps << snapshot
    end
  end
  #
  volume_map.each() do |volume_id, snapshots|
    to_delete = snapshots.size - @context[:max_duplicate_snapshots]
    if to_delete <= 0
      post_message("Number of snapshots for volume #{volume_id} (=#{snapshots.size}) is smaller than #{@context[:max_duplicate_snapshots]} => ignore")
    else
      sorted_snaps = snapshots.sort() do |snap1, snap2|
        Time.parse(snap1['startTime']) <=> Time.parse(snap2['startTime'])
      end
      post_message("Identified #{to_delete} snapshots for volume #{volume_id}")
      @logger.info("not sorted   = #{snapshots.inspect}")
      @logger.info("sorted snaps = #{sorted_snaps.inspect}")
      0.upto(to_delete-1) do |i|
        @context[:result][:duplicate_snapshots] << sorted_snaps[i]['snapshotId']
      end
    end
  end
  if @context[:delete_snapshots]
    DeleteDuplicateSnapshots.new(@context)
  else
    RetrieveVolumes.new(@context)
  end
end