80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
# File 'lib/routes/cloudformation_create.rb', line 80
def opts_validate
Blufin::Tools::os_not_supported([Blufin::Tools::OS_WINDOWS])
if @opts[:clear_cache]
App::AWSProfile::download_s3_ssh_users(false)
end
terminal_width_actual = Blufin::Terminal::get_terminal_width
terminal_required_width = 227
Blufin::Terminal::error("Output for this command \x1B[38;5;240m(#{Blufin::Terminal::format_action(terminal_required_width)}\x1B[38;5;240m-width)\x1B[0m does not fit in Terminal \x1B[38;5;240m(#{Blufin::Terminal::format_action(terminal_width_actual)}\x1B[38;5;240m-width)\x1B[0m", 'Please make your terminal wider and try again.', true) if terminal_width_actual < terminal_required_width
@warnings = []
@lookups = App::AWSReports::get_lookups(@data)
users = App::AWSProfile::get_ssh_users
@lookups[App::Replacer::SSH_USERS] = users if users.any?
@options_default[OPTION_ENVIRONMENT] = %w(dev test staging prod)
@options_default[OPTION_REGION] = App::AWSProfile::get_profile[App::AWSProfile::CLOUDFORMATION]['Defaults']['Regions']
@options_default[OPTION_STACK_NAME] = App::AWSProfile::get_profile[App::AWSProfile::CLOUDFORMATION]['Defaults']['StackName']
@options_default[OPTION_TIMEOUT] = App::AWSProfile::get_profile[App::AWSProfile::CLOUDFORMATION]['Defaults']['Timeout']
@options_default[OPTION_PROJECT] = Blufin::Projects::get_project_names
Blufin::Files::get_dirs_in_dir(App::AWSCloudFormation::get_cloudformation_path).each do |path|
ps = path.split('/')
category = ps[ps.length - 1]
template_folders = Blufin::Files::get_dirs_in_dir(path)
template_folders.each do |path_inner|
ps = path_inner.split('/')
template = ps[ps.length - 1]
template_name = "#{category}/#{template}"
template_path = nil
file_cloudformation = nil
file_ruby = nil
method_before_create = nil
method_after_create = nil
method_before_teardown = nil
method_after_teardown = nil
intro = nil
description = nil
stack_name = nil
projects = nil
environments = nil
regions = nil
timeout = nil
single_serve = false
stack_id = nil
parameters = {}
parameters_no_sort = nil
warnings_count = @warnings.length
raise RuntimeError, "Template name must consist of [service]/[service-description] with exactly one slash, instead got: #{template_name}" if template_name.strip.split('/').length != 2
Blufin::Files::get_files_in_dir(path_inner).each do |file|
filename = Blufin::Files::(file)
template_path = Blufin::Files::(file)
if filename == 'template.yml'
begin
yml_data = YAML.load_file(File.expand_path(file))
rescue Exception => e
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 #{e.message.split('template.yml): ')[1].capitalize}"
next
end
if yml_data.is_a?(Hash)
file_cloudformation = file
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 AWSTemplateFormatVersion: '2010-09-09' is missing." unless yml_data.has_key?('AWSTemplateFormatVersion')
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 This template either has no resources, invalid resources, or something else wrong with the resources." if !yml_data.has_key?('Resources') || yml_data['Resources'].nil? || !yml_data['Resources'].is_a?(Hash) || yml_data['Resources'].length == 0
if yml_data.has_key?(PARAMETERS) && yml_data[PARAMETERS].is_a?(Hash) && yml_data[PARAMETERS].length > 0
yml_data[PARAMETERS].each do |param_name, param_data|
expected = {
'Type' => true,
'Description' => false,
'Default' => false,
'AllowedValues' => false,
'AllowedPattern' => false,
'MinLength' => false,
'MinValue' => false,
'MaxLength' => false,
'MaxValue' => false,
'ConstraintDescription' => false,
'NoEcho' => false,
}
Blufin::Validate::assert_valid_keys(expected, param_data.keys, "#{file} \xe2\x86\x92 #{param_name}")
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 Template has reserved parameter: #{Blufin::Terminal::format_invalid(param_name)}" if [OPTION_STACK_NAME.downcase].concat(RESERVED_WORDS).include?(param_name.downcase)
parameters[param_name] = param_data
if @lookups.has_key?(param_name)
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 Parameter: #{param_name} cannot have default value: '#{param_data[DEFAULT]}' because it is a live look-up list (from AWS)." if param_data.keys.include?(DEFAULT)
end
valid_parameter_types = %w(String Number List<Number> CommaDelimitedList)
valid_parameter_regexes = [/^AWS::[A-Za-z0-9]+::[A-Za-z0-9]+::[A-Za-z0-9]+$/, /^List<AWS::[A-Za-z0-9]+::[A-Za-z0-9]+::[A-Za-z0-9]+>$/]
unless valid_parameter_types.include?(param_data['Type'])
matches_vpr = false
valid_parameter_regexes.each do |vpr|
matches_vpr = true if param_data['Type'] =~ vpr
end
if matches_vpr
constraints = []
constraints << 'AllowedPattern' if param_data.has_key?('AllowedPattern')
constraints << 'MinLength' if param_data.has_key?('MinLength')
constraints << 'MaxLength' if param_data.has_key?('MaxLength')
constraints << 'MinValue' if param_data.has_key?('MinValue')
constraints << 'MaxValue' if param_data.has_key?('MaxValue')
if constraints.any?
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 Invalid parameter constraint(s): #{Blufin::Terminal::format_invalid(constraints.join(','))}. These don't apply to AWS-Specific parameters."
end
else
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 Invalid parameter type: #{Blufin::Terminal::format_invalid(param_data['Type'])}"
end
end
if param_data.has_key?('MinLength') || param_data.has_key?('MaxLength') || param_data.has_key?('AllowedPattern')
unless param_data['Type'] == 'String'
constraints = []
constraints << 'MinLength' if param_data.has_key?('MinLength')
constraints << 'MaxLength' if param_data.has_key?('MaxLength')
constraints << 'AllowedPattern' if param_data.has_key?('AllowedPattern')
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 Invalid parameter constraint(s): #{Blufin::Terminal::format_invalid(constraints.join(','))}. To use these, type must be String."
end
end
if param_data.has_key?('MinValue') || param_data.has_key?('MaxValue')
unless param_data['Type'] == 'Number'
constraints = []
constraints << 'MinValue' if param_data.has_key?('MinValue')
constraints << 'MaxValue' if param_data.has_key?('MaxValue')
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 Invalid parameter constraint(s): #{Blufin::Terminal::format_invalid(constraints.join(','))}. To use these, type must be Number."
end
end
if param_data.has_key?('AllowedValues')
%w(AllowedPattern MinLength MaxLength MinValue MaxValue).each do |invalid_key|
if param_data.has_key?(invalid_key)
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 AllowedValues is set so cannot have: #{Blufin::Terminal::format_invalid(param_data['invalid_key'])}"
end
end
unless param_data['AllowedValues'].is_a?(Array)
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 AllowedValues must be Array, instead got: #{Blufin::Terminal::format_invalid(param_data['AllowedValues'].class)}"
end
end
end
end
if yml_data.has_key?(OPTION_DESCRIPTION)
description = yml_data[OPTION_DESCRIPTION]
else
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 Template is missing description."
end
if yml_data.has_key?('Resources') && yml_data['Resources'].is_a?(Hash)
key_has_dp = {}
yml_data['Resources'].keys { |k| key_has_dp[k] = 0 }
yml_data['Resources'].each do |key, value|
key_has_dp[key] = 1 if value.is_a?(Hash) && value.has_key?('DeletionPolicy') && value['DeletionPolicy'].downcase == 'retain'
end
key_has_dp_sum = 0
key_has_dp.each { |k, v| key_has_dp_sum += v }
if key_has_dp_sum == 0 || key_has_dp_sum == yml_data['Resources'].keys.length
single_serve = key_has_dp_sum > 0
else
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 If DeletionPolicy: Retain is used, it must be set on all resources for template."
end
end
end
elsif filename == 'template.rb'
file_ruby = file
require file
expected_constants = %w()
Template::constants.each do |constant|
constant = constant.to_s
expected_constants.delete(constant) if expected_constants.include?(constant)
intro = Template::INTRO if constant == 'INTRO'
stack_name = Template::STACK_NAME if constant == 'STACK_NAME'
projects = Template::PROJECTS if constant == 'PROJECTS'
environments = Template::ENVIRONMENTS if constant == 'ENVIRONMENTS'
regions = Template::REGIONS if constant == 'REGIONS'
timeout = Template::TIMEOUT if constant == 'TIMEOUT'
stack_id = Template::STACK_ID if constant == 'STACK_ID'
parameters_no_sort = Template::PARAMETERS_NO_SORT if constant == 'PARAMETERS_NO_SORT'
end
if stack_name.nil? || stack_name.strip == ''
stack_name = @options_default[OPTION_STACK_NAME]
end
results = App::Replacer::scan_string(stack_name)
validate_matchers(file_cloudformation, results, template_name, parameters)
if stack_id.is_a?(String) && %w(lambda).include?(stack_id.downcase)
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 #{Blufin::Terminal::format_highlight('STACK_ID')} value is a reserved word: #{Blufin::Terminal::format_invalid(stack_id)}"
end
expected_constants.each { |missing_constant| @warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 The #{Blufin::Terminal::format_highlight('template.rb')}\x1B[38;5;240m file is missing a constant: #{Blufin::Terminal::format_invalid(missing_constant)}" }
Template::methods.each do |method|
if SPECIAL_METHODS.include?(method)
if method == :before_create
method_params = Template::method(:before_create).parameters
if method_params == [[:req, :params]]
method_before_create = Template::method(:before_create)
else
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 Invalid #{Blufin::Terminal::format_highlight(':before_create')}\x1B[38;5;240m parameters: #{Blufin::Terminal::format_invalid(method_params.inspect)}"
end
end
if method == :after_create
method_params = Template::method(:after_create).parameters
if method_params == [[:req, :params], [:req, :output]]
method_after_create = Template::method(:after_create)
else
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 Invalid #{Blufin::Terminal::format_highlight(':after_create')}\x1B[38;5;240m parameters: #{Blufin::Terminal::format_invalid(method_params.inspect)}"
end
end
if method == :before_teardown
method_params = Template::method(:before_teardown).parameters
if method_params == [[:req, :output]]
method_before_teardown = Template::method(:before_teardown)
else
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 Invalid #{Blufin::Terminal::format_highlight(':before_teardown')}\x1B[38;5;240m parameters: #{Blufin::Terminal::format_invalid(method_params.inspect)}"
end
end
if method == :after_teardown
method_params = Template::method(:after_teardown).parameters
if method_params == [[:req, :output]]
method_after_teardown = Template::method(:after_teardown)
else
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 Invalid #{Blufin::Terminal::format_highlight(':after_teardown')}\x1B[38;5;240m parameters: #{Blufin::Terminal::format_invalid(method_params.inspect)}"
end
end
end
end
intro = nil if !intro.nil? && intro.gsub("\n", '').gsub("\t", '').strip == ''
stack_name = nil if !stack_name.nil? && stack_name.gsub("\n", '').gsub("\t", '').strip == ''
description = nil if !description.nil? && description.gsub("\n", '').gsub("\t", '').strip == ''
Object.send(:remove_const, :Template)
end
end
unless parameters_no_sort.nil?
if parameters_no_sort.is_a?(Array)
parameters_no_sort.each do |pns|
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 Non-existent #{Blufin::Terminal::format_highlight('PARAMETERS_NO_SORT')} parameter: #{Blufin::Terminal::format_invalid(pns)}" unless parameters.keys.include?(pns)
end
else
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 Expected constant #{Blufin::Terminal::format_highlight('PARAMETERS_NO_SORT')} to be Array, instead got: #{Blufin::Terminal::format_invalid(parameters_no_sort.class)}"
end
end
unless file_cloudformation.nil?
results = App::Replacer::scan_file(file_cloudformation)
validate_matchers(file_cloudformation, results, template_name, parameters)
end
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 The #{Blufin::Terminal::format_highlight('template.yml')}\x1B[38;5;240m is missing, empty or invalid." if file_cloudformation.nil?
if category == NESTED
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 #{Blufin::Terminal::format_highlight('template.rb')}\x1B[38;5;240m for nested stacks will be ignored, please remove." unless file_ruby.nil?
else
@warnings << "\x1B[38;5;196m#{template_name}\x1B[38;5;240m \xe2\x80\x94 The #{Blufin::Terminal::format_highlight('template.rb')}\x1B[38;5;240m is missing, empty or invalid." if file_ruby.nil?
@templates[category] = {} unless @templates.has_key?(category)
if @warnings.length > warnings_count
@templates[category][template] = {
:name => template,
:broken => 'Broken'
}
else
@templates[category][template] = {
:name => template,
:path => template_path,
:broken => false,
:file_cloudformation => file_cloudformation,
:file_ruby => file_ruby,
:method_before_create => method_before_create,
:method_after_create => method_after_create,
:method_before_teardown => method_before_teardown,
:method_after_teardown => method_after_teardown,
:parameters => parameters,
:parameters_no_sort => parameters_no_sort,
:intro => intro,
:description => description,
:stack_name => stack_name,
:projects => projects,
:environments => environments,
:regions => regions,
:timeout => timeout,
:single_serve => single_serve
}
@templates[category][template][:stack_id] = stack_id unless stack_id.nil?
end
end
end
end
system('clear')
if @warnings.any?
Blufin::Terminal::warning('Some template(s) had issues:', @warnings)
exit if @opts[:test]
end
end
|