capify-ec2 1.1.16.pre.2 → 1.2.0.pre.3

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/capify-ec2.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
- # s.add_dependency('activesupport', '=3.1.1')
22
- s.add_dependency('fog', '=1.0.0')
21
+ s.add_dependency('fog', '=1.1.1')
23
22
  s.add_dependency('colored', '=1.2')
23
+ s.add_dependency('capistrano')
24
24
  end
data/lib/capify-ec2.rb CHANGED
@@ -1,100 +1,74 @@
1
1
  require 'rubygems'
2
2
  require 'fog'
3
3
  require 'colored'
4
+ require File.expand_path(File.dirname(__FILE__) + '/capify-ec2/server')
5
+
4
6
 
5
7
  class CapifyEc2
6
8
 
7
- attr_accessor :load_balancer
9
+ attr_accessor :load_balancer, :instances
8
10
  SLEEP_COUNT = 5
9
-
10
- def self.ec2_config
11
- YAML.load(File.new("config/ec2.yml"))
12
- end
13
11
 
14
- def self.determine_regions(region = nil)
15
- region.nil? ? (ec2_config[:aws_params][:regions] || [ec2_config[:aws_params][:region]]) : [region]
16
- end
17
-
18
- def self.running_instances(region = nil)
19
- regions = determine_regions(region)
20
- instances = []
12
+ def initialize()
13
+ @ec2_config = YAML.load(File.new("config/ec2.yml"))
14
+ regions = determine_regions()
15
+
16
+ @instances = []
21
17
  regions.each do |region|
22
- 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)
23
- project_tag = ec2_config[:project_tag]
24
- running_instances = ec2.servers.select {|instance| instance.state == "running" && (project_tag.nil? || instance.tags["Project"] == project_tag) }
25
- running_instances.each do |instance|
26
- instance.instance_eval do
27
- def case_insensitive_tag(key)
28
- tags[key] || tags[key.downcase]
29
- end
30
-
31
- def name
32
- case_insensitive_tag("Name").split('-').reject {|portion| portion.include?(".")}.join("-")
33
- end
34
-
35
- def roles
36
- role = case_insensitive_tag("Role")
37
- roles = role.nil? ? [] : [role]
38
- if (roles_tag = case_insensitive_tag("Roles"))
39
- roles += case_insensitive_tag("Roles").split(/\s*,\s*/)
40
- end
41
- roles
42
- end
43
- def options
44
- option = case_insensitive_tag("Option")
45
- options = option.nil? ? [] : [option]
46
- if (options_tag = case_insensitive_tag("Options"))
47
- options += case_insensitive_tag("Options").split(/\s*,\s*/)
48
- end
49
- options
50
- end
51
- end
52
- instances << instance
18
+ servers = Fog::Compute.new(:provider => 'AWS', :aws_access_key_id => @ec2_config[:aws_access_key_id],
19
+ :aws_secret_access_key => @ec2_config[:aws_secret_access_key], :region => region).servers
20
+ servers.each do |server|
21
+ @instances << server if server.ready?
53
22
  end
54
23
  end
55
- instances
24
+ end
25
+
26
+ def determine_regions()
27
+ @ec2_config[:aws_params][:regions] || [@ec2_config[:aws_params][:region]]
56
28
  end
57
29
 
58
- def self.instance_health(load_balancer, instance)
59
- elb.describe_instance_health(load_balancer.id, instance.id).body['DescribeInstanceHealthResult']['InstanceStates'][0]['State']
30
+ def display_instances
31
+ @instances.each_with_index do |instance, i|
32
+ puts sprintf "%-11s: %-40s %-20s %-20s %-62s %-20s (%s)",
33
+ i.to_s.magenta, instance.name, instance.id.red, instance.flavor_id.cyan,
34
+ instance.dns_name.blue, instance.availability_zone.green, (instance.role rescue "").yellow
35
+ end
36
+ end
37
+
38
+ def project_instances(project_tag)
39
+ @instances.select {|instance| instance.tags["Project"] == project_tag}
60
40
  end
61
41
 
62
- def self.get_instances_by_role(role)
63
- filter_instances_by_role(running_instances, role)
42
+ def running_instances(region = nil)
43
+ instances = @ec2_config[:project_tag].nil? ? @instances : project_instances(@ec2_config[:project_tag])
64
44
  end
65
45
 
66
- def self.get_instances_by_region(role, region)
67
- return unless region
68
- region_instances = running_instances(region)
69
- filter_instances_by_role(region_instances,role)
70
- end
46
+ def get_instances_by_role(role)
47
+ @instances.select {|instance| instance.role == role.to_s rescue false}
48
+ end
71
49
 
