capistrano-ami 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 043c077f7f5158a6b23387193bd1aa8b08e30dac
4
+ data.tar.gz: a6885baffe755f2f07fab43687761caeba3e85f1
5
+ SHA512:
6
+ metadata.gz: 4e9fa6380478338c62226202d2b1fab04de59fab8e37a95929b9d2b20bcb68328a0b667dc4e2037e304d3534d9d3fc77de7ebf1464e8cf2ae6e763bcfc5d525d
7
+ data.tar.gz: 4691961e3babee0d332c3bf295354c13d4f011013be7e7668d85a04733c9fe95d86845a6ff679d38615c0232f300831ec8a5ea259adbb4df455ae8420de0d8f1
@@ -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/.reek ADDED
@@ -0,0 +1,12 @@
1
+ IrresponsibleModule:
2
+ enabled: false
3
+ exclude: []
4
+ DuplicateMethodCall:
5
+ enabled: true
6
+ exclude: []
7
+ max_calls: 2
8
+ UncommunicativeVariableName:
9
+ enabled: true
10
+ accept:
11
+ - a
12
+ - b
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,17 @@
1
+ Style/FileName:
2
+ Enabled: true
3
+ Exclude:
4
+ - lib/capistrano-ami.rb
5
+
6
+ Style/Documentation:
7
+ Enabled: false
8
+
9
+ Style/MethodLength:
10
+ Enabled: true
11
+ Max: 15
12
+
13
+ Metrics/LineLength:
14
+ Enabled: true
15
+ Max: 120
16
+ Exclude:
17
+ - capistrano-ami.gemspec
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at watassbass@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capistrano3-ami.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 wata_mac
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.
@@ -0,0 +1,94 @@
1
+ # Capistrano AMI
2
+
3
+ [![build status](https://circleci.com/gh/wata727/capistrano-ami.svg?style=shield&circle-token=490e040fc0a638ff54e85f9f0ac71c0330bcafa6)](https://circleci.com/gh/wata727/capistrano-ami)
4
+ [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE.txt)
5
+
6
+ This plugin that create AMI (Amazon Machine Image) and manage generations tasks into capistrano script. `capistrano-ami` tasks are able to run when deploy target servers exists in AWS (http://aws.amazon.com/).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'capistrano-ami'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install capistrano-ami
23
+
24
+ ## Usage
25
+ Load `capistrano-ami` into your capistrano configuration file `Capfile`:
26
+ ```ruby
27
+ require 'capistrano/ami'
28
+ ```
29
+ And set config values in deploy script `config/deploy.rb`:
30
+ ```ruby
31
+ set :aws_region, 'ap-northeast-1' # deploy servers region name
32
+ set :base_ami_name, 'web-role' # name: "#{base_ami_name}_#{instance_id}_#{deploy_timestamp}" default is capistrano-ami
33
+ set :keep_amis, 3 # keeps number of AMIs. default is 5
34
+ ```
35
+ If you do not specify, `base_ami_name` and `keep_amis` uses default value.
36
+
37
+ ### Credentials
38
+
39
+ `capistrano-ami` supports various credential providers. As of the following priority:
40
+
41
+ - Specified shared credentials
42
+ - Key values
43
+ - Environment values
44
+ - Default shared credentials
45
+ - IAM role
46
+
47
+ #### Key values
48
+
49
+ You can set credentials in deploy script `config/deploy.rb`:
50
+ ```ruby
51
+ set :aws_access_key_id, 'YOUR_AWS_ACCESS_KEY'
52
+ set :aws_secret_access_key, 'YOUR_AWS_SECRET_KEY'
53
+ ```
54
+
55
+ #### Environment values
56
+
57
+ `capistrano-ami` looks `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`, `AWS_REGION` environemt values.
58
+ ```
59
+ $ export AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY
60
+ $ export AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_KEY
61
+ ```
62
+
63
+ If you do not want to fix region in deploy script. use `AWS_REGION` environment values and other credential providers.
64
+
65
+ #### Shared credentials
66
+
67
+ Shared credentials are credentials file in local machine. default location is `~/.aws/credentials`. [More infomation](https://blogs.aws.amazon.com/security/post/Tx3D6U6WSFGOK2H/A-New-and-Standardized-Way-to-Manage-Credentials-in-the-AWS-SDKs).
68
+ If you want to use `default`, do not specify key values in delopy script. But if you want to use other profile, you should specify following:
69
+
70
+ ```ruby
71
+ set :aws_credentials_profile_name, 'profile_name'
72
+ ```
73
+
74
+ #### IAM role
75
+
76
+ IAM role is most secure credential provider. If you can, should use this. [More infomation](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html).
77
+ This provider used in no credentials configuration. Because you should not specify key values.
78
+
79
+ ## Versions
80
+
81
+ We check working this plugin following platform versions.
82
+
83
+ - Ruby
84
+ - 2.3.0
85
+ - Capistrano
86
+ - 3.5.0
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wata727/capistrano-ami. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
91
+
92
+ ## License
93
+
94
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'capistrano/ami'
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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 'capistrano/ami/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'capistrano-ami'
8
+ spec.version = Capistrano::Ami::VERSION
9
+ spec.authors = ['wata727']
10
+ spec.email = ['watassbass@gmail.com']
11
+
12
+ spec.summary = 'create AMI (Amazon Machine Image) and manage generations after deployment by capistrano3'
13
+ spec.description = "After deployed, create the instance's AMI and delete old AMI versions. If you spefied 'keep_amis', it's keep number of AMIs."
14
+ spec.homepage = 'https://github.com/wata727/capistrano-ami'
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.required_ruby_version = '~> 2.0'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.11'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'webmock', '~> 2.0'
28
+
29
+ spec.add_dependency 'aws-sdk', '~> 2'
30
+ spec.add_dependency 'capistrano', '~> 3'
31
+ end
@@ -0,0 +1,7 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.3.0
4
+
5
+ dependencies:
6
+ pre:
7
+ - gem install bundler --pre
File without changes
@@ -0,0 +1,52 @@
1
+ require 'date'
2
+ require 'aws-sdk'
3
+ require 'capistrano/ami/credentials'
4
+ require 'capistrano/ami/instance'
5
+ require 'capistrano/ami/version'
6
+
7
+ module Capistrano
8
+ module Ami
9
+ extend Capistrano::Ami::Instance
10
+
11
+ def self.create(instance_id, base_name)
12
+ image = instance(instance_id).create_image(
13
+ name: ami_name(instance_id, base_name),
14
+ no_reboot: true,
15
+ description: 'created by capistrano-ami'
16
+ )
17
+ image.create_tags(tags: [{ key: 'created_by', value: 'capistrano-ami' }])
18
+ image.create_tags(tags: [{ key: 'base_instance_id', value: instance_id }])
19
+ client.wait_until(:image_available, image_ids: [image.id])
20
+ image
21
+ end
22
+
23
+ def self.old_amis(instance_id, keep_amis)
24
+ images = ec2_resource.images(
25
+ owners: ['self'],
26
+ filters: [{
27
+ name: 'tag:created_by',
28
+ values: ['capistrano-ami']
29
+ }, {
30
+ name: 'tag:base_instance_id',
31
+ values: [instance_id]
32
+ }]
33
+ )
34
+ images = images.sort { |a, b| b.creation_date <=> a.creation_date }
35
+ Array(images[keep_amis, images.length])
36
+ end
37
+
38
+ def self.delete_snapshot(block_device_mappings)
39
+ block_device_mappings.each do |device|
40
+ client.delete_snapshot(snapshot_id: device[:ebs][:snapshot_id])
41
+ end
42
+ end
43
+
44
+ def self.ami_name(instance_id, base_name)
45
+ base_name + '_' + instance_id + '_' + Time.now.to_i.to_s
46
+ end
47
+
48
+ private_class_method :ami_name
49
+ end
50
+ end
51
+
52
+ load File.expand_path('../tasks/ami.rake', __FILE__)
@@ -0,0 +1,13 @@
1
+ module Capistrano
2
+ module Ami
3
+ module Credentials
4
+ def credentials(profile_name)
5
+ @credentials ||= ::Aws::SharedCredentials.new(profile_name: profile_name) if profile_name
6
+ @credentials ||= ::Aws::Credentials.new(
7
+ fetch(:aws_access_key_id, ENV['AWS_ACCESS_KEY_ID']),
8
+ fetch(:aws_secret_access_key, ENV['AWS_SECRET_ACCESS_KEY'])
9
+ )
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ module Capistrano
2
+ module Ami
3
+ module Instance
4
+ include Capistrano::Ami::Credentials
5
+
6
+ def client
7
+ @client ||= ::Aws::EC2::Client.new(
8
+ region: fetch(:aws_region, ENV['AWS_REGION']),
9
+ credentials: credentials(fetch(:aws_credentials_profile_name))
10
+ ) if credentials(fetch(:aws_credentials_profile_name)).set?
11
+ @client ||= ::Aws::EC2::Client.new(region: fetch(:aws_region, ENV['AWS_REGION']))
12
+ end
13
+
14
+ def ec2_resource
15
+ @ec2_resource ||= ::Aws::EC2::Resource.new(client: client)
16
+ end
17
+
18
+ def instance(instance_id)
19
+ @instance ||= ec2_resource.instances(instance_ids: [instance_id]).first
20
+ end
21
+
22
+ module_function :credentials
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module Ami
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ namespace :ami do
2
+ task :create do
3
+ on roles(:all) do
4
+ instance_id = capture 'curl -s http://169.254.169.254/latest/meta-data/instance-id'
5
+ info "[capistrano-ami] [#{instance_id}] Createing AMI..."
6
+ ami = Capistrano::Ami.create(instance_id, fetch(:base_ami_name, 'capistrano-ami'))
7
+ info "[capistrano-ami] [#{instance_id}] Created AMI (#{ami.id})"
8
+ end
9
+ end
10
+
11
+ task :delete_old_amis do
12
+ on roles(:all) do
13
+ instance_id = capture 'curl -s http://169.254.169.254/latest/meta-data/instance-id'
14
+ info "[capistrano-ami] [#{instance_id}] Deleting old AMIs...."
15
+ Capistrano::Ami.old_amis(instance_id, fetch(:keep_amis, 5)).each do |ami|
16
+ ami.deregister
17
+ Capistrano::Ami.delete_snapshot(ami.block_device_mappings)
18
+ info "[capistrano-ami] [#{instance_id}] Deleted AMI (#{ami.id})"
19
+ end
20
+ info "[capistrano-ami] [#{instance_id}] Finished deleting AMIs"
21
+ end
22
+ end
23
+ end
24
+
25
+ after 'deploy', 'ami:create'
26
+ after 'ami:create', 'ami:delete_old_amis'
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-ami
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - wata727
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-07 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: aws-sdk
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: capistrano
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3'
97
+ description: After deployed, create the instance's AMI and delete old AMI versions.
98
+ If you spefied 'keep_amis', it's keep number of AMIs.
99
+ email:
100
+ - watassbass@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".reek"
107
+ - ".rspec"
108
+ - ".rubocop.yml"
109
+ - ".travis.yml"
110
+ - CODE_OF_CONDUCT.md
111
+ - Gemfile
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - bin/console
116
+ - bin/setup
117
+ - capistrano-ami.gemspec
118
+ - circle.yml
119
+ - lib/capistrano-ami.rb
120
+ - lib/capistrano/ami.rb
121
+ - lib/capistrano/ami/credentials.rb
122
+ - lib/capistrano/ami/instance.rb
123
+ - lib/capistrano/ami/version.rb
124
+ - lib/capistrano/tasks/ami.rake
125
+ homepage: https://github.com/wata727/capistrano-ami
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '2.0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.5.1
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: create AMI (Amazon Machine Image) and manage generations after deployment
149
+ by capistrano3
150
+ test_files: []