Class: Stacker::Stack

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/stacker/stack.rb,
lib/stacker/stack/errors.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, TemplateSyntaxError, 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.



46
47
48
# File 'lib/stacker/stack.rb', line 46

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



44
45
46
# File 'lib/stacker/stack.rb', line 44

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



44
45
46
# File 'lib/stacker/stack.rb', line 44

def options
  @options
end

#regionObject (readonly)

Returns the value of attribute region.



44
45
46
# File 'lib/stacker/stack.rb', line 44

def region
  @region
end

Instance Method Details

#capabilitiesObject



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

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

#clientObject



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

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

#create(blocking = true) ⇒ Object



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

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



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

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

#parametersObject



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

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

#templateObject



61
62
63
# File 'lib/stacker/stack.rb', line 61

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

#update(options = {}) ⇒ Object



106
107
108
109
110
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
# File 'lib/stacker/stack.rb', line 106

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