capify-ec2 1.1.2 → 1.1.5
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.
- data/lib/capify-ec2.rb +62 -31
- data/lib/capify-ec2/capistrano.rb +3 -1
- data/lib/capify-ec2/version.rb +1 -1
- data/readme.md +19 -5
- metadata +4 -4
data/lib/capify-ec2.rb
CHANGED
@@ -3,11 +3,15 @@ require 'fog'
|
|
3
3
|
|
4
4
|
class CapifyEc2
|
5
5
|
|
6
|
-
attr_accessor :
|
6
|
+
attr_accessor :load_balancer
|
7
|
+
SLEEP_COUNT = 5
|
7
8
|
|
9
|
+
def self.ec2_config
|
10
|
+
YAML.load(File.new("config/ec2.yml"))
|
11
|
+
end
|
12
|
+
|
8
13
|
def self.running_instances
|
9
|
-
|
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])
|
14
|
+
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])
|
11
15
|
running_instances = ec2.servers.select {|instance| instance.state == "running"}
|
12
16
|
running_instances.each do |instance|
|
13
17
|
instance.instance_eval do
|
@@ -22,6 +26,10 @@ class CapifyEc2
|
|
22
26
|
end
|
23
27
|
end
|
24
28
|
|
29
|
+
def self.instance_health(load_balancer, instance)
|
30
|
+
elb.describe_instance_health(load_balancer.id, instance.id).body['DescribeInstanceHealthResult']['InstanceStates'][0]['State']
|
31
|
+
end
|
32
|
+
|
25
33
|
def self.get_instances_by_role(role)
|
26
34
|
selected_instances = running_instances.select do |instance|
|
27
35
|
value = instance.case_insensitive_tag("Role")
|
@@ -40,38 +48,61 @@ class CapifyEc2
|
|
40
48
|
running_instances.map {|instance| instance.name}
|
41
49
|
end
|
42
50
|
|
43
|
-
def self.
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
51
|
+
def self.elb
|
52
|
+
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])
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.get_load_balancer_by_instance(instance_id)
|
56
|
+
hash = elb.load_balancers.inject({}) do |collect, load_balancer|
|
57
|
+
load_balancer.instances.each {|load_balancer_instance_id| collect[load_balancer_instance_id] = load_balancer}
|
58
|
+
collect
|
59
|
+
end
|
60
|
+
hash[instance_id]
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.get_load_balancer_by_name(load_balancer_name)
|
64
|
+
lbs = {}
|
65
|
+
elb.load_balancers.each do |load_balancer|
|
66
|
+
lbs[load_balancer.id] = load_balancer
|
48
67
|
end
|
49
|
-
|
68
|
+
lbs[load_balancer_name]
|
69
|
+
|
50
70
|
end
|
51
71
|
|
52
72
|
def self.deregister_instance_from_elb(instance_name)
|
53
|
-
return unless
|
54
|
-
|
55
|
-
|
56
|
-
|
73
|
+
return unless ec2_config[:load_balanced]
|
74
|
+
instance = get_instance_by_name(instance_name).first
|
75
|
+
return if instance.nil?
|
76
|
+
@@load_balancer = get_load_balancer_by_instance(instance.id)
|
77
|
+
return if @@load_balancer.nil?
|
78
|
+
|
79
|
+
elb.deregister_instances_from_load_balancer(instance.id, @@load_balancer.id)
|
57
80
|
end
|
58
81
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
82
|
+
def self.register_instance_in_elb(instance_name, load_balancer_name = '')
|
83
|
+
return if !ec2_config[:load_balanced]
|
84
|
+
instance = get_instance_by_name(instance_name).first
|
85
|
+
return if instance.nil?
|
86
|
+
load_balancer = get_load_balancer_by_name(load_balancer_name) || @@load_balancer
|
87
|
+
return if load_balancer.nil?
|
88
|
+
|
89
|
+
elb.register_instances_with_load_balancer(instance.id, load_balancer.id)
|
90
|
+
|
91
|
+
fail_after = ec2_config[:fail_after] || 30
|
92
|
+
state = instance_health(load_balancer, instance)
|
93
|
+
time_elapsed = 0
|
94
|
+
|
95
|
+
while time_elapsed < fail_after
|
96
|
+
break if state == "InService"
|
97
|
+
sleep SLEEP_COUNT
|
98
|
+
time_elapsed += SLEEP_COUNT
|
99
|
+
STDERR.puts 'Verifying Instance Health'
|
100
|
+
state = instance_health(load_balancer, instance)
|
76
101
|
end
|
77
|
-
|
102
|
+
if state == 'InService'
|
103
|
+
STDERR.puts "#{instance.name}: Healthy"
|
104
|
+
else
|
105
|
+
STDERR.puts "#{instance.name}: tests timed out after #{time_elapsed} seconds."
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -37,7 +37,9 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
task :register_instance do
|
40
|
-
|
40
|
+
servers = variables[:logger].instance_variable_get("@options")[:actions].first
|
41
|
+
load_balancer_name = variables[:logger].instance_variable_get("@options")[:vars][:loadbalancer]
|
42
|
+
CapifyEc2.register_instance_in_elb(servers, load_balancer_name)
|
41
43
|
end
|
42
44
|
|
43
45
|
task :date do
|
data/lib/capify-ec2/version.rb
CHANGED
data/readme.md
CHANGED
@@ -54,6 +54,19 @@ Running
|
|
54
54
|
|
55
55
|
will run the date command on all server's tagged with the web role
|
56
56
|
|
57
|
+
Running
|
58
|
+
|
59
|
+
cap server-1 register-instance -s loadbalancer=elb-1
|
60
|
+
|
61
|
+
will register server-1 to be used by elb-1
|
62
|
+
|
63
|
+
Running
|
64
|
+
|
65
|
+
cap server-1 deregister-instance
|
66
|
+
|
67
|
+
will remove server-1 from whatever instance it is currently
|
68
|
+
registered against.
|
69
|
+
|
57
70
|
More options
|
58
71
|
====================================================
|
59
72
|
|
@@ -90,11 +103,12 @@ The yml file needs to look something like this:
|
|
90
103
|
:aws_secret_access_key: "YOUR SECRET"
|
91
104
|
:aws_params:
|
92
105
|
:region: 'eu-west-1'
|
93
|
-
|
106
|
+
:load_balanced: true
|
94
107
|
|
95
108
|
The :aws_params are optional.
|
96
|
-
If :load_balanced is set to true, the gem
|
97
|
-
|
98
|
-
|
109
|
+
If :load_balanced is set to true, the gem uses pre and post-deploy
|
110
|
+
hooks to deregister the instance, reregister it, and validate its
|
111
|
+
health.
|
99
112
|
:load_balanced only works for individual instances, not
|
100
|
-
for roles.
|
113
|
+
for roles.
|
114
|
+
====================================================
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: capify-ec2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 1.1.
|
9
|
+
- 5
|
10
|
+
version: 1.1.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Noah Cantor
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-05-
|
19
|
+
date: 2011-05-26 00:00:00 +01:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|