capify-ec2 1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/capify-ec2.gemspec +17 -12
- data/lib/capify-ec2.rb +25 -4
- data/lib/capify-ec2/capistrano.rb +17 -13
- data/lib/capify-ec2/version.rb +5 -0
- data/readme.md +100 -0
- metadata +22 -10
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/capify-ec2.gemspec
CHANGED
@@ -1,18 +1,23 @@
|
|
1
|
-
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
2
|
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "capify-ec2/version"
|
3
4
|
|
4
5
|
Gem::Specification.new do |s|
|
5
|
-
s.name
|
6
|
-
s.version
|
7
|
-
s.
|
8
|
-
s.
|
9
|
-
s.
|
10
|
-
s.
|
11
|
-
s.
|
12
|
-
s.
|
13
|
-
|
14
|
-
s.
|
6
|
+
s.name = "capify-ec2"
|
7
|
+
s.version = Capify::Ec2::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Noah Cantor", "Siddharth Dawara"]
|
10
|
+
s.email = ["noah.cantor@forward.co.uk", "siddharth.dawara@forward.co.uk"]
|
11
|
+
s.homepage = "http://github.com/forward/capify-ec2"
|
12
|
+
s.summary = %q{Grabs roles from ec2's tags and autogenerates capistrano tasks}
|
13
|
+
s.description = %q{Grabs roles from ec2's tags and autogenerates capistrano tasks}
|
14
|
+
|
15
|
+
s.rubyforge_project = "capify-ec2"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
15
21
|
s.add_dependency('activesupport', '>= 3.0.0')
|
16
22
|
s.add_dependency('fog')
|
17
23
|
end
|
18
|
-
|
data/lib/capify-ec2.rb
CHANGED
@@ -3,9 +3,11 @@ require 'fog'
|
|
3
3
|
|
4
4
|
class CapifyEc2
|
5
5
|
|
6
|
+
attr_accessor :ec2_config, :instance, :elb_name, :elb
|
7
|
+
|
6
8
|
def self.running_instances
|
7
|
-
ec2_config = YAML.load(File.new("config/ec2.yml"))
|
8
|
-
ec2 = Fog::Compute.new(:provider => 'AWS', :aws_access_key_id => ec2_config[:aws_access_key_id], :aws_secret_access_key => ec2_config[:aws_secret_access_key], :region => ec2_config[:aws_params][:region])
|
9
|
+
@ec2_config = YAML.load(File.new("config/ec2.yml"))
|
10
|
+
ec2 = Fog::Compute.new(:provider => 'AWS', :aws_access_key_id => @ec2_config[:aws_access_key_id], :aws_secret_access_key => @ec2_config[:aws_secret_access_key], :region => @ec2_config[:aws_params][:region])
|
9
11
|
running_instances = ec2.servers.select {|instance| instance.state == "running"}
|
10
12
|
running_instances.each do |instance|
|
11
13
|
instance.instance_eval do
|
@@ -27,7 +29,7 @@ class CapifyEc2
|
|
27
29
|
end
|
28
30
|
end
|
29
31
|
|
30
|
-
def self.
|
32
|
+
def self.get_instance_by_name(name)
|
31
33
|
selected_instances = running_instances.select do |instance|
|
32
34
|
value = instance.case_insensitive_tag("Name")
|
33
35
|
value == name.to_s
|
@@ -36,6 +38,25 @@ class CapifyEc2
|
|
36
38
|
|
37
39
|
def self.server_names
|
38
40
|
running_instances.map {|instance| instance.name}
|
39
|
-
end
|
41
|
+
end
|
40
42
|
|
43
|
+
def self.get_elb_name_by_instance(instance_id)
|
44
|
+
@elb = Fog::AWS::ELB.new(:aws_access_key_id => @ec2_config[:aws_access_key_id], :aws_secret_access_key => @ec2_config[:aws_secret_access_key], :region => @ec2_config[:aws_params][:region])
|
45
|
+
@elb.load_balancers.each do |load_balancer|
|
46
|
+
load_balancer.instances.each {|instance| return load_balancer.id if instance_id == instance}
|
47
|
+
end
|
48
|
+
return nil
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.deregister_instance_from_elb(instance_name)
|
52
|
+
return unless @ec2_config[:load_balanced]
|
53
|
+
@instance = get_instance_by_name(instance_name).first
|
54
|
+
@elb_name = get_elb_name_by_instance(@instance.id)
|
55
|
+
@elb.deregister_instances_from_load_balancer(@instance.id, @elb_name) unless @elb_name.nil?
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.register_instance_in_elb
|
59
|
+
return unless @ec2_config[:load_balanced]
|
60
|
+
@elb.register_instances_with_load_balancer(@instance.id, @elb_name) unless @elb_name.nil?
|
61
|
+
end
|
41
62
|
end
|
@@ -2,15 +2,10 @@ require File.join(File.dirname(__FILE__), '../capify-ec2')
|
|
2
2
|
|
3
3
|
Capistrano::Configuration.instance(:must_exist).load do
|
4
4
|
def ec2_role(role_to_define)
|
5
|
-
|
6
|
-
if role_to_define.is_a?(Hash)
|
7
|
-
defined_role = role_to_define[:name]
|
8
|
-
subroles = role_to_define[:options]
|
9
|
-
else
|
10
|
-
defined_role = role_to_define
|
11
|
-
end
|
5
|
+
defined_role = role_to_define.is_a?(Hash) ? role_to_define[:name] : role_to_define
|
12
6
|
instances = CapifyEc2.get_instances_by_role(defined_role)
|
13
|
-
|
7
|
+
subroles = role_to_define.is_a?(Hash) ? role_to_define[:options] : {}
|
8
|
+
|
14
9
|
instances.each do |instance|
|
15
10
|
task instance.name.to_sym do
|
16
11
|
set :server_address, instance.dns_name
|
@@ -31,16 +26,20 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
31
26
|
end
|
32
27
|
end
|
33
28
|
end
|
34
|
-
|
35
|
-
# task :add_tag do
|
36
|
-
# instance_name = variables[:logger].instance_variable_get("@options")[:actions].first
|
37
|
-
# CapifyEc2.add_tag(instance_name, tag, value)
|
38
|
-
# end
|
39
29
|
|
40
30
|
def ec2_roles(*roles)
|
41
31
|
roles.each {|role| ec2_role(role)}
|
42
32
|
end
|
43
33
|
|
34
|
+
task :deregister_instance do
|
35
|
+
servers = variables[:logger].instance_variable_get("@options")[:actions].first
|
36
|
+
CapifyEc2.deregister_instance_from_elb(servers)
|
37
|
+
end
|
38
|
+
|
39
|
+
task :register_instance do
|
40
|
+
CapifyEc2.register_instance_in_elb
|
41
|
+
end
|
42
|
+
|
44
43
|
task :date do
|
45
44
|
run "date"
|
46
45
|
end
|
@@ -48,4 +47,9 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
48
47
|
task :server_names do
|
49
48
|
puts CapifyEc2.server_names.sort
|
50
49
|
end
|
50
|
+
|
51
|
+
namespace :deploy do
|
52
|
+
before "deploy", "deregister_instance"
|
53
|
+
after "deploy", "register_instance"
|
54
|
+
end
|
51
55
|
end
|
data/readme.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
Capify Ec2
|
2
|
+
====================================================
|
3
|
+
|
4
|
+
capify-ec2 is used to generate capistrano namespaces using ec2 tags.
|
5
|
+
|
6
|
+
eg: If you have three servers on amazon's ec2.
|
7
|
+
|
8
|
+
server-1 Tag: Role => "web"
|
9
|
+
server-2 Tag: Role => "db"
|
10
|
+
server-3 Tag: Role => "web"
|
11
|
+
|
12
|
+
Installing
|
13
|
+
|
14
|
+
gem install capify-ec2
|
15
|
+
|
16
|
+
In your deploy.rb:
|
17
|
+
|
18
|
+
require "capify-ec2/capistrano"
|
19
|
+
ec2_roles :web
|
20
|
+
|
21
|
+
Will generate
|
22
|
+
|
23
|
+
task :server-1 do
|
24
|
+
role :web, {server-1 public dns fetched from Amazon}
|
25
|
+
end
|
26
|
+
|
27
|
+
task :server-3 do
|
28
|
+
role :web, {server-1 public dns fetched from Amazon}
|
29
|
+
end
|
30
|
+
|
31
|
+
task :web do
|
32
|
+
role :web, {server-1 public dns fetched from Amazon}
|
33
|
+
role :web, {server-3 public dns fetched from Amazon}
|
34
|
+
end
|
35
|
+
|
36
|
+
Additionally
|
37
|
+
|
38
|
+
require "capify-ec2/capistrano"
|
39
|
+
ec2_roles :db
|
40
|
+
|
41
|
+
Will generate
|
42
|
+
|
43
|
+
task :server-2 do
|
44
|
+
role :web, {server-2 public dns fetched from Amazon}
|
45
|
+
end
|
46
|
+
|
47
|
+
task :db do
|
48
|
+
role :db, {server-2 public dns fetched from Amazon}
|
49
|
+
end
|
50
|
+
|
51
|
+
Running
|
52
|
+
|
53
|
+
cap web date
|
54
|
+
|
55
|
+
will run the date command on all server's tagged with the web role
|
56
|
+
|
57
|
+
More options
|
58
|
+
====================================================
|
59
|
+
|
60
|
+
ec2_roles {:name=>"web", :options=>{:cron=>"server-1"}}
|
61
|
+
|
62
|
+
Will generate
|
63
|
+
|
64
|
+
task :server-1 do
|
65
|
+
role :web, {server-1 public dns fetched from Amazon}, :cron=>true
|
66
|
+
end
|
67
|
+
|
68
|
+
task :server-3 do
|
69
|
+
role :web, {server-1 public dns fetched from Amazon}
|
70
|
+
end
|
71
|
+
|
72
|
+
task :web do
|
73
|
+
role :web, {server-1 public dns fetched from Amazon}, :cron=>true
|
74
|
+
role :web, {server-3 public dns fetched from Amazon}
|
75
|
+
end
|
76
|
+
|
77
|
+
Which is cool if you want a task like this in deploy.rb
|
78
|
+
|
79
|
+
task :update_cron => :web, :only=>{:cron} do
|
80
|
+
Do something to a server with cron on it
|
81
|
+
end
|
82
|
+
|
83
|
+
Ec2 config
|
84
|
+
====================================================
|
85
|
+
|
86
|
+
This gem requires 'config/ec2.yml' in your project.
|
87
|
+
The yml file needs to look something like this:
|
88
|
+
|
89
|
+
:aws_access_key_id: "YOUR ACCESS KEY"
|
90
|
+
:aws_secret_access_key: "YOUR SECRET"
|
91
|
+
:aws_params:
|
92
|
+
:region: 'eu-west-1'
|
93
|
+
:load_balanced: true
|
94
|
+
|
95
|
+
The :aws_params are optional.
|
96
|
+
If :load_balanced is set to true, the gem will deregister
|
97
|
+
the instance from the load balancer before deploying and
|
98
|
+
reregister it after, using before and after deploy hooks.
|
99
|
+
:load_balanced only works for individual instances, not
|
100
|
+
for roles.
|
metadata
CHANGED
@@ -1,19 +1,23 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: capify-ec2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
9
11
|
platform: ruby
|
10
12
|
authors:
|
11
13
|
- Noah Cantor
|
14
|
+
- Siddharth Dawara
|
12
15
|
autorequire:
|
13
16
|
bindir: bin
|
14
17
|
cert_chain: []
|
15
18
|
|
16
|
-
date: 2011-05-
|
19
|
+
date: 2011-05-20 00:00:00 +01:00
|
20
|
+
default_executable:
|
17
21
|
dependencies:
|
18
22
|
- !ruby/object:Gem::Dependency
|
19
23
|
name: activesupport
|
@@ -45,8 +49,10 @@ dependencies:
|
|
45
49
|
version: "0"
|
46
50
|
type: :runtime
|
47
51
|
version_requirements: *id002
|
48
|
-
description:
|
49
|
-
email:
|
52
|
+
description: Grabs roles from ec2's tags and autogenerates capistrano tasks
|
53
|
+
email:
|
54
|
+
- noah.cantor@forward.co.uk
|
55
|
+
- siddharth.dawara@forward.co.uk
|
50
56
|
executables: []
|
51
57
|
|
52
58
|
extensions: []
|
@@ -54,10 +60,16 @@ extensions: []
|
|
54
60
|
extra_rdoc_files: []
|
55
61
|
|
56
62
|
files:
|
57
|
-
-
|
58
|
-
-
|
63
|
+
- .gitignore
|
64
|
+
- Gemfile
|
65
|
+
- Rakefile
|
59
66
|
- capify-ec2.gemspec
|
60
|
-
|
67
|
+
- lib/capify-ec2.rb
|
68
|
+
- lib/capify-ec2/capistrano.rb
|
69
|
+
- lib/capify-ec2/version.rb
|
70
|
+
- readme.md
|
71
|
+
has_rdoc: true
|
72
|
+
homepage: http://github.com/forward/capify-ec2
|
61
73
|
licenses: []
|
62
74
|
|
63
75
|
post_install_message:
|
@@ -85,8 +97,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
97
|
version: "0"
|
86
98
|
requirements: []
|
87
99
|
|
88
|
-
rubyforge_project:
|
89
|
-
rubygems_version: 1.
|
100
|
+
rubyforge_project: capify-ec2
|
101
|
+
rubygems_version: 1.6.2
|
90
102
|
signing_key:
|
91
103
|
specification_version: 3
|
92
104
|
summary: Grabs roles from ec2's tags and autogenerates capistrano tasks
|