ec2-snapshot-replicator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b48d31de7f61485b9989ba74e0b87202f35f5fd5
4
+ data.tar.gz: a9b6f7f55d77bf5b89ebd977cd8784a0ef9d3d1b
5
+ SHA512:
6
+ metadata.gz: b3e2a3cba19ef7822db45f9610bf2c87718cceb1cffa4f05ec6aad9879141de044f6b5d1ae389b02b8948f270eb2d4917381c3e6dc2c851a383f90e0aabd9476
7
+ data.tar.gz: 1e1dabbd1780bab1f4b36c6096f5c9f0ccbe264adb7663b1fca10d651d5ea83847cf6dad518bc8afea7933b54c3f01e51b4167d5a5026fdbd00e439ad21ae9ca
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ec2-snapshot-replicator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ryota Arai
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # ec2-snapshot-replicator
2
+
3
+ **THIS IS NOT READY TO USE!!**
4
+
5
+ Replicate snapshots to another region with delayed deletion.
6
+
7
+ ## Installation
8
+
9
+ $ gem install ec2-snapshot-replicator
10
+
11
+ ## Usage
12
+
13
+ ```
14
+ $ ec2-snapshot-replicator start \
15
+ --delay-deletion-sec=3600 \
16
+ --interval-sec=600 \
17
+ --source-region=ap-northeast-1 \
18
+ --destination-region=us-east-1 \
19
+ --owner-id=123456789
20
+ ```
21
+
22
+ If the above settings are provided:
23
+
24
+ - loop the following:
25
+ - If a snapshot in the source region is not found in the destination region, copy it to the destination region and create `SourceSnapshotId` tag.
26
+ - If a snapshot in the destination region is not found in the source region, create `DeleteAfter` tag which is (now + 1 day) in seconds since unix epoch.
27
+ - If `DeleteAfter` time of a snapshot in the destination region is over now, delete it.
28
+ - Sleep 10 minutes
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( https://github.com/ryotarai/ec2-snapshot-replicator/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ec2/snapshot/replicator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ec2/snapshot/replicator'
3
+
4
+ EC2::Snapshot::Replicator::CLI.start
5
+
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ec2/snapshot/replicator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ec2-snapshot-replicator"
8
+ spec.version = EC2::Snapshot::Replicator::VERSION
9
+ spec.authors = ["Ryota Arai"]
10
+ spec.email = ["ryota.arai@gmail.com"]
11
+
12
+ spec.summary = %q{Replicate EC2 snapshots to another region.}
13
+ spec.homepage = "https://github.com/ryotarai/ec2-snapshot-replicator"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "thor"
22
+ spec.add_dependency "aws-sdk", "~> 2"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.9"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,13 @@
1
+ require "ec2/snapshot/replicator/cli"
2
+ require "ec2/snapshot/replicator/config"
3
+ require "ec2/snapshot/replicator/engine"
4
+ require "ec2/snapshot/replicator/logger"
5
+ require "ec2/snapshot/replicator/version"
6
+
7
+ module EC2
8
+ module Snapshot
9
+ module Replicator
10
+ # Your code goes here...
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+ require "ec2/snapshot/replicator"
2
+ require 'thor'
3
+
4
+ module EC2
5
+ module Snapshot
6
+ module Replicator
7
+ class CLI < Thor
8
+ desc "version", "Show version"
9
+ def version
10
+ puts "v#{EC2::Snapshot::Replicator::VERSION}"
11
+ end
12
+
13
+ desc "start", "Start"
14
+ method_option :source_region, type: :string, required: true
15
+ method_option :destination_region, type: :string, required: true
16
+ method_option :interval_sec, type: :numeric, default: 60 * 10
17
+ method_option :delay_deletion_sec, type: :numeric, default: 60 * 60 * 24 * 7
18
+ method_option :owner_id, type: :string, required: true
19
+ method_option :debug, type: :boolean, default: false
20
+ method_option :once, type: :boolean, default: false
21
+ def start
22
+ config = Config.new
23
+ config.load_options(options)
24
+
25
+ if options[:once]
26
+ Engine.new(config).run_once
27
+ else
28
+ Engine.new(config).start
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,17 @@
1
+ module EC2
2
+ module Snapshot
3
+ module Replicator
4
+ class Config < Struct.new(:source_region, :destination_region, :interval_sec, :delay_deletion_sec, :owner_id, :debug)
5
+ def load_options(options)
6
+ self.source_region = options[:source_region]
7
+ self.destination_region = options[:destination_region]
8
+ self.interval_sec = options[:interval_sec]
9
+ self.delay_deletion_sec = options[:delay_deletion_sec]
10
+ self.owner_id = options[:owner_id]
11
+ self.debug = options[:debug]
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,147 @@
1
+ require 'aws-sdk'
2
+
3
+ module EC2
4
+ module Snapshot
5
+ module Replicator
6
+ class Engine
7
+ SOURCE_SNAPSHOT_ID_TAG_KEY = 'SourceSnapshotId'
8
+ DELETE_AFTER_TAG_KEY = 'DeleteAfter'
9
+
10
+ attr_reader :source_ec2, :destination_ec2
11
+
12
+ def initialize(config)
13
+ @config = config
14
+
15
+ @source_ec2 = Aws::EC2::Resource.new(region: @config.source_region)
16
+ @destination_ec2 = Aws::EC2::Resource.new(region: @config.destination_region)
17
+ end
18
+
19
+ def start
20
+ Logger.info "start loop"
21
+ while true
22
+ run_once
23
+
24
+ Logger.info "sleeping for #{@config.interval_sec} sec..."
25
+ sleep @config.interval_sec
26
+ end
27
+ end
28
+
29
+ def run_once
30
+ replicate_snapshots
31
+ mark_deleted_snapshots
32
+ delete_snapshots
33
+ end
34
+
35
+ def replicate_snapshots
36
+ Logger.info ">>> replicating snapshots..."
37
+
38
+ source_snapshots = @source_ec2.snapshots(owner_ids: [@config.owner_id])
39
+ source_snapshot_ids = source_snapshots.map {|s| s.id }
40
+
41
+ destination_snapshots = @destination_ec2.snapshots(owner_ids: [@config.owner_id], filters: [{name: "tag:#{SOURCE_SNAPSHOT_ID_TAG_KEY}", values: source_snapshot_ids}])
42
+ source_snapshots.each do |snapshot|
43
+ destination_snapshot = destination_snapshots.find do |s|
44
+ s.tags.find do |t|
45
+ t.key == SOURCE_SNAPSHOT_ID_TAG_KEY &&
46
+ t.value == snapshot.id
47
+ end
48
+ end
49
+
50
+ if destination_snapshot
51
+ Logger.debug "[#{snapshot.id}] already replicated"
52
+ else
53
+ Logger.info "[#{snapshot.id}] replicating..."
54
+
55
+ ask_continue("Copy snapshot.")
56
+
57
+ res = @destination_ec2.snapshot(snapshot.id).copy(
58
+ source_region: @config.source_region,
59
+ destination_region: @config.destination_region,
60
+ description: snapshot.description,
61
+ )
62
+
63
+ Logger.debug "[#{res.snapshot_id}] created in #{@config.destination_region}"
64
+ copied_snapshot = @destination_ec2.snapshot(res.snapshot_id)
65
+ copied_snapshot.create_tags(
66
+ tags: [
67
+ {key: SOURCE_SNAPSHOT_ID_TAG_KEY, value: snapshot.id},
68
+ ],
69
+ )
70
+ end
71
+ end
72
+ end
73
+
74
+ def mark_deleted_snapshots
75
+ Logger.info ">>> marking deleted snapshots..."
76
+
77
+ destination_snapshots = @destination_ec2.snapshots(owner_ids: [@config.owner_id]).select do |snapshot|
78
+ if snapshot.tags.find {|t| t.key == DELETE_AFTER_TAG_KEY }
79
+ next false
80
+ end
81
+
82
+ unless snapshot.tags.find {|t| t.key == SOURCE_SNAPSHOT_ID_TAG_KEY }
83
+ Logger.debug "[#{snapshot.id}] tag #{SOURCE_SNAPSHOT_ID_TAG_KEY} is not found."
84
+ next false
85
+ end
86
+
87
+ true
88
+ end
89
+
90
+ source_snapshot_ids = destination_snapshots.map do |snapshot|
91
+ snapshot.tags.find {|t| t.key == SOURCE_SNAPSHOT_ID_TAG_KEY }.value
92
+ end
93
+
94
+ source_snapshots = @source_ec2.snapshots(owner_ids: [@config.owner_id], filters: [{name: 'snapshot-id', values: source_snapshot_ids}])
95
+
96
+ destination_snapshots.each do |snapshot|
97
+ source_snapshot_id = snapshot.tags.find {|t| t.key == SOURCE_SNAPSHOT_ID_TAG_KEY }.value
98
+ if source_snapshots.find {|s| s.id == source_snapshot_id }
99
+ Logger.debug "[#{snapshot.id}] source snapshot (#{source_snapshot_id}) exists"
100
+ else
101
+ Logger.info "[#{snapshot.id}] creating #{DELETE_AFTER_TAG_KEY} tag because source snapshot (#{source_snapshot_id}) is deleted."
102
+
103
+ delete_after = (Time.now + @config.delay_deletion_sec).to_i
104
+ ask_continue("Create a tag #{DELETE_AFTER_TAG_KEY}:#{delete_after}.")
105
+ snapshot.create_tags(
106
+ tags: [
107
+ {key: DELETE_AFTER_TAG_KEY, value: delete_after.to_s},
108
+ ],
109
+ )
110
+ end
111
+ end
112
+ end
113
+
114
+ def delete_snapshots
115
+ Logger.info ">>> deleting snapshots..."
116
+ @destination_ec2.snapshots(owner_ids: [@config.owner_id]).each do |snapshot|
117
+ tag = snapshot.tags.find {|t| t.key == DELETE_AFTER_TAG_KEY }
118
+
119
+ unless tag
120
+ Logger.debug "[#{snapshot.id}] tag #{DELETE_AFTER_TAG_KEY} is not found."
121
+ next
122
+ end
123
+
124
+ delete_after = Time.at(tag.value.to_i)
125
+ if delete_after < Time.now
126
+ Logger.info "[#{snapshot.id}] deleting..."
127
+ ask_continue("Delete #{snapshot.id}.")
128
+ snapshot.delete
129
+ end
130
+ end
131
+ end
132
+
133
+ private
134
+
135
+ def ask_continue(msg)
136
+ return unless @config.debug
137
+
138
+ print "#{msg} continue? (y/N): "
139
+ unless $stdin.gets.downcase =~ /\Ay/
140
+ abort
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+
@@ -0,0 +1,21 @@
1
+ require 'logger'
2
+
3
+ module EC2
4
+ module Snapshot
5
+ module Replicator
6
+ class Logger
7
+ @logger = ::Logger.new($stdout)
8
+
9
+ class << self
10
+ %w!fatal error info debug trace!.each do |meth|
11
+ meth = meth.to_sym
12
+ define_method(meth) do |msg|
13
+ @logger.public_send(meth, msg)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,7 @@
1
+ module EC2
2
+ module Snapshot
3
+ module Replicator
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ec2-snapshot-replicator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryota Arai
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - ryota.arai@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/ec2-snapshot-replicator
99
+ - bin/setup
100
+ - ec2-snapshot-replicator.gemspec
101
+ - lib/ec2/snapshot/replicator.rb
102
+ - lib/ec2/snapshot/replicator/cli.rb
103
+ - lib/ec2/snapshot/replicator/config.rb
104
+ - lib/ec2/snapshot/replicator/engine.rb
105
+ - lib/ec2/snapshot/replicator/logger.rb
106
+ - lib/ec2/snapshot/replicator/version.rb
107
+ homepage: https://github.com/ryotarai/ec2-snapshot-replicator
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.5
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Replicate EC2 snapshots to another region.
131
+ test_files: []