cap-elb 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -39,16 +39,22 @@ If you wish, you can also set other AWS specfic parameters:
39
39
 
40
40
  set :aws_params, :region => 'us-east-1'
41
41
 
42
- In order to define your instance groups, you must specify the security group name, the roles and params:
43
- Next you will set up your instance sets associated with a named load balancer instance in your AWS account.
44
- You will call out the load balancer name (e.g. 'lb_webserver'), the capistrano role associated with that load balancer (.e.g. 'web'),
42
+ Setting region is required if you are not using default region 'us-east-1'.
43
+ If your ELB is not in us-east-1, and you do not specify region parameter as above,
44
+ you will get an error indicating that no ELB could be found with that name in that region.
45
+
46
+ In order to define your instance sets associated with your load balancer based deploys, you must specify the load balancer name, the associated roles for that load balancer and any optional params:
47
+ For example, in deploy.rb, you would enter the load balancer name (e.g. 'lb_webserver'), the capistrano role associated with that load balancer (.e.g. 'web'),
45
48
  and any optional params.
46
49
 
47
50
  loadbalancer :lb_webserver, :web
48
51
  loadbalancer :lb_appserver, :app
49
52
  loadbalancer :lb_dbserver, :db, :port => 22000
50
53
 
51
- There are two special parameters you can add, :require and :exclude.
54
+ There are two special optional parameters you can add, :require and :exclude. These allow you to exclude instances associated with your named load balancer from the deploy,
55
+ if they meet or fail to meet your :require/:exclude specifications.
56
+
57
+ The :require and :exclude parameters work on Amazon EC2 instance metadata.
52
58
 
53
59
  AWS instances have top level metadata and user defined tag data, and this data can be used by your loadbalancer rule
54
60
  to include or exclude certain instances from the instance set.
@@ -1,5 +1,5 @@
1
1
  module Cap
2
2
  module Elb
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
data/lib/cap-elb.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'right_aws'
2
- require "cap-elb/version"
2
+ require 'aws-sdk' # need for ELB instance getter
3
+ require 'cap-elb/version'
3
4
 
4
5
  unless Capistrano::Configuration.respond_to?(:instance)
5
6
  abort "cap-elb requires Capistrano 2"
@@ -8,32 +9,111 @@ end
8
9
  module Capistrano
9
10
  class Configuration
10
11
  module LoadBalancers