72
- def self.filter_instances_by_role(instances, role)
73
- selected_instances = instances.select do |instance|
74
- server_roles = [instance.case_insensitive_tag("Role")] || []
75
- if (roles_tag = instance.case_insensitive_tag("Roles"))
76
- server_roles += roles_tag.split(/\s*,\s*/)
77
- end
78
- server_roles.member?(role.to_s)
79
- end
50
+ def get_instances_by_region(role, region)
51
+ return unless region
52
+ @instances.select {|instance| instance.availability_zone.match(region) && instance.role == role.to_s rescue false}
80
53
  end
81
54
 
82
- def self.get_instance_by_name(name)
83
- selected_instances = running_instances.select do |instance|
84
- value = instance.case_insensitive_tag("Name")
85
- value == name.to_s
86
- end.first
55
+ def get_instance_by_name(name)
56
+ @instances.select {|instance| instance.name == name}.first
57
+ end
58
+
59
+ def instance_health(load_balancer, instance)
60
+ elb.describe_instance_health(load_balancer.id, instance.id).body['DescribeInstanceHealthResult']['InstanceStates'][0]['State']
87
61
  end
88
62
 
89
- def self.server_names
90
- running_instances.map {|instance| instance.case_insensitive_tag("Name")}
63
+ def server_names
64
+ @instances.map {|instance| instance.name}
91
65
  end
92
66
 
93
- def self.elb
94
- 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])
67
+ def elb
68
+ 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])
95
69
  end
96
70
 
97
- def self.get_load_balancer_by_instance(instance_id)
71
+ def get_load_balancer_by_instance(instance_id)
98
72
  hash = elb.load_balancers.inject({}) do |collect, load_balancer|
99
73
  load_balancer.instances.each {|load_balancer_instance_id| collect[load_balancer_instance_id] = load_balancer}
100
74
  collect
@@ -102,7 +76,7 @@ class CapifyEc2
102
76
  hash[instance_id]
103
77
  end
104
78
 
105
- def self.get_load_balancer_by_name(load_balancer_name)
79
+ def get_load_balancer_by_name(load_balancer_name)
106
80
  lbs = {}
107
81
  elb.load_balancers.each do |load_balancer|
108
82
  lbs[load_balancer.id] = load_balancer
@@ -111,8 +85,8 @@ class CapifyEc2
111
85
 
112
86
  end
113
87
 
114
- def self.deregister_instance_from_elb(instance_name)
115
- return unless ec2_config[:load_balanced]
88
+ def deregister_instance_from_elb(instance_name)
89
+ return unless @ec2_config[:load_balanced]
116
90
  instance = get_instance_by_name(instance_name)
117
91
  return if instance.nil?
118
92
  @@load_balancer = get_load_balancer_by_instance(instance.id)
@@ -121,8 +95,8 @@ class CapifyEc2
121
95
  elb.deregister_instances_from_load_balancer(instance.id, @@load_balancer.id)
122
96
  end
123
97
 
124
- def self.register_instance_in_elb(instance_name, load_balancer_name = '')
125
- return if !ec2_config[:load_balanced]
98
+ def register_instance_in_elb(instance_name, load_balancer_name = '')
99
+ return if !@ec2_config[:load_balanced]
126
100
  instance = get_instance_by_name(instance_name)
127
101
  return if instance.nil?
128
102
  load_balancer = get_load_balancer_by_name(load_balancer_name) || @@load_balancer
@@ -130,7 +104,7 @@ class CapifyEc2
130
104
 
131
105
  elb.register_instances_with_load_balancer(instance.id, load_balancer.id)
132
106
 
133
- fail_after = ec2_config[:fail_after] || 30
107
+ fail_after = @ec2_config[:fail_after] || 30
134
108
  state = instance_health(load_balancer, instance)
135
109
  time_elapsed = 0
136
110
 
@@ -1,27 +1,25 @@
1
1
  require File.join(File.dirname(__FILE__), '../capify-ec2')
2
2
  require 'colored'
3
3
 
4
- Capistrano::Configuration.instance(:must_exist).load do
4
+ Capistrano::Configuration.instance(:must_exist).load do
5
5
  namespace :ec2 do
6
+
6
7
  desc "Prints out all ec2 instances. index, name, instance_id, size, dns_name, region, tags"
7
8
  task :status do
