ec2-rotate-volume-snapshots 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "right_aws", ">= 2.0.0"
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "shoulda", ">= 0"
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.5.2"
11
+ gem "rcov", ">= 0"
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ right_aws (2.0.0)
12
+ right_http_connection (>= 1.2.1)
13
+ right_http_connection (1.2.4)
14
+ shoulda (2.11.3)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ bundler (~> 1.0.0)
21
+ jeweler (~> 1.5.2)
22
+ rcov
23
+ right_aws (>= 2.0.0)
24
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Zach Wily
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = ec2-rotate-volume-snapshots
2
+
3
+ Provides a simple way to rotate ec2 snapshots in a father/grandfather/etc setup.
4
+
5
+ == Contributing to ec2-rotate-volume-snapshots
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Zach Wily. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "ec2-rotate-volume-snapshots"
16
+ gem.homepage = "http://github.com/zwily/ec2-rotate-volume-snapshots"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Amazon EC2 snapshot rotator}
19
+ gem.description = %Q{Provides a simple way to rotate EC2 snapshots with configurable retention periods.}
20
+ gem.email = "zach@zwily.com"
21
+ gem.authors = ["Zach Wily"]
22
+ end
23
+ Jeweler::RubygemsDotOrgTasks.new
24
+
25
+ require 'rake/testtask'
26
+ Rake::TestTask.new(:test) do |test|
27
+ test.pattern = 'test/**/test_*.rb'
28
+ test.verbose = true
29
+ end
30
+
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+
38
+ task :default => :test
39
+
40
+ require 'rake/rdoctask'
41
+ Rake::RDocTask.new do |rdoc|
42
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "ec2-rotate-volume-snapshots #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'right_aws'
5
+ require 'optparse'
6
+
7
+ opts = {
8
+ :aws_access_key => ENV["AWS_ACCESS_KEY_ID"],
9
+ :aws_secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"],
10
+ :aws_region => 'us-east-1',
11
+ :pattern => nil,
12
+ :dry_run => false
13
+ }
14
+
15
+ time_periods = {
16
+ :hourly => { :seconds => 60 * 60, :format => '%Y-%m-%d-%H', :keep => 0, :keeping => {} },
17
+ :daily => { :seconds => 24 * 60 * 60, :format => '%Y-%m-%d', :keep => 0, :keeping => {} },
18
+ :weekly => { :seconds => 7 * 24 * 60 * 60, :format => '%Y-%W', :keep => 0, :keeping => {} },
19
+ :monthly => { :seconds => 30 * 7 * 24 * 60 * 60, :format => '%Y-%m', :keep => 0, :keeping => {} },
20
+ :yearly => { :seconds => 12 * 30 * 7 * 24 * 60 * 60, :format => '%Y', :keep => 0, :keeping => {} },
21
+ }
22
+
23
+ OptionParser.new do |o|
24
+ o.on("--aws-access-key ACCESS_KEY", "AWS Access Key") do |v|
25
+ opts[:aws_access_key] = v
26
+ end
27
+
28
+ o.on("--aws-secret-access-key SECRET_KEY", "AWS Secret Access Key") do |v|
29
+ opts[:aws_secret_access_key] = v
30
+ end
31
+
32
+ o.on("--aws-region REGION", "AWS Region") do |v|
33
+ opts[:aws_region] = v
34
+ end
35
+
36
+ o.on("--pattern STRING", "Snapshots without this string in the description will be ignored") do |v|
37
+ opts[:pattern] = v
38
+ end
39
+
40
+ time_periods.keys.sort { |a, b| time_periods[a][:seconds] <=> time_periods[b][:seconds] }.each do |period|
41
+ o.on("--keep-#{period} NUMBER", Integer, "Number of #{period} snapshots to keep") do |v|
42
+ time_periods[period][:keep] = v
43
+ end
44
+ end
45
+
46
+ o.on("--dry-run", "Shows what would happen without doing anything") do |v|
47
+ opts[:dry_run] = true
48
+ end
49
+ end.parse!
50
+
51
+ if opts[:aws_access_key].nil? || opts[:aws_secret_access_key].nil?
52
+ puts "You must specify your Amazon credentials via --aws-access-key and --aws-secret_access-key"
53
+ exit 1
54
+ end
55
+
56
+ if ARGV.empty?
57
+ puts "You must provide at least one volume id with snapshots to rotate"
58
+ exit 1
59
+ end
60
+ volume_ids = ARGV
61
+
62
+ time_periods.freeze
63
+
64
+ ec2 = RightAws::Ec2.new(opts[:aws_access_key], opts[:aws_secret_access_key], :region => opts[:aws_region])
65
+ all_snapshots = ec2.describe_snapshots
66
+
67
+ volume_ids.each do |volume_id|
68
+ if volume_id !~ /^vol-/
69
+ # sanity check
70
+ puts "Invalid volume id: #{volume_id}"
71
+ exit 1
72
+ end
73
+
74
+ periods = time_periods.dup
75
+
76
+ snapshots_to_keep = {}
77
+ snapshots = all_snapshots.select {|ss| ss[:aws_volume_id] == volume_id }.sort {|a,b| a[:aws_started_at] <=> b[:aws_started_at] }
78
+
79
+ snapshots.each do |snapshot|
80
+ time = Time.parse(snapshot[:aws_started_at])
81
+ snapshot_id = snapshot[:aws_id]
82
+ description = snapshot[:aws_description]
83
+ keep_reason = nil
84
+
85
+ if opts[:pattern] && description !~ /#{opts[:pattern]}/
86
+ puts " #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Skipping snapshot with description #{description}"
87
+ next
88
+ end
89
+
90
+ periods.keys.sort { |a, b| periods[a][:seconds] <=> periods[b][:seconds] }.each do |period|
91
+ period_info = periods[period]
92
+ keep = period_info[:keep]
93
+ keeping = period_info[:keeping]
94
+
95
+ time_string = time.strftime period_info[:format]
96
+ if Time.now - time < keep * period_info[:seconds]
97
+ if !keeping.key?(time_string) && keeping.length < keep
98
+ keep_reason = period
99
+ keeping[time_string] = snapshot
100
+ end
101
+ break
102
+ end
103
+ end
104
+
105
+ if !keep_reason.nil?
106
+ puts " #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Keeping for #{keep_reason}"
107
+ else
108
+ puts " #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Deleting"
109
+ ec2.delete_snapshot(snapshot_id) unless opts[:dry_run]
110
+ end
111
+ end
112
+ end
data/lib/.empty ADDED
@@ -0,0 +1 @@
1
+ Jeweler doesn't like me not having a lib/ directory.
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'ec2-rotate-volume-snapshots'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestEc2RotateVolumeSnapshots < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ec2-rotate-volume-snapshots
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Zach Wily
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-29 00:00:00 -07:00
19
+ default_executable: ec2-rotate-volume-snapshots
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ prerelease: false
24
+ name: right_aws
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 15
31
+ segments:
32
+ - 2
33
+ - 0
34
+ - 0
35
+ version: 2.0.0
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ type: :development
39
+ prerelease: false
40
+ name: shoulda
41
+ version_requirements: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ requirement: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ prerelease: false
54
+ name: bundler
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 23
61
+ segments:
62
+ - 1
63
+ - 0
64
+ - 0
65
+ version: 1.0.0
66
+ requirement: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ prerelease: false
70
+ name: jeweler
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ hash: 7
77
+ segments:
78
+ - 1
79
+ - 5
80
+ - 2
81
+ version: 1.5.2
82
+ requirement: *id004
83
+ - !ruby/object:Gem::Dependency
84
+ type: :development
85
+ prerelease: false
86
+ name: rcov
87
+ version_requirements: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ requirement: *id005
97
+ description: Provides a simple way to rotate EC2 snapshots with configurable retention periods.
98
+ email: zach@zwily.com
99
+ executables:
100
+ - ec2-rotate-volume-snapshots
101
+ extensions: []
102
+
103
+ extra_rdoc_files:
104
+ - LICENSE.txt
105
+ - README.rdoc
106
+ files:
107
+ - .document
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.rdoc
112
+ - Rakefile
113
+ - VERSION
114
+ - bin/ec2-rotate-volume-snapshots
115
+ - lib/.empty
116
+ - test/helper.rb
117
+ - test/test_ec2-rotate-volume-snapshots.rb
118
+ has_rdoc: true
119
+ homepage: http://github.com/zwily/ec2-rotate-volume-snapshots
120
+ licenses:
121
+ - MIT
122
+ post_install_message:
123
+ rdoc_options: []
124
+
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: 3
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ hash: 3
142
+ segments:
143
+ - 0
144
+ version: "0"
145
+ requirements: []
146
+
147
+ rubyforge_project:
148
+ rubygems_version: 1.3.7
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: Amazon EC2 snapshot rotator
152
+ test_files:
153
+ - test/helper.rb
154
+ - test/test_ec2-rotate-volume-snapshots.rb