capify-ec2 1.1.6 → 1.1.7.pre.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/capify-ec2.rb +35 -22
- data/lib/capify-ec2/capistrano.rb +43 -4
- data/lib/capify-ec2/version.rb +1 -1
- data/readme.md +22 -2
- metadata +13 -9
data/lib/capify-ec2.rb
CHANGED
@@ -1,47 +1,60 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'fog'
|
3
|
+
require 'colored'
|
3
4
|
|
4
5
|
class CapifyEc2
|
5
6
|
|
6
7
|
attr_accessor :load_balancer
|
7
8
|
SLEEP_COUNT = 5
|
8
|
-
|
9
|
+
|
9
10
|
def self.ec2_config
|
10
11
|
YAML.load(File.new("config/ec2.yml"))
|
11
12
|
end
|
12
|
-
|
13
|
-
def self.
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
13
|
+
|
14
|
+
def self.determine_regions(region = nil)
|
15
|
+
regions = ec2_config[:aws_params][:regions] || [ec2_config[:aws_params][:region]]
|
16
|
+
regions = [region] if region
|
17
|
+
regions
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.running_instances(region = nil)
|
21
|
+
regions = determine_regions(region)
|
22
|
+
instances = []
|
23
|
+
regions.each do |region|
|
24
|
+
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 => region)
|
25
|
+
project_tag = ec2_config[:project_tag]
|
26
|
+
running_instances = ec2.servers.select {|instance| instance.state == "running" && (project_tag.nil? || instance.tags["Project"] == project_tag) }
|
27
|
+
running_instances.each do |instance|
|
28
|
+
instance.instance_eval do
|
29
|
+
def case_insensitive_tag(key)
|
30
|
+
tags[key] || tags[key.downcase]
|
31
|
+
end
|
22
32
|
|
23
|
-
|
24
|
-
|
25
|
-
|
33
|
+
def name
|
34
|
+
case_insensitive_tag("Name").split('-').reject {|portion| portion.include?(".")}.join("-")
|
35
|
+
end
|
26
36
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
37
|
+
def roles
|
38
|
+
role = case_insensitive_tag("Role")
|
39
|
+
roles = role.nil? ? [] : [role]
|
40
|
+
if (roles_tag = case_insensitive_tag("Roles"))
|
41
|
+
roles += case_insensitive_tag("Roles").split(/\s*,\s*/)
|
42
|
+
end
|
43
|
+
roles
|
32
44
|
end
|
33
|
-
roles
|
34
45
|
end
|
46
|
+
instances << instance
|
35
47
|
end
|
36
48
|
end
|
49
|
+
instances
|
37
50
|
end
|
38
51
|
|
39
52
|
def self.instance_health(load_balancer, instance)
|
40
53
|
elb.describe_instance_health(load_balancer.id, instance.id).body['DescribeInstanceHealthResult']['InstanceStates'][0]['State']
|
41
54
|
end
|
42
55
|
|
43
|
-
def self.get_instances_by_role(role)
|
44
|
-
selected_instances = running_instances.select do |instance|
|
56
|
+
def self.get_instances_by_role(role, region = nil)
|
57
|
+
selected_instances = running_instances(region).select do |instance|
|
45
58
|
server_roles = [instance.case_insensitive_tag("Role")] || []
|
46
59
|
if (roles_tag = instance.case_insensitive_tag("Roles"))
|
47
60
|
server_roles += roles_tag.split(/\s*,\s*/)
|
@@ -1,40 +1,69 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), '../capify-ec2')
|
2
|
+
require 'colored'
|
2
3
|
|
3
4
|
Capistrano::Configuration.instance(:must_exist).load do
|
4
5
|
def ec2_role(role_name_or_hash)
|
5
6
|
role = role_name_or_hash.is_a?(Hash) ? role_name_or_hash : {:name => role_name_or_hash,:options => {}}
|
6
7
|
instances = CapifyEc2.get_instances_by_role(role[:name])
|
7
8
|
|
9
|
+
set :specified_roles, []
|
10
|
+
|
11
|
+
if role[:options].delete(:default)
|
12
|
+
instances.each do |instance|
|
13
|
+
define_role(role, instance)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
regions = CapifyEc2.ec2_config[:aws_params][:regions] || [CapifyEc2.ec2_config[:aws_params][:region]]
|
18
|
+
regions.each do |region|
|
19
|
+
define_regions(region, role)
|
20
|
+
end unless regions.nil?
|
21
|
+
|
8
22
|
define_instance_roles(role, instances)
|
9
23
|
define_role_roles(role, instances)
|
10
24
|
end
|
11
25
|
|
26
|
+
def define_regions(region, role)
|
27
|
+
instances = CapifyEc2.get_instances_by_role(role[:name], region)
|
28
|
+
task region.to_sym do
|
29
|
+
remove_default_roles
|
30
|
+
instances.each do |instance|
|
31
|
+
define_role(role, instance)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
12
36
|
def define_instance_roles(role, instances)
|
13
37
|
instances.each do |instance|
|
14
38
|
task instance.name.to_sym do
|
15
|
-
|
39
|
+
specified_roles << role[:name]
|
40
|
+
remove_default_roles
|
16
41
|
define_role(role, instance)
|
17
42
|
end
|
18
43
|
end
|
19
44
|
end
|
20
45
|
|
21
46
|
def define_role_roles(role, instances)
|
22
|
-
defined_role =
|
23
47
|
task role[:name].to_sym do
|
48
|
+
specified_roles << role[:name]
|
24
49
|
instances.each do |instance|
|
50
|
+
remove_default_roles
|
25
51
|
define_role(role, instance)
|
26
52
|
end
|
27
53
|
end
|
28
54
|
end
|
29
55
|
|
56
|
+
def remove_default_roles
|
57
|
+
roles.reject! { |role_name, v| !specified_roles.member?(role_name) }
|
58
|
+
end
|
59
|
+
|
30
60
|
def define_role(role, instance)
|
31
|
-
dns_names = [dns_names] if dns_names.is_a?(String)
|
32
61
|
subroles = role[:options]
|
33
62
|
new_options = {}
|
34
63
|
subroles.each {|key, value| new_options[key] = true if value.to_s == instance.name}
|
35
64
|
|
36
65
|
if new_options
|
37
|
-
role role[:name].to_sym, instance.dns_name, new_options
|
66
|
+
role role[:name].to_sym, instance.dns_name, new_options
|
38
67
|
else
|
39
68
|
role role[:name].to_sym, instance.dns_name
|
40
69
|
end
|
@@ -71,9 +100,19 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
71
100
|
end
|
72
101
|
end
|
73
102
|
|
103
|
+
task :ssh do
|
104
|
+
instances = CapifyEc2.running_instances
|
105
|
+
instance = respond_to?(:i) ? instances[i.to_i] : instances.first
|
106
|
+
port = ssh_options[:port] || 22
|
107
|
+
command = "ssh -p #{port} #{user}@#{instance.dns_name}"
|
108
|
+
puts "Running `#{command}`"
|
109
|
+
system(command)
|
110
|
+
end
|
111
|
+
|
74
112
|
namespace :deploy do
|
75
113
|
before "deploy", "deregister_instance"
|
76
114
|
after "deploy", "register_instance"
|
77
115
|
after "deploy:rollback", "register_instance"
|
78
116
|
end
|
117
|
+
|
79
118
|
end
|
data/lib/capify-ec2/version.rb
CHANGED
data/readme.md
CHANGED
@@ -79,6 +79,15 @@ Running
|
|
79
79
|
will list the currently running servers and their associated details
|
80
80
|
(public dns, instance id, roles etc)
|
81
81
|
|
82
|
+
Running
|
83
|
+
|
84
|
+
cap ssh -s i=1
|
85
|
+
|
86
|
+
will launch ssh using the user and port specified in your configuration.
|
87
|
+
The 'i' argument is the index of the server to ssh into. Use the 'ec2_status'
|
88
|
+
command to see the list of servers with their indices. If the 'i' argument
|
89
|
+
is omitted then the first instance will be used.
|
90
|
+
|
82
91
|
More options
|
83
92
|
====================================================
|
84
93
|
|
@@ -105,6 +114,17 @@ Which is cool if you want a task like this in deploy.rb
|
|
105
114
|
Do something to a server with cron on it
|
106
115
|
end
|
107
116
|
|
117
|
+
|
118
|
+
ec2_roles :name=>:web, :options=>{ :default => true }
|
119
|
+
|
120
|
+
Will make :web the default role so you can just type 'cap deploy'.
|
121
|
+
Multiple roles can be defaults so:
|
122
|
+
|
123
|
+
ec2_roles :name=>:web, :options=>{ :default => true }
|
124
|
+
ec2_roles :name=>:app, :options=>{ :default => true }
|
125
|
+
|
126
|
+
would be the equivalent of 'cap app web deploy'
|
127
|
+
|
108
128
|
Ec2 config
|
109
129
|
====================================================
|
110
130
|
|
@@ -116,7 +136,7 @@ The yml file needs to look something like this:
|
|
116
136
|
:aws_params:
|
117
137
|
:region: 'eu-west-1'
|
118
138
|
:load_balanced: true
|
119
|
-
:
|
139
|
+
:project_tag: "YOUR APP NAME"
|
120
140
|
|
121
141
|
The :aws_params are optional.
|
122
142
|
If :load_balanced is set to true, the gem uses pre and post-deploy
|
@@ -124,7 +144,7 @@ hooks to deregister the instance, reregister it, and validate its
|
|
124
144
|
health.
|
125
145
|
:load_balanced only works for individual instances, not for roles.
|
126
146
|
|
127
|
-
The :
|
147
|
+
The :project_tag parameter is optional. It will limit any commands to
|
128
148
|
running against those instances with a "Project" tag set to the value
|
129
149
|
"YOUR APP NAME".
|
130
150
|
|
metadata
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: capify-ec2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 1923831979
|
5
|
+
prerelease: true
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
|
9
|
+
- 7
|
10
|
+
- pre
|
11
|
+
- 5
|
12
|
+
version: 1.1.7.pre.5
|
11
13
|
platform: ruby
|
12
14
|
authors:
|
13
15
|
- Noah Cantor
|
@@ -16,7 +18,7 @@ autorequire:
|
|
16
18
|
bindir: bin
|
17
19
|
cert_chain: []
|
18
20
|
|
19
|
-
date: 2011-06-
|
21
|
+
date: 2011-06-22 00:00:00 +01:00
|
20
22
|
default_executable:
|
21
23
|
dependencies:
|
22
24
|
- !ruby/object:Gem::Dependency
|
@@ -103,12 +105,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
103
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
106
|
none: false
|
105
107
|
requirements:
|
106
|
-
- - "
|
108
|
+
- - ">"
|
107
109
|
- !ruby/object:Gem::Version
|
108
|
-
hash:
|
110
|
+
hash: 25
|
109
111
|
segments:
|
110
|
-
-
|
111
|
-
|
112
|
+
- 1
|
113
|
+
- 3
|
114
|
+
- 1
|
115
|
+
version: 1.3.1
|
112
116
|
requirements: []
|
113
117
|
|
114
118
|
rubyforge_project: capify-ec2
|