Class: Balancer::Create

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
AwsService, SecurityGroup
Defined in:
lib/balancer/create.rb

Instance Method Summary collapse

Methods included from SecurityGroup

#authorize_elb_port, #aws_cli_command, #create_security_group, #destroy_security_group, #find_security_group, #option_transformer, #param, #pretty_display, #sg_vpc_id

Methods included from AwsService

#ec2, #elb

Constructor Details

#initialize(options) ⇒ Create

Returns a new instance of Create.



9
10
11
12
# File 'lib/balancer/create.rb', line 9

def initialize(options)
  @options = options
  @name = options[:name]
end

Instance Method Details

#add_tags(arn) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/balancer/create.rb', line 108

def add_tags(arn)
  params = {
    resource_arns: [arn],
    tags: [{ key: "balancer", value: @name }]
  }
  aws_cli_command("aws elbv2 add-tags", params)
  elb.add_tags(params)
end

#create_elbObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/balancer/create.rb', line 41

def create_elb
  puts "Creating load balancer with params:"
  params = param.create_load_balancer
  params[:security_groups] ||= []
  params[:security_groups] += [@security_group_id]
  params[:security_groups] = params[:security_groups].uniq
  pretty_display(params)
  aws_cli_command("aws elbv2 create-load-balancer", params)
  return if @options[:noop]

  begin
    resp = elb.create_load_balancer(params)
  rescue Exception => e
    puts "ERROR: #{e.class}: #{e.message}".colorize(:red)
    exit 1
  end

  elb = resp.load_balancers.first
  puts "Load balancer created: #{elb.load_balancer_arn}"
  @load_balancer_arn = elb.load_balancer_arn # used later
  puts
end

#create_listenerObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/balancer/create.rb', line 83

def create_listener
  puts "Creating listener with params:"
  params = param.create_listener
  params.merge!(
    load_balancer_arn: @load_balancer_arn,
    default_actions: [{type: "forward", target_group_arn: @target_group_arn}]
  )
  pretty_display(params)
  aws_cli_command("aws elbv2 create-listener", params)

  resp = run_with_error_handling do
    elb.create_listener(params)
  end
  listener = resp.listeners.first
  puts "Listener created: #{listener.listener_arn}"
  puts
end

#create_target_groupObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/balancer/create.rb', line 64

def create_target_group
  puts "Creating target group with params:"
  params = param.create_target_group
  pretty_display(params)
  aws_cli_command("aws elbv2 create-target-group", params)

  begin
    resp = elb.create_target_group(params)
  rescue Exception => e
    puts "ERROR: #{e.class}: #{e.message}".colorize(:red)
    exit 1
  end
  target_group = resp.target_groups.first
  puts "Target group created: #{target_group.target_group_arn}"
  @target_group_arn = target_group.target_group_arn # used later
  add_tags(@target_group_arn)
  puts
end

#elb_exists?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
# File 'lib/balancer/create.rb', line 32

def elb_exists?
  begin
    resp = elb.describe_load_balancers(names: [@name])
    true
  rescue Aws::ElasticLoadBalancingV2::Errors::LoadBalancerNotFound
    false
  end
end

#runObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/balancer/create.rb', line 15

def run
  if ENV['TEST'] # ghetto way to for sanity cli specs
    puts "Creating load balancer"
    return
  end

  if elb_exists?
    puts "Load balancer #{@name} already exists"
    return
  end

  @security_group_id = create_security_group
  create_elb
  create_target_group
  create_listener
end

#run_with_error_handlingObject



101
102
103
104
105
106
# File 'lib/balancer/create.rb', line 101

def run_with_error_handling
  yield
rescue Exception => e
  puts "ERROR: #{e.class}: #{e.message}".colorize(:red)
  exit 1
end