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] = '' @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 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 @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
203 204 205 |
# File 'lib/cfer/core/stack.rb', line 203 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
174 175 176 |
# File 'lib/cfer/core/stack.rb', line 174 def client @options[: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.
180 181 182 183 184 185 186 |
# File 'lib/cfer/core/stack.rb', line 180 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.
191 192 193 |
# File 'lib/cfer/core/stack.rb', line 191 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.
197 198 199 200 |
# File 'lib/cfer/core/stack.rb', line 197 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
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.
157 158 159 160 161 |
# File 'lib/cfer/core/stack.rb', line 157 def output(name, value, **) opt = .each_with_object({}) { |(k,v),h| h[k.to_s.capitalize] = v } # capitalize all keys export = opt.has_key?('Export') ? {'Name' => opt['Export']} : nil self[:Outputs][name] = opt.merge('Value' => value, 'Export' => export).compact 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.
165 166 167 168 169 170 171 |
# File 'lib/cfer/core/stack.rb', line 165 def to_cfn if @options[:pretty_print] JSON.pretty_generate(to_h) else to_h.to_json end end |