Class: Moonshot::ChangeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/moonshot/change_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, stack_name) ⇒ ChangeSet

Returns a new instance of ChangeSet.



7
8
9
10
11
12
# File 'lib/moonshot/change_set.rb', line 7

def initialize(name, stack_name)
  @name = name
  @stack_name = stack_name
  @change_set = nil
  @cf_client = Aws::CloudFormation::Client.new
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/moonshot/change_set.rb', line 5

def name
  @name
end

#stack_nameObject (readonly)

Returns the value of attribute stack_name.



5
6
7
# File 'lib/moonshot/change_set.rb', line 5

def stack_name
  @stack_name
end

Instance Method Details

#confirm?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/moonshot/change_set.rb', line 14

def confirm?
  raise 'Cannot confirm ChangeSet when interactive mode is disabled!' unless Moonshot.config.interactive

  loop do
    print 'Apply changes? '
    resp = gets.chomp.downcase

    return true if resp == 'yes'
    return false if resp == 'no'

    puts "Please enter 'yes' or 'no'!"
  end
end

#deleteObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/moonshot/change_set.rb', line 68

def delete
  wait_for_change_set unless @change_set
  @cf_client.delete_change_set(
    change_set_name: @name,
    stack_name: @stack_name
  )
rescue Aws::CloudFormation::Errors::InvalidChangeSetStatus
  sleep 1
  retry
end

#display_changesObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/moonshot/change_set.rb', line 36

def display_changes
  wait_for_change_set unless @change_set

  @change_set.changes.map(&:resource_change).each do |c|
    puts "* #{c.action} #{c.logical_resource_id} (#{c.resource_type})"

    case c.replacement
    when 'True'
      puts ' - Will be replaced'
    when 'Conditional'
      puts ' - May be replaced (Conditional)'
    end

    c.details.each do |d|
      case d.change_source
      when 'ResourceReference', 'ParameterReference'
        puts " - Caused by #{d.causing_entity.blue} (#{d.change_source})"
      when 'DirectModification'
        puts " - Caused by template change (#{d.target.attribute}: #{d.target.name})"
      end
    end
  end
end

#executeObject



60
61
62
63
64
65
66
# File 'lib/moonshot/change_set.rb', line 60

def execute
  wait_for_change_set unless @change_set
  @cf_client.execute_change_set(
    change_set_name: @name,
    stack_name: @stack_name
  )
end

#invalid_reasonObject



32
33
34
# File 'lib/moonshot/change_set.rb', line 32

def invalid_reason
  @change_set.status_reason
end

#valid?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/moonshot/change_set.rb', line 28

def valid?
  @change_set.status == 'CREATE_COMPLETE'
end

#wait_for_change_setObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/moonshot/change_set.rb', line 79

def wait_for_change_set
  begin
    @cf_client.wait_until(:change_set_create_complete,
                          stack_name: @stack_name,
                          change_set_name: @name)
  rescue Aws::Waiters::Errors::FailureStateError => e
    if e.message != 'stopped waiting, encountered a failure state'
      throw e
    else
      puts "The change set didn't contain any new changes."
    end
  end

  @change_set = @cf_client.describe_change_set(stack_name: @stack_name,
                                               change_set_name: @name)
end