Class: Stacker::Stack

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/stacker/stack.rb,
lib/stacker/stack/template.rb,
lib/stacker/stack/component.rb,
lib/stacker/stack/parameters.rb,
lib/stacker/stack/capabilities.rb

Defined Under Namespace

Classes: Capabilities, Component, DoesNotExistError, Error, MissingParameters, Parameters, StackPolicyError, Template, UpToDateError

Constant Summary collapse

CLIENT_METHODS =
%w[
  creation_time
  description
  exists?
  last_updated_time
  status
  status_reason
]
SAFE_UPDATE_POLICY =
<<-JSON
{
  "Statement" : [
    {
      "Effect" : "Deny",
      "Action" : ["Update:Replace", "Update:Delete"],
      "Principal" : "*",
      "Resource" : "*"
    },
    {
      "Effect" : "Allow",
      "Action" : "Update:*",
      "Principal" : "*",
      "Resource" : "*"
    }
  ]
}
JSON

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region, name, options = {}) ⇒ Stack

Returns a new instance of Stack.



51
52
53
# File 'lib/stacker/stack.rb', line 51

def initialize region, name, options = {}
  @region, @name, @options = region, name, options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



49
50
51
# File 'lib/stacker/stack.rb', line 49

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



49
50
51
# File 'lib/stacker/stack.rb', line 49

def options
  @options
end

#regionObject (readonly)

Returns the value of attribute region.



49
50
51
# File 'lib/stacker/stack.rb', line 49

def region
  @region
end

Instance Method Details

#capabilitiesObject



74
75
76
# File 'lib/stacker/stack.rb', line 74

def capabilities
  @capabilities ||= Capabilities.new self
end

#clientObject



55
56
57
# File 'lib/stacker/stack.rb', line 55

def client
  @client ||= region.client.stacks[name]
end

#create(blocking = true) ⇒ Object



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
# File 'lib/stacker/stack.rb', line 85

def create blocking = true
  if exists?
    Stacker.logger.warn 'Stack already exists'
    return
  end

  if parameters.missing.any?
    raise MissingParameters.new(
      "Required parameters missing: #{parameters.missing.join ', '}"
    )
  end

  Stacker.logger.info 'Creating stack'

  region.client.stacks.create(
    name,
    template.local,
    parameters: parameters.resolved,
    capabilities: capabilities.local
  )

  wait_while_status 'CREATE_IN_PROGRESS' if blocking
rescue AWS::CloudFormation::Errors::ValidationError => err
  raise Error.new err.message
end

#outputsObject



78
79
80
81
82
83
# File 'lib/stacker/stack.rb', line 78

def outputs
  @outputs ||= begin
    return {} unless complete?
    Hash[client.outputs.map { |output| [ output.key, output.value ] }]
  end
end

#parametersObject



70
71
72
# File 'lib/stacker/stack.rb', line 70

def parameters
  @parameters ||= Parameters.new self
end

#templateObject



66
67
68
# File 'lib/stacker/stack.rb', line 66

def template
  @template ||= Template.new self
end

#update(options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/stacker/stack.rb', line 111

def update options = {}
  options.assert_valid_keys(:blocking, :allow_destructive)

  blocking = options.fetch(:blocking, true)
  allow_destructive = options.fetch(:allow_destructive, false)

  if parameters.missing.any?
    raise MissingParameters.new(
      "Required parameters missing: #{parameters.missing.join ', '}"
    )
  end

  Stacker.logger.info 'Updating stack'

  update_params = {
    template: template.local,
    parameters: parameters.resolved,
    capabilities: capabilities.local
  }

  unless allow_destructive
    update_params[:stack_policy_during_update_body] = SAFE_UPDATE_POLICY
  end

  client.update(update_params)

  wait_while_status 'UPDATE_IN_PROGRESS' if blocking
rescue AWS::CloudFormation::Errors::ValidationError => err
  case err.message
  when /does not exist/
    raise DoesNotExistError.new err.message
  when /No updates/
    raise UpToDateError.new err.message
  else
    raise Error.new err.message
  end
end