Class: Awsborn::LoadBalancer

Inherits:
Object
  • Object
show all
Includes:
AwsConstants
Defined in:
lib/awsborn/load_balancer.rb

Constant Summary collapse

DEFAULT_LISTENERS =
[ { :protocol => :http, :load_balancer_port => 80, :instance_port => 80} ]
DEFAULT_HEALTH_CONFIG =
{
  :healthy_threshold => 10,
  :unhealthy_threshold => 2,
  :target => "TCP:80",
  :timeout => 5,
  :interval => 30
}

Constants included from AwsConstants

AwsConstants::AVAILABILITY_ZONES, AwsConstants::INSTANCE_TYPES, AwsConstants::INSTANCE_TYPES_32_BIT, AwsConstants::INSTANCE_TYPES_64_BIT, AwsConstants::REGIONS, AwsConstants::SYMBOL_CONSTANT_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AwsConstants

#aws_constant, #aws_instance_type_to_symbol, #aws_zone_to_symbol, #endpoint_for_zone_and_service, #symbol_to_aws_instance_type, #symbol_to_aws_zone, #zone_to_aws_region

Constructor Details

#initialize(name, options = {}) ⇒ LoadBalancer

Returns a new instance of LoadBalancer.



16
17
18
19
20
21
22
23
24
25
# File 'lib/awsborn/load_balancer.rb', line 16

def initialize (name, options={})
  @name = name
  @only   = options[:only] || []
  @except = options[:except] || []
  @region = zone_to_aws_region(options[:region])
  @listeners = options[:listeners] || DEFAULT_LISTENERS
  @sticky_cookies = options[:sticky_cookies] || []
  @health_check_config = DEFAULT_HEALTH_CONFIG.merge(options[:health_check] || {})
  @dns_alias = options[:dns_alias]
end

Instance Attribute Details

#dns_aliasObject

Returns the value of attribute dns_alias.



5
6
7
# File 'lib/awsborn/load_balancer.rb', line 5

def dns_alias
  @dns_alias
end

#exceptObject

Returns the value of attribute except.



5
6
7
# File 'lib/awsborn/load_balancer.rb', line 5

def except
  @except
end

#health_check_configObject

Returns the value of attribute health_check_config.



5
6
7
# File 'lib/awsborn/load_balancer.rb', line 5

def health_check_config
  @health_check_config
end

#listenersObject

Returns the value of attribute listeners.



5
6
7
# File 'lib/awsborn/load_balancer.rb', line 5

def listeners
  @listeners
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/awsborn/load_balancer.rb', line 5

def name
  @name
end

#onlyObject

Returns the value of attribute only.



5
6
7
# File 'lib/awsborn/load_balancer.rb', line 5

def only
  @only
end

#regionObject

Returns the value of attribute region.



5
6
7
# File 'lib/awsborn/load_balancer.rb', line 5

def region
  @region
end

#sticky_cookiesObject

Returns the value of attribute sticky_cookies.



5
6
7
# File 'lib/awsborn/load_balancer.rb', line 5

def sticky_cookies
  @sticky_cookies
end

Instance Method Details

#aws_dns_nameObject



39
40
41
# File 'lib/awsborn/load_balancer.rb', line 39

def aws_dns_name
  elb.dns_name(@name)
end

#canonical_hosted_zone_name_idObject



43
44
45
# File 'lib/awsborn/load_balancer.rb', line 43

def canonical_hosted_zone_name_id
  elb.canonical_hosted_zone_name_id(@name)
end

#configure_dnsObject



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/awsborn/load_balancer.rb', line 125

def configure_dns
  route53.create_zone @dns_alias unless route53.zone_exists?(@dns_alias)
  case route53.alias_target(@dns_alias)
  when aws_dns_name
    # It is already good
  when nil
    route53.add_alias_record(:alias => @dns_alias, :lb_fqdn => aws_dns_name, :lb_zone => canonical_hosted_zone_name_id)
  else
    route53.remove_alias_records(@dns_alias)
    route53.add_alias_record(:alias => @dns_alias, :lb_fqdn => aws_dns_name, :lb_zone => canonical_hosted_zone_name_id)
  end
