Class: Lono::Cfn::Base
Constant Summary
collapse
- @@generate_all =
Use class variable to cache this only runs once across all classes. base.rb, diff.rb, preview.rb
nil
Instance Method Summary
collapse
Methods included from Util
#are_you_sure?, #switch_current
Methods included from Suffix
#allow_suffix?, #append_suffix, #random_suffix, #remove_suffix, #stack_name_suffix
#template_param_convention
#bundler_groups, #find_blueprint_root, #require_bundle_gems, #set_blueprint_root
#cfn, #ec2, #iam, #s3, #s3_presigner, #s3_resource, #sts
#find_stack, #rollback_complete?, #stack_exists?, #testing_update?
Constructor Details
#initialize(stack_name, options = {}) ⇒ Base
Returns a new instance of Base.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/lono/cfn/base.rb', line 12
def initialize(stack_name, options={})
@options = options
stack_name = switch_current(stack_name)
@stack_name = append_suffix(stack_name)
Lono::ProjectChecker.check
@blueprint = options[:blueprint] || remove_suffix(@stack_name)
@template, @param = template_param_convention(options)
@options[:blueprint], @options[:template], @options[:param] = @blueprint, @template, @param
set_blueprint_root(@blueprint)
@template_path = "#{Lono.config.output_path}/#{@blueprint}/templates/#{@template}.yml"
end
|
Instance Method Details
#build_files ⇒ Object
176
177
178
|
# File 'lib/lono/cfn/base.rb', line 176
def build_files
Lono::AppFile::Build.new(@blueprint, @options).run
end
|
#build_scripts ⇒ Object
172
173
174
|
# File 'lib/lono/cfn/base.rb', line 172
def build_scripts
Lono::Script::Build.new(@blueprint, @options).run
end
|
#capabilities ⇒ Object
264
265
266
267
268
269
|
# File 'lib/lono/cfn/base.rb', line 264
def capabilities
return @options[:capabilities] if @options[:capabilities]
if @options[:sure] || @options[:iam]
["CAPABILITY_IAM", "CAPABILITY_NAMED_IAM"]
end
end
|
#check_files ⇒ Object
231
232
233
234
235
236
237
|
# File 'lib/lono/cfn/base.rb', line 231
def check_files
errors = []
unless File.exist?(@template_path)
errors << "Template file missing: could not find #{@template_path}"
end
errors
end
|
#check_for_errors ⇒ Object
222
223
224
225
226
227
228
229
|
# File 'lib/lono/cfn/base.rb', line 222
def check_for_errors
errors = check_files
unless errors.empty?
puts "Please double check the command you ran. There were some errors."
puts "ERROR: #{errors.join("\n")}".color(:red)
exit
end
end
|
#command_with_iam(capabilities) ⇒ Object
116
117
118
|
# File 'lib/lono/cfn/base.rb', line 116
def command_with_iam(capabilities)
"#{File.basename($0)} #{ARGV.join(' ')} --capabilities #{capabilities}"
end
|
#continue_update_rollback ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/lono/cfn/base.rb', line 87
def continue_update_rollback
continue_update_rollback_sure?
params = {stack_name: @stack_name}
show_parameters(params, "cfn.continue_update_rollback")
begin
cfn.continue_update_rollback(params)
rescue Aws::CloudFormation::Errors::ValidationError => e
puts "ERROR5: #{e.message}".red
exit 1
end
end
|
#continue_update_rollback_sure? ⇒ Boolean
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/lono/cfn/base.rb', line 74
def continue_update_rollback_sure?
puts <<~EOL
The stack is in the UPDATE_ROLLBACK_FAILED state. More info here: https://amzn.to/2IiEjc5
Would you like to try to continue the update rollback? (y/N)
EOL
sure = @options[:sure] ? "y" : $stdin.gets
unless sure =~ /^y/
puts "Exiting without continuing the update rollback."
exit 0
end
end
|
#delete_rollback_stack ⇒ Object
99
100
101
102
|
# File 'lib/lono/cfn/base.rb', line 99
def delete_rollback_stack
rollback = Rollback.new(@stack_name)
rollback.delete_stack
end
|
#ensure_s3_bucket_exist ⇒ Object
166
167
168
169
170
|
# File 'lib/lono/cfn/base.rb', line 166
def ensure_s3_bucket_exist
bucket = Lono::S3::Bucket.new
return if bucket.exist?
bucket.deploy
end
|
#exit_unless_updatable!(status) ⇒ Object
249
250
251
252
253
254
255
256
257
|
# File 'lib/lono/cfn/base.rb', line 249
def exit_unless_updatable!(status)
return true if testing_update?
return false if @options[:noop]
unless status =~ /_COMPLETE$/ || status == "UPDATE_ROLLBACK_FAILED"
puts "Cannot create a change set for the stack because the #{@stack_name} is not in an updatable state. Stack status: #{status}".color(:red)
quit(1)
end
end
|
#generate_all ⇒ Object
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
|
# File 'lib/lono/cfn/base.rb', line 122
def generate_all
return @@generate_all if @@generate_all
if @options[:lono]
ensure_s3_bucket_exist
build_scripts
generate_templates build_files
post_process_templates
unless @options[:noop]
upload_files
upload_scripts
upload_templates
end
end
param_generator.generate @@generate_all = param_generator.params
unless @options[:mute_using]
puts "Using template: #{pretty_path(@template_path)}"
param_generator.puts_param_message(:base)
param_generator.puts_param_message(:env)
end
check_for_errors
@@generate_all
end
|
#generate_templates ⇒ Object
#param_generator ⇒ Object
157
158
159
160
161
162
163
|
# File 'lib/lono/cfn/base.rb', line 157
def param_generator
generator_options = {
regenerate: false,
allow_not_exists: true
}.merge(@options)
Lono::Param::Generator.new(@blueprint, generator_options)
end
|
#post_process_templates ⇒ Object
#pretty_path(path) ⇒ Object
294
295
296
|
# File 'lib/lono/cfn/base.rb', line 294
def pretty_path(path)
path.sub("#{Lono.root}/",'')
end
|
#prompt_for_iam(capabilities) ⇒ Object
108
109
110
111
112
113
114
|
# File 'lib/lono/cfn/base.rb', line 108
def prompt_for_iam(capabilities)
puts "This stack will create IAM resources. Please approve to run the command again with #{capabilities} capabilities."
puts " #{command_with_iam(capabilities)}"
puts "Please confirm (y/n)"
$stdin.gets
end
|
#quit(signal) ⇒ Object
To allow mocking in specs
260
261
262
|
# File 'lib/lono/cfn/base.rb', line 260
def quit(signal)
exit signal
end
|
#run ⇒ Object
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
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/lono/cfn/base.rb', line 35
def run
starting_message
params = generate_all
begin
save_stack(params) rescue Aws::CloudFormation::Errors::InsufficientCapabilitiesException => e
capabilities = e.message.match(/\[(.*)\]/)[1]
confirm = prompt_for_iam(capabilities)
if confirm =~ /^y/
@options.merge!(capabilities: [capabilities])
puts "Re-running: #{command_with_iam(capabilities).color(:green)}"
retry
else
puts "Exited"
exit
end
rescue Aws::CloudFormation::Errors::ValidationError => e
if e.message.include?("No updates") puts "WARN: #{e.message}".color(:yellow)
elsif e.message.include?("UPDATE_ROLLBACK_FAILED") continue_update_rollback
else
puts "ERROR: #{e.message}".color(:red)
exit 1
end
end
return unless @options[:wait]
success = false
if !@options[:noop]
success = status.wait
end
exit 1 unless success
success
end
|
#set_template_body!(params) ⇒ Object
286
287
288
289
290
291
292
|
# File 'lib/lono/cfn/base.rb', line 286
def set_template_body!(params)
upload = Lono::Template::Upload.new(@blueprint)
url_path = @template_path.sub("#{Lono.root}/",'')
url = upload.s3_presigned_url(url_path)
params[:template_url] = url
params
end
|
#show_parameters(params, meth = nil) ⇒ Object
271
272
273
274
275
276
277
278
|
# File 'lib/lono/cfn/base.rb', line 271
def show_parameters(params, meth=nil)
params = params.clone.compact
params[:template_body] = "Hidden due to size... View at: #{pretty_path(@template_path)}"
params[:template_url] = params[:template_url].sub(/\?.*/,'')
to = meth || "AWS API"
puts "Parameters passed to #{to}:"
puts YAML.dump(params.deep_stringify_keys)
end
|
#stack_status(stack_name) ⇒ Object
241
242
243
244
245
246
247
|
# File 'lib/lono/cfn/base.rb', line 241
def stack_status(stack_name)
return true if testing_update?
return false if @options[:noop]
resp = cfn.describe_stacks(stack_name: stack_name)
resp.stacks[0].stack_status
end
|
#starting_message ⇒ Object
30
31
32
33
|
# File 'lib/lono/cfn/base.rb', line 30
def starting_message
action = self.class.to_s.split('::').last
puts "#{action} #{@stack_name.color(:green)} stack..."
end
|
#status ⇒ Object
104
105
106
|
# File 'lib/lono/cfn/base.rb', line 104
def status
@status ||= Cfn::Status.new(@stack_name)
end
|
Maps to CloudFormation format. Example:
{"a"=>"1", "b"=>"2"}
To
[{key: "a", value: "1"}, {key: "b", value: "2"}]
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/lono/cfn/base.rb', line 206
def tags
tags = @options[:tags] || []
tags = tags.map do |k,v|
{ key: k, value: v }
end
update_operation = %w[Preview Update].include?(self.class.to_s)
if tags.empty? && update_operation
resp = cfn.describe_stacks(stack_name: @stack_name)
tags = resp.stacks.first.tags
tags = tags.map(&:to_h)
end
tags
end
|
#upload_scripts ⇒ Object
192
193
194
|
# File 'lib/lono/cfn/base.rb', line 192
def upload_scripts
Lono::Script::Upload.new(@blueprint).run
end
|
#upload_templates ⇒ Object