Class: Cfer::Core::Stack
- Defined in:
- lib/cfer/core/stack.rb
Overview
Defines the structure of a CloudFormation stack
Instance Attribute Summary collapse
-
#git_version ⇒ Object
readonly
Returns the value of attribute git_version.
-
#input_parameters ⇒ Object
readonly
The parameters strictly as passed via command line.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parameters ⇒ Object
readonly
The fully resolved parameters, including defaults and parameters fetched from an existing stack during an update.
Class Method Summary collapse
Instance Method Summary collapse
-
#client ⇒ Object
Gets the Cfn client, if one exists, or throws an error if one does not.
-
#condition(name, expr) ⇒ Object
Adds a condition to the template.
- #converge!(options = {}) ⇒ Object
-
#description(desc) ⇒ Object
Sets the description for this CloudFormation stack.
-
#include_template(*files) ⇒ Object
Includes template code from one or more files, and evals it in the context of this stack.
-
#initialize(options = {}) ⇒ Stack
constructor
A new instance of Stack.
-
#lookup_output(stack, out) ⇒ Object
Looks up a specific output of another CloudFormation stack in the same region.
-
#lookup_outputs(stack) ⇒ Object
Looks up a hash of all outputs from another CloudFormation stack in the same region.
-
#mappings(mappings) ⇒ Object
Sets the mappings block for this stack.
-
#output(name, value, options = {}) ⇒ Object
Adds an output to the CloudFormation stack.
-
#parameter(name, options = {}) ⇒ Object
Declares a CloudFormation parameter.
-
#resource(name, type, options = {}, &block) ⇒ Object
Creates a CloudFormation resource.
- #tail!(options = {}, &block) ⇒ Object
-
#to_cfn ⇒ String
Renders the stack into a CloudFormation template.
Methods included from Hooks
included, #post_block, #pre_block
Methods included from Functions
#and, #equals, #find_in_map, #get_att, #get_azs, #if, #join, #not, #notification_arns, #or, #ref, #select, #sub
Methods inherited from Block
#build_from_block, #build_from_file, #build_from_string, #include_file, #post_block, #pre_block
Constructor Details
#initialize(options = {}) ⇒ Stack
Returns a new instance of Stack.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 71 |
# File 'lib/cfer/core/stack.rb', line 30 def initialize( = {}) self[:AWSTemplateFormatVersion] = '2010-09-09' self[:Description] = '' @options = self[:Metadata] = { :Cfer => { :Version => Cfer::SEMANTIC_VERSION.to_h.delete_if { |k, v| v === nil } } } self[:Parameters] = {} self[:Mappings] = {} self[:Conditions] = {} self[:Resources] = {} self[:Outputs] = {} if [:client] && git = [:client].git && @git_version = (git.object('HEAD^').sha rescue nil) self[:Metadata][:Cfer][:Git] = { Rev: @git_version, Clean: git.status.changed.empty? } end @parameters = HashWithIndifferentAccess.new @input_parameters = HashWithIndifferentAccess.new if [:client] begin @parameters.merge! [:client].fetch_parameters rescue Cfer::Util::StackDoesNotExistError Cfer::LOGGER.debug "Can't include current stack parameters because the stack doesn't exist yet." end end if [:parameters] [:parameters].each do |key, val| @input_parameters[key] = @parameters[key] = val end end end |
Instance Attribute Details
#git_version ⇒ Object (readonly)
Returns the value of attribute git_version.
16 17 18 |
# File 'lib/cfer/core/stack.rb', line 16 def git_version @git_version end |
#input_parameters ⇒ Object (readonly)
The parameters strictly as passed via command line
9 10 11 |
# File 'lib/cfer/core/stack.rb', line 9 def input_parameters @input_parameters end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/cfer/core/stack.rb', line 14 def @options end |
#parameters ⇒ Object (readonly)
The fully resolved parameters, including defaults and parameters fetched from an existing stack during an update
12 13 14 |
# File 'lib/cfer/core/stack.rb', line 12 def parameters @parameters end |
Class Method Details
.extend_stack(&block) ⇒ Object
195 196 197 |
# File 'lib/cfer/core/stack.rb', line 195 def extend_stack(&block) class_eval(&block) end |
Instance Method Details
#client ⇒ Object
Gets the Cfn client, if one exists, or throws an error if one does not
166 167 168 |
# File 'lib/cfer/core/stack.rb', line 166 def client @options[:client] || raise('No client set on this stack') end |
#condition(name, expr) ⇒ Object
Adds a condition to the template.
128 129 130 |
# File 'lib/cfer/core/stack.rb', line 128 def condition(name, expr) self[:Conditions][name] = expr end |
#converge!(options = {}) ⇒ Object
22 23 24 |
# File 'lib/cfer/core/stack.rb', line 22 def converge!( = {}) client.converge self, end |
#description(desc) ⇒ Object
Sets the description for this CloudFormation stack
74 75 76 |
# File 'lib/cfer/core/stack.rb', line 74 def description(desc) self[:Description] = desc end |
#include_template(*files) ⇒ Object
Includes template code from one or more files, and evals it in the context of this stack. Filenames are relative to the file containing the invocation of this method.
172 173 174 175 176 177 178 |
# File 'lib/cfer/core/stack.rb', line 172 def include_template(*files) include_base = [:include_base] || File.dirname(caller.first.split(/:\d/,2).first) files.each do |file| path = File.join(include_base, file) include_file(path) end end |
#lookup_output(stack, out) ⇒ Object
Looks up a specific output of another CloudFormation stack in the same region.
183 184 185 |
# File 'lib/cfer/core/stack.rb', line 183 def lookup_output(stack, out) lookup_outputs(stack).fetch(out) end |
#lookup_outputs(stack) ⇒ Object
Looks up a hash of all outputs from another CloudFormation stack in the same region.
189 190 191 192 |
# File 'lib/cfer/core/stack.rb', line 189 def lookup_outputs(stack) client = @options[:client] || raise(Cfer::Util::CferError, "Can not fetch stack outputs without a client") client.fetch_outputs(stack) end |
#mappings(mappings) ⇒ Object
Sets the mappings block for this stack. See The CloudFormation Documentation for more details
121 122 123 |
# File 'lib/cfer/core/stack.rb', line 121 def mappings(mappings) self[:Mappings] = mappings end |
#output(name, value, options = {}) ⇒ Object
Adds an output to the CloudFormation stack.
151 152 153 |
# File 'lib/cfer/core/stack.rb', line 151 def output(name, value, = {}) self[:Outputs][name] = .merge('Value' => value) end |
#parameter(name, options = {}) ⇒ Object
Declares a CloudFormation parameter
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/cfer/core/stack.rb', line 99 def parameter(name, = {}) param = {} .each do |key, v| next if v === nil k = key.to_s.camelize.to_sym param[k] = case k when :AllowedPattern if v.class == Regexp v.source end when :Default @parameters[name] ||= v end param[k] ||= v end param[:Type] ||= 'String' self[:Parameters][name] = param end |
#resource(name, type, options = {}, &block) ⇒ Object
Creates a CloudFormation resource
136 137 138 139 140 141 142 143 144 |
# File 'lib/cfer/core/stack.rb', line 136 def resource(name, type, = {}, &block) Preconditions.check_argument(/[[:alnum:]]+/ =~ name, "Resource name must be alphanumeric") clazz = Cfer::Core::Resource.resource_class(type) rc = clazz.new(name, type, self, , &block) self[:Resources][name] = rc rc end |
#tail!(options = {}, &block) ⇒ Object
26 27 28 |
# File 'lib/cfer/core/stack.rb', line 26 def tail!( = {}, &block) client.tail self, , &block end |
#to_cfn ⇒ String
Renders the stack into a CloudFormation template.
157 158 159 160 161 162 163 |
# File 'lib/cfer/core/stack.rb', line 157 def to_cfn if @options[:pretty_print] JSON.pretty_generate(to_h) else to_h.to_json end end |