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

#capabilitiesObject

Returns the value of attribute capabilities.



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

def capabilities
  @capabilities
end

#clientObject

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_behaviorObject

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

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#parametersObject

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

#stateObject

Returns the value of attribute state.



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

def state
  @state
end

#template_filenameObject

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_waitObject

Wait for create complete



132
133
134
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 132

def create_wait
  wait(name, :create_complete)
end

#delete(should_wait: true) ⇒ Object

Delete the cloudformation stack



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 113

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_waitObject

Wait for delete complete



142
143
144
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 142

def delete_wait
  wait(name, :delete_complete)
end

#exist?Boolean

Get the cloudformation stack

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 73

def exist?
  !client.describe_stacks(stack_name: name).stacks.empty?
rescue ::Aws::CloudFormation::Errors::ValidationError
  false
end

#failed?Boolean

State matches the failed state

Returns:

  • (Boolean)


177
178
179
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 177

def failed?
  state == FAILED
end

#finished?Boolean

State matches the finished state

Returns:

  • (Boolean)


182
183
184
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 182

def finished?
  state == FINISHED
end

#no_changes?Boolean

State matches the no_changes state

Returns:

  • (Boolean)


172
173
174
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 172

def no_changes?
  state == NO_CHANGES
end

#not_started?Boolean

State matches the not started state

Returns:

  • (Boolean)


162
163
164
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 162

def not_started?
  state == NOT_STARTED
end

#started?Boolean

State matches the started state

Returns:

  • (Boolean)


167
168
169
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 167

def started?
  state == STARTED
end

#update(should_wait: true) ⇒ Object

Update the cloudformation stack



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 80

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_waitObject

Wait for update complete



137
138
139
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 137

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



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/firespring_dev_commands/aws/cloudformation.rb', line 148

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