11
- # Associate a group of EC2 instances behind a load balancer with a role. In order to use this, you
12
- # must use the Load Balancer feature in Amazon EC2 to group your servers
13
- # by role.
14
- #
15
- # First, specify the load balancer name, then the roles and params:
16
- #
17
- # group :webserver, :web
18
- # group :app_myappname, :app
19
- # group "MySQL Servers", :db, :port => 22000
12
+ # Associate a group of EC2 instances behind a load balancer with a role to be used for Capistrano tasks. In order to use this, you
13
+ # must use the Load Balancer feature in Amazon EC2 to group your servers
14
+ # by role.
15
+ #
16
+ # In order to use the loadbalancer extension in your capistrano scripts.
17
+ # you will need to install the `cap-elb` plugin, then krequire it in your `deploy.rb`:
18
+
19
+ # require 'cap-elb'
20
+
21
+ # If you have already been doing capistrano deploys to your AWS instances, you probably already have your
22
+ # AWD credentials configured. Either add your credentials to your ~/.caprc :
23
+
24
+ # set :aws_access_key_id, 'YOUR_AWS_ACCESS_KEY_ID'
25
+ # set :aws_secret_access_key, 'YOUR_AWS_SECRET_ACCESS_KEY'
26
+
27
+ # or, directly in your deploy file:
28
+
29
+ # set :aws_access_key_id, 'YOUR_AWS_ACCESS_KEY_ID'
30
+ # set :aws_secret_access_key, 'YOUR_AWS_SECRET_ACCESS_KEY'
31
+
32
+ # If you wish, you can also set other AWS specfic parameters:
33
+
34
+ # set :aws_params, :region => 'us-east-1'
35
+
36
+ # In order to define your instance groups, you must specify the security group name, the roles and params.
37
+ # Next you will set up your instance sets associated with a named load balancer instance in your AWS account.
38
+ # You will call out the load balancer name (e.g. 'lb_webserver'), the capistrano role associated with that load balancer (.e.g. 'web'),
39
+ # and any optional params.
40
+
41
+ # loadbalancer :lb_webserver, :web
42
+ # loadbalancer :lb_appserver, :app
43
+ # loadbalancer :lb_dbserver, :db, :port => 22000
44
+
45
+ # There are two special parameters you can add, :require and :exclude.
46
+
47
+ # AWS instances have top level metadata and user defined tag data, and this data can be used by your loadbalancer rule
48
+ # to include or exclude certain instances from the instance set.
49
+
50
+ # Take the :require keyword; Lets say we only want to deploy to AWS instances which are in the 'running' state. To do that:
51
+
52
+ # loadbalancer :lb_appserver, :app, :require => { :aws_state => "running" }
53
+
54
+ # The server set defined here for role :app are all instances in the loadbalancer 'lb_appserver' with aws_state set to 'running'.
55
+
56
+ # Perhaps you have added tags to your instances, if so, you might want to deploy to only the instances meeting a specific tag value:
57
+
58
+ # loadbalancer :lb_appserver, :app, :require => { :aws_state => "running", :tags => {'fleet_color' => "green", 'tier' => 'free'} }
59
+
60
+ # The server set defined here for role :app are all instances in the loadbalancer 'lb_appserver' with aws_state set to 'running',
61
+ # and that have the named 2 tags set, with exactly those values for each. There can be other tags in the instance, but the named tags in the rule must be present
62
+ # for the given instance to make it into the server set.
63
+
64
+ # Now consider the :exclude keyword; Lets say we do not want to deploy to AWS instances which are 'micro' sized. To do that:
65
+
66
+ # loadbalancer :lb_appserver, :app, :exclude => { :aws_instance_type => "t1.micro" }
67
+
68
+ # You can exclude instances that have certain tags:
69
+
70
+ # loadbalancer :lb_appserver, :app, :exclude => { :aws_instance_type => "t1.micro", :tags => {'state' => 'dontdeploy' } }
71
+
72
+ # When your capistrono script is complete, you can deploy to all instances within the ELB that meet your criteria with:
73
+
74
+ # % cap deploy
75
+
76
+ # Here's an example of a task that does a quick list of the instance ids (if any) within the load balancer associated with the 'app' role
77
+ # that meets the criteria you laid out in the loadbalancer definition line,
78
+ # add this to your cap deploy file:
79
+
80
+ # # run with cap ec2:list
81
+ # namespace :ec2 do
82
+ # desc "list instances"
83
+ # task :list, :roles => :app do
84
+ # run "hostname"
85
+ # end
86
+ # end
87
+
88
+ # This will give you the list of hosts behind the load balancer that meet the criteria.
89
+ # % cap ec2:list
90
+
20
91
  def loadbalancer (named_load_balancer, *args)
92
+
93
+ aws_config_setup
94
+
21
95
  require_arglist = args[1][:require] rescue {}
22
96
  exclude_arglist = args[1][:exclude] rescue {}
97
+ named_region = fetch(:aws_params)[:region] rescue 'us-east-1'
23
98
 
24
99
  # list of all the instances assoc'ed with this account
25
100
  @ec2_api ||= RightAws::Ec2.new(fetch(:aws_access_key_id), fetch(:aws_secret_access_key), fetch(:aws_params, {}))
26
101
 
27
- # fetch a raw list all the load balancers
28
- @elb_api ||= RightAws::ElbInterface.new(fetch(:aws_access_key_id), fetch(:aws_secret_access_key))
102
+ # fetch a raw list all the load balancers, this is best done in Amaazons' aws-sdk, as it allows region specification for LBs.
103
+ # strangely, RightScale gem doesnt seem to support this.
104
+ # i filed a bug with RightScale: https://github.com/rightscale/right_aws/issues/95
105
+ @elb_api = AWS::ELB.new
29
106
 
