capify-ec2 1.5.1 → 1.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +7 -0
- data/lib/capify-ec2/capistrano.rb +38 -32
- data/lib/capify-ec2/version.rb +1 -1
- data/readme.md +17 -8
- metadata +4 -4
data/Changelog.md
CHANGED
@@ -2,13 +2,13 @@ require File.join(File.dirname(__FILE__), '../capify-ec2')
|
|
2
2
|
require 'colored'
|
3
3
|
require 'pp'
|
4
4
|
|
5
|
-
Capistrano::Configuration.instance(:must_exist).load do
|
5
|
+
Capistrano::Configuration.instance(:must_exist).load do
|
6
6
|
def capify_ec2
|
7
7
|
@capify_ec2 ||= CapifyEc2.new(fetch(:ec2_config, 'config/ec2.yml'), fetch(:stage, ''))
|
8
8
|
end
|
9
9
|
|
10
10
|
namespace :ec2 do
|
11
|
-
|
11
|
+
|
12
12
|
desc "Prints out all ec2 instances. index, name, instance_id, size, DNS/IP, region, tags"
|
13
13
|
task :status do
|
14
14
|
capify_ec2.display_instances
|
@@ -42,16 +42,19 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
42
42
|
if "ec2:ssh" == server then
|
43
43
|
server = variables[:logger].instance_variable_get("@options")[:actions][2]
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
if server
|
47
47
|
instance = numeric?(server) ? capify_ec2.desired_instances[server.to_i] : capify_ec2.get_instance_by_name(server)
|
48
48
|
|
49
49
|
if instance and instance.contact_point then
|
50
|
-
port = ssh_options
|
50
|
+
port = ssh_options.fetch(:port, 22)
|
51
51
|
key = ""
|
52
|
-
if !ssh_options[:keys].to_s.empty?
|
52
|
+
if ssh_options[:keys].is_a?(String) && !ssh_options[:keys].to_s.empty?
|
53
53
|
key = "-i #{ssh_options[:keys]}"
|
54
54
|
end
|
55
|
+
if ssh_options[:keys].is_a?(Array) && ssh_options[:keys].length > 0
|
56
|
+
key = "-i #{ssh_options[:keys].first}"
|
57
|
+
end
|
55
58
|
command = "ssh -p #{port} #{key} #{user}@#{instance.contact_point}"
|
56
59
|
puts "Running `#{command}`"
|
57
60
|
exec(command)
|
@@ -71,7 +74,7 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
71
74
|
after "deploy", "ec2:register_instance"
|
72
75
|
after "deploy:rollback", "ec2:register_instance"
|
73
76
|
end
|
74
|
-
|
77
|
+
|
75
78
|
desc "Deploy to servers one at a time."
|
76
79
|
task :rolling_deploy do
|
77
80
|
puts "[Capify-EC2] Performing rolling deployment..."
|
@@ -101,7 +104,10 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
101
104
|
puts "[Capify-EC2] Determining release revision..."
|
102
105
|
set :revision, (fetch :real_revision)
|
103
106
|
|
104
|
-
all_servers.
|
107
|
+
all_servers.each_with_index do |server,index|
|
108
|
+
|
109
|
+
server_dns = server[0]
|
110
|
+
server_roles = server[1]
|
105
111
|
|
106
112
|
roles.clear
|
107
113
|
|
@@ -112,9 +118,9 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
112
118
|
role a_role, server_dns, all_options[a_role][server_dns]
|
113
119
|
is_load_balanced = true if all_options[a_role][server_dns][:load_balanced]
|
114
120
|
end
|
115
|
-
|
121
|
+
|
116
122
|
puts "[Capify-EC2]"
|
117
|
-
puts "[Capify-EC2] Beginning deployment to #{instance_dns_with_name_tag(server_dns)} with #{server_roles.count > 1 ? 'roles' : 'role'} '#{server_roles.join(', ')}'...".bold
|
123
|
+
puts "[Capify-EC2] (#{index+1} of #{all_servers.length}) Beginning deployment to #{instance_dns_with_name_tag(server_dns)} with #{server_roles.count > 1 ? 'roles' : 'role'} '#{server_roles.join(', ')}'...".bold
|
118
124
|
|
119
125
|
unless dry_run
|
120
126
|
load_balancer_to_reregister = capify_ec2.deregister_instance_from_elb_by_dns(server_dns) if is_load_balanced
|
@@ -124,7 +130,7 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
124
130
|
top.deploy.default
|
125
131
|
|
126
132
|
server_roles.each do |a_role|
|
127
|
-
|
133
|
+
|
128
134
|
# If healthcheck(s) are defined for this role, run them.
|
129
135
|
if all_options[a_role][server_dns][:healthcheck]
|
130
136
|
healthchecks_for_role = [ all_options[a_role][server_dns][:healthcheck] ].flatten
|
@@ -137,9 +143,9 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
137
143
|
options[:timeout] = healthcheck_for_role[:timeout] ||= 60
|
138
144
|
|
139
145
|
healthcheck = capify_ec2.instance_health_by_url( server_dns,
|
140
|
-
healthcheck_for_role[:port],
|
141
|
-
healthcheck_for_role[:path],
|
142
|
-
healthcheck_for_role[:result],
|
146
|
+
healthcheck_for_role[:port],
|
147
|
+
healthcheck_for_role[:path],
|
148
|
+
healthcheck_for_role[:result],
|
143
149
|
options )
|
144
150
|
if healthcheck
|
145
151
|
puts "[Capify-EC2] Healthcheck #{i+1} of #{healthchecks_for_role.size} for role '#{a_role}' successful.".green.bold
|
@@ -161,7 +167,7 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
161
167
|
raise CapifyEC2RollingDeployError.new("ELB registration timeout exceeded", server_dns)
|
162
168
|
end
|
163
169
|
end
|
164
|
-
|
170
|
+
|
165
171
|
puts "[Capify-EC2] Deployment successful to #{instance_dns_with_name_tag(server_dns)}.".green.bold
|
166
172
|
successful_deploys << server_dns
|
167
173
|
|
@@ -186,7 +192,7 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
186
192
|
exit 1
|
187
193
|
else
|
188
194
|
puts "[Capify-EC2]"
|
189
|
-
puts "[Capify-EC2] Rolling deployment completed.".green.bold
|
195
|
+
puts "[Capify-EC2] Rolling deployment completed.".green.bold
|
190
196
|
|
191
197
|
rolling_deploy_status(all_servers, successful_deploys, failed_deploys)
|
192
198
|
end
|
@@ -196,11 +202,11 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
196
202
|
puts "[Capify-EC2]"
|
197
203
|
puts "[Capify-EC2] Successful servers:"
|
198
204
|
format_rolling_deploy_results( all_servers, successful_deploys )
|
199
|
-
|
205
|
+
|
200
206
|
puts "[Capify-EC2]"
|
201
207
|
puts "[Capify-EC2] Failed servers:"
|
202
208
|
format_rolling_deploy_results( all_servers, failed_deploys )
|
203
|
-
|
209
|
+
|
204
210
|
puts "[Capify-EC2]"
|
205
211
|
puts "[Capify-EC2] Pending servers:"
|
206
212
|
pending_deploys = (all_servers.keys - successful_deploys) - failed_deploys
|
@@ -209,10 +215,10 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
209
215
|
|
210
216
|
def ec2_roles(*roles)
|
211
217
|
server_name = variables[:logger].instance_variable_get("@options")[:actions].first unless variables[:logger].instance_variable_get("@options")[:actions][1].nil?
|
212
|
-
|
218
|
+
|
213
219
|
if !server_name.nil? && !server_name.empty?
|
214
220
|
named_instance = capify_ec2.get_instance_by_name(server_name)
|
215
|
-
|
221
|
+
|
216
222
|
task named_instance.name.to_sym do
|
217
223
|
remove_default_roles
|
218
224
|
server_address = named_instance.contact_point
|
@@ -221,8 +227,8 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
221
227
|
roles = named_instance.roles
|
222
228
|
else
|
223
229
|
roles = [named_instance.tags[ capify_ec2.ec2_config[:aws_roles_tag] ]].flatten
|
224
|
-
end
|
225
|
-
|
230
|
+
end
|
231
|
+
|
226
232
|
roles.each do |role|
|
227
233
|
define_role({:name => role, :options => {:on_no_matching_servers => :continue}}, named_instance)
|
228
234
|
end
|
@@ -230,10 +236,10 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
230
236
|
end
|
231
237
|
roles.each {|role| ec2_role(role)}
|
232
238
|
end
|
233
|
-
|
239
|
+
|
234
240
|
def ec2_role(role_name_or_hash)
|
235
241
|
role = role_name_or_hash.is_a?(Hash) ? role_name_or_hash : {:name => role_name_or_hash, :options => {}, :variables => {}}
|
236
|
-
|
242
|
+
|
237
243
|
instances = capify_ec2.get_instances_by_role(role[:name])
|
238
244
|
if role[:options] && role[:options].delete(:default)
|
239
245
|
instances.each do |instance|
|
@@ -248,7 +254,7 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
248
254
|
|
249
255
|
define_role_roles(role, instances)
|
250
256
|
define_instance_roles(role, instances)
|
251
|
-
end
|
257
|
+
end
|
252
258
|
|
253
259
|
def define_regions(region, role)
|
254
260
|
instances = []
|
@@ -279,29 +285,29 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
279
285
|
instances.each do |instance|
|
280
286
|
define_role(role, instance)
|
281
287
|
end
|
282
|
-
end
|
288
|
+
end
|
283
289
|
end
|
284
290
|
|
285
291
|
def define_role(role, instance)
|
286
292
|
options = role[:options] || {}
|
287
293
|
variables = role[:variables] || {}
|
288
294
|
|
289
|
-
cap_options = options.inject({}) do |cap_options, (key, value)|
|
295
|
+
cap_options = options.inject({}) do |cap_options, (key, value)|
|
290
296
|
cap_options[key] = true if value.to_s == instance.name
|
291
297
|
cap_options
|
292
|
-
end
|
298
|
+
end
|
293
299
|
|
294
300
|
ec2_options = instance.tags[capify_ec2.ec2_config[:aws_options_tag]] || ""
|
295
301
|
ec2_options.split(%r{,\s*}).compact.each { |ec2_option| cap_options[ec2_option.to_sym] = true }
|
296
302
|
|
297
|
-
variables.each do |key, value|
|
303
|
+
variables.each do |key, value|
|
298
304
|
set key, value
|
299
305
|
cap_options[key] = value unless cap_options.has_key? key
|
300
306
|
end
|
301
307
|
|
302
308
|
role role[:name].to_sym, instance.contact_point, cap_options
|
303
309
|
end
|
304
|
-
|
310
|
+
|
305
311
|
def numeric?(object)
|
306
312
|
true if Float(object) rescue false
|
307
313
|
end
|
@@ -315,9 +321,9 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
315
321
|
"#{singular}s"
|
316
322
|
end
|
317
323
|
end
|
318
|
-
|
319
|
-
def remove_default_roles
|
324
|
+
|
325
|
+
def remove_default_roles
|
320
326
|
roles.reject! { true }
|
321
327
|
end
|
322
|
-
|
328
|
+
|
323
329
|
end
|
data/lib/capify-ec2/version.rb
CHANGED
data/readme.md
CHANGED
@@ -147,7 +147,7 @@ In our examples, imagine that you have three servers on EC2 named and tagged as
|
|
147
147
|
You need to add a call to `ec2_roles` in your `deploy.rb`, like so:
|
148
148
|
|
149
149
|
```ruby
|
150
|
-
ec2_roles :web
|
150
|
+
ec2_roles {:name => :web}
|
151
151
|
```
|
152
152
|
|
153
153
|
This will generate the following tasks:
|
@@ -170,7 +170,7 @@ end
|
|
170
170
|
Note that there are no tasks created for 'server-2', as it does not have the role 'web'. If we were to change the `ec2_roles` definition in your `deploy.rb` to the following:
|
171
171
|
|
172
172
|
```ruby
|
173
|
-
ec2_roles :db
|
173
|
+
ec2_roles {:name => :db}
|
174
174
|
```
|
175
175
|
|
176
176
|
Then we will instead see the following tasks generated:
|
@@ -257,6 +257,15 @@ end
|
|
257
257
|
|
258
258
|
|
259
259
|
|
260
|
+
#### Shorthand Role Definition
|
261
|
+
If you are defining a role with no options or variables, you can define it using the following shorthand:
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
ec2_roles :web
|
265
|
+
```
|
266
|
+
|
267
|
+
|
268
|
+
|
260
269
|
#### Options
|
261
270
|
|
262
271
|
##### Via EC2 Tags
|
@@ -548,7 +557,7 @@ Using the `cap ec2:ssh` command, you can quickly connect to a specific instance,
|
|
548
557
|
cap ec2:ssh 1
|
549
558
|
```
|
550
559
|
|
551
|
-
will attempt to connect to instance number 1 (as shown in `ec2:status`), using the public DNS address provided by AWS
|
560
|
+
will attempt to connect to instance number 1 (as shown in `ec2:status`), using the public DNS address provided by AWS and the first SSH key listed in ` ssh_options[:keys] `.
|
552
561
|
|
553
562
|
|
554
563
|
|
@@ -640,25 +649,25 @@ In our examples, imagine that you have three servers on EC2 named and tagged as
|
|
640
649
|
<td>'Name' Tag</td>
|
641
650
|
<td>'Roles' Tag</td>
|
642
651
|
<td>'Options' Tag</td>
|
643
|
-
|
652
|
+
<td>'Stages' Tag</td>
|
644
653
|
</tr>
|
645
654
|
<tr>
|
646
655
|
<td>server-1</td>
|
647
656
|
<td>web</td>
|
648
657
|
<td>cron,resque</td>
|
649
|
-
|
658
|
+
<td>production</td>
|
650
659
|
</tr>
|
651
660
|
<tr>
|
652
661
|
<td>server-2</td>
|
653
662
|
<td>db</td>
|
654
663
|
<td></td>
|
655
|
-
|
664
|
+
<td>production</td>
|
656
665
|
</tr>
|
657
666
|
<tr>
|
658
667
|
<td>server-3</td>
|
659
668
|
<td>web,db</td>
|
660
669
|
<td></td>
|
661
|
-
|
670
|
+
<td>staging</td>
|
662
671
|
</tr>
|
663
672
|
</table>
|
664
673
|
|
@@ -736,4 +745,4 @@ Report Issues/Feature requests on [GitHub Issues](http://github.com/forward/capi
|
|
736
745
|
|
737
746
|
### Copyright
|
738
747
|
|
739
|
-
Copyright (c) 2011, 2012, 2013 Forward. See [LICENSE](https://github.com/forward/capify-ec2/blob/master/LICENSE) for details.
|
748
|
+
Copyright (c) 2011, 2012, 2013 Forward. See [LICENSE](https://github.com/forward/capify-ec2/blob/master/LICENSE) for details.
|
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: 7
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 1.5.
|
9
|
+
- 2
|
10
|
+
version: 1.5.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Noah Cantor
|
@@ -18,7 +18,7 @@ autorequire:
|
|
18
18
|
bindir: bin
|
19
19
|
cert_chain: []
|
20
20
|
|
21
|
-
date: 2013-
|
21
|
+
date: 2013-10-02 00:00:00 Z
|
22
22
|
dependencies:
|
23
23
|
- !ruby/object:Gem::Dependency
|
24
24
|
name: fog
|