Class: Cfer::Core::Stack
- Defined in:
- lib/cfer/core/stack.rb
Overview
Defines the structure of a CloudFormation stack
Instance Attribute Summary collapse
-
#git_state ⇒ Object
readonly
Returns the value of attribute git_state.
-
#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, #cidr, #equals, #find_in_map, #get_att, #get_azs, #if, #join, #not, #notification_arns, #or, #ref, #select, #split, #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 72 73 74 75 76 |
# File 'lib/cfer/core/stack.rb', line 30 def initialize( = {}) self[:AWSTemplateFormatVersion] = '2010-09-09' self[:Description] = '' = 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 begin @git_state = git.object('HEAD^') self[:Metadata][:Cfer][:Git] = { Rev: git_state.sha, Clean: git.status.changed.empty? } rescue => e Cfer::LOGGER.warn("Unable to add Git information to CloudFormation Metadata. #{e}") end 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_state ⇒ Object (readonly)
Returns the value of attribute git_state.
16 17 18 |
# File 'lib/cfer/core/stack.rb', line 16 def git_state @git_state 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 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
200 201 202 |
# File 'lib/cfer/core/stack.rb', line 200 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
171 172 173 |
# File 'lib/cfer/core/stack.rb', line 171 def client [:client] || raise('No client set on this stack') end |
#condition(name, expr) ⇒ Object
Adds a condition to the template.
133 134 135 |
# File 'lib/cfer/core/stack.rb', line 133 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
79 80 81 |
# File 'lib/cfer/core/stack.rb', line 79 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.
177 178 179 180 181 182 183 |
# File 'lib/cfer/core/stack.rb', line 177 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.
188 189 190 |
# File 'lib/cfer/core/stack.rb', line 188 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.
194 195 196 197 |
# File 'lib/cfer/core/stack.rb', line 194 def lookup_outputs(stack) client = [: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
126 127 128 |
# File 'lib/cfer/core/stack.rb', line 126 def mappings(mappings) self[:Mappings] = mappings end |
#output(name, value, options = {}) ⇒ Object
Adds an output to the CloudFormation stack.
156 157 158 |
# File 'lib/cfer/core/stack.rb', line 156 def output(name, value, = {}) self[:Outputs][name] = .merge('Value' => value) end |
#parameter(name, options = {}) ⇒ Object
Declares a CloudFormation parameter
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/cfer/core/stack.rb', line 104 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
141 142 143 144 145 146 147 148 149 |
# File 'lib/cfer/core/stack.rb', line 141 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.handle 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.
162 163 164 165 166 167 168 |
# File 'lib/cfer/core/stack.rb', line 162 def to_cfn if [:pretty_print] JSON.pretty_generate(to_h) else to_h.to_json end end |