ebs_snapper 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ebs_snapper.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2012 NeuStar, Inc. All rights reserved.
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # EbsSnapper
2
+
3
+ Take snapshots of EBS volumes and keep them for a period of time. We use it take nightly snapshots and retain them for a several days before they are purged.
4
+
5
+
6
+
7
+ ## Installation
8
+
9
+
10
+ $ gem install ebs_snapper
11
+
12
+ ### Bundler
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'ebs_snapper'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+
23
+
24
+ ## Usage
25
+
26
+ * Set up the configuration file, see: config/sample.config.yaml
27
+ * Tag your volume with the key 'Snapper'
28
+ * Setup a cron job to run nightly from a single instance (for example)
29
+ * Once created, the snapshots will be tagged 'Snapper' and contain a timestamp of creation time and the description will also contain the creation time.
30
+
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :test => :spec
data/bin/ebs_snapper ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ###
4
+ # Copyright 2012 NeuStar, Inc. All rights reserved.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+
19
+ begin
20
+ require 'ebs_snapper'
21
+
22
+ EbsSnapper::CLI.run
23
+
24
+ exit 0
25
+ rescue
26
+ exit 1
27
+ end
@@ -0,0 +1,7 @@
1
+ ---
2
+ aws:
3
+ access_key_id : 'ACCESS KEY ID'
4
+ secret_access_key : 'ACCESS KEY'
5
+ volume_tag : 'Snapper' # tag to use in AWS for the EBS backup flags
6
+ retain : '10.days'
7
+ log_to: '/var/log/ebs_snapper.log'
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/ebs_snapper/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.authors = ["ultradns"]
6
+ s.email = ["ultradns@neustar.biz"]
7
+
8
+ s.description = %q{Manage snapshots of EBS volumes}
9
+ s.summary = %q{Manage snapshots of EBS volumes}
10
+ s.homepage = "https://github.com/ultradns/ebs_snapper"
11
+
12
+ s.files = `git ls-files`.split($\)
13
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
15
+ s.name = "ebs_snapper"
16
+ s.require_paths = ["lib"]
17
+ s.version = EbsSnapper::VERSION
18
+
19
+ s.add_runtime_dependency "aws-sdk"
20
+
21
+ s.add_development_dependency "rake"
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "fakeweb"
24
+
25
+ end
@@ -0,0 +1,22 @@
1
+ # Copyright 2012 NeuStar, Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "ebs_snapper/version"
16
+ require 'logger'
17
+
18
+ module EbsSnapper
19
+ end
20
+
21
+ require 'ebs_snapper/cli'
22
+ require 'ebs_snapper/ebs'
@@ -0,0 +1,107 @@
1
+ # Copyright 2012 NeuStar, Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'optparse'
16
+ require 'ostruct'
17
+ require 'yaml'
18
+
19
+
20
+ class EbsSnapper::CLI
21
+
22
+ def self.run
23
+ opts = parse(ARGV)
24
+ ebs = EbsSnapper::Ebs.new(opts[:aws])
25
+ ebs.snapshot_and_purge
26
+ end
27
+
28
+ def self.load_config(opts, filename)
29
+ config = symbolize_keys(YAML.load_file(filename))
30
+ # merge the new file config & update the logger config
31
+ configure_logger(opts.merge!(config))
32
+ end
33
+
34
+ def self.configure_logger(opts)
35
+ if opts[:log_to]
36
+ @logger = ::Logger.new(opts[:log_to])
37
+ end
38
+ @logger ||= ::Logger.new(STDOUT)
39
+
40
+ @logger.level = opts[:verbose] ? Logger::DEBUG : Logger::INFO
41
+
42
+ opts[:aws][:logger] = @logger if opts[:aws]
43
+
44
+ opts
45
+ end
46
+
47
+ def self.parse(args)
48
+ options = {}
49
+ options[:aws] = {}
50
+ options[:ultradns] = {}
51
+ options[:log_to] = nil
52
+ options[:verbose] = false
53
+ options[:out] = ''
54
+ options[:config] = nil
55
+
56
+ opts = OptionParser.new do |opts|
57
+ opts.banner = "Usage: ebs_snapper [options]"
58
+
59
+ opts.separator ""
60
+ opts.separator "Specific options:"
61
+
62
+ opts.on("-c", "--config config.yaml",
63
+ "Configuration for the updater") do |config|
64
+ options[:config] = config
65
+ end
66
+
67
+ opts.separator ""
68
+
69
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
70
+ options[:verbose] = v
71
+ end
72
+
73
+ opts.on_tail("-h", "--help", "Show this message") do
74
+ puts opts
75
+ exit
76
+ end
77
+
78
+ opts.on_tail("--version", "Show version") do
79
+ puts EbsSnapper::VERSION
80
+ exit
81
+ end
82
+ end
83
+
84
+ opts.parse!(args)
85
+ # load the config
86
+ load_config(options, options[:config])
87
+ end
88
+
89
+ def self.symbolize_keys(hash)
90
+ unless hash.nil?
91
+ hash.replace(
92
+ hash.each_key.inject({}) do |h, k|
93
+ v = hash.delete(k)
94
+ key = k.to_sym rescue k
95
+ if v.is_a? (Hash)
96
+ h[key] = symbolize_keys(v)
97
+ else
98
+ h[key] = v
99
+ end
100
+ h
101
+ end
102
+ )
103
+ end
104
+ hash
105
+ end
106
+ end
107
+
@@ -0,0 +1,117 @@
1
+ # Copyright 2012 NeuStar, Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'aws-sdk'
16
+
17
+ class EbsSnapper::Ebs
18
+
19
+ DEFAULT_TAG_NAME = 'Snapper'
20
+
21
+ def initialize(opts = {})
22
+ AWS.config(
23
+ :access_key_id => opts[:access_key_id],
24
+ :secret_access_key => opts[:secret_access_key])
25
+
26
+ @logger = opts[:logger] || Logger.new(STDOUT)
27
+ @retain = opts[:retain]
28
+ @tag_name = opts[:volume_tag] || DEFAULT_TAG_NAME # default
29
+ end
30
+
31
+ def snapshot_and_purge
32
+ # now snapshot the list
33
+ tagged_volumes.each do |vol_info|
34
+ snapshot_volume(vol_info[:region], vol_info[:volume_id])
35
+ purge_old_snapshots(vol_info[:region], vol_info[:volume_id])
36
+ end
37
+ end
38
+
39
+ def tagged_volumes
40
+ volumes = []
41
+
42
+ each_region do |r|
43
+ r.tags.filter('resource-type', 'volume').filter('key', @tag_name).each do |tag|
44
+ # if the tag exists, it's using the default retention (TTL)
45
+ volumes << {
46
+ :ttl => TTL.new(@retain),
47
+ :region => r,
48
+ :volume_id => tag.resource.id # volume id
49
+ }
50
+ end
51
+ end
52
+ volumes
53
+ end
54
+
55
+ def snapshot_volume(region, vol_id)
56
+ timestamp = Time.now.utc
57
+ @logger.info {"Snapshotting #{vol_id} at: #{timestamp}"}
58
+ vol = region.volumes[vol_id]
59
+ if vol != nil
60
+ snapshot = vol.create_snapshot("Snapper Backup #{timestamp}")
61
+ # tag the snapshot with the timestamp so we can look it up later for cleanup
62
+ snapshot.tag(@tag_name, :value => "#{timestamp.to_i}")
63
+ else
64
+ @logger.error "Error: Volume #{vol_id} in Region: #{region} not found"
65
+ end
66
+ end
67
+
68
+ def purge_old_snapshots(ttl, region, vol_id)
69
+ region.snapshots.filter('volume-id', vol_id).filter('tag-key', @tag_name).each do |snapshot|
70
+ unless snapshot.status == :pending
71
+ ts = snapshot.tags[@tag_name]
72
+ if ts > 0 && ttl.purge?(ts)
73
+ @logger.info {"Purging #{vol_id} snapshot: #{snapshot}"}
74
+ snapshot.delete
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+
81
+ def each_region
82
+ ec2.regions.each do |region|
83
+ yield (region)
84
+ end
85
+ end
86
+
87
+ def ec2
88
+ @ec2 ||= AWS::EC2.new
89
+ @ec2
90
+ end
91
+
92
+ class TTL
93
+ DEFAULT_TTL = (86400 * 10) + 3600 # 10 days in hours
94
+ attr_reader :cut_off
95
+
96
+ def initialize(ttl = "10.days")
97
+ @cut_off = Time.now.utc.to_i - convert_to_seconds(ttl)
98
+ end
99
+
100
+ def purge?(timestamp)
101
+ timestamp < @cut_off
102
+ end
103
+
104
+ def convert_to_seconds(ttl)
105
+ ttl_secs = ttl.to_i
106
+ if ttl.kind_of?(String)
107
+ if ttl =~ /\.day/
108
+ # add 1 hr for backup time to extend the save-period.. TODO: maybe more..
109
+ ttl_secs = (ttl.to_i * 86400) + 3600
110
+ elsif ttl =~ /\.hour/
111
+ ttl_secs = ttl.to_i * 3600
112
+ end
113
+ end
114
+ ttl_secs == 0 ? DEFAULT_TTL : ttl_secs
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,17 @@
1
+ # Copyright 2012 NeuStar, Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module EbsSnapper
16
+ VERSION = "0.0.2"
17
+ end
@@ -0,0 +1,143 @@
1
+ # Copyright 2012 NeuStar, Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'spec_helper'
16
+ require 'fakeweb'
17
+ require 'ostruct'
18
+
19
+ describe EbsSnapper::Ebs do
20
+ DEFAULT_TAG_NAME = EbsSnapper::Ebs::DEFAULT_TAG_NAME
21
+
22
+ it "should find tagged volumes in a region" do
23
+ ebs = EbsSnapper::Ebs.new
24
+
25
+ tag = OpenStruct.new
26
+ tag.resource = OpenStruct.new(:id => 1)
27
+
28
+ tags = [tag]
29
+ tags.stub(:filter).and_return(tags)
30
+
31
+ region = OpenStruct.new
32
+ region.tags = tags
33
+ region.id = '999'
34
+
35
+ ebs.stub(:each_region).and_yield(region)
36
+
37
+ vols = ebs.tagged_volumes
38
+ vols.size.should == 1
39
+
40
+ vols[0][:region].id.should == region.id
41
+ vols[0][:volume_id].should == tag.resource.id
42
+ vols[0][:ttl].should_not == nil
43
+ end
44
+
45
+ it "should pruge old timestamps" do
46
+ ttl = EbsSnapper::Ebs::TTL.new("1.day") # 1 day
47
+ ttl.purge?(Time.now.utc.to_i - (86400 * 3)).should == true
48
+ ttl.purge?(Time.now.utc.to_i - (86400 + 3601)).should == true
49
+ end
50
+
51
+ it "shouldn't pruge new timestamps" do
52
+ ttl = EbsSnapper::Ebs::TTL.new("1.day") # 1 day
53
+ ttl.purge?(Time.now.utc.to_i - (3600)).should == false
54
+ ttl.purge?(Time.now.utc.to_i - (86400 - 3600)).should == false
55
+ ttl.purge?(Time.now.utc.to_i - (86400 + 3600)).should == false
56
+ # we give it 1 hr around the timestamp
57
+ ttl.purge?(Time.now.utc.to_i - 86400).should == false
58
+ end
59
+
60
+ it "should convert day ttls to seconds" do
61
+ ttl = EbsSnapper::Ebs::TTL.new("1.day") # 1 day
62
+ ttl.convert_to_seconds("1.day").should == 86400 + 3600
63
+ cut_off_10 = ttl.convert_to_seconds("10.days")
64
+ cut_off_10.should == (86400 * 10) + 3600
65
+ end
66
+
67
+ it "should convert hour ttls to seconds" do
68
+ ttl = EbsSnapper::Ebs::TTL.new()
69
+ ttl.convert_to_seconds("3.hours").should == (3600 * 3)
70
+ end
71
+
72
+ it "should set default ttl to seconds" do
73
+ ten_days_secs = (86400 * 10) + 3600
74
+ between_a = Time.now.utc.to_i - ten_days_secs
75
+ ttl = EbsSnapper::Ebs::TTL.new()
76
+ tm = Time.now.utc.to_i
77
+ between_b = tm - ten_days_secs
78
+ ttl.cut_off.should >= between_a
79
+ ttl.cut_off.should <= between_b
80
+
81
+ ttl.convert_to_seconds("asfljasdfjl").should == ten_days_secs
82
+ end
83
+
84
+ it "should snapshot a volume" do
85
+
86
+ ebs = EbsSnapper::Ebs.new
87
+
88
+ tag = OpenStruct.new
89
+ tag.resource = OpenStruct.new(:id => 1)
90
+
91
+ tags = [tag]
92
+ tags.stub(:filter).and_return(tags)
93
+
94
+ region = OpenStruct.new
95
+ region.tags = tags
96
+ region.id = '999'
97
+
98
+ snapshot = Object.new
99
+ snapshot.should_receive(:tag).with(DEFAULT_TAG_NAME,
100
+ hash_including(:value => RegexpMatcher.new(/^\d+/))) {true}
101
+
102
+ volume = OpenStruct.new
103
+ volume.should_receive(:create_snapshot) {snapshot}
104
+ region.volumes = {3 => volume}
105
+ ebs.snapshot_volume(region, 3).should == true
106
+
107
+ # id 1 is not found (nil)
108
+ ebs.snapshot_volume(region, 1).should == true
109
+ end
110
+
111
+ it "should purge old snapshots" do
112
+ ebs = EbsSnapper::Ebs.new
113
+ ttl = EbsSnapper::Ebs::TTL.new("1.day") # 1 day
114
+ region = OpenStruct.new
115
+
116
+ snapshot_old = OpenStruct.new
117
+ snapshot_old.status = :complete
118
+ snapshot_old.tags = {DEFAULT_TAG_NAME => Time.now.utc.to_i - (86400 * 2)}
119
+ snapshot_old.should_receive(:delete)
120
+
121
+ snapshot_old_error = OpenStruct.new
122
+ snapshot_old_error.status = :error
123
+ snapshot_old_error.tags = {DEFAULT_TAG_NAME => Time.now.utc.to_i - (86400 * 2)}
124
+ snapshot_old_error.should_receive(:delete)
125
+
126
+
127
+ snapshot_new = OpenStruct.new
128
+ snapshot_new.status = :complete
129
+ snapshot_new.tags = {DEFAULT_TAG_NAME => Time.now.utc.to_i - (3600 * 3)}
130
+ snapshot_new.should_receive(:delete).never
131
+
132
+ snapshot_pending = OpenStruct.new
133
+ snapshot_pending.status = :pending
134
+ snapshot_pending.tags = {DEFAULT_TAG_NAME => Time.now.utc.to_i - (86400 * 3)}
135
+ snapshot_pending.should_receive(:delete).never
136
+
137
+ region.snapshots = [snapshot_old, snapshot_new, snapshot_pending, snapshot_old_error]
138
+ region.snapshots.stub(:filter).and_return(region.snapshots)
139
+
140
+ ebs.purge_old_snapshots(ttl, region, 2)
141
+ end
142
+
143
+ end
@@ -0,0 +1,18 @@
1
+ # Copyright 2012 NeuStar, Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'bundler/setup'
16
+ require 'ebs_snapper'
17
+
18
+
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ebs_snapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ultradns
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-21 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: aws-sdk
16
+ requirement: &2152830900 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2152830900
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &2152830480 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2152830480
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &2152830060 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2152830060
47
+ - !ruby/object:Gem::Dependency
48
+ name: fakeweb
49
+ requirement: &2152829640 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2152829640
58
+ description: Manage snapshots of EBS volumes
59
+ email:
60
+ - ultradns@neustar.biz
61
+ executables:
62
+ - ebs_snapper
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - Gemfile
68
+ - LICENSE
69
+ - README.md
70
+ - Rakefile
71
+ - bin/ebs_snapper
72
+ - config/sample.config.yaml
73
+ - ebs_snapper.gemspec
74
+ - lib/ebs_snapper.rb
75
+ - lib/ebs_snapper/cli.rb
76
+ - lib/ebs_snapper/ebs.rb
77
+ - lib/ebs_snapper/version.rb
78
+ - spec/lib/ebs_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: https://github.com/ultradns/ebs_snapper
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.6
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Manage snapshots of EBS volumes
104
+ test_files:
105
+ - spec/lib/ebs_spec.rb
106
+ - spec/spec_helper.rb