Skip to content

Commit

Permalink
Capistrano 3 plugin for Updating Lanuch Template AMI
Browse files Browse the repository at this point in the history
  • Loading branch information
Aftab-Akram committed Sep 18, 2021
0 parents commit 1c92784
Show file tree
Hide file tree
Showing 13 changed files with 423 additions and 0 deletions.
53 changes: 53 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/spec/examples.txt
/test/tmp/
/test/version_tmp/
/tmp/

# Used by dotenv library to load environment variables.
# .env

## Specific to RubyMotion:
.dat*
.repl_history
build/
*.bridgesupport
build-iPhoneOS/
build-iPhoneSimulator/

## Specific to RubyMotion (use of CocoaPods):
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# vendor/Pods/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalization:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

# RubyMine folder
.idea
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'http://rubygems.org'

# Specify your gem's dependencies in capistrano3-asg-ami.gemspec
gemspec
69 changes: 69 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
PATH
remote: .
specs:
capistrano3-asg-ami (1.0.0)
activesupport (>= 4.0.0)
aws-sdk-autoscaling (~> 1)
aws-sdk-ec2 (~> 1)
capistrano (> 3.0.0)
capistrano-bundler (~> 2)

GEM
remote: http://rubygems.org/
specs:
activesupport (6.1.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
airbrussh (1.4.0)
sshkit (>= 1.6.1, != 1.7.0)
aws-eventstream (1.1.0)
aws-partitions (1.430.0)
aws-sdk-autoscaling (1.55.0)
aws-sdk-core (~> 3, >= 3.112.0)
aws-sigv4 (~> 1.1)
aws-sdk-core (3.112.0)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.239.0)
aws-sigv4 (~> 1.1)
jmespath (~> 1.0)
aws-sdk-ec2 (1.225.0)
aws-sdk-core (~> 3, >= 3.112.0)
aws-sigv4 (~> 1.1)
aws-sigv4 (1.2.2)
aws-eventstream (~> 1, >= 1.0.2)
capistrano (3.16.0)
airbrussh (>= 1.0.0)
i18n
rake (>= 10.0.0)
sshkit (>= 1.9.0)
capistrano-bundler (2.0.1)
capistrano (~> 3.1)
concurrent-ruby (1.1.8)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
jmespath (1.4.0)
minitest (5.14.4)
net-scp (3.0.0)
net-ssh (>= 2.6.5, < 7.0.0)
net-ssh (6.1.0)
rake (13.0.3)
sshkit (1.21.2)
net-scp (>= 1.1.2)
net-ssh (>= 2.8.0)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
zeitwerk (2.4.2)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 2.1.4)
capistrano3-asg-ami!
rake

BUNDLED WITH
2.1.4
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Aftab Akram

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[![Gem Version](https://badge.fury.io/rb/capistrano3-asg-ami.png)](http://badge.fury.io/rb/capistrano3-asg-ami)
# capistrano3-asg-ami
Capistrano 3 plugin for Updating Lanuch Template AMI with autoscale group first healthy instance.

## Requirements

* aws-sdk-ec2 ~> 1
* aws-sdk-autoscaling ~> 1
* capistrano ~> 3


## Installation

Add this line to your application's Gemfile:

gem 'capistrano3-asg-ami'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install capistrano3-asg-ami

Add this line to your application's Capfile:

```ruby
require 'capistrano/autoscaling'
```

## Usage

Set credentials with AmazonEC2FullAccess permission and in the capistrano deploy script / stage files add the following lines

```ruby
set :aws_region, 'ap-northeast-1'
set :aws_access_key_id, 'YOUR AWS KEY ID'
set :aws_secret_access_key, 'YOUR AWS SECRET KEY'
set :aws_autoscaling_group_name, 'YOUR NAME OF AUTO SCALING GROUP NAME'
set :aws_launch_template_id, 'lt-03f39c5689155e374'
set :aws_autoscale_ami_prefix, 'server-name-'
set :aws_keep_prev_no_of_ami, 1
```

To call this task after deploy, in your desire capistrano environment file
add the following in deploy section

```ruby
namespace :deploy do
after :finishing, 'autoscaling:update_ami'
end
```

And to update autoscale group launch template manually

```ruby
cap [stage] autoscaling:update_ami
```


## How this works

1- Fetch only running instances that have an auto scaling group name you specified
2- Create AMI with first healthy instance
3- Tag AMI with autoscale group name
4- Wait for AMI to be available for use
5- Update Launch template with update AMI
6- Deleting stale images base on (aws_keep_prev_no_of_ami) variable
if not set will only keep the latest one
and delete the other AMI tagged with autoscale group name
**


## Contributing

1. Fork it ( https://github.com/Aftab-Akram/capistrano3-asg-ami/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

To test while developoing just `bundle console` on the project root directory and execute
`Capistrano::AutoScaling::VERSION` for a quick test
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'bundler/gem_tasks'
29 changes: 29 additions & 0 deletions capistrano3-asg-ami.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# encoding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'capistrano/autoscaling/version'

Gem::Specification.new do |spec|
spec.name = 'capistrano3-asg-ami'
spec.version = Capistrano::AutoScaling::VERSION
spec.authors = ['Aftab Akram']
spec.email = ['aftabakram04@gmail.com']
spec.summary = %q{Deploy to AWS Auto Scaling group.}
spec.description = %q{Capistrano 3 plugin for Updating Lanuch Template AMI with autoscale group first healthy instance}
spec.homepage = 'https://github.com/Aftab-Akram/capistrano3-asg-ami'
spec.license = 'MIT'

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_development_dependency 'bundler', '~> 2.1.4'
spec.add_development_dependency 'rake'

spec.add_dependency 'aws-sdk-ec2', '~> 1'
spec.add_dependency 'aws-sdk-autoscaling', '~> 1'
spec.add_dependency 'capistrano', '> 3.0.0'
spec.add_dependency 'activesupport', '>= 4.0.0'
spec.add_dependency 'capistrano-bundler', '~> 2'
end
2 changes: 2 additions & 0 deletions lib/capistrano/autoscaling.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'capistrano/bundler'
load File.expand_path('../tasks/autoscaling.rake', __FILE__)
1 change: 1 addition & 0 deletions lib/capistrano/autoscaling/autoscaling.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
load File.expand_path('../../tasks/autoscaling.rake', __FILE__)
5 changes: 5 additions & 0 deletions lib/capistrano/autoscaling/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Capistrano
module AutoScaling
VERSION = '1.0.0'
end
end
Loading

0 comments on commit 1c92784

Please sign in to comment.