Class: Ufo::Stack::Context
- Inherits:
-
Object
- Object
- Ufo::Stack::Context
show all
- Extended by:
- Memoist
- Includes:
- Helper
- Defined in:
- lib/ufo/stack/context.rb
Instance Method Summary
collapse
Methods included from Helper
#adjust_stack_name, #find_stack, #status
Methods included from Util
#default_cluster, #display_params, #execute, #pretty_time, #task_definition_arns, #user_params
Methods included from AwsService
#cloudformation, #cloudwatchlogs, #ec2, #ecr, #ecs, #elb
Constructor Details
#initialize(options) ⇒ Context
Returns a new instance of Context.
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/ufo/stack/context.rb', line 6
def initialize(options)
@options = options
@task_definition = options[:task_definition]
@service = options[:service]
@cluster = options[:cluster]
@stack_name = options[:stack_name]
@stack = options[:stack]
@new_stack = !@stack
end
|
Instance Method Details
#build_subnet_mappings(allocations) ⇒ Object
204
205
206
207
208
209
210
|
# File 'lib/ufo/stack/context.rb', line 204
def build_subnet_mappings(allocations)
mappings = []
allocations.sort.each_with_index do |allocation_id, i|
mappings << [allocation_id, network[:elb_subnets][i]]
end
mappings
end
|
#build_subnet_mappings!(allocations) ⇒ Object
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/ufo/stack/context.rb', line 189
def build_subnet_mappings!(allocations)
unless allocations.size == network[:elb_subnets].size
puts "ERROR: The allocation_ids must match in length to the subnets.".color(:red)
puts "Please double check that .ufo/settings/network/#{settings[:network_profile]} has the same number of subnets as the eip allocation ids are you specifying."
subnets = network[:elb_subnets]
puts "Conigured subnets: #{subnets.inspect}"
puts "Specified allocation ids: #{allocations.inspect}"
exit 1
end
build_subnet_mappings(allocations)
end
|
#cfn ⇒ Object
250
251
252
|
# File 'lib/ufo/stack/context.rb', line 250
def cfn
Ufo::Setting::Profile.new(:cfn, settings[:cfn_profile]).data
end
|
#container ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/ufo/stack/context.rb', line 65
def container
resp = ecs.describe_task_definition(task_definition: @task_definition)
task_definition = resp.task_definition
container_def = task_definition["container_definitions"].first
requires_compatibilities = task_definition["requires_compatibilities"]
fargate = requires_compatibilities && requires_compatibilities == ["FARGATE"]
network_mode = task_definition["network_mode"]
mappings = container_def["port_mappings"] || []
unless mappings.empty?
port = mappings.first["container_port"]
end
result = {
name: container_def["name"],
fargate: fargate,
network_mode: network_mode, }
result[:port] = port if port
result
end
|
#create_elb? ⇒ Boolean
89
90
91
92
|
# File 'lib/ufo/stack/context.rb', line 89
def create_elb?
create_elb, _ = elb_options
create_elb == "true" end
|
#create_listener_ssl? ⇒ Boolean
if the configuration is set to anything then enable it
61
62
63
|
# File 'lib/ufo/stack/context.rb', line 61
def create_listener_ssl?
cfn[:listener_ssl] && cfn[:listener_ssl][:port]
end
|
#default_elb_options ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/ufo/stack/context.rb', line 122
def default_elb_options
unless @new_stack
create_elb = get_parameter_value(@stack, "CreateElb")
elb_target_group = get_parameter_value(@stack, "ElbTargetGroup")
return [create_elb, elb_target_group]
end
create_elb = container[:name] == "web" ? "true" : "false"
elb_target_group = ""
[create_elb, elb_target_group]
end
|
#default_listener_protocol ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/ufo/stack/context.rb', line 48
def default_listener_protocol
if elb_type == 'network'
cfn[:listener][:port] == 443 ? 'TLS' : 'TCP'
else
cfn[:listener][:port] == 443 ? 'HTTPS' : 'HTTP'
end
end
|
#default_listener_ssl_protocol ⇒ Object
56
57
58
|
# File 'lib/ufo/stack/context.rb', line 56
def default_listener_ssl_protocol
elb_type == 'network' ? 'TLS' : 'HTTPS'
end
|
#default_target_group_protocol ⇒ Object
43
44
45
46
|
# File 'lib/ufo/stack/context.rb', line 43
def default_target_group_protocol
return 'TCP' if elb_type == 'network'
'HTTP'
end
|
#elb_eip_ids ⇒ Object
Returns string, used as CloudFormation parameter.
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/ufo/stack/context.rb', line 176
def elb_eip_ids
return '' if reset_empty_eip_ids?
elb_eip_ids = normalize_elb_eip_ids
return elb_eip_ids.join(',') unless elb_eip_ids.empty?
unless @new_stack
return get_parameter_value(@stack, "ElbEipIds")
end
''
end
|
#elb_options ⇒ Object
If –elb is not set at all and is nil, then it defaults to creating the load balancer if the ecs service has a container name “web”.
–elb ” - will not crete an elb –elb ‘auto’ - creates an elb –elb arn:… - will not create elb and use the existing target group
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/ufo/stack/context.rb', line 101
def elb_options
case @options[:elb]
when "auto", "true", "yes"
create_elb = "true"
elb_target_group = ""
when "false", "0", "no"
create_elb = "false"
elb_target_group = ""
when /^arn:aws:elasticloadbalancing.*targetgroup/
create_elb = "false"
elb_target_group = @options[:elb]
when "", nil
create_elb, elb_target_group = default_elb_options
else
puts "Invalid --elb option provided: #{@options[:elb].inspect}".color(:red)
puts "Exiting."
exit 1
end
[create_elb, elb_target_group]
end
|
#elb_type ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
# File 'lib/ufo/stack/context.rb', line 212
def elb_type
return @options[:elb_type] if @options[:elb_type]
elb_eip_ids = normalize_elb_eip_ids
if !elb_eip_ids.empty?
return "network"
end
if @new_stack return "application"
end
resp = cloudformation.describe_stack_resources(stack_name: @stack_name)
resources = resp.stack_resources
elb_resource = resources.find do |resource|
resource.logical_resource_id == "Elb"
end
return "application" unless elb_resource
resp = elb.describe_load_balancers(load_balancer_arns: [elb_resource.physical_resource_id])
load_balancer = resp.load_balancers.first
load_balancer.type
end
|
#get_parameter_value(stack, key) ⇒ Object
138
139
140
141
142
143
|
# File 'lib/ufo/stack/context.rb', line 138
def get_parameter_value(stack, key)
param = stack.parameters.find do |p|
p.parameter_key == key
end
param.parameter_value if param
end
|
#network ⇒ Object
245
246
247
|
# File 'lib/ufo/stack/context.rb', line 245
def network
Ufo::Setting::Profile.new(:network, settings[:network_profile]).data
end
|
#normalize_elb_eip_ids ⇒ Object
169
170
171
172
173
|
# File 'lib/ufo/stack/context.rb', line 169
def normalize_elb_eip_ids
elb_eip_ids = @options[:elb_eip_ids] || []
elb_eip_ids.uniq!
elb_eip_ids
end
|
#reset_empty_eip_ids? ⇒ Boolean
152
153
154
155
|
# File 'lib/ufo/stack/context.rb', line 152
def reset_empty_eip_ids?
@options[:elb_eip_ids] && @options[:elb_eip_ids].detect { |x| [' ', 'empty'].include?(x) }
end
|
#scheduling_strategy ⇒ Object
145
146
147
148
149
150
|
# File 'lib/ufo/stack/context.rb', line 145
def scheduling_strategy
unless @new_stack
scheduling_strategy = get_parameter_value(@stack, "EcsSchedulingStrategy")
end
scheduling_strategy || 'REPLICA' end
|
#scope ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/ufo/stack/context.rb', line 18
def scope
scope = Ufo::TemplateScope.new(Ufo::DSL::Helper.new, nil)
vars = {
cluster: @cluster,
stack_name: @stack_name, container: container,
create_elb: create_elb?, elb_type: elb_type,
subnet_mappings: subnet_mappings,
create_route53: create_elb? && cfn[:dns] && cfn[:dns][:name],
default_target_group_protocol: default_target_group_protocol,
default_listener_protocol: default_listener_protocol,
default_listener_ssl_protocol: default_listener_ssl_protocol,
create_listener_ssl: create_listener_ssl?,
}
scope.assign_instance_variables(vars)
scope
end
|
#settings ⇒ Object
255
256
257
|
# File 'lib/ufo/stack/context.rb', line 255
def settings
Ufo.settings
end
|
#subnet_mappings ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/ufo/stack/context.rb', line 157
def subnet_mappings
return [] if reset_empty_eip_ids?
elb_eip_ids = normalize_elb_eip_ids
return build_subnet_mappings!(elb_eip_ids) unless elb_eip_ids.empty?
unless @new_stack
elb_eip_ids = get_parameter_value(@stack, "ElbEipIds").split(',')
build_subnet_mappings(elb_eip_ids)
end
end
|