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?
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
180
181
182
|
# File 'lib/lono/cfn/base.rb', line 180
def build_files
Lono::AppFile::Build.new(@blueprint, @options).run
end
|
#build_scripts ⇒ Object
176
177
178
|
# File 'lib/lono/cfn/base.rb', line 176
def build_scripts
Lono::Script::Build.new(@blueprint, @options).run
end
|
#capabilities ⇒ Object
268
269
270
271
272
273
|
# File 'lib/lono/cfn/base.rb', line 268
def capabilities
return @options[:capabilities] if @options[:capabilities]
if @options[:iam]
["CAPABILITY_IAM", "CAPABILITY_NAMED_IAM"]
end
end
|
#check_files ⇒ Object
235
236
237
238
239
240
241
|
# File 'lib/lono/cfn/base.rb', line 235
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
226
227
228
229
230
231
232
233
|
# File 'lib/lono/cfn/base.rb', line 226
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
120
121
122
|
# File 'lib/lono/cfn/base.rb', line 120
def command_with_iam(capabilities)
"#{File.basename($0)} #{ARGV.join(' ')} --capabilities #{capabilities}"
end
|
#continue_update_rollback ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/lono/cfn/base.rb', line 91
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
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/lono/cfn/base.rb', line 78
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
103
104
105
106
|
# File 'lib/lono/cfn/base.rb', line 103
def delete_rollback_stack
rollback = Rollback.new(@stack_name)
rollback.delete_stack
end
|
#ensure_s3_bucket_exist ⇒ Object
170
171
172
173
174
|
# File 'lib/lono/cfn/base.rb', line 170
def ensure_s3_bucket_exist
bucket = Lono::S3::Bucket.new
return if bucket.exist?
bucket.create
end
|
#exit_unless_updatable!(status) ⇒ Object
253
254
255
256
257
258
259
260
261
|
# File 'lib/lono/cfn/base.rb', line 253
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
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
|
# File 'lib/lono/cfn/base.rb', line 126
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
161
162
163
164
165
166
167
|
# File 'lib/lono/cfn/base.rb', line 161
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
297
298
299
|
# File 'lib/lono/cfn/base.rb', line 297
def pretty_path(path)
path.sub("#{Lono.root}/",'')
end
|
#prompt_for_iam(capabilities) ⇒ Object
112
113
114
115
116
117
118
|
# File 'lib/lono/cfn/base.rb', line 112
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
264
265
266
|
# File 'lib/lono/cfn/base.rb', line 264
def quit(signal)
exit signal
end
|
#run ⇒ Object
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
73
74
75
76
|
# File 'lib/lono/cfn/base.rb', line 39
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
289
290
291
292
293
294
295
|
# File 'lib/lono/cfn/base.rb', line 289
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
275
276
277
278
279
280
281
|
# File 'lib/lono/cfn/base.rb', line 275
def show_parameters(params, meth=nil)
params = params.clone.compact
params[:template_body] = "Hidden due to size... View at: #{pretty_path(@template_path)}"
to = meth || "AWS API"
puts "Parameters passed to #{to}:"
puts YAML.dump(params.deep_stringify_keys)
end
|
#stack_status(stack_name) ⇒ Object
245
246
247
248
249
250
251
|
# File 'lib/lono/cfn/base.rb', line 245
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
34
35
36
37
|
# File 'lib/lono/cfn/base.rb', line 34
def starting_message
action = self.class.to_s.split('::').last
puts "#{action} #{@stack_name.color(:green)} stack..."
end
|
#status ⇒ Object
108
109
110
|
# File 'lib/lono/cfn/base.rb', line 108
def status
@status ||= Cfn::Status.new(@stack_name)
end
|
#switch_current(stack_name) ⇒ Object
30
31
32
|
# File 'lib/lono/cfn/base.rb', line 30
def switch_current(stack_name)
Current.name!(stack_name)
end
|
Maps to CloudFormation format. Example:
{"a"=>"1", "b"=>"2"}
To
[{key: "a", value: "1"}, {key: "b", value: "2"}]
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/lono/cfn/base.rb', line 210
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
196
197
198
|
# File 'lib/lono/cfn/base.rb', line 196
def upload_scripts
Lono::Script::Upload.new(@blueprint).run
end
|
#upload_templates ⇒ Object