Class: Bosh::AwsCliPlugin::ELB

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh_cli_plugin_aws/elb.rb

Defined Under Namespace

Classes: BadCertificateError

Instance Method Summary collapse

Constructor Details

#initialize(credentials) ⇒ ELB

Returns a new instance of ELB.


6
7
8
# File 'lib/bosh_cli_plugin_aws/elb.rb', line 6

def initialize(credentials)
  @aws_provider = AwsProvider.new(credentials)
end

Instance Method Details

#create(name, vpc, settings, certs) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bosh_cli_plugin_aws/elb.rb', line 10

def create(name, vpc, settings, certs)
  subnet_names = settings['subnets']
  subnet_ids = vpc.subnets.select { |k, v| subnet_names.include?(k) }.values
  security_group_name = settings['security_group']
  security_group_id = vpc.security_group_by_name(security_group_name).id
  options = {
    :listeners => [{
                     port: 80,
                     protocol: :http,
                     instance_port: 80,
                     instance_protocol: :http,
                   }],
    :subnets => subnet_ids,
    :security_groups => [security_group_id]
  }

  if settings['https']
    domain = settings['domain']
    cert_name = settings['ssl_cert']
    cert = certs[cert_name]
    dns_record = settings['dns_record']

    certificate = Bosh::Ssl::Certificate.new(cert['private_key_path'],
                                             cert['certificate_path'],
                                             "#{dns_record}.#{domain}",
                                             cert['certificate_chain_path']
    ).load_or_create

    uploaded_cert = upload_certificate(cert_name, certificate)

    options[:listeners] << {
      :port => 443,
      :protocol => :https,
      :instance_port => 80,
      :instance_protocol => :http,
      # passing through 'ssl_certificate_id' is undocumented, but we're
      # working around a bug filed here: https://github.com/aws/aws-sdk-ruby/issues/216
      :ssl_certificate_id => uploaded_cert.arn
    }
  end

  Bosh::Common.retryable(tries: 15, on: AWS::ELB::Errors::CertificateNotFound) do
    aws_elb.load_balancers.create(name, options).tap do |new_elb|
      new_elb.configure_health_check({
                                       :healthy_threshold => 5,
                                       :unhealthy_threshold => 2,
                                       :interval => 5,
                                       :timeout => 2,
                                       :target => 'TCP:80'
                                     })
    end
  end
end

#delete_elbsObject


72
73
74
75
# File 'lib/bosh_cli_plugin_aws/elb.rb', line 72

def delete_elbs
  aws_elb.load_balancers.each(&:delete)
  delete_server_certificates
end

#delete_server_certificatesObject


77
78
79
80
81
82
# File 'lib/bosh_cli_plugin_aws/elb.rb', line 77

def delete_server_certificates
  Bosh::Common.retryable(tries: 5, sleep: 2) do
    aws_iam.server_certificates.each(&:delete)
    aws_iam.server_certificates.to_a.empty?
  end
end

#find_by_name(name) ⇒ Object


84
85
86
# File 'lib/bosh_cli_plugin_aws/elb.rb', line 84

def find_by_name(name)
  aws_elb.load_balancers.find { |lb| lb.name == name }
end

#namesObject


64
65
66
# File 'lib/bosh_cli_plugin_aws/elb.rb', line 64

def names
  aws_elb.load_balancers.map(&:name)
end

#server_certificate_namesObject


68
69
70
# File 'lib/bosh_cli_plugin_aws/elb.rb', line 68

def server_certificate_names
  aws_iam.server_certificates.map(&:name)
end