8
- CapifyEc2.running_instances.each_with_index do |instance, i|
9
- puts sprintf "%-11s: %-40s %-20s %-20s %-62s %-20s (%s)",
10
- i.to_s.magenta, instance.case_insensitive_tag("Name"), instance.id.red, instance.flavor_id.cyan,
11
- instance.dns_name.blue, instance.availability_zone.green, instance.roles.join(", ").yellow
12
- end
9
+ CapifyEc2.new.display_instances
13
10
  end
11
+
14
12
  desc "Deregisters instance from its ELB"
15
13
  task :deregister_instance do
16
14
  instance_name = variables[:logger].instance_variable_get("@options")[:actions].first
17
- CapifyEc2.deregister_instance_from_elb(instance_name)
15
+ CapifyEc2.new.deregister_instance_from_elb(instance_name)
18
16
  end
19
17
 
20
18
  desc "Registers an instance with an ELB."
21
19
  task :register_instance do
22
20
  instance_name = variables[:logger].instance_variable_get("@options")[:actions].first
23
21
  load_balancer_name = variables[:logger].instance_variable_get("@options")[:vars][:loadbalancer]
24
- CapifyEc2.register_instance_in_elb(instance_name, load_balancer_name)
22
+ CapifyEc2.new.register_instance_in_elb(instance_name, load_balancer_name)
25
23
  end
26
24
 
27
25
  task :date do
@@ -30,19 +28,18 @@ Capistrano::Configuration.instance(:must_exist).load do
30
28
 
31
29
  desc "Prints list of ec2 server names"
32
30
  task :server_names do
33
- puts CapifyEc2.server_names.sort
31
+ puts CapifyEc2.new.server_names.sort
34
32
  end
35
33
 
36
34
  desc "Allows ssh to instance by id. cap ssh <INSTANCE NAME>"
37
35
  task :ssh do
38
36
  server = variables[:logger].instance_variable_get("@options")[:actions][1]
39
- instance = numeric?(server) ? CapifyEc2.running_instances[server.to_i] : CapifyEc2.get_instance_by_name(server)
37
+ instance = numeric?(server) ? CapifyEc2.new.instances[server.to_i] : CapifyEc2.new.get_instance_by_name(server)
40
38
  port = ssh_options[:port] || 22
41
39
  command = "ssh -p #{port} #{user}@#{instance.dns_name}"
42
40
  puts "Running `#{command}`"
43
41
  exec(command)
44
42
  end
45
-
46
43
  end
47
44
 
48
45
  namespace :deploy do
@@ -50,42 +47,49 @@ Capistrano::Configuration.instance(:must_exist).load do
50
47
  after "deploy", "ec2:register_instance"
51
48
  after "deploy:rollback", "ec2:register_instance"
52
49
  end
53
-
50
+
54
51
  def ec2_roles(*roles)
52
+ @capify_ec2 = CapifyEc2.new
55
53
  server_name = variables[:logger].instance_variable_get("@options")[:actions].first unless variables[:logger].instance_variable_get("@options")[:actions][1].nil?
56
- named_instance = CapifyEc2.get_instance_by_name(server_name)
54
+ named_instance = @capify_ec2.get_instance_by_name(server_name)
55
+
57
56
  task named_instance.name.to_sym do
58
57
  remove_default_roles
59
58
  server_address = named_instance.dns_name
60
- named_instance.roles.each do |role|
61
- define_role({:name => role, :options => {}}, named_instance)
59
+ named_instance.role.each do |role|
60
+ define_role({:name => role, :options => {:on_no_matching_servers => :continue}}, named_instance)
62
61
  end
63
62
  end unless named_instance.nil?
63
+
64
64
  roles.each {|role| ec2_role(role)}
65
65
  end
66
66
 
67
67
  def ec2_role(role_name_or_hash)
68
68
  role = role_name_or_hash.is_a?(Hash) ? role_name_or_hash : {:name => role_name_or_hash,:options => {}}
69
+ @roles[role[:name]]
69
70
 
70
- instances = CapifyEc2.get_instances_by_role(role[:name])
71
+ instances = @capify_ec2.get_instances_by_role(role[:name])
71
72
  if role[:options].delete(:default)
72
73
  instances.each do |instance|
73
74
  define_role(role, instance)
74
75
  end
75
- end
76
-
77
- regions = CapifyEc2.ec2_config[:aws_params][:regions] || [CapifyEc2.ec2_config[:aws_params][:region]]
76
+ end
77
+ regions = @capify_ec2.determine_regions
78
78
  regions.each do |region|
79
79
  define_regions(region, role)
80
80
  end unless regions.nil?
81
-
81
+
82
82
  define_role_roles(role, instances)
83
83
  define_instance_roles(role, instances)
84
84
 
85
85
  end
86
86
 
