Class: Lono::Cfn::Preview

Inherits:
Base
  • Object
show all
Defined in:
lib/lono/cfn/preview.rb

Instance Method Summary collapse

Methods inherited from Base

#build_files, #build_scripts, #capabilities, #check_files, #check_for_errors, #command_with_iam, #continue_update_rollback, #continue_update_rollback_sure?, #delete_rollback_stack, #ensure_s3_bucket_exist, #exit_unless_updatable!, #generate_all, #generate_templates, #initialize, #param_generator, #post_process_templates, #pretty_path, #prompt_for_iam, #quit, #set_template_body!, #show_parameters, #stack_status, #starting_message, #status, #tags, #upload_files, #upload_scripts, #upload_templates

Methods included from Util

#are_you_sure?, #switch_current

Methods included from Suffix

#allow_suffix?, #append_suffix, #random_suffix, #remove_suffix, #stack_name_suffix

Methods included from Lono::Conventions

#template_param_convention

Methods included from Blueprint::Root

#bundler_groups, #find_blueprint_root, #require_bundle_gems, #set_blueprint_root

Methods included from AwsServices

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

Methods included from AwsServices::Util

#find_stack, #rollback_complete?, #stack_exists?, #testing_update?

Constructor Details

This class inherits a constructor from Lono::Cfn::Base

Instance Method Details

#change_set_nameObject

generates a change set name



101
102
103
# File 'lib/lono/cfn/preview.rb', line 101

def change_set_name
  @change_set_name ||= "changeset-#{Time.now.strftime("%Y%d%m%H%M%S")}"
end

#create_change_set(params) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lono/cfn/preview.rb', line 19

def create_change_set(params)
  unless stack_exists?(@stack_name)
    puts "WARN: Cannot create a change set for the stack because the #{@stack_name} does not exists.".color(:yellow)
    return false
  end
  exit_unless_updatable!(stack_status(@stack_name))

  params = {
    change_set_name: change_set_name,
    stack_name: @stack_name,
    parameters: params,
    capabilities: capabilities, # ["CAPABILITY_IAM", "CAPABILITY_NAMED_IAM"],
  }
  params[:tags] = tags unless tags.empty?
  set_template_body!(params)
  show_parameters(params, "cfn.create_change_set")
  begin
    cfn.create_change_set(params)
  rescue Aws::CloudFormation::Errors::ValidationError => e
    handle_error(e)
  end
  true
end

#delete_change_setObject



86
87
88
89
90
91
# File 'lib/lono/cfn/preview.rb', line 86

def delete_change_set
  cfn.delete_change_set(
    change_set_name: change_set_name,
    stack_name: @stack_name
  )
end

#display_change_setObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lono/cfn/preview.rb', line 58

def display_change_set
  print "Generating CloudFormation Change Set for preview.."
  change_set = describe_change_set
  until change_set_finished?(change_set) do
    change_set = describe_change_set
    sleep 1
    print '.'
  end
  puts

  case change_set.status
  when "CREATE_COMPLETE"
    puts "CloudFormation preview for '#{@stack_name}' stack update. Changes:"
    changes = change_set.changes.sort_by do |change|
      change["resource_change"]["action"]
    end
    changes.each do |change|
      display_change(change)
    end
  when "FAILED"
    puts "WARN: Fail to create a CloudFormation preview for '#{@stack_name}' stack update. Reason:".color(:yellow)
    puts change_set.status_reason
    quit(0)
  else
    raise "hell: never come here"
  end
end

#execute_change_setObject



93
94
95
96
97
98
# File 'lib/lono/cfn/preview.rb', line 93

def execute_change_set
  cfn.execute_change_set(
    change_set_name: change_set_name,
    stack_name: @stack_name
  )
end

#handle_error(e) ⇒ Object

Example errors: “Template error: variable names in Fn::Sub syntax must contain only alphanumeric characters, underscores, periods, and colons”



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

def handle_error(e)
  raise if ENV['FULL_BACKTRACE']

  if e.message =~ /^Parameters: / || e.message =~ /^Template error: /
    puts "Error creating CloudFormation preview because invalid CloudFormation parameters. Full error message:".color(:red)
    puts e.message
    puts "For full backtrace run command again with FULL_BACKTRACE=1"
    quit(1)
  else
    raise
  end
end

#preview_change_set(params) ⇒ Object



14
15
16
17
# File 'lib/lono/cfn/preview.rb', line 14

def preview_change_set(params)
  success = create_change_set(params)
  display_change_set if success
end

#runObject

Override run from Base superclass, the run method is different enough with Preview



4
5
6
7
8
9
10
11
12
# File 'lib/lono/cfn/preview.rb', line 4

def run
  if @options[:noop]
    puts "NOOP CloudFormation preview for #{@stack_name} update"
  else
    params = generate_all
    success = preview_change_set(params)
    delete_change_set if success && !@options[:keep] # Clean up and delete the change set
  end
end