end

#descriptionObject



74
75
76
# File 'lib/awsborn/load_balancer.rb', line 74

def description
  elb.describe_load_balancer(@name)
end

#dns_infoObject



138
139
140
141
142
# File 'lib/awsborn/load_balancer.rb', line 138

def dns_info
  if dns_alias
    route53.zone_for(dns_alias)
  end
end

#ec2Object



35
36
37
# File 'lib/awsborn/load_balancer.rb', line 35

def ec2
  @ec2 ||= Ec2.new(@region)
end

#elbObject



27
28
29
# File 'lib/awsborn/load_balancer.rb', line 27

def elb
  @elb ||= Elb.new(@region)
end

#health_statusObject



86
87
88
# File 'lib/awsborn/load_balancer.rb', line 86

def health_status
  elb.health_status(@name)
end

#instancesObject



47
48
49
# File 'lib/awsborn/load_balancer.rb', line 47

def instances
  elb.instances(@name)
end

#instances=(new_instances) ⇒ Object



51
52
53
54
55
# File 'lib/awsborn/load_balancer.rb', line 51

def instances= (new_instances)
  previous_instances = self.instances
  register_instances(new_instances - previous_instances)
  deregister_instances(previous_instances - new_instances)
end

#instances_dns_namesObject



57
58
59
60
61
62
# File 'lib/awsborn/load_balancer.rb', line 57

def instances_dns_names
  instances.map do |instance_id|
    ec2.instance_id = instance_id
    (ec2.describe_instance || {})[:dns_name]
  end.compact
end

#launchObject



82
83
84
# File 'lib/awsborn/load_balancer.rb', line 82

def launch
  elb.create_load_balancer(@name)
end

#launch_or_update(running_servers) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/awsborn/load_balancer.rb', line 116

def launch_or_update (running_servers)
  launch unless running?

  set_instances_to_selected(running_servers)
  update_settings

  configure_dns if @dns_alias
end

#route53Object



31
32
33
# File 'lib/awsborn/load_balancer.rb', line 31

def route53
  @route53 ||= Route53.new
end

#running?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/awsborn/load_balancer.rb', line 78

def running?
  elb.running?(@name)
end

#update_health_configObject



112
113
114
# File 'lib/awsborn/load_balancer.rb', line 112

def update_health_config
  elb.configure_health_check(@name, @health_check_config)
end

#update_listenersObject



90
91
92
# File 'lib/awsborn/load_balancer.rb', line 90

def update_listeners
  elb.set_load_balancer_listeners(@name, @listeners)
end

#update_sticky_cookiesObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/awsborn/load_balancer.rb', line 94

def update_sticky_cookies
  elb.remove_all_cookie_policies(@name)
  @sticky_cookies.each do |sc|
    raise "Option :ports is missing for #{sc.inspect}" if sc[:ports].nil?

    case sc[:policy]
    when :disabled
      set_disabled_cookie_policy(sc[:ports])
    when :lb_generated
      set_lb_cookie_policy(sc[:ports], sc[:expiration_period])
    when :app_generated
      set_app_cookie_policy(sc[:ports], sc[:cookie_name])
    else
      raise "unknown policy #{sc.inspect}. Accepted policies => :disabled, :lb_generated, :app_generated"
    end
  end
end

#zonesObject



64
65
66
# File 'lib/awsborn/load_balancer.rb', line 64

def zones
  elb.zones(@name)
end

#zones=(new_zones) ⇒ Object



68
69
70
71
72
# File 'lib/awsborn/load_balancer.rb', line 68

def zones= (new_zones)
  previous_zones = self.zones
  enable_zones(new_zones - previous_zones)
  disable_zones(previous_zones - new_zones)
end