87
87
  def define_regions(region, role)
88
- instances = CapifyEc2.get_instances_by_region(role[:name], region)
88
+ instances = []
89
+ @roles.each do |role_name, junk|
90
+ region_instances = @capify_ec2.get_instances_by_region(role_name, region)
91
+ region_instances.each {|instance| instances << instance} unless region_instances.nil?
92
+ end
89
93
  task region.to_sym do
90
94
  remove_default_roles
91
95
  instances.each do |instance|
@@ -116,7 +120,7 @@ Capistrano::Configuration.instance(:must_exist).load do
116
120
  options = role[:options]
117
121
  new_options = {}
118
122
  options.each {|key, value| new_options[key] = true if value.to_s == instance.name}
119
- instance.options.each {|option| new_options[option.to_sym] = true}
123
+ instance.option.each { |option| new_options[option.to_sym] = true } rescue false
120
124
 
121
125
  if new_options
122
126
  role role[:name].to_sym, instance.dns_name, new_options
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'fog/aws/models/compute/server'
3
+
4
+ module Fog
5
+ module Compute
6
+ class AWS
7
+ class Server
8
+ def method_missing(method_sym, *arguments, &block)
9
+ tags.each do |key, value|
10
+ tag = key.downcase.gsub(/\W/, '').chomp('s')
11
+ return value if method_sym.to_s == tag
12
+ end if tags
13
+ super
14
+ end
15
+
16
+ def respond_to?(method_sym, include_private = false)
17
+ tags.each do |key, value|
18
+ tag = key.downcase.gsub(/\W/, '').chomp('s')
19
+ return true if method_sym.to_s == tag
20
+ end if tags
21
+ super
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  module Capify
2
2
  module Ec2
3
- VERSION = "1.1.16.pre.2"
3
+ VERSION = "1.2.0.pre.3"
4
4
  end
5
5
  end
data/readme.md CHANGED
@@ -166,8 +166,8 @@ The yml file needs to look something like this:
166
166
  :load_balanced: true
167
167
  :project_tag: "YOUR APP NAME"
168
168
  ```
169
+ aws_access_key_id, aws_secret_access_key, and region are required. Other settings are optional.
169
170
 
170
- The :aws_params are optional.
171
171
  If :load_balanced is set to true, the gem uses pre and post-deploy
172
172
  hooks to deregister the instance, reregister it, and validate its
173
173
  health.
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capify-ec2
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1923831837
4
+ hash: 1923831983
5
5
  prerelease: true
6
6
  segments:
7
7
  - 1
8
- - 1
9
- - 16
10
- - pre
11
8
  - 2
12
- version: 1.1.16.pre.2
9
+ - 0
10
+ - pre
11
+ - 3
12
+ version: 1.2.0.pre.3
13
13
  platform: ruby
14
14
  authors:
15
15
  - Noah Cantor
@@ -18,7 +18,7 @@ autorequire:
18
18
  bindir: bin
19
19
  cert_chain: []
20
20
 
21
- date: 2011-10-11 00:00:00 +01:00
21
+ date: 2011-12-02 00:00:00 +00:00
22
22
  default_executable:
23
23
  dependencies:
24
24
  - !ruby/object:Gem::Dependency
@@ -29,12 +29,12 @@ dependencies:
29
29
  requirements:
30
30
  - - "="
31
31
  - !ruby/object:Gem::Version
32
- hash: 23
32
+ hash: 17
33
33
  segments:
34
34
  - 1
35
- - 0
36
- - 0
37
- version: 1.0.0
35
+ - 1
36
+ - 1
37
+ version: 1.1.1
38
38
  type: :runtime
39
39
  version_requirements: *id001
40
40
  - !ruby/object:Gem::Dependency
@@ -52,6 +52,20 @@ dependencies:
52
52
  version: "1.2"
53
53
  type: :runtime
54
54
  version_requirements: *id002
55
+ - !ruby/object:Gem::Dependency
56
+ name: capistrano
57
+ prerelease: false
58
+ requirement: &id003 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ type: :runtime
68
+ version_requirements: *id003
55
69
  description: Grabs roles from ec2's tags and autogenerates capistrano tasks
56
70
  email:
57
71
  - noah.cantor@forward.co.uk
@@ -71,6 +85,7 @@ files:
71
85
  - capify-ec2.gemspec
72
86
  - lib/capify-ec2.rb
73
87
  - lib/capify-ec2/capistrano.rb
88
+ - lib/capify-ec2/server.rb
74
89
  - lib/capify-ec2/version.rb
75
90
  - readme.md
76
91
  has_rdoc: true