capify-ec2 1.1.5 → 1.1.6.pre

Sign up to get free protection for your applications and to get access to all the features.
data/capify-ec2.gemspec CHANGED
@@ -20,4 +20,5 @@ Gem::Specification.new do |s|
20
20
  s.require_paths = ["lib"]
21
21
  s.add_dependency('activesupport', '>= 3.0.0')
22
22
  s.add_dependency('fog')
23
+ s.add_dependency('colored')
23
24
  end
data/lib/capify-ec2.rb CHANGED
@@ -12,7 +12,8 @@ class CapifyEc2
12
12
 
13
13
  def self.running_instances
14
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])
15
- running_instances = ec2.servers.select {|instance| instance.state == "running"}
15
+ project_tag = ec2_config[:project_tag]
16
+ running_instances = ec2.servers.select {|instance| instance.state == "running" && (project_tag.nil? || instance.tags["Project"] == project_tag) }
16
17
  running_instances.each do |instance|
17
18
  instance.instance_eval do
18
19
  def case_insensitive_tag(key)
@@ -22,6 +23,15 @@ class CapifyEc2
22
23
  def name
23
24
  case_insensitive_tag("Name").split('-').reject {|portion| portion.include?(".")}.join("-")
24
25
  end
26
+
27
+ def roles
28
+ role = case_insensitive_tag("Role")
29
+ roles = role.nil? ? [] : [role]
30
+ if (roles_tag = case_insensitive_tag("Roles"))
31
+ roles += case_insensitive_tag("Roles").split(/\s*,\s*/)
32
+ end
33
+ roles
34
+ end
25
35
  end
26
36
  end
27
37
  end
@@ -31,9 +41,12 @@ class CapifyEc2
31
41
  end
32
42
 
33
43
  def self.get_instances_by_role(role)
34
- selected_instances = running_instances.select do |instance|
35
- value = instance.case_insensitive_tag("Role")
36
- value == role.to_s
44
+ selected_instances = running_instances.select do |instance|
45
+ server_roles = [instance.case_insensitive_tag("Role")] || []
46
+ if (roles_tag = instance.case_insensitive_tag("Roles"))
47
+ server_roles += roles_tag.split(/\s*,\s*/)
48
+ end
49
+ server_roles.member?(role.to_s)
37
50
  end
38
51
  end
39
52
 
@@ -4,28 +4,42 @@ Capistrano::Configuration.instance(:must_exist).load do
4
4
  def ec2_role(role_to_define)
5
5
  defined_role = role_to_define.is_a?(Hash) ? role_to_define[:name] : role_to_define
6
6
  instances = CapifyEc2.get_instances_by_role(defined_role)
7
- subroles = role_to_define.is_a?(Hash) ? role_to_define[:options] : {}
8
7
 
8
+ define_instance_roles(defined_role, instances)
9
+ define_role_roles(defined_role, instances)
10
+ end
11
+
12
+ def define_instance_roles(role_to_define, instances)
9
13
  instances.each do |instance|
10
14
  task instance.name.to_sym do
11
15
  set :server_address, instance.dns_name
12
- role :web, *server_address
13
- role :app, *server_address
16
+ server instance.dns_name, role_to_define.to_sym
14
17
  end
15
18
  end
16
-
17
- task defined_role.to_sym do
19
+ end
20
+
21
+ def define_role_roles(role_to_define, instances)
22
+ task role_to_define.to_sym do
18
23
  instances.each do |instance|
19
- new_options = {}
20
- subroles.each {|key, value| new_options[key] = true if value.to_s == instance.name}
21
- if new_options
22
- role defined_role.to_sym, instance.dns_name, new_options
23
- else
24
- role defined_role.to_sym, instance.dns_name
25
- end
26
- end
24
+ define_role(role_to_define, instance.dns_name)
25
+ end
27
26
  end
28
- end
27
+ end
28
+
29
+ def define_role(role_to_define, dns_names)
30
+ dns_names = [dns_names] if dns_names.is_a?(String)
31
+ subroles = role_to_define.is_a?(Hash) ? role_to_define[:options] : {}
32
+ new_options = {}
33
+ subroles.each {|key, value| new_options[key] = true if value.to_s == instance.name}
34
+
35
+ dns_names.each do |dns_name|
36
+ if new_options
37
+ role role_to_define.to_sym, dns_name, new_options
38
+ else
39
+ role role_to_define.to_sym, dns_name
40
+ end
41
+ end
42
+ end
29
43
 
30
44
  def ec2_roles(*roles)
31
45
  roles.each {|role| ec2_role(role)}
@@ -50,6 +64,14 @@ Capistrano::Configuration.instance(:must_exist).load do
50
64
  puts CapifyEc2.server_names.sort
