Class: Lono::Cfn::Base

Inherits:
AbstractBase show all
Extended by:
Memoist
Includes:
AwsServices, Utils::Sure
Defined in:
lib/lono/cfn/base.rb

Instance Method Summary collapse

Methods included from Utils::Sure

#sure?

Methods included from AwsServices

#cfn, #ec2, #iam, #s3, #s3_presigner, #s3_resource, #sts

Methods included from AwsServices::Helper

#rollback_complete?, #testing_update?

Methods included from AwsServices::StackSet

#find_stack_set, #stack_set_exists?

Methods included from AwsServices::Stack

#find_stack, #stack_exists?

Methods inherited from AbstractBase

#initialize, #reinitialize, #template_path

Methods included from Blueprint::Root

#find_blueprint_root, #set_blueprint_root

Constructor Details

This class inherits a constructor from Lono::AbstractBase

Instance Method Details

#capabilitiesObject



159
160
161
162
163
164
# File 'lib/lono/cfn/base.rb', line 159

def capabilities
  return @options[:capabilities] if @options[:capabilities]
  if @options[:sure] || @options[:iam]
    ["CAPABILITY_IAM", "CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND"]
  end
end

#command_with_iam(capabilities) ⇒ Object



104
105
106
# File 'lib/lono/cfn/base.rb', line 104

def command_with_iam(capabilities)
  "#{File.basename($0)} #{ARGV.join(' ')} --capabilities #{capabilities}"
end

#continue_update_rollbackObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lono/cfn/base.rb', line 59

def continue_update_rollback
  continue_update_rollback_sure?
  options = {stack_name: @stack}
  show_options(options, "cfn.continue_update_rollback")
  begin
    cfn.continue_update_rollback(options)
  rescue Aws::CloudFormation::Errors::ValidationError => e
    puts "ERROR5: #{e.message}".color(:red)
    exit 1
  end
end

#continue_update_rollback_sure?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lono/cfn/base.rb', line 46

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_stackObject



71
72
73
74
# File 'lib/lono/cfn/base.rb', line 71

def delete_rollback_stack
  rollback = Rollback.new(@stack)
  rollback.delete_stack
end

#exit_unless_updatable!Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/lono/cfn/base.rb', line 134

def exit_unless_updatable!
  return true if testing_update?
  return false if @options[:noop]

  status = stack_status
  unless status =~ /_COMPLETE$/ || status == "UPDATE_ROLLBACK_FAILED"
    puts "Cannot create a change set for the stack because the #{@stack} is not in an updatable state.  Stack status: #{status}".color(:red)
    quit(1)
  end
end

#generate_allObject



108
109
110
# File 'lib/lono/cfn/base.rb', line 108

def generate_all
  Lono::Generate.new(@options).all
end

#notification_arnsObject



166
167
168
169
170
171
# File 'lib/lono/cfn/base.rb', line 166

def notification_arns
  @setting ||= Lono::Setting.new
  settings = @setting.data
  arns = @options["notification_arns"] || settings["notification_arns"]
  arns == [''] ? [] : arns # allow removing the notification_arns setting
end

#pretty_path(path) ⇒ Object



198
199
200
# File 'lib/lono/cfn/base.rb', line 198

def pretty_path(path)
  path.sub("#{Lono.root}/",'')
end

#prompt_for_iam(capabilities) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/lono/cfn/base.rb', line 96

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



155
156
157
# File 'lib/lono/cfn/base.rb', line 155

def quit(signal)
  exit signal
end

#rerun_with_iam?(e) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/lono/cfn/base.rb', line 81

def rerun_with_iam?(e)
  # e.message is "Requires capabilities : [CAPABILITY_IAM]"
  # grab CAPABILITY_IAM with regexp
  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)}"
    true
  else
    puts "Exited"
    exit 1
  end
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lono/cfn/base.rb', line 14

def run
  starting_message
  parameters = generate_all
  begin
    save(parameters) # defined in the sub class
  rescue Aws::CloudFormation::Errors::InsufficientCapabilitiesException => e
    yes = rerun_with_iam?(e)
    retry if yes
  rescue Aws::CloudFormation::Errors::ValidationError => e
    if e.message.include?("No updates") # No updates are to be performed.
      puts "WARN: #{e.message}".color(:yellow)
    elsif e.message.include?("UPDATE_ROLLBACK_FAILED") # https://amzn.to/2IiEjc5
      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 code for cfn.rb cli, so there's less duplication
  exit 1 unless success
  Lono::Registration.check
  success
end

#set_template_url!(options) ⇒ Object

Lono always uploads the template to s3 so we can use much larger templates.

template_body: 51,200 bytes - filesystem limit
template_url: 460,800 bytes - s3 limit

Reference: docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html



190
191
192
193
194
195
196
# File 'lib/lono/cfn/base.rb', line 190

def set_template_url!(options)
  url_path = template_path.sub("#{Lono.root}/",'')
  url = Lono::S3::Uploader.new(url_path).presigned_url
  url.gsub!(/\.yml.*/, ".yml") # Interesting dont need presign query string. For stack sets it actually breaks it. So removing.
  options[:template_url] = url
  options
end

#show_options(options, meth = nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
# File 'lib/lono/cfn/base.rb', line 173

def show_options(options, meth=nil)
  options = options.clone.compact
  if options[:template_body] # continue_update_rollback
    options[:template_body] = "Hidden due to size... View at: #{pretty_path(template_path)}"
    options[:template_url] = options[:template_url].sub(/\?.*/,'')
  end
  to = meth || "AWS API"
  puts "Parameters passed to #{to}:"
  puts YAML.dump(options.deep_stringify_keys)
end

#stack_statusObject



147
148
149
150
151
152
# File 'lib/lono/cfn/base.rb', line 147

def stack_status
  return true if testing_update?
  return false if @options[:noop]
  resp = cfn.describe_stacks(stack_name: @stack)
  resp.stacks[0].stack_status
end

#starting_messageObject



9
10
11
12
# File 'lib/lono/cfn/base.rb', line 9

def starting_message
  action = self.class.to_s.split('::').last
  puts "#{action} #{@stack.color(:green)} stack..."
end

#statusObject



76
77
78
# File 'lib/lono/cfn/base.rb', line 76

def status
  Status.new(@stack)
end

#tagsObject

Maps to CloudFormation format. Example:

{"a"=>"1", "b"=>"2"}

To

[{key: "a", value: "1"}, {key: "b", value: "2"}]


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lono/cfn/base.rb', line 118

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)
    tags = resp.stacks.first.tags
    tags = tags.map(&:to_h)
  end

  tags
end