Sha256: d838a64597604e315d45f3096d180767f1a3609c880722d50ebd4e5bf27ccdcb

Contents?: true

Size: 1.74 KB

Versions: 20

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true
require 'aws-sdk'
require 'hako'

module Hako
  module Schedulers
    class EcsElb
      def initialize(app_id, elb, elb_config)
        @app_id = app_id
        @elb = elb
        @elb_config = elb_config
      end

      def describe_load_balancer
        @elb.describe_load_balancers(load_balancer_names: [name]).load_balancer_descriptions[0]
      end

      def find_or_create_load_balancer(front_port)
        if @elb_config
          unless exist?
            listeners = @elb_config.fetch('listeners').map do |l|
              {
                protocol: l.fetch('protocol'),
                load_balancer_port: l.fetch('load_balancer_port'),
                instance_port: front_port,
                ssl_certificate_id: l.fetch('ssl_certificate_id', nil),
              }
            end
            lb = @elb.create_load_balancer(
              load_balancer_name: name,
              listeners: listeners,
              subnets: @elb_config.fetch('subnets'),
              security_groups: @elb_config.fetch('security_groups'),
              tags: @elb_config.fetch('tags', {}).map { |k, v| { key: k, value: v.to_s } },
            )
            Hako.logger.info "Created ELB #{lb.dns_name} with instance_port=#{front_port}"
          end
          name
        end
      end

      def destroy
        if exist?
          @elb.delete_load_balancer(load_balancer_name: name)
          Hako.logger.info "Deleted ELB #{name}"
        else
          Hako.logger.info "ELB #{name} doesn't exist"
        end
      end

      def exist?
        describe_load_balancer
        true
      rescue Aws::ElasticLoadBalancing::Errors::LoadBalancerNotFound
        false
      end

      def name
        "hako-#{@app_id}"
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
hako-0.13.1 lib/hako/schedulers/ecs_elb.rb
hako-0.13.0 lib/hako/schedulers/ecs_elb.rb
hako-0.12.0 lib/hako/schedulers/ecs_elb.rb
hako-0.11.2 lib/hako/schedulers/ecs_elb.rb
hako-0.11.1 lib/hako/schedulers/ecs_elb.rb
hako-0.11.0 lib/hako/schedulers/ecs_elb.rb
hako-0.10.0 lib/hako/schedulers/ecs_elb.rb
hako-0.9.4 lib/hako/schedulers/ecs_elb.rb
hako-0.9.3 lib/hako/schedulers/ecs_elb.rb
hako-0.9.2 lib/hako/schedulers/ecs_elb.rb
hako-0.9.1 lib/hako/schedulers/ecs_elb.rb
hako-0.9.0 lib/hako/schedulers/ecs_elb.rb
hako-0.8.5 lib/hako/schedulers/ecs_elb.rb
hako-0.8.4 lib/hako/schedulers/ecs_elb.rb
hako-0.8.3 lib/hako/schedulers/ecs_elb.rb
hako-0.8.2 lib/hako/schedulers/ecs_elb.rb
hako-0.8.1 lib/hako/schedulers/ecs_elb.rb
hako-0.8.0 lib/hako/schedulers/ecs_elb.rb
hako-0.7.4 lib/hako/schedulers/ecs_elb.rb
hako-0.7.3 lib/hako/schedulers/ecs_elb.rb