Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#backoffObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'bin/ec2-rotate-volume-snapshots', line 24

def backoff()
  $backoffed = $backoffed + 1

  if $opts[:backoff_limit] > 0 && $opts[backoff_limit] < $backoffed
    puts "Too many backoff attempts. Sorry it didn't work out."
    exit 2
  end

  naptime = rand(60) * $backoffed
  puts "Backing off for #{naptime} seconds..."
  sleep naptime
end

#rotate_em(snapshots) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
# File 'bin/ec2-rotate-volume-snapshots', line 37

def rotate_em(snapshots)
  # poor man's way to get a deep copy of our time_periods definition hash
  periods = Marshal.load(Marshal.dump($time_periods))
  
  snapshots.each do |snapshot|
    time = Time.parse(snapshot[:aws_started_at])
    snapshot_id = snapshot[:aws_id]
    description = snapshot[:aws_description]
    keep_reason = nil
    
    if $opts[:pattern] && description !~ /#{$opts[:pattern]}/
      puts "  #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Skipping snapshot with description #{description}"
      next
    end
    
    periods.keys.sort { |a, b| periods[a][:seconds] <=> periods[b][:seconds] }.each do |period|
      period_info = periods[period]
      keep = period_info[:keep]
      keeping = period_info[:keeping]
      
      time_string = time.strftime period_info[:format]
      if Time.now - time < keep * period_info[:seconds]
        if !keeping.key?(time_string) && keeping.length < keep
          keep_reason = period
          keeping[time_string] = snapshot
        end
        break
      end
    end
    
    if keep_reason.nil? && snapshot == snapshots.last && $opts[:keep_last]
      keep_reason = 'last snapshot'
    end
    
    if !keep_reason.nil?
      puts "  #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Keeping for #{keep_reason}"
    else
      puts "  #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Deleting"
      begin
        $ec2.delete_snapshot(snapshot_id) unless $opts[:dry_run]
      rescue RightAws::AwsError => e
        # If we have been sending too many EC2 requests, then let's backoff a random
        # amount of time and try again later.
        if e.errors.kind_of? Array
          if e.errors[0][0] == 'RequestLimitExceeded'
            backoff()
            retry
          else
            raise e
          end
        else
          raise e
        end
      end
    end
  end
end

#split_tag(hash, v) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'bin/ec2-rotate-volume-snapshots', line 96

def split_tag(hash,v)
    v.split(',').each do |pair|
        tag, value = pair.split('=',2)
        if value.nil?
          puts "invalid tag=value format"
          exit 1
        end
        hash[tag] = value
    end
end