aws_blue_green_deploy 0.0.1

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: a6c06739e02e7d9e900d8392d5f2ed55dceeddd3
4
+ data.tar.gz: fd9758012fb8623eec630cc4a605d22939184665
5
+ SHA512:
6
+ metadata.gz: 905bae91e4dd00c3941702f46632f39c68849e7ad277e56b9e7f7c04d62212d0001e567880f02d0031ae678d8468870cb0a3789d271c4808b1dd83213b6fe77d
7
+ data.tar.gz: 5bdb8c3dbcd8c0490104e56acf15e3b3870fedbeea7d24fcbc5dc95bfc58203323989772ceaacdcc50c7babbb4e014ea6efa6bf09c12350e273108d1da738d5d
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rvmrc ADDED
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 2.1.3@aws_blue_green_deploy" > .rvmrc
9
+ environment_id="ruby-2.1.3@aws_blue_green_deploy"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.25.33 (stable)" # 1.10.1 seems like a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | __rvm_awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
27
+ do
28
+ if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
29
+ then \. "${__hook}" || true
30
+ fi
31
+ done
32
+ unset __hook
33
+ if (( ${rvm_use_flag:=1} >= 2 )) # display only when forced
34
+ then
35
+ if [[ $- == *i* ]] # check for interactive shells
36
+ then printf "%b" "Using: $(tput setaf 2 2>/dev/null)$GEM_HOME$(tput sgr0 2>/dev/null)
37
+ " # show the user the ruby and gemset they are using in green
38
+ else printf "%b" "Using: $GEM_HOME
39
+ " # don't use colors in non-interactive shells
40
+ fi
41
+ fi
42
+ else
43
+ # If the environment file has not yet been created, use the RVM CLI to select.
44
+ rvm --create "$environment_id" || {
45
+ echo "Failed to create RVM environment '${environment_id}'."
46
+ return 1
47
+ }
48
+ fi
49
+
50
+ # If you use bundler, this might be useful to you:
51
+ # if [[ -s Gemfile ]] && {
52
+ # ! builtin command -v bundle >/dev/null ||
53
+ # builtin command -v bundle | GREP_OPTIONS=""
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.1.1"
4
+
5
+ script: rake spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aws_blue_green_deploy.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jon Parten
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.
@@ -0,0 +1,77 @@
1
+ # AwsBlueGreenDeploy
2
+
3
+ ## Summary
4
+
5
+ This gem automates a "Blue/Green" style of web server farm deployments, in which one set of servers is active, in load balancer rotation, while the other set is out of rotation. Out of rotation servers are deployed to, validated and then put into rotation. The the formerly active servers are pulled out.
6
+
7
+ This gem automates this process with AutoScaling groups, and actually eliminates the need to have a pool of inactive servers. These servers are created at deploy time.
8
+
9
+ This gem assumes you have 2 static autoscaling groups asscociated to the load balancer the deploy will be run against.
10
+ This gem manipulates the launch configuration and asg size of any auto scaling group that does not have instances in load balancer rotation in order to create new instances with a new version of code.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'aws_blue_green_deploy'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install aws_blue_green_deploy
27
+
28
+ ## Usage
29
+
30
+ https://travis-ci.org/jparten/aws_blue_green_deploy.svg?branch=master
31
+
32
+ To use:
33
+
34
+ This gem assumes you have 2 static autoscaling groups asscociated to the load balancer the deploy will be run against.
35
+ This gem manipulates the launch configuration and asg size of any auto scaling group that does not have instances in load balancer rotation in order to create new instances with
36
+ a new version of code. s
37
+
38
+
39
+ Build a hash or a json config with the following parameters:
40
+
41
+ * "elb_name" - Name of ELB to be manipulated by deploy action, the automation will identify which associated ASG's need to be manipulated.
42
+ * "asg_size" - Number of instances to scale load balancer during deployment
43
+ * "lc_name" - Name of launch configuration to be created by deployment process
44
+ * "lc_image" - Ami ID to be used by new launch configuration
45
+ * "lc_instance_type" - Instance size to be used by new new launch config
46
+ * "lc_key_pair" - Authentication keypair to be used by new launch config
47
+ * "lc_security_groups" - Array of security groups to be used by new launch config
48
+ * "lc_user_data" - User data to be passed in to each instance created by the new launch config
49
+ * "region" - AWS region where operation should is begin performed
50
+ * "access_key_id" - Key with access to perform EC2 operations
51
+ * "secret_access_key" - Secret key with access to perform EC2 operations
52
+
53
+ Once the parameters above are in a json file, a A/B style release can be enacted by:
54
+
55
+ ```ruby
56
+ AwsBlueGreenDeploy.configure_with("path to json config")
57
+ AwsBlueGreenDeploy.enact_deployment
58
+ ```
59
+
60
+ Alternatively you can configure by passing in a hash with the values above, overriding any values passed in via json:
61
+
62
+ ```ruby
63
+ options = {:elb_name => "Test"}
64
+ AwsBlueGreenDeploy.configure(options)
65
+ AwsBlueGreenDeploy.enact_deployment
66
+ ```
67
+
68
+ The module methods drive the DeployController class. This class can be used independently of the module to better control timing/validation neccessary for a production release.
69
+
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it ( https://github.com/jparten/aws_blue_green_deploy/fork )
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aws_blue_green_deploy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aws_blue_green_deploy"
8
+ spec.version = AwsBlueGreenDeploy::VERSION
9
+ spec.authors = ["Jon Parten"]
10
+ spec.email = ["jon.parten@gmail.com"]
11
+ spec.summary = %q{Provide an interface for conducting Blue Green deployments.}
12
+ spec.description = %q{Provide an interface for conducting Blue Green deployments in AWS EC2 using ASG's and ELB's.}
13
+ spec.homepage = ""
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.1"
24
+
25
+ spec.add_runtime_dependency 'aws-sdk'
26
+ end
@@ -0,0 +1,56 @@
1
+ require "aws_blue_green_deploy/version"
2
+ require "aws_blue_green_deploy/deploy_controller"
3
+ require 'json'
4
+ require 'aws-sdk'
5
+
6
+ module AwsBlueGreenDeploy
7
+ # Configure through hash
8
+ @config = {}
9
+
10
+ def self.configure(opts = {})
11
+ opts.each {|k,v| @config[k.to_sym] = v}
12
+ end
13
+
14
+ # Configure through yaml file
15
+ def self.configure_with(path_to_json_file)
16
+ begin
17
+ config = JSON.parse(IO.read(path_to_json_file), {:symbolize_names => true})
18
+ rescue Errno::ENOENT
19
+ log(:warning, "JSON configuration file couldn't be found. Using defaults."); return
20
+ end
21
+
22
+ configure(config)
23
+ "Loaded Config Successfully"
24
+ end
25
+
26
+ def self.config
27
+ @config
28
+ end
29
+
30
+ def self.enact_deployment
31
+ auth_options = {:access_key_id => config[:access_key_id], :secret_access_key => config[:secret_access_key], :session_token => config[:session_token], :region => config[:region]}
32
+ as_controller = AWS::AutoScaling.new(auth_options)
33
+ lb_controller = AWS::ELB.new(auth_options)
34
+ dc = DeployController.new(as_controller, lb_controller)
35
+
36
+ elb_name = config[:elb_name]
37
+ asg_size = config[:asg_size].to_i
38
+ lc_name = config[:lc_name]
39
+ lc_image = config[:lc_image]
40
+ lc_instance_type = config[:lc_instance_type]
41
+ lc_key_pair = config[:lc_key_pair]
42
+ lc_security_groups = config[:lc_security_groups]
43
+ lc_user_data = config[:lc_user_data]
44
+
45
+ lc = dc.create_new_launch_config(lc_name, lc_image, lc_instance_type, lc_key_pair, lc_security_groups, lc_user_data)
46
+ puts "Created Launch Config: #{lc.name}"
47
+ oor_groups = dc.out_rotation_groups(elb_name)
48
+ puts "Out of rotation ASG: #{oor_groups.each{|group| group.name + " "}}"
49
+ ir_groups = dc.in_rotation_groups(elb_name)
50
+ puts "In rotation ASG: #{ir_groups.each{|group| group.name + " "}}"
51
+ dc.update_groups_lc_and_scale_up(oor_groups, lc, asg_size)
52
+ puts "Scaled up #{oor_groups.each{|group| group.name + " "}} to #{asg_size} instances with new launch config}"
53
+ dc.rotate_groups(ir_groups, elb_name)
54
+ puts "Removed #{ir_groups.each{|group| group.name + " "}} instances from load balancer."
55
+ end
56
+ end
@@ -0,0 +1,61 @@
1
+ module AwsBlueGreenDeploy
2
+ class DeployController
3
+ def initialize(as_controller, lb_controller)
4
+ @as_controller = as_controller
5
+ @lb_controller = lb_controller
6
+ end
7
+
8
+ def create_new_launch_config(name, image, instance_type, key_pair, security_groups, user_data)
9
+ options = {:key_pair => key_pair, :security_groups => security_groups, :user_data => user_data}
10
+ launch_config = @as_controller.launch_configurations.create(name, image, instance_type, options)
11
+ end
12
+
13
+ def out_rotation_groups(elb_name)
14
+ target_groups = groups_associated_to_elb(elb_name)
15
+ in_rotation_groups = in_rotation_groups(elb_name)
16
+ out_rotation_groups = []
17
+ target_groups.each do |target_group|
18
+ in_rotation_groups.each do |ir_group|
19
+ out_rotation_groups.push(target_group) unless target_group.name == ir_group.name
20
+ end
21
+ end
22
+
23
+ out_rotation_groups.each{|group| puts group.name}
24
+ end
25
+
26
+ def in_rotation_groups(elb_name)
27
+ target_groups = groups_associated_to_elb(elb_name)
28
+ in_rotation_groups = []
29
+ in_rotation_instances = @lb_controller.load_balancers[elb_name].instances.map{|instance| instance.id}
30
+ target_groups.each do |group|
31
+ group_instances = group.ec2_instances.map{|instance| instance.id}
32
+ if (in_rotation_instances & group_instances).count > 0
33
+ in_rotation_groups.push(group)
34
+ end
35
+ end
36
+ in_rotation_groups
37
+ end
38
+
39
+ def update_groups_lc_and_scale_up(autoscaling_groups, launch_config, asg_size)
40
+ autoscaling_groups.each{|group| replace_launch_config(group, launch_config, asg_size)}
41
+ end
42
+
43
+ def rotate_groups(groups, elb_name)
44
+ instance_ids = []
45
+ groups.each do |group|
46
+ group.ec2_instances.each{|instance| instance_ids.push(instance.instance_id)}
47
+ end
48
+ instance_ids.each{|instance_id| puts instance_id}
49
+ instance_ids.each{|instance_id| @lb_controller.load_balancers[elb_name].instances.deregister(instance_id)}
50
+ end
51
+
52
+ private
53
+ def replace_launch_config(group, launch_config, asg_size)
54
+ group.update(desired_capacity: asg_size, min_size: asg_size, max_size: asg_size, launch_configuration: launch_config)
55
+ end
56
+
57
+ def groups_associated_to_elb(elb_name)
58
+ @as_controller.groups.select{|group| group.load_balancer_names.include?(elb_name)}
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module AwsBlueGreenDeploy
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe "DeployController" do
4
+ before do
5
+ @as_controller = AWS::AutoScaling.new(:stub_requests => true, :access_key_id => "faux_key", :secret_access_key => "faux_access_key")
6
+ @lb_controller = AWS::ELB.new(:stub_requests => true, :access_key_id => "faux_key", :secret_access_key => "faux_access_key")
7
+ @dc = AwsBlueGreenDeploy::DeployController.new(@as_controller, @lb_controller)
8
+ end
9
+
10
+ describe "#create_new_launch_config" do
11
+ before do
12
+ @lc_name = "test-launch-config"
13
+ @lc_image = "ami-000000a0"
14
+ @lc_instance_type = "m3.medium"
15
+ @lc_key_pair = "test-key-pair"
16
+ @lc_security_groups = ["sg-00a00000","sg-00b00000", "sg-00c00000"]
17
+ @lc_user_data = "test-user-data"
18
+ @lc = @dc.create_new_launch_config(@lc_name, @lc_image, @lc_instance_type, @lc_key_pair, @lc_security_groups, @lc_user_data)
19
+ end
20
+
21
+ it "return an aws launch config" do
22
+ expect(@lc).to be_a AWS::AutoScaling::LaunchConfiguration
23
+ end
24
+
25
+ it "should make a launch config in aws with the name specified" do
26
+ expect(@as_controller.launch_configurations["test-launch-config"].name).to eq(@lc_name)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1 @@
1
+ require 'aws_blue_green_deploy'
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws_blue_green_deploy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jon Parten
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-12 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: aws-sdk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Provide an interface for conducting Blue Green deployments in AWS EC2
70
+ using ASG's and ELB's.
71
+ email:
72
+ - jon.parten@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rvmrc"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - aws_blue_green_deploy.gemspec
85
+ - lib/aws_blue_green_deploy.rb
86
+ - lib/aws_blue_green_deploy/deploy_controller.rb
87
+ - lib/aws_blue_green_deploy/version.rb
88
+ - spec/lib/aws_blue_green_deploy_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: ''
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.2.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Provide an interface for conducting Blue Green deployments.
114
+ test_files:
115
+ - spec/lib/aws_blue_green_deploy_spec.rb
116
+ - spec/spec_helper.rb