51
65
  end
52
66
 
67
+ task :ec2_status do
68
+ CapifyEc2.running_instances.each_with_index do |instance, i|
69
+ puts sprintf "%-11s: %-40s %-20s %-20s %-62s %-20s (%s)",
70
+ i.to_s.magenta, instance.name, instance.id.red, instance.flavor_id.cyan,
71
+ instance.dns_name.blue, instance.availability_zone.green, instance.roles.join(", ").yellow
72
+ end
73
+ end
74
+
53
75
  namespace :deploy do
54
76
  before "deploy", "deregister_instance"
55
77
  after "deploy", "register_instance"
@@ -1,5 +1,5 @@
1
1
  module Capify
2
2
  module Ec2
3
- VERSION = "1.1.5"
3
+ VERSION = "1.1.6.pre"
4
4
  end
5
5
  end
data/readme.md CHANGED
@@ -7,7 +7,7 @@ eg: If you have three servers on amazon's ec2.
7
7
 
8
8
  server-1 Tag: Role => "web"
9
9
  server-2 Tag: Role => "db"
10
- server-3 Tag: Role => "web"
10
+ server-3 Tag: Roles => "web, db"
11
11
 
12
12
  Installing
13
13
 
@@ -41,12 +41,17 @@ Additionally
41
41
  Will generate
42
42
 
43
43
  task :server-2 do
44
- role :web, {server-2 public dns fetched from Amazon}
44
+ role :db, {server-2 public dns fetched from Amazon}
45
45
  end
46
-
46
+
47
+ task :server-3 do
48
+ role :db, {server-3 public dns fetched from Amazon}
49
+ end
50
+
47
51
  task :db do
48
52
  role :db, {server-2 public dns fetched from Amazon}
49
- end
53
+ role :db, {server-3 public dns fetched from Amazon}
54
+ end
50
55
 
51
56
  Running
52
57
 
@@ -67,6 +72,13 @@ Running
67
72
  will remove server-1 from whatever instance it is currently
68
73
  registered against.
69
74
 
75
+ Running
76
+
77
+ cap ec2_status
78
+
79
+ will list the currently running servers and their associated details
80
+ (public dns, instance id, roles etc)
81
+
70
82
  More options
71
83
  ====================================================
72
84
 
@@ -104,11 +116,15 @@ The yml file needs to look something like this:
104
116
  :aws_params:
105
117
  :region: 'eu-west-1'
106
118
  :load_balanced: true
119
+ :project_name: "YOUR APP NAME"
107
120
 
108
121
  The :aws_params are optional.
109
122
  If :load_balanced is set to true, the gem uses pre and post-deploy
110
123
  hooks to deregister the instance, reregister it, and validate its
111
124
  health.
112
- :load_balanced only works for individual instances, not
113
- for roles.
114
- ====================================================
125
+ :load_balanced only works for individual instances, not for roles.
126
+
127
+ The :project_name parameter is optional. It will limit any commands to
128
+ running against those instances with a "Project" tag set to the value
129
+ "YOUR APP NAME".
130
+
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capify-ec2
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease: false
4
+ hash: 961915972
5
+ prerelease: true
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 5
10
- version: 1.1.5
9
+ - 6
10
+ - pre
11
+ version: 1.1.6.pre
11
12
  platform: ruby
12
13
  authors:
13
14
  - Noah Cantor
@@ -16,7 +17,7 @@ autorequire:
16
17
  bindir: bin
17
18
  cert_chain: []
18
19
 
19
- date: 2011-05-26 00:00:00 +01:00
20
+ date: 2011-06-09 00:00:00 +01:00
20
21
  default_executable:
21
22
  dependencies:
22
23
  - !ruby/object:Gem::Dependency
@@ -49,6 +50,20 @@ dependencies:
49
50
  version: "0"
50
51
  type: :runtime
51
52
  version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: colored
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :runtime
66
+ version_requirements: *id003
52
67
  description: Grabs roles from ec2's tags and autogenerates capistrano tasks
53
68
  email:
54
69
  - noah.cantor@forward.co.uk
@@ -89,12 +104,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
104
  required_rubygems_version: !ruby/object:Gem::Requirement
90
105
  none: false
91
106
  requirements:
92
- - - ">="
107
+ - - ">"
93
108
  - !ruby/object:Gem::Version
94
- hash: 3
109
+ hash: 25
95
110
  segments:
96
- - 0
97
- version: "0"
111
+ - 1
112
+ - 3
113
+ - 1
114
+ version: 1.3.1
98
115
  requirements: []
99
116
 
100
117
  rubyforge_project: capify-ec2