30
107
  # only get the named load balancer
31
- named_elb_instance = @elb_api.describe_load_balancers.delete_if{ |instance| instance[:load_balancer_name] != named_load_balancer.to_s }
108
+ named_elb = @elb_api.load_balancers[named_load_balancer]
32
109
 
33
110
  # must exit if no load balancer on record for this account by given name in cap config file
34
- raise Exception, "No load balancer named: #{named_load_balancer.to_s} for aws account with this access key: #{:aws_access_key_id}" if named_elb_instance.empty?
111
+ raise Exception, "No load balancer found named: #{named_load_balancer.to_s} for aws account with this access key: #{fetch(:aws_access_key_id)} in this region: #{named_region}" if named_elb.nil?
112
+ # probe for a count of the instances, if this raises Exception, load balancer can't be found
113
+ named_elb.instances.count rescue raise Exception, "No load balancer found named: #{named_load_balancer.to_s} for aws account with this access key: #{fetch(:aws_access_key_id)} in this region: #{named_region}"
114
+ raise Exception, "No instances within this load balancer: #{named_load_balancer.to_s} for aws account with this access key: #{fetch(:aws_access_key_id)} in this region: #{named_region}" if named_elb.instances.count == 0
35
115
 
36
- elb_ec2_instances = named_elb_instance[0][:instances] rescue {}
116
+ elb_ec2_instances = named_elb.instances.map { |i| i.id } # list of instance ids
37
117
 
38
118
  # get the full instance list for account, this is necessary to subsquently fish out the :dns_name for the instances that survive our reduction steps
39
119
  account_instance_list = @ec2_api.describe_instances
@@ -55,6 +135,27 @@ module Capistrano
55
135
 
56
136
  private
57
137
 
138
+ # AWS SDK setup
139
+ def aws_config_setup()
140
+
141
+ # derive AWS canonical endpoints from given region
142
+ # set :ec2_endpoint , 'ec2.us-west-1.amazonaws.com'
143
+ # set :elb_endpoint ,'us-west-1.elasticloadbalancing.amazonaws.com'
144
+
145
+ named_region = fetch(:aws_params)[:region] rescue 'us-east-1'
146
+
147
+ ec2_endpoint = "ec2.#{named_region}.amazonaws.com"
148
+ elb_endpoint = "#{named_region}.elasticloadbalancing.amazonaws.com"
149
+
150
+ # aws configuration, required separately
151
+ AWS.config({
152
+ :access_key_id => fetch(:aws_access_key_id),
153
+ :secret_access_key => fetch(:aws_secret_access_key),
154
+ :ec2_endpoint => ec2_endpoint,
155
+ :elb_endpoint => elb_endpoint,
156
+ })
157
+ end
158
+
58
159
  def any_args_within_instance(instance, exclude_arglist)
59
160
  exargs = exclude_arglist.clone # must copy since delete transcends scope; if we don't copy, subsequent 'map'ped enum arglists would be side-effected
60
161
  tag_exclude_state = nil # default assumption
@@ -85,7 +186,7 @@ module Capistrano
85
186
  end
86
187
 
87
188
  # stub for future extensions
88
- module Cap
89
- module Elb
90
- end
91
- end
189
+ # module Cap
190
+ # module Elb
191
+ # end
192
+ # end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cap-elb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-28 00:00:00.000000000Z
12
+ date: 2011-11-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: right_aws
16
- requirement: &2156273780 !ruby/object:Gem::Requirement
16
+ requirement: &2156644440 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - =
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2156273780
24
+ version_requirements: *2156644440
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2156273160 !ruby/object:Gem::Requirement
27
+ requirement: &2156643680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '2.6'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2156273160
35
+ version_requirements: *2156643680
36
36
  description: Capistrano can perform tasks on the EC2 instances managed by named Amazon
37
37
  ELB instances. Various filters are qvailable to allow EC2 instance top level metadata
38
38
  and tags to determine whether the task should be applied on the given instance.