elbas 0.0.3

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: 90bb6cb862637a21301a677843b68ba62aba314e
4
+ data.tar.gz: e9fb96364446ed69a34198de897165c5611dfaed
5
+ SHA512:
6
+ metadata.gz: 80f3281169f1026110e97e7cbea95f72bd267111564529220cd34c57155a68c0eced49098f24daf3d052e5db56f48743fe54e9620c59d3077908fad56fc29f57
7
+ data.tar.gz: 29109f1e625a8e4994e09c2825efd4b5efac68e0d64792484ae1d35a537a62ac1843605961e8b7c80291a248c6c910525f2889e9280df81537eb449bfabeaeb5
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in elbas.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Logan Serman
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # ELBAS (Elastic Load Balancer & AutoScaling)
2
+
3
+ ELBAS was written to ease the deployment of Rails applications to AWS AutoScale groups. ELBAS will:
4
+
5
+ - Deploy your code to each running instance connected to a given AutoScale group
6
+ - After deployment, create an AMI from one of the running instances
7
+ - Attach the AMI with the new code to a new AWS Launch Configuration
8
+ - Update your AutoScale group to use the new launch configuration
9
+ - Delete any old AMIs created by ELBAS
10
+ - Delete any old launch configurations created by ELBAS
11
+
12
+ This ensures that your current and future servers will be running the newly deployed code.
13
+
14
+ ## Installation
15
+
16
+ Bundle the gem, this is only available from GitHub for now, sorry!
17
+
18
+ Add this statement to your Capfile:
19
+
20
+ `require 'elbas/capistrano'`
21
+
22
+ ## Configuration
23
+
24
+ Below are the Capistrano configuration options with their defaults:
25
+
26
+ ```ruby
27
+ set :aws_access_key_id, ENV['AWS_ACCESS_KEY_ID']
28
+ set :aws_secret_access_key, ENV['AWS_SECRET_ACCESS_KEY']
29
+ set :aws_region, ENV['AWS_REGION']
30
+
31
+ set :aws_autoscale_instance_size, 'm1.small'
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ Instead of using Capistrano's `server` method, use `autoscale` instead in `deploy/production.rb` (or
37
+ whichever environment you're deploying to). Provide the name of your AutoScale group instead of a
38
+ hostname:
39
+
40
+ ```ruby
41
+ autoscale 'production', user: 'apps', roles: [:app, :web, :db]
42
+ ```
43
+
44
+ That's it! Run `cap production deploy`. ELBAS will print the following log statements during your
45
+ deployment:
46
+
47
+ ```
48
+ "ELBAS: Adding server: ec2-XX-XX-XX-XXX.compute-1.amazonaws.com"
49
+ "ELBAS: Creating EC2 AMI from i-123abcd"
50
+ "ELBAS: Created AMI: ami-123456"
51
+ "ELBAS: Creating an EC2 Launch Configuration for AMI: ami-123456"
52
+ "ELBAS: Created Launch Configuration: elbas-lc-ENVIRONMENT-UNIX_TIMESTAMP"
53
+ "ELBAS: Attaching Launch Configuration to AutoScale Group"
54
+ "ELBAS: Deleting old launch configuration: elbas-lc-production-123456"
55
+ "ELBAS: Deleting old image: ami-999999"
56
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/**/*_test.rb"
6
+ end
data/elbas.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'elbas/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "elbas"
8
+ spec.version = Elbas::VERSION
9
+ spec.authors = ["Logan Serman"]
10
+ spec.email = ["logan.serman@metova.com"]
11
+ spec.summary = 'Capistrano plugin for deploying to AWS ASGroups.'
12
+ spec.description = "#{spec.summary}. Deploys to all instances in a group, creates a fresh AMI post-deploy, and attaches the AMI to your ASGroup."
13
+ spec.homepage = "http://github.com/metova/elbas"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ spec.add_development_dependency "minitest-reporters"
25
+ spec.add_development_dependency "webmock"
26
+
27
+ spec.add_dependency 'aws-sdk'
28
+ spec.add_dependency 'capistrano', '> 3.0.0'
29
+
30
+ end
data/lib/elbas.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "elbas/version"
2
+
3
+ module Elbas
4
+ end
data/lib/elbas/ami.rb ADDED
@@ -0,0 +1,41 @@
1
+ module Elbas
2
+ class AMI < AWS
3
+ include Capistrano::DSL
4
+
5
+ def self.create(&block)
6
+ ami = new
7
+ ami.cleanup do
8
+ ami.save
9
+ yield ami
10
+ end
11
+ end
12
+
13
+ def save
14
+ info "Creating EC2 AMI from #{base_ec2_instance.id}"
15
+ @aws_counterpart = ec2.images.create \
16
+ name: timestamp(name_prefix),
17
+ instance_id: base_ec2_instance.id,
18
+ no_reboot: true
19
+ end
20
+
21
+ def destroy(images = [])
22
+ images.each do |i|
23
+ info "Deleting old image: #{i.id}"
24
+ i.delete
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def name_prefix
31
+ "elbas-ami-#{environment}"
32
+ end
33
+
34
+ def garbage
35
+ ec2.images.with_owner('self').to_a.select do |i|
36
+ i.name =~ /#{name_prefix}/i
37
+ end
38
+ end
39
+
40
+ end
41
+ end
data/lib/elbas/aws.rb ADDED
@@ -0,0 +1,61 @@
1
+ require 'aws-sdk'
2
+
3
+ module Elbas
4
+ class AWS
5
+ include Capistrano::DSL
6
+
7
+ attr_reader :aws_counterpart
8
+
9
+ def cleanup(&block)
10
+ items = garbage || []
11
+ yield
12
+ destroy items
13
+ self
14
+ end
15
+
16
+ def info(message)
17
+ p "ELBAS: #{message}"
18
+ end
19
+
20
+ def method_missing(_method, *args, &block)
21
+ aws_counterpart.send _method, *args
22
+ end
23
+
24
+ protected
25
+
26
+ def autoscaling
27
+ @_autoscaling ||= ::AWS::AutoScaling.new(credentials)
28
+ end
29
+
30
+ def ec2
31
+ @_ec2 ||= ::AWS::EC2.new(credentials)
32
+ end
33
+
34
+ def asgroup
35
+ @_asgroup ||= autoscaling.groups[fetch(:aws_autoscale_group)]
36
+ end
37
+
38
+ def base_ec2_instance
39
+ @_base_ec2_instance ||= asgroup.ec2_instances.filter('instance-state-name', 'running').first
40
+ end
41
+
42
+ def environment
43
+ fetch(:rails_env, 'production')
44
+ end
45
+
46
+ def timestamp(str)
47
+ "#{str}-#{Time.now.to_i}"
48
+ end
49
+
50
+ private
51
+
52
+ def credentials
53
+ _credentials = {
54
+ access_key_id: fetch(:aws_access_key_id),
55
+ secret_access_key: fetch(:aws_secret_access_key),
56
+ }
57
+ _credentials.merge(region: fetch(:aws_region)) if fetch(:aws_region)
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,27 @@
1
+ require 'aws-sdk'
2
+ require 'capistrano/dsl'
3
+
4
+ load File.expand_path("../tasks/elbas.rake", __FILE__)
5
+
6
+ def autoscale(groupname, *args)
7
+ include Capistrano::DSL
8
+
9
+ asgroup = autoscaling.groups[groupname]
10
+ set :aws_autoscale_group, groupname
11
+
12
+ asgroup.ec2_instances.filter('instance-state-name', 'running').each do |instance|
13
+ hostname = instance.dns_name || instance.private_ip_address
14
+ p "ELBAS: Adding server: #{hostname}"
15
+ server(hostname, *args)
16
+ end
17
+
18
+ after('deploy', 'elbas:scale')
19
+ end
20
+
21
+ private
22
+
23
+ def autoscaling
24
+ @_autoscaling ||= AWS::AutoScaling.new \
25
+ access_key_id: fetch(:aws_access_key_id, ENV['AWS_ACCESS_KEY_ID']),
26
+ secret_access_key: fetch(:aws_secret_access_key, ENV['AWS_SECRET_ACCESS_KEY'])
27
+ end
@@ -0,0 +1,54 @@
1
+ module Elbas
2
+ class LaunchConfiguration < AWS
3
+
4
+ def self.create(ami, &block)
5
+ lc = new
6
+ lc.cleanup do
7
+ lc.save(ami)
8
+ yield lc
9
+ end
10
+ end
11
+
12
+ def save(ami)
13
+ info "Creating an EC2 Launch Configuration for AMI: #{ami.id}"
14
+ @aws_counterpart = autoscaling.launch_configurations.create(timestamp(name_prefix), ami.id, instance_size, create_options)
15
+ end
16
+
17
+ def attach_to_asgroup!
18
+ info "Attaching Launch Configuration to AutoScale Group"
19
+ asgroup.update(launch_configuration: aws_counterpart)
20
+ end
21
+
22
+ def destroy(launch_configurations = [])
23
+ launch_configurations.each do |lc|
24
+ info "Deleting old launch configuration: #{lc.name}"
25
+ lc.delete
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def name_prefix
32
+ "elbas-lc-#{environment}"
33
+ end
34
+
35
+ def create_options
36
+ {
37
+ security_groups: base_ec2_instance.security_groups.to_a,
38
+ detailed_instance_monitoring: true,
39
+ associate_public_ip_address: true
40
+ }
41
+ end
42
+
43
+ def instance_size
44
+ fetch(:aws_autoscale_instance_size, 'm1.small')
45
+ end
46
+
47
+ def garbage
48
+ autoscaling.launch_configurations.to_a.select do |lc|
49
+ lc.name =~ /#{name_prefix}/i
50
+ end
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,19 @@
1
+ require 'elbas'
2
+ require 'elbas/aws'
3
+ require 'elbas/ami'
4
+ require 'elbas/launch_configuration'
5
+
6
+ namespace :elbas do
7
+ task :scale do
8
+ set :aws_access_key_id, fetch(:aws_access_key_id, ENV['AWS_ACCESS_KEY_ID'])
9
+ set :aws_secret_access_key, fetch(:aws_secret_access_key, ENV['AWS_SECRET_ACCESS_KEY'])
10
+
11
+ Elbas::AMI.create do |ami|
12
+ p "ELBAS: Created AMI: #{ami.id}"
13
+ Elbas::LaunchConfiguration.create(ami) do |lc|
14
+ p "ELBAS: Created Launch Configuration: #{lc.name}"
15
+ lc.attach_to_asgroup!
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Elbas
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,9 @@
1
+ require_relative '../test_helper'
2
+
3
+ class AMITest < Minitest::Test
4
+ def test_creates_ec2_instance
5
+ Elbas::AMI.create do |ami|
6
+ p ami
7
+ end
8
+ end
9
+ end
data/test/stubs.rb ADDED
@@ -0,0 +1,26 @@
1
+ def stub(url, method = :get, with = {}, &block)
2
+ body = yield
3
+ WebMock.stub_request(method, %r[#{url}]).with(with).to_return body: body
4
+ end
5
+
6
+ stub %r[(.*)/meta-data/iam/security-credentials] do
7
+ <<-BODY
8
+ {
9
+ "Code" : "Success",
10
+ "LastUpdated" : "2012-04-26T16:39:16Z",
11
+ "Type" : "AWS-HMAC",
12
+ "AccessKeyId" : "FAKE_ACCESS_KEY_ID",
13
+ "SecretAccessKey" : "FAKE_SECRET_ACCESS_KEY",
14
+ "Token" : "token",
15
+ "Expiration" : "2012-04-27T22:39:16Z"
16
+ }
17
+ BODY
18
+ end
19
+
20
+ stub %r[ec2.us-east-1.amazonaws.com], :post, body: /DescribeImages&Owner.1=self/ do
21
+ <<-BODY
22
+ {
23
+ "ImageId": "ami-5731123e"
24
+ }
25
+ BODY
26
+ end
@@ -0,0 +1,15 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/reporters'
3
+ require 'webmock'
4
+
5
+ require 'capistrano/all'
6
+
7
+ require 'elbas'
8
+ require 'elbas/aws'
9
+ require 'elbas/ami'
10
+ require 'elbas/launch_configuration'
11
+
12
+ Minitest::Reporters.use!
13
+
14
+ WebMock.disable_net_connect!
15
+ load File.expand_path("../stubs.rb", __FILE__)
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elbas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Logan Serman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-05 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
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: minitest-reporters
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: webmock
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: capistrano
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">"
102
+ - !ruby/object:Gem::Version
103
+ version: 3.0.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">"
109
+ - !ruby/object:Gem::Version
110
+ version: 3.0.0
111
+ description: Capistrano plugin for deploying to AWS ASGroups.. Deploys to all instances
112
+ in a group, creates a fresh AMI post-deploy, and attaches the AMI to your ASGroup.
113
+ email:
114
+ - logan.serman@metova.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - elbas.gemspec
125
+ - lib/elbas.rb
126
+ - lib/elbas/ami.rb
127
+ - lib/elbas/aws.rb
128
+ - lib/elbas/capistrano.rb
129
+ - lib/elbas/launch_configuration.rb
130
+ - lib/elbas/tasks/elbas.rake
131
+ - lib/elbas/version.rb
132
+ - test/aws/ami_test.rb
133
+ - test/stubs.rb
134
+ - test/test_helper.rb
135
+ homepage: http://github.com/metova/elbas
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.2.2
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Capistrano plugin for deploying to AWS ASGroups.
159
+ test_files:
160
+ - test/aws/ami_test.rb
161
+ - test/stubs.rb
162
+ - test/test_helper.rb