amirotate 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4520504403a0ddd45d4ba57759d9204dfe5a694f
4
+ data.tar.gz: 32ff5532336748f4184b00bc230345460b776790
5
+ SHA512:
6
+ metadata.gz: c508ab01f3a02fa34366f06ab8cf6a15508dfa2a100a62b532ea295afcedd1006b034558be34d9bc200695b40c6a218f33498eea9e98bb692cba023a79e20a3c
7
+ data.tar.gz: ff1984f9b157d682710eff4d18780988072c8d712c881e5dd5c2faf334cbb0e5bc9400aaf30d1f2a325a8916c834ff451ab2ad408253552dd0fc36e8231bfb23
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ vendor
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in amirotate.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 y13i
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,44 @@
1
+ # Amirotate
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/amirotate`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'amirotate'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install amirotate
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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. Run `bundle exec amirotate` to use the code located in this directory, ignoring other installed copies of this gem.
30
+
31
+ 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).
32
+
33
+ ## TODO
34
+
35
+ * Write spec.
36
+ * etc.
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it ( https://github.com/y13i/amirotate/fork )
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/amirotate.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'amirotate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "amirotate"
8
+ spec.version = AMIRotate::VERSION
9
+ spec.authors = ["y13i"]
10
+ spec.email = ["email@y13i.com"]
11
+
12
+ spec.summary = %()
13
+ spec.description = %()
14
+ spec.homepage = "https://github.com/y13i/amirotate"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "guard-rspec"
26
+ spec.add_development_dependency "pry"
27
+
28
+ spec.add_dependency "aws-sdk"
29
+ spec.add_dependency "thor"
30
+ spec.add_dependency "thor-aws"
31
+ end
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "amirotate"
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
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
data/exe/amirotate ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "amirotate"
4
+
5
+ AMIRotate::CLI.start
data/lib/amirotate.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "amirotate/version"
2
+ require "amirotate/cli"
3
+ require "amirotate/client"
@@ -0,0 +1,67 @@
1
+ require "thor"
2
+ require "thor/aws"
3
+
4
+ module AMIRotate
5
+ class CLI < Thor
6
+ include Thor::Aws
7
+
8
+ class_option :verbose, type: :boolean, default: false, aliases: [:v]
9
+
10
+ desc :version, "Puts Gem version."
11
+
12
+ def version
13
+ puts "AMIRotate #{VERSION}"
14
+ end
15
+
16
+ desc :setup, "Create EC2 tags with given profile name and options."
17
+ method_option :profile_name, default: "default"
18
+ method_option :retention_period, default: "2 days"
19
+ method_option :overwrite, type: :boolean, default: false
20
+ method_option :dry_run, type: :boolean, default: false
21
+ method_option :filter_tags
22
+
23
+ def setup
24
+ client.setup
25
+ end
26
+
27
+ desc :preserve, "Create AMIs with given option by profile name."
28
+ method_option :profile_name, default: "default"
29
+ method_option :reboot, type: :boolean, default: true
30
+ method_option :dry_run, type: :boolean, default: false
31
+ method_option :filter_tags
32
+
33
+ def preserve
34
+ client.preserve
35
+ end
36
+
37
+ desc :invalidate, "Delete expired AMIs by profile name."
38
+ method_option :profile_name, default: "default"
39
+ method_option :expiration_offset, default: "30 minutes"
40
+ method_option :retain_snapshot, type: :boolean, default: false
41
+ method_option :dry_run, type: :boolean, default: false
42
+ method_option :filter_tags
43
+
44
+ def invalidate
45
+ client.invalidate
46
+ end
47
+
48
+ desc :rotate, "Execute :preserve and :invalidate at a time."
49
+ method_option :profile_name, default: "default"
50
+ method_option :reboot, type: :boolean, default: true
51
+ method_option :expiration_offset, default: "30 minutes"
52
+ method_option :retain_snapshot, type: :boolean, default: false
53
+ method_option :dry_run, type: :boolean, default: false
54
+ method_option :filter_tags
55
+
56
+ def rotate
57
+ preserve
58
+ invalidate
59
+ end
60
+
61
+ private
62
+
63
+ def client
64
+ @client ||= Client.new options, aws_configuration
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,214 @@
1
+ require "aws-sdk"
2
+ require "time"
3
+
4
+ module AMIRotate
5
+ class Client
6
+ attr_reader :logger
7
+
8
+ def initialize(cli_options = {}, aws_configuration = {})
9
+ @cli_options = cli_options
10
+ @logger ||= Logger.new STDOUT
11
+
12
+ aws_configuration[:logger] = Logger.new STDOUT if @cli_options.verbose
13
+
14
+ logger.info "Started."
15
+
16
+ at_exit do
17
+ logger.info "Exiting."
18
+ end
19
+
20
+ @ec2 = Aws::EC2::Resource.new aws_configuration
21
+ end
22
+
23
+ def setup
24
+ instances.each do |instance|
25
+ tag_key = "amirotate:#{@cli_options[:profile_name]}:retention_period"
26
+ tag_value = @cli_options[:retention_period]
27
+ tag = instance.tags.find {|tag| tag.key == tag_key}
28
+
29
+ if tag.nil? or @cli_options[:overwrite]
30
+ logger.info "Create tag `#{tag_key} => #{tag_value}` on instance #{instance.instance_id}."
31
+
32
+ begin
33
+ instance.create_tags(
34
+ dry_run: @cli_options[:dry_run],
35
+
36
+ tags: [
37
+ key: tag_key,
38
+ value: tag_value,
39
+ ],
40
+ )
41
+ rescue Aws::EC2::Errors::DryRunOperation
42
+ logger.info "Did nothing due to dry run."
43
+ end
44
+ else
45
+ logger.info "Tag `#{tag_key}` is already exist on instance #{instance.instance_id}."
46
+ end
47
+ end
48
+ end
49
+
50
+ def preserve
51
+ instances.each do |instance|
52
+ tag_key = "amirotate:#{@cli_options[:profile_name]}:retention_period"
53
+ tag = instance.tags.find {|tag| tag.key == tag_key}
54
+
55
+ if tag.nil?
56
+ logger.info "Tag `#{tag_key}` is not set on instance #{instance.instance_id} (#{name}). Skipping."
57
+ next
58
+ end
59
+
60
+ name = begin
61
+ instance.tags.find {|tag| tag.key == "Name"}.value
62
+ rescue
63
+ ""
64
+ end
65
+
66
+ logger.info "Create image from instance #{instance.instance_id} (#{name}). Retention period is #{tag.value}."
67
+
68
+ begin
69
+ image = instance.create_image(
70
+ dry_run: @cli_options[:dry_run],
71
+ no_reboot: !@cli_options[:reboot],
72
+
73
+ name: [
74
+ name,
75
+ instance.instance_id,
76
+ Time.now.strftime("%Y-%m-%d %H.%M.%S"),
77
+ ].join(" - ")
78
+ )
79
+
80
+ image.wait_until {|image| image.state.match /available|pending/}
81
+
82
+ image.create_tags(
83
+ dry_run: @cli_options[:dry_run],
84
+
85
+ tags: [
86
+ key: tag.key,
87
+ value: tag.value,
88
+ ],
89
+ )
90
+ rescue Aws::EC2::Errors::DryRunOperation
91
+ logger.info "Did nothing due to dry run."
92
+ end
93
+ end
94
+ end
95
+
96
+ def invalidate
97
+ images.each do |image|
98
+ tag_key = "amirotate:#{@cli_options[:profile_name]}:retention_period"
99
+ tag = image.tags.find {|tag| tag.key == tag_key}
100
+
101
+ if tag.nil?
102
+ logger.info "Tag `#{tag_key}` is not set on image #{image.id} (#{image.name}). Skipping."
103
+ next
104
+ end
105
+
106
+ expiration_time = Time.parse(image.creation_date) + parse_time(tag.value) + parse_time(@cli_options[:expiration_offset])
107
+
108
+ if Time.now > expiration_time
109
+ logger.info "Image #{image.id} (#{image.name}) is expired at #{expiration_time}. Deregister AMI."
110
+
111
+ begin
112
+ snapshot_ids = image.block_device_mappings.map do |block_device|
113
+ begin
114
+ block_device.ebs.snapshot_id
115
+ rescue
116
+ nil
117
+ end
118
+ end
119
+
120
+ image.deregister(
121
+ dry_run: @cli_options[:dry_run],
122
+ )
123
+
124
+ unless @cli_options[:retain_snapshot]
125
+ snapshot_ids.compact.each do |snapshot_id|
126
+ logger.info "Delete snapshot #{snapshot_id}."
127
+
128
+ @ec2.snapshot(snapshot_id).delete(
129
+ dry_run: @cli_options[:dry_run],
130
+ )
131
+ end
132
+ end
133
+ rescue Aws::EC2::Errors::DryRunOperation
134
+ logger.info "Did nothing due to dry run."
135
+ end
136
+ else
137
+ logger.info "Image #{image.id} (#{image.name}) is not yet expired (Expire at #{expiration_time}). Skipping."
138
+ end
139
+ end
140
+ end
141
+
142
+ private
143
+
144
+ def instances
145
+ return @instances if @instances
146
+
147
+ @instances = @ec2.instances.to_a
148
+
149
+ filter! @instances if @cli_options.filter_tags
150
+
151
+ logger.info "Found #{@instances.size} instance(s)."
152
+
153
+ @instances
154
+ end
155
+
156
+ def images
157
+ return @images if @images
158
+
159
+ @images = @ec2.images(
160
+ owners: ["self"],
161
+
162
+ filters: [
163
+ {
164
+ name: "state",
165
+ values: ["available"]
166
+ },
167
+ ],
168
+ ).to_a
169
+
170
+ filter! @images if @cli_options.filter_tags
171
+
172
+ logger.info "Found #{@images.size} image(s)."
173
+
174
+ @images
175
+ end
176
+
177
+ def filter!(array)
178
+ filter = @cli_options.filter_tags.split(/,/).inject Hash.new do |hash, keyvalue|
179
+ key, value = keyvalue.split(/:/)
180
+ hash.merge key => value
181
+ end
182
+
183
+ array.select! do |object|
184
+ filter.all? do |filter_key, filter_value|
185
+ object.tags.any? do |tag|
186
+ [
187
+ (tag.key == filter_key),
188
+ (tag.value.match Regexp.compile(filter_value)),
189
+ ].all?
190
+ end
191
+ end
192
+ end
193
+ end
194
+
195
+ def parse_time(string)
196
+ integer = string.to_i
197
+
198
+ multiplier = case string
199
+ when /minutes?/
200
+ 60
201
+ when /hours?/
202
+ 60 * 60
203
+ when /days?/
204
+ 60 * 60 * 24
205
+ when /weeks?/
206
+ 60 * 60 * 24 * 7
207
+ else
208
+ 1
209
+ end
210
+
211
+ integer * multiplier
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,3 @@
1
+ module AMIRotate
2
+ VERSION = "0.0.10"
3
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amirotate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.10
5
+ platform: ruby
6
+ authors:
7
+ - y13i
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
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
+ - !ruby/object:Gem::Dependency
84
+ name: aws-sdk
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: thor
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: thor-aws
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: ''
126
+ email:
127
+ - email@y13i.com
128
+ executables:
129
+ - amirotate
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - amirotate.gemspec
141
+ - bin/console
142
+ - bin/setup
143
+ - exe/amirotate
144
+ - lib/amirotate.rb
145
+ - lib/amirotate/cli.rb
146
+ - lib/amirotate/client.rb
147
+ - lib/amirotate/version.rb
148
+ homepage: https://github.com/y13i/amirotate
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.4.5
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: ''
172
+ test_files: []