ec2-rotate-volume-snapshots 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
@@ -9,7 +9,8 @@ opts = {
9
9
  :aws_secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"],
10
10
  :aws_region => 'us-east-1',
11
11
  :pattern => nil,
12
- :dry_run => false
12
+ :dry_run => false,
13
+ :backoff_limit => 0
13
14
  }
14
15
 
15
16
  time_periods = {
@@ -37,6 +38,10 @@ OptionParser.new do |o|
37
38
  opts[:pattern] = v
38
39
  end
39
40
 
41
+ o.on("--backoff-limit LIMIT", "Backoff and retry when hitting EC2 Request Limit exceptions no more than this many times. Default is 0 (no limit)") do |v|
42
+ opts[:backoff_limit] = v
43
+ end
44
+
40
45
  time_periods.keys.sort { |a, b| time_periods[a][:seconds] <=> time_periods[b][:seconds] }.each do |period|
41
46
  o.on("--keep-#{period} NUMBER", Integer, "Number of #{period} snapshots to keep") do |v|
42
47
  time_periods[period][:keep] = v
@@ -63,6 +68,12 @@ if ARGV.empty?
63
68
  end
64
69
  volume_ids = ARGV
65
70
 
71
+ if opts[:backoff_limit] < 0
72
+ puts "A negative backoff limit doesn't make much sense."
73
+ exit 1
74
+ end
75
+
76
+ backoffed = 0
66
77
  ec2 = RightAws::Ec2.new(opts[:aws_access_key], opts[:aws_secret_access_key], :region => opts[:aws_region])
67
78
  all_snapshots = ec2.describe_snapshots(:filters => { 'volume-id' => volume_ids })
68
79
 
@@ -116,18 +127,30 @@ volume_ids.each do |volume_id|
116
127
  puts " #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Keeping for #{keep_reason}"
117
128
  else
118
129
  puts " #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Deleting"
119
- ec2.delete_snapshot(snapshot_id) unless opts[:dry_run]
120
-
121
- # Hardcore rigorous testing has shown that sleeping 5 seconds after 50 deletes
122
- # keeps the RequestLimitExceeded error away in 2 out of 2 trials, while sleeping
123
- # 1 second was insufficient.
124
- # There doesn't seem to be any published numbers for what the limit actually is,
125
- # and there does seem to be plenty of implications that it depends on your account,
126
- # the AWS service, and possibly phases of the moon and AWS's internal activities.
127
- deletes = deletes + 1
128
- if deletes > 50
129
- deletes = 0
130
- sleep 5
130
+ begin
131
+ ec2.delete_snapshot(snapshot_id) unless opts[:dry_run]
132
+ rescue RightAws::AwsError => e
133
+ # If we have been sending too many EC2 requests, then let's backoff a random
134
+ # amount of time and try again later.
135
+ if e.errors.kind_of? Array
136
+ if e.errors[0][0] == 'RequestLimitExceeded'
137
+ backoffed = backoffed + 1
138
+
139
+ if opts[:backoff_limit] > 0 && opts[backoff_limit] < backoffed
140
+ puts "Too many backoff attempts. Sorry it didn't work out."
141
+ exit 2
142
+ end
143
+
144
+ naptime = rand(60) * backoffed
145
+ puts "Backing off for #{naptime} seconds..."
146
+ sleep naptime
147
+ retry
148
+ else
149
+ raise e
150
+ end
151
+ else
152
+ raise e
153
+ end
131
154
  end
132
155
  end
133
156
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ec2-rotate-volume-snapshots}
8
- s.version = "0.4.0"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Zach Wily"]
12
- s.date = %q{2012-07-25}
12
+ s.date = %q{2012-10-03}
13
13
  s.default_executable = %q{ec2-rotate-volume-snapshots}
14
14
  s.description = %q{Provides a simple way to rotate EC2 snapshots with configurable retention periods.}
15
15
  s.email = %q{zach@zwily.com}
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ec2-rotate-volume-snapshots
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
8
+ - 5
9
9
  - 0
10
- version: 0.4.0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Zach Wily
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-07-25 00:00:00 -06:00
18
+ date: 2012-10-03 00:00:00 -06:00
19
19
  default_executable: ec2-rotate-volume-snapshots
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency