Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #238 from rtyler/elb-rebase
Browse files Browse the repository at this point in the history
Elb rebase
  • Loading branch information
R. Tyler Croy committed Jun 2, 2014
2 parents 138e135 + 2d6962a commit f8525a2
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@ Vagrant.configure("2") do |config|
end
```

### Elastic Load Balancers

You can automatically attach an instance to an ELB during boot and detach on destroy.

```ruby
Vagrant.configure("2") do |config|
# ... other stuff

config.vm.provider "aws" do |aws|
aws.elb = "production-web"
end
end
```

## Development

To work on the `vagrant-aws` plugin, clone this repository out, and use
Expand Down
13 changes: 6 additions & 7 deletions lib/vagrant-aws/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def self.action_destroy
end
end
b2.use ConnectAWS
b2.use ElbDeregisterInstance
b2.use TerminateInstance
b2.use ProvisionerCleanup if defined?(ProvisionerCleanup)
else
Expand Down Expand Up @@ -113,13 +114,9 @@ def self.action_ssh_run
end
end

def self.action_prepare_boot
Vagrant::Action::Builder.new.tap do |b|
b.use Provision
b.use SyncFolders
b.use WarnNetworks
end
end
def self.action_prepare_boot Vagrant::Action::Builder.new.tap do |b|
b.use Provision b.use SyncFolders b.use WarnNetworks b.use
ElbRegisterInstance end end

# This action is called to bring the box up from nothing.
def self.action_up
Expand Down Expand Up @@ -185,6 +182,8 @@ def self.action_reload
autoload :TimedProvision, action_root.join("timed_provision") # some plugins now expect this action to exist
autoload :WaitForState, action_root.join("wait_for_state")
autoload :WarnNetworks, action_root.join("warn_networks")
autoload :ElbRegisterInstance, action_root.join("elb_register_instance")
autoload :ElbDeregisterInstance, action_root.join("elb_deregister_instance")
end
end
end
1 change: 1 addition & 0 deletions lib/vagrant-aws/action/connect_aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def call(env)

@logger.info("Connecting to AWS...")
env[:aws_compute] = Fog::Compute.new(fog_config)
env[:aws_elb] = Fog::AWS::ELB.new(fog_config.except(:provider))

@app.call(env)
end
Expand Down
24 changes: 24 additions & 0 deletions lib/vagrant-aws/action/elb_deregister_instance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'vagrant-aws/util/elb'

module VagrantPlugins
module AWS
module Action
# This registers instance in ELB
class ElbDeregisterInstance
include ElasticLoadBalancer

def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant_aws::action::elb_deregister_instance")
end

def call(env)
if elb_name = env[:machine].provider_config.elb
deregister_instance env, elb_name, env[:machine].id
end
@app.call(env)
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/vagrant-aws/action/elb_register_instance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'vagrant-aws/util/elb'

module VagrantPlugins
module AWS
module Action
# This registers instance in ELB
class ElbRegisterInstance
include ElasticLoadBalancer

def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant_aws::action::elb_register_instance")
end

def call(env)
@app.call(env)
if elb_name = env[:machine].provider_config.elb
register_instance env, elb_name, env[:machine].id
end
end
end
end
end
end
10 changes: 10 additions & 0 deletions lib/vagrant-aws/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ class Config < Vagrant.plugin("2", :config)
# @return [Boolean]
attr_accessor :associate_public_ip

# The name of ELB, which an instance should be
# attached to
#
# @return [String]
attr_accessor :elb

def initialize(region_specific=false)
@access_key_id = UNSET_VALUE
@ami = UNSET_VALUE
Expand All @@ -164,6 +170,7 @@ def initialize(region_specific=false)
@monitoring = UNSET_VALUE
@ebs_optimized = UNSET_VALUE
@associate_public_ip = UNSET_VALUE
@elb = UNSET_VALUE

# Internal state (prefix with __ so they aren't automatically
# merged)
Expand Down Expand Up @@ -305,6 +312,9 @@ def finalize!
# default false
@associate_public_ip = false if @associate_public_ip == UNSET_VALUE

# Don't attach instance to any ELB by default
@elb = nil if @elb == UNSET_VALUE

# Compile our region specific configurations only within
# NON-REGION-SPECIFIC configurations.
if !@__region_specific
Expand Down
3 changes: 3 additions & 0 deletions lib/vagrant-aws/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class RsyncError < VagrantAWSError

class MkdirError < VagrantAWSError
error_key(:mkdir_error)

class ElbDoesNotExistError < VagrantAWSError
error_key("elb_does_not_exist")
end
end
end
Expand Down
56 changes: 56 additions & 0 deletions lib/vagrant-aws/util/elb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module VagrantPlugins
module AWS
module ElasticLoadBalancer

def register_instance(env, elb_name, instance_id)
env[:ui].info I18n.t("vagrant_aws.elb.registering", instance_id: instance_id, elb_name: elb_name), :new_line => false
elb = get_load_balancer(env[:aws_elb], elb_name)
unless elb.instances.include? instance_id
elb.register_instances([instance_id])
env[:ui].info I18n.t("vagrant_aws.elb.ok"), :prefix => false
adjust_availability_zones env, elb
else
env[:ui].info I18n.t("vagrant_aws.elb.skipped"), :prefix => false
end
end

def deregister_instance(env, elb_name, instance_id)
env[:ui].info I18n.t("vagrant_aws.elb.deregistering", instance_id: instance_id, elb_name: elb_name), :new_line => false
elb = get_load_balancer(env[:aws_elb], elb_name)
if elb.instances.include? instance_id
elb.deregister_instances([instance_id])
env[:ui].info I18n.t("vagrant_aws.elb.ok"), :prefix => false
adjust_availability_zones env, elb
else
env[:ui].info I18n.t("vagrant_aws.elb.skipped"), :prefix => false
end
end

def adjust_availability_zones(env, elb)
env[:ui].info I18n.t("vagrant_aws.elb.adjusting", elb_name: elb.id), :new_line => false

instances = env[:aws_compute].servers.all("instance-id" => elb.instances)

azs = if instances.empty?
["#{env[:machine].provider_config.region}a"]
else
instances.map(&:availability_zone).uniq
end

az_to_disable = elb.availability_zones - azs
az_to_enable = azs - elb.availability_zones

elb.enable_availability_zones az_to_enable unless az_to_enable.empty?
elb.disable_availability_zones az_to_disable unless az_to_disable.empty?

env[:ui].info I18n.t("vagrant_aws.elb.ok"), :prefix => false
end

private

def get_load_balancer(aws, name)
aws.load_balancers.find { |lb| lb.id == name } or raise Errors::ElbDoesNotExistError
end
end
end
end
14 changes: 14 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ en:
vagrant_aws:
already_status: |-
The machine is already %{status}.
elb:
adjusting: |-
Adjusting availability zones of ELB %{elb_name}...
registering: |-
Registering %{instance_id} at ELB %{elb_name}...
deregistering: |-
Deregistering %{instance_id} from ELB %{elb_name}...
ok: |-
ok
skipped: |-
skipped
launching_instance: |-
Launching an instance with the following settings...
launch_no_keypair: |-
Expand Down Expand Up @@ -86,6 +98,8 @@ en:
Host path: %{hostpath}
Error: %{err}
elb_does_not_exist: |-
ELB configured for the instance does not exist
states:
short_not_created: |-
Expand Down

0 comments on commit f8525a2

Please sign in to comment.