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



163
164
165
166
167
168
# File 'lib/lono/cfn/base.rb', line 163

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

#check_registrationObject



46
47
48
# File 'lib/lono/cfn/base.rb', line 46

def check_registration
  Lono::Registration::Check.new.check
end

#command_with_iam(capabilities) ⇒ Object



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

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

#continue_update_rollbackObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lono/cfn/base.rb', line 63

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)


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lono/cfn/base.rb', line 50

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



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

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

#exit_unless_updatable!Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/lono/cfn/base.rb', line 138

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



112
113
114
# File 'lib/lono/cfn/base.rb', line 112

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

#pretty_path(path) ⇒ Object



194
195
196
# File 'lib/lono/cfn/base.rb', line 194

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

#prompt_for_iam(capabilities) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/lono/cfn/base.rb', line 100

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



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

def quit(signal)
  exit signal
end

#rerun_with_iam?(e) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/lono/cfn/base.rb', line 85

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
    check_registration
    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
  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



185
186
187
188
189
190
191
192
# File 'lib/lono/cfn/base.rb', line 185

def set_template_url!(options)
  upload = Lono::Template::Upload.new(@options)
  url_path = template_path.sub("#{Lono.root}/",'')
  url = upload.s3_presigned_url(url_path)
  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



170
171
172
173
174
175
176
177
# File 'lib/lono/cfn/base.rb', line 170

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

#stack_statusObject



151
152
153
154
155
156
# File 'lib/lono/cfn/base.rb', line 151

def stack_status
  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_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



80
81
82
# File 'lib/lono/cfn/base.rb', line 80

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"}]


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/lono/cfn/base.rb', line 122

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