cap3-elb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +68 -0
- data/cap-scm-upload.gemspec +17 -0
- data/lib/cap3-elb.rb +1 -0
- data/lib/tasks/elb.cap +72 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f0cbf43b4f3d4cc5e1cda8080a3d504c94f77ec8
|
4
|
+
data.tar.gz: 35c7785fd21fd5f67ccb3eb1a91abab7e7389830
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 72960d53bcc040f8fbbdbab0dfe188fdb2aacb787d89be6bb113f0c06016a3f2a3797604b3f803dfbc8b5337969855a69f3dcb5ff835ceef48a37691b6444552
|
7
|
+
data.tar.gz: c207cd3ea003bc83674a2d5a58c5795584dd27394b784d9b2b008c885e309ea368c33cc71e231c5bacd78ca1424dcb2005bd3abe488de02bdddb818d762f4b87
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
This gem adds capistrano3 tasks for adding and removing nodes from
|
2
|
+
Amazon's Elastic Load Balancer.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
1. Add `cap3-elb` to your Gemfile
|
7
|
+
|
8
|
+
```
|
9
|
+
# Gemfile
|
10
|
+
gem 'cap3-elb'
|
11
|
+
```
|
12
|
+
|
13
|
+
2. Use `upload` scm in Capistrano
|
14
|
+
```
|
15
|
+
# Capfile
|
16
|
+
require 'cap3-elb'
|
17
|
+
```
|
18
|
+
|
19
|
+
3. Configure the name of the load balancer and your AWS credentials. I suggest
|
20
|
+
storing the credentials in something like
|
21
|
+
[dotgpg](https://github.com/ConradIrwin/dotgpg) and using environment
|
22
|
+
variables to avoid storing these keys alongside your code.
|
23
|
+
|
24
|
+
```
|
25
|
+
# config/deploy.rb
|
26
|
+
|
27
|
+
set :load_balancer, "website"
|
28
|
+
|
29
|
+
set :aws_secret_access_key, ENV.fetch("AWS_SECRET_ACCESS_KEY")
|
30
|
+
set :aws_access_key_id, ENV.fetch("AWS_ACCESS_KEY_ID")
|
31
|
+
```
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
### cap elb:status
|
36
|
+
|
37
|
+
This shows you a list of all your hosts along with their load-balancer status.
|
38
|
+
|
39
|
+
```
|
40
|
+
bundle exec cap elb:status
|
41
|
+
```
|
42
|
+
|
43
|
+
### cap elb:add
|
44
|
+
|
45
|
+
The `elb:add` task adds hosts to the load balancer. By default it will add
|
46
|
+
all hosts, but you can filter things to only add one specific host using
|
47
|
+
capistrano's `--host` flag:
|
48
|
+
|
49
|
+
```
|
50
|
+
bundle exec cap elb:add --host web1.example.com
|
51
|
+
```
|
52
|
+
|
53
|
+
### cap elb:remove
|
54
|
+
|
55
|
+
The `elb:remove` task removes hosts from the load balancer. It will abort
|
56
|
+
if you try and remove more than one node at a time, and also if removing
|
57
|
+
the node would result in no healthy instances.
|
58
|
+
|
59
|
+
```
|
60
|
+
bundle exec cap elb:remove --host web1.example.com
|
61
|
+
```
|
62
|
+
|
63
|
+
## Meta-fu
|
64
|
+
|
65
|
+
`cap3-elb` is released under the MIT license, see LICENSE.MIT for
|
66
|
+
details. Bug reports and feature requests are welcome.
|
67
|
+
|
68
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.name = 'cap3-elb'
|
3
|
+
gem.version = '0.1.0'
|
4
|
+
|
5
|
+
gem.summary = 'Capistrano tasks to maintain a load balancer'
|
6
|
+
gem.description = "Provides new commands to manage your ELB load balancer from capistrano."
|
7
|
+
|
8
|
+
gem.authors = ['Conrad Irwin']
|
9
|
+
gem.email = %w(conrad@bugsnag.com)
|
10
|
+
gem.homepage = 'http://github.com/ConradIrwin/cap3-elb'
|
11
|
+
|
12
|
+
gem.license = 'MIT'
|
13
|
+
|
14
|
+
gem.add_dependency 'capistrano', '~> 3.0'
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split("\n")
|
17
|
+
end
|
data/lib/cap3-elb.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
load File.expand_path("../tasks/elb.cap", __FILE__)
|
data/lib/tasks/elb.cap
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'aws'
|
2
|
+
require 'resolv'
|
3
|
+
|
4
|
+
namespace :elb do
|
5
|
+
task :configure do
|
6
|
+
$load_balancer = AWS::ELB.new(
|
7
|
+
access_key_id: fetch(:aws_access_key_id),
|
8
|
+
secret_access_key: fetch(:aws_secret_access_key)
|
9
|
+
).load_balancers[fetch(:load_balancer)]
|
10
|
+
end
|
11
|
+
|
12
|
+
desc "List the servers in the load balancer"
|
13
|
+
task :status => :configure do
|
14
|
+
in_balancer = $load_balancer.instances.each_with_object({}) { |i, result| result[i.ip_address] = i.elb_health }
|
15
|
+
roles(:app).each do |host|
|
16
|
+
ip = Resolv::DNS.new.getaddress(host.hostname).to_s
|
17
|
+
puts "#{host.hostname}: #{in_balancer.include?(ip) ? in_balancer[ip][:state] : "DOWN"}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
desc "Remove server from the load balancer"
|
22
|
+
task :remove => :configure do
|
23
|
+
raise "don't remove more than one machine" if roles(:app).length > 1
|
24
|
+
|
25
|
+
on roles(:app), in: :sequence do |host|
|
26
|
+
instances = $load_balancer.instances.to_a
|
27
|
+
name = host.hostname[/^[^.]*/]
|
28
|
+
|
29
|
+
|
30
|
+
to_remove = instances.detect{ |instance| instance.tags[:Name] == name }
|
31
|
+
if to_remove
|
32
|
+
|
33
|
+
|
34
|
+
to_keep = instances - [to_remove]
|
35
|
+
unless to_keep.any?{ |instance| instance.elb_health[:state] == 'InService' }
|
36
|
+
raise "Refusing to take #{host} out of rotation as there are no other machines up"
|
37
|
+
end
|
38
|
+
|
39
|
+
puts "Removing #{host.hostname} from the #{fetch(:load_balancer).inspect} load balancer"
|
40
|
+
|
41
|
+
to_remove.remove_from_load_balancer
|
42
|
+
until to_remove.elb_health[:state] == "OutOfService"
|
43
|
+
print "."
|
44
|
+
sleep 1
|
45
|
+
end
|
46
|
+
puts "\n#{host.hostname} #{to_remove.elb_health[:state]}"
|
47
|
+
else
|
48
|
+
puts "#{host.hostname} is not in the #{fetch(:load_balancer).inspect} load balancer"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
desc "Add server to the load balancer"
|
54
|
+
task :add => :configure do
|
55
|
+
on roles(:app), in: :sequence do |host|
|
56
|
+
puts "Adding #{host.hostname} to the #{fetch(:load_balancer).inspect} load balancer."
|
57
|
+
|
58
|
+
instance = AWS::EC2.new.instances.filter('ip-address', Resolv::DNS.new.getaddress(host.hostname).to_s).first
|
59
|
+
|
60
|
+
$load_balancer.instances.register instance
|
61
|
+
|
62
|
+
puts "Waiting for #{host.hostname} to become active..."
|
63
|
+
|
64
|
+
instance = $load_balancer.instances[instance.id]
|
65
|
+
until instance.elb_health[:state] == "InService"
|
66
|
+
print "."
|
67
|
+
sleep 1
|
68
|
+
end
|
69
|
+
puts "\n#{host.hostname} #{instance.elb_health[:state]}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cap3-elb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Conrad Irwin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: capistrano
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
description: Provides new commands to manage your ELB load balancer from capistrano.
|
28
|
+
email:
|
29
|
+
- conrad@bugsnag.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- README.md
|
35
|
+
- cap-scm-upload.gemspec
|
36
|
+
- lib/cap3-elb.rb
|
37
|
+
- lib/tasks/elb.cap
|
38
|
+
homepage: http://github.com/ConradIrwin/cap3-elb
|
39
|
+
licenses:
|
40
|
+
- MIT
|
41
|
+
metadata: {}
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 2.2.2
|
59
|
+
signing_key:
|
60
|
+
specification_version: 4
|
61
|
+
summary: Capistrano tasks to maintain a load balancer
|
62
|
+
test_files: []
|
63
|
+
has_rdoc:
|