capistrano-asg-rolling 0.1.0

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
+ SHA256:
3
+ metadata.gz: 7f6b6214ffbebb1d5c6866126ce143ca0ca1917be593c03a60b3e53bbc425294
4
+ data.tar.gz: 8098e48a08bbbd61e1945c5a420939118d2951ddfb250ea669b0f4d8d141ed10
5
+ SHA512:
6
+ metadata.gz: '08d8756a4754a72fb9e5e688f633a5cbc4f495d1db2abdb777548f05efddc25a0714d8d53cf13de66fa01691ec15fd99f8797997205018c7861ccf7939ebaa63'
7
+ data.tar.gz: d985258bb1de520e96a577a5fcbf1c77de176172aa674a381e5e729d661d4218b683d8fd21e9629f2a016fbf56cc1a803cd661381c569d0ac91b7a7b14af49ca
@@ -0,0 +1,21 @@
1
+ name: CI
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ test:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ matrix:
10
+ ruby-version: ['2.5', '2.6', '2.7', '3.0']
11
+
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: ${{ matrix.ruby-version }}
17
+ bundler-cache: true
18
+ - name: Install dependencies
19
+ run: bundle install
20
+ - name: Run tests
21
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .ruby-version
14
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,28 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-rspec
5
+
6
+ AllCops:
7
+ NewCops: enable
8
+ TargetRubyVersion: 2.5
9
+ DisplayCopNames: true
10
+ DisplayStyleGuide: true
11
+
12
+ Layout/LineLength:
13
+ Enabled: false
14
+
15
+ Metrics/AbcSize:
16
+ Enabled: false
17
+
18
+ Metrics/BlockLength:
19
+ Enabled: false
20
+
21
+ Metrics/MethodLength:
22
+ Enabled: false
23
+
24
+ RSpec/ExampleLength:
25
+ Max: 9
26
+
27
+ RSpec/MultipleExpectations:
28
+ Max: 5
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at peter.postma@kentaa.nl. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in capistrano-asg-rolling.gemspec
8
+ gemspec
9
+
10
+ gem 'rubocop', '~> 1.22.1'
11
+ gem 'rubocop-performance', '~> 1.11.5'
12
+ gem 'rubocop-rake', '~> 0.6.0'
13
+ gem 'rubocop-rspec', '~> 2.5.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,113 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ capistrano-asg-rolling (0.1.0)
5
+ aws-sdk-autoscaling (~> 1, >= 1.39)
6
+ aws-sdk-ec2 (~> 1)
7
+ capistrano (~> 3)
8
+ concurrent-ruby (~> 1)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ addressable (2.8.0)
14
+ public_suffix (>= 2.0.2, < 5.0)
15
+ airbrussh (1.4.0)
16
+ sshkit (>= 1.6.1, != 1.7.0)
17
+ ast (2.4.2)
18
+ aws-eventstream (1.2.0)
19
+ aws-partitions (1.516.0)
20
+ aws-sdk-autoscaling (1.69.0)
21
+ aws-sdk-core (~> 3, >= 3.120.0)
22
+ aws-sigv4 (~> 1.1)
23
+ aws-sdk-core (3.121.1)
24
+ aws-eventstream (~> 1, >= 1.0.2)
25
+ aws-partitions (~> 1, >= 1.239.0)
26
+ aws-sigv4 (~> 1.1)
27
+ jmespath (~> 1.0)
28
+ aws-sdk-ec2 (1.270.0)
29
+ aws-sdk-core (~> 3, >= 3.120.0)
30
+ aws-sigv4 (~> 1.1)
31
+ aws-sigv4 (1.4.0)
32
+ aws-eventstream (~> 1, >= 1.0.2)
33
+ capistrano (3.16.0)
34
+ airbrussh (>= 1.0.0)
35
+ i18n
36
+ rake (>= 10.0.0)
37
+ sshkit (>= 1.9.0)
38
+ concurrent-ruby (1.1.9)
39
+ crack (0.4.5)
40
+ rexml
41
+ diff-lcs (1.4.4)
42
+ hashdiff (1.0.1)
43
+ i18n (1.8.10)
44
+ concurrent-ruby (~> 1.0)
45
+ jmespath (1.4.0)
46
+ net-scp (3.0.0)
47
+ net-ssh (>= 2.6.5, < 7.0.0)
48
+ net-ssh (6.1.0)
49
+ parallel (1.21.0)
50
+ parser (3.0.2.0)
51
+ ast (~> 2.4.1)
52
+ public_suffix (4.0.6)
53
+ rainbow (3.0.0)
54
+ rake (13.0.6)
55
+ regexp_parser (2.1.1)
56
+ rexml (3.2.5)
57
+ rspec (3.10.0)
58
+ rspec-core (~> 3.10.0)
59
+ rspec-expectations (~> 3.10.0)
60
+ rspec-mocks (~> 3.10.0)
61
+ rspec-core (3.10.1)
62
+ rspec-support (~> 3.10.0)
63
+ rspec-expectations (3.10.1)
64
+ diff-lcs (>= 1.2.0, < 2.0)
65
+ rspec-support (~> 3.10.0)
66
+ rspec-mocks (3.10.2)
67
+ diff-lcs (>= 1.2.0, < 2.0)
68
+ rspec-support (~> 3.10.0)
69
+ rspec-support (3.10.2)
70
+ rubocop (1.22.1)
71
+ parallel (~> 1.10)
72
+ parser (>= 3.0.0.0)
73
+ rainbow (>= 2.2.2, < 4.0)
74
+ regexp_parser (>= 1.8, < 3.0)
75
+ rexml
76
+ rubocop-ast (>= 1.12.0, < 2.0)
77
+ ruby-progressbar (~> 1.7)
78
+ unicode-display_width (>= 1.4.0, < 3.0)
79
+ rubocop-ast (1.12.0)
80
+ parser (>= 3.0.1.1)
81
+ rubocop-performance (1.11.5)
82
+ rubocop (>= 1.7.0, < 2.0)
83
+ rubocop-ast (>= 0.4.0)
84
+ rubocop-rake (0.6.0)
85
+ rubocop (~> 1.0)
86
+ rubocop-rspec (2.5.0)
87
+ rubocop (~> 1.19)
88
+ ruby-progressbar (1.11.0)
89
+ sshkit (1.21.2)
90
+ net-scp (>= 1.1.2)
91
+ net-ssh (>= 2.8.0)
92
+ unicode-display_width (2.1.0)
93
+ webmock (3.14.0)
94
+ addressable (>= 2.8.0)
95
+ crack (>= 0.3.2)
96
+ hashdiff (>= 0.4.0, < 2.0.0)
97
+
98
+ PLATFORMS
99
+ ruby
100
+
101
+ DEPENDENCIES
102
+ bundler (~> 1.17)
103
+ capistrano-asg-rolling!
104
+ rake (~> 13.0)
105
+ rspec (~> 3.0)
106
+ rubocop (~> 1.22.1)
107
+ rubocop-performance (~> 1.11.5)
108
+ rubocop-rake (~> 0.6.0)
109
+ rubocop-rspec (~> 2.5.0)
110
+ webmock (~> 3.11)
111
+
112
+ BUNDLED WITH
113
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Kentaa BV
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,214 @@
1
+ # Capistrano Auto Scaling Group rolling deployments
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/capistrano-asg-rolling.svg)](https://badge.fury.io/rb/capistrano-asg-rolling)
4
+ [![Build Status](https://github.com/KentaaNL/capistrano-asg-rolling/actions/workflows/test.yml/badge.svg)](https://github.com/KentaaNL/capistrano-asg-rolling/actions)
5
+ [![Code Climate](https://codeclimate.com/github/KentaaNL/capistrano-asg-rolling/badges/gpa.svg)](https://codeclimate.com/github/KentaaNL/capistrano-asg-rolling)
6
+
7
+ Capistrano plugin for performing rolling updates to AWS Auto Scaling Groups using the [instance refresh feature](https://docs.aws.amazon.com/autoscaling/ec2/userguide/asg-instance-refresh.html).
8
+
9
+ Instead of deploying to live servers, capistrano-asg-rolling will create a temporary instance for deployment and then trigger an instance refresh to perform a rolling update of the Auto Scaling Group(s). In more detail, during deployment it will:
10
+
11
+ - Launch an instance from the AMI defined in the Launch Template of the Auto Scaling Group(s).
12
+ - Deploy your application to the launched instances.
13
+ - After deployment, stop the instances and create an AMI for each instance.
14
+ - Create new Launch Template versions with the new AMIs.
15
+ - Trigger Instance Refresh on the Auto Scaling Group(s) to perform an rolling update.
16
+ - Delete any outdated Launch Template versions, AMIs and snapshots created by previous deployments.
17
+ - Terminate the no longer needed instances.
18
+
19
+ ## Caveats
20
+
21
+ #### Instance refresh limitations
22
+
23
+ Please be aware of the limitations of using instance refresh, in particular "Instances terminated before launch": https://docs.aws.amazon.com/autoscaling/ec2/userguide/asg-instance-refresh.html#instance-refresh-limitations
24
+
25
+ #### Launch Templates
26
+
27
+ This gem depends on Auto Scaling Groups with Launch Templates. Using an Auto Scaling Group with a Launch Configuration is not supported, and will raise an `Capistrano::ASG::Rolling::NoLaunchTemplate`.
28
+
29
+ Make sure you have configured the Auto Scaling Groups with the Launch Template version set to `Latest` so instance refresh will work.
30
+
31
+ #### Experimental
32
+
33
+ This gem is experimental, it works for our configuration / use case, but might not for yours.
34
+
35
+ The configuration options are not considered stable and might be changed or removed in future releases.
36
+
37
+ The gem could have a better / fancier name.
38
+
39
+ #### Bugs
40
+
41
+ Currently all Launch Template versions and AMIs will be deleted, also when not created by this gem.
42
+
43
+ When an Instance refresh is already in progress, the deployment will fail with an `Aws::AutoScaling::Errors::InstanceRefreshInProgress`.
44
+
45
+ Documentation is incomplete.
46
+
47
+ ## Installation
48
+
49
+ Add this line to your application's Gemfile:
50
+
51
+ ```ruby
52
+ gem 'capistrano-asg-rolling'
53
+ ```
54
+
55
+ And then execute:
56
+
57
+ $ bundle
58
+
59
+ Or install it yourself as:
60
+
61
+ $ gem install capistrano-asg-rolling
62
+
63
+ Add the plugin to Capistrano's Capfile:
64
+
65
+ ```ruby
66
+ # Capfile
67
+ require 'capistrano/asg/rolling'
68
+ install_plugin Capistrano::ASG::Rolling::Plugin
69
+ ```
70
+
71
+ ## Configuration
72
+
73
+ Below are the Capistrano configuration options:
74
+
75
+ Setup AWS credentials:
76
+
77
+ ```ruby
78
+ # config/deploy.rb
79
+ set :aws_access_key_id, ENV['AWS_ACCESS_KEY_ID']
80
+ set :aws_secret_access_key, ENV['AWS_SECRET_ACCESS_KEY']
81
+ set :aws_region, ENV['AWS_REGION']
82
+ ```
83
+
84
+ After deployment, any outdated Launch Template versions, AMIs and snapshots will be deleted. By default, the number of `keep_releases` will be kept. Change this with:
85
+
86
+ ```ruby
87
+ # config/deploy.rb
88
+ set :asg_rolling_keep_versions, 10
89
+ ```
90
+
91
+ Servers are added using their private IP address by default. Set to false to use the public IP address instead:
92
+
93
+ ```ruby
94
+ # config/deploy.rb
95
+ set :asg_rolling_use_private_ip_address, false
96
+ ```
97
+
98
+ Verbose logging is enabled by default, set to false for less verbose logging:
99
+
100
+ ```ruby
101
+ # config/deploy.rb
102
+ set :asg_rolling_verbose, false
103
+ ```
104
+
105
+ When launching an Instance, you can override any settings defined in the Launch Template with:
106
+
107
+ ```ruby
108
+ # config/deploy.rb
109
+ set :asg_rolling_instance_overrides, { instance_type: 'c5.large' }
110
+ ```
111
+
112
+ ## Usage
113
+
114
+ Specify the Auto Scaling Groups with the keyword `autoscale` instead of using the `server` keyword in Capistrano's stage configuration. Provide the name of the Auto Scaling Group and any properties you want to pass to the server:
115
+
116
+ ```ruby
117
+ # config/deploy/<stage>.rb
118
+ autoscale 'app-autoscale-group', user: 'deployer', roles: %w[db assets]
119
+ autoscale 'web-autoscale-group', user: 'deployer'
120
+ ```
121
+
122
+ Now start a deployment with `cap <stage> deploy` and enjoy.
123
+
124
+ ### Deploy without rolling update
125
+
126
+ There might be cases where you just want to deploy your code to the servers in the Auto Scaling Group(s) without a rolling update.
127
+
128
+ You can configure rolling updates per autoscaling group by using the `rolling` option:
129
+
130
+ ```ruby
131
+ # config/deploy/<stage>.rb
132
+ autoscale 'app-autoscale-group', rolling: true # default: use rolling deployment
133
+ autoscale 'web-autoscale-group', rolling: false # override: use normal deployment
134
+ ```
135
+
136
+ ### Custom stage
137
+
138
+ The rolling configuration of the stage has a side-effect: any Capistrano tasks you run, will also launch instances per Auto Scaling Group.
139
+
140
+ For example the command: `cap production rails:console`, will launch a new instance and run `rails:console` and that instance. While that can be useful, you often just want to run the task on the primary server. A solution is to create two stages with different rolling configurations, for example:
141
+
142
+ ```ruby
143
+ # config/deploy/production.rb
144
+ set :stage, :production
145
+
146
+ autoscale 'app-autoscale-group', rolling: false, user: 'deployer', roles: %w[db assets]
147
+ autoscale 'web-autoscale-group', rolling: false, user: 'deployer'
148
+ ```
149
+
150
+ and:
151
+
152
+ ```ruby
153
+ # config/deploy/production_rolling.rb
154
+ set :stage, :production_rolling
155
+ set :rails_env, :production
156
+
157
+ autoscale 'app-autoscale-group', rolling: true, user: 'deployer', roles: %w[db assets]
158
+ autoscale 'web-autoscale-group', rolling: true, user: 'deployer'
159
+ ```
160
+
161
+ With these two stages, you can run any tasks with `cap production <task name>` and rolling deployments with `cap production_rolling deploy`.
162
+
163
+ ## IAM policy
164
+
165
+ The following IAM permissions are required:
166
+
167
+ ```json
168
+ {
169
+ "Version": "2012-10-17",
170
+ "Statement": [
171
+ {
172
+ "Effect": "Allow",
173
+ "Action": [
174
+ "autoscaling:DescribeAutoScalingGroups",
175
+ "autoscaling:DescribeAutoScalingInstances",
176
+ "autoscaling:EnterStandby",
177
+ "autoscaling:ExitStandby",
178
+ "autoscaling:StartInstanceRefresh",
179
+ "ec2:CreateImage",
180
+ "ec2:CreateLaunchTemplateVersion",
181
+ "ec2:CreateTags",
182
+ "ec2:DeleteLaunchTemplateVersions",
183
+ "ec2:DeleteSnapshot",
184
+ "ec2:DeregisterImage",
185
+ "ec2:DescribeImages",
186
+ "ec2:DescribeInstances",
187
+ "ec2:DescribeLaunchTemplateVersions",
188
+ "ec2:RunInstances",
189
+ "ec2:StopInstances",
190
+ "ec2:TerminateInstances"
191
+ ],
192
+ "Resource": "*"
193
+ }
194
+ ]
195
+ }
196
+ ```
197
+
198
+ ## Development
199
+
200
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
201
+
202
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
203
+
204
+ ## Contributing
205
+
206
+ Bug reports and pull requests are welcome on GitHub at https://github.com/KentaaNL/capistrano-asg-rolling. 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.
207
+
208
+ ## License
209
+
210
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
211
+
212
+ ## Code of Conduct
213
+
214
+ Everyone interacting in the Capistrano::ASG::Rolling project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/KentaaNL/capistrano-asg-rolling/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new(:rubocop)
9
+
10
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'capistrano/asg/rolling'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'capistrano/asg/rolling/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'capistrano-asg-rolling'
9
+ spec.version = Capistrano::ASG::Rolling::VERSION
10
+ spec.authors = ['Kentaa']
11
+ spec.email = ['developers@kentaa.nl']
12
+
13
+ spec.summary = 'Capistrano plugin for performing rolling updates to AWS Auto Scaling Groups using Instance Refresh'
14
+ spec.homepage = 'https://github.com/KentaaNL/capistrano-asg-rolling'
15
+ spec.license = 'MIT'
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.required_ruby_version = '>= 2.5.0'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.17'
30
+ spec.add_development_dependency 'rake', '~> 13.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.0'
32
+ spec.add_development_dependency 'webmock', '~> 3.11'
33
+
34
+ spec.add_dependency 'aws-sdk-autoscaling', '~> 1', '>= 1.39'
35
+ spec.add_dependency 'aws-sdk-ec2', '~> 1'
36
+ spec.add_dependency 'capistrano', '~> 3'
37
+ spec.add_dependency 'concurrent-ruby', '~> 1'
38
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Capistrano
4
+ module ASG
5
+ module Rolling
6
+ # AWS EC2 Machine Image.
7
+ class AMI
8
+ include AWS
9
+
10
+ attr_reader :id, :instance
11
+
12
+ def initialize(id, instance = nil)
13
+ @id = id
14
+ @instance = instance
15
+ end
16
+
17
+ # Create an AMI from an instance and wait until the AMI is available.
18
+ def self.create(instance:, name:, description: nil, tags: nil)
19
+ aws_ec2_client = instance.aws_ec2_client
20
+
21
+ options = {
22
+ instance_id: instance.id,
23
+ name: name,
24
+ description: description
25
+ }
26
+
27
+ if tags
28
+ options[:tag_specifications] = tags.flat_map do |key, value|
29
+ [
30
+ { resource_type: 'image', tags: [{ key: key, value: value }] },
31
+ { resource_type: 'snapshot', tags: [{ key: key, value: value }] }
32
+ ]
33
+ end
34
+ end
35
+
36
+ response = aws_ec2_client.create_image(options)
37
+
38
+ begin
39
+ aws_ec2_client.wait_until(:image_available, image_ids: [response.image_id])
40
+ rescue Aws::Waiters::Errors::TooManyAttemptsError
41
+ # When waiting for the AMI takes longer than the default (10 minutes),
42
+ # then assume it will eventually succeed and just continue.
43
+ end
44
+
45
+ new(response.image_id, instance)
46
+ end
47
+
48
+ def exists?
49
+ aws_ec2_image.exists?
50
+ end
51
+
52
+ def delete
53
+ # Retrieve the snapshots first because we can't call #describe_images anymore
54
+ # after deregistering the image.
55
+ image_snapshots = snapshots
56
+
57
+ aws_ec2_client.deregister_image(image_id: id)
58
+
59
+ image_snapshots.each(&:delete)
60
+ end
61
+
62
+ def snapshots
63
+ @snapshots ||= aws_ec2_image.block_device_mappings.map do |mapping|
64
+ Snapshot.new(mapping.ebs.snapshot_id)
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def aws_ec2_image
71
+ @aws_ec2_image ||= ::Aws::EC2::Image.new(id, client: aws_ec2_client)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end