Class: Dev::Aws::Cloudformation

Inherits:
Object
  • Object
show all
Defined in:
lib/firespring_dev_commands/aws/cloudformation.rb,
lib/firespring_dev_commands/aws/cloudformation/parameters.rb

Overview

Class for performing cloudformation functions

Defined Under Namespace

Classes: Parameters

Constant Summary collapse

NOT_STARTED =

Not Started status

:not_started
STARTED =

Started status

:started
NO_CHANGES =

No Changes status

:no_changes
FAILED =

Failed status

:failed
FINISHED =

Finished status

:finished

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, template_filename, parameters: Dev::Aws::Cloudformation::Parameters.new, capabilities: [], failure_behavior: 'ROLLBACK') ⇒ Cloudformation

Returns a new instance of Cloudformation.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 26

def initialize(name, template_filename, parameters: Dev::Aws::Cloudformation::Parameters.new, capabilities: [], failure_behavior: 'ROLLBACK')
  raise 'parameters must be an intsance of parameters' unless parameters.is_a?(Dev::Aws::Cloudformation::Parameters)

  @client = nil
  @name = name
  @template_filename = template_filename
  @parameters = parameters
  @capabilities = capabilities
  @failure_behavior = failure_behavior
  @state = NOT_STARTED
end

Instance Attribute Details

#capabilities ⇒ Object

Returns the value of attribute capabilities.



24
25
26
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 24

def capabilities
  @capabilities
end

#client ⇒ Object

Create/set a new client if none is present Return the client



40
41
42
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 40

def client
  @client
end

#failure_behavior ⇒ Object

Returns the value of attribute failure_behavior.



24
25
26
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 24

def failure_behavior
  @failure_behavior
end

#name ⇒ Object

Returns the value of attribute name.



24
25
26
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 24

def name
  @name
end

#parameters ⇒ Object

Returns the value of attribute parameters.



24
25
26
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 24

def parameters
  @parameters
end

#state ⇒ Object

Returns the value of attribute state.



24
25
26
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 24

def state
  @state
end

#template_filename ⇒ Object

Returns the value of attribute template_filename.



24
25
26
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 24

def template_filename
  @template_filename
end

Instance Method Details

#create(should_wait: true) ⇒ Object

Create the cloudformation stack



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
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 45

def create(should_wait: true)
  # Call upload function to get the s3 url
  template_url = upload(template_filename)

  # Create the cloudformation stack
  client.create_stack(
    stack_name: name,
    template_url:,
    parameters: parameters.default,
    capabilities:,
    on_failure: failure_behavior
  )
  @state = STARTED
  LOG.info "#{name} stack create started at #{Time.now.to_s.light_yellow}"

  # return if we aren't waiting here
  return unless should_wait

  # Wait if we are supposed to wait
  create_wait
  @state = FINISHED
  LOG.info "#{name} stack create finished at #{Time.now.to_s.light_yellow}"
rescue => e
  LOG.error "Error creating stack: #{e.message}"
  @state = FAILED
end

#create_wait ⇒ Object

Wait for create complete



125
126
127
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 125

def create_wait
  wait(name, :create_complete)
end

#delete(should_wait: true) ⇒ Object

Delete the cloudformation stack



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 106

def delete(should_wait: true)
  # Delete the cloudformation stack
  client.delete_stack(stack_name: name)
  @state = STARTED
  LOG.info "#{name} stack delete started at #{Time.now.to_s.light_yellow}"

  # Return if we aren't waiting here
  return unless should_wait

  # Wait if we are supposed to wait
  delete_wait
  @state = FINISHED
  LOG.info "#{name} stack delete finished at #{Time.now.to_s.light_yellow}"
rescue => e
  LOG.error "Error deleting stack: #{e.message}"
  @state = FAILED
end

#delete_wait ⇒ Object

Wait for delete complete



135
136
137
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 135

def delete_wait
  wait(name, :delete_complete)
end

#failed? ⇒ Boolean

State matches the failed state

Returns:

  • (Boolean)


170
171
172
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 170

def failed?
  state == FAILED
end

#finished? ⇒ Boolean

State matches the finished state

Returns:

  • (Boolean)


175
176
177
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 175

def finished?
  state == FINISHED
end

#no_changes? ⇒ Boolean

State matches the no_changes state

Returns:

  • (Boolean)


165
166
167
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 165

def no_changes?
  state == NO_CHANGES
end

#not_started? ⇒ Boolean

State matches the not started state

Returns:

  • (Boolean)


155
156
157
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 155

def not_started?
  state == NOT_STARTED
end

#started? ⇒ Boolean

State matches the started state

Returns:

  • (Boolean)


160
161
162
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 160

def started?
  state == STARTED
end

#update(should_wait: true) ⇒ Object

Update the cloudformation stack



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 73

def update(should_wait: true)
  # Call upload function to get the s3 url
  template_url = upload(template_filename)

  # Update the cloudformation stack
  client.update_stack(
    stack_name: name,
    template_url:,
    parameters: parameters.preserve,
    capabilities:
  )
  @state = STARTED
  LOG.info "#{name} stack update started at #{Time.now.to_s.light_yellow}"

  # return if we aren't waiting here
  return unless should_wait

  # Wait if we are supposed to wait
  update_wait
  @state = FINISHED
  LOG.info "#{name} stack update finished at #{Time.now.to_s.light_yellow}"
rescue => e
  if /no updates/i.match?(e.message)
    LOG.info "No updates to needed on #{name}".light_yellow
    @state = NO_CHANGES
  else

    LOG.error "Error updating stack: #{e.message}"
    @state = FAILED
  end
end

#update_wait ⇒ Object

Wait for update complete



130
131
132
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 130

def update_wait
  wait(name, :update_complete)
end

#wait(stack_name, type = 'exists', max_attempts: 360, delay: 5) ⇒ Object

Wait for the stack name to complete the specified type of action Defaults to exists



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 141

def wait(stack_name, type = 'exists', max_attempts: 360, delay: 5)
  # Don't wait if there's nothing to wait for
  return if no_changes? || finished?

  client.wait_until(
    :"stack_#{type}",
    {stack_name:},
    {max_attempts:, delay:}
  )
rescue ::Aws::Waiters::Errors::WaiterFailed => e
  raise "Action failed to complete: #{e.message}"
end