stack_master 2.13.4 → 2.14.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 583b1be28160e2e7a7d483c6c98d4c04cd2a40d2af68aaac909d20d8a89d481e
4
- data.tar.gz: 145c997f67b7ddc582f9662cdae40982de289290a844987864c1f51586f82666
3
+ metadata.gz: 1199ce77646538900036cbc5f15765e3ff709e3732214787da844e6843757339
4
+ data.tar.gz: '086c79b68d70852700324fb12afd8f1f7fb3142104270ff9cd3bdb1f077e1fdd'
5
5
  SHA512:
6
- metadata.gz: f64207bb7cffe0fa1eb57bec402f96516772d7ead47ffce6ad74439ab5372df9322aa2cb0c5970af35b1f6563940a32e116275408dd4743475c1e8256bfdc024
7
- data.tar.gz: cb54cad90ae22381bc3f410f661abc878edf1abfa9b69cfb4c42e6d85ac4f562eca9d0cf9098b24b0e35e97d2022176949ade4ba609000cd2cbb239a786304d7
6
+ metadata.gz: 03431d20607e92edf9c666597a7b47ebeb9bf1309c407fbbaec455f2104f8bab3e22fd645f0ee2745943eb0d7229e87d1069e603db2b1f5885c4a2f98d6c7aca
7
+ data.tar.gz: b3f011fe065c9c5993ed048ed21beedac418cf7e50ba8161e3ebbd0618f8f7ce804285d86e78a25ea5098041dbea9f38248e000326ef6b218feee400bb409017
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'erubis'
4
+
5
+ module StackMaster
6
+ # This class is a modified version of `Erubis::Eruby`. It allows using
7
+ # `<%= %>` ERB expressions to interpolate values into a source string. We use
8
+ # this capability to enrich user data scripts with data and parameters pulled
9
+ # from the AWS CloudFormation service. The evaluation produces an array of
10
+ # objects ready for use in a CloudFormation `Fn::Join` intrinsic function.
11
+ class CloudFormationInterpolatingEruby < Erubis::Eruby
12
+ include Erubis::ArrayEnhancer
13
+
14
+ # Load a template from a file at the specified path and evaluate it.
15
+ def self.evaluate_file(source_path, context = Erubis::Context.new)
16
+ template_contents = File.read(source_path)
17
+ eruby = new(template_contents)
18
+ eruby.filename = source_path
19
+ eruby.evaluate(context)
20
+ end
21
+
22
+ # @return [Array] The result of evaluating the source: an array of strings
23
+ # from the source intermindled with Hash objects from the ERB
24
+ # expressions. To be included in a CloudFormation template, this
25
+ # value needs to be used in a CloudFormation `Fn::Join` intrinsic
26
+ # function.
27
+ # @see Erubis::Eruby#evaluate
28
+ # @example
29
+ # CloudFormationInterpolatingEruby.new("my_variable=<%= { 'Ref' => 'Param1' } %>;").evaluate
30
+ # #=> ['my_variable=', { 'Ref' => 'Param1' }, ';']
31
+ def evaluate(_context = Erubis::Context.new)
32
+ format_lines_for_cloudformation(super)
33
+ end
34
+
35
+ # @see Erubis::Eruby#add_expr
36
+ def add_expr(src, code, indicator)
37
+ if indicator == '='
38
+ src << " #{@bufvar} << (" << code << ');'
39
+ else
40
+ super
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ # Split up long strings containing multiple lines. One string per line in the
47
+ # CloudFormation array makes the compiled template and diffs more readable.
48
+ def format_lines_for_cloudformation(source)
49
+ source.flat_map do |lines|
50
+ lines = lines.to_s if lines.is_a?(Symbol)
51
+ next(lines) unless lines.is_a?(String)
52
+
53
+ lines.scan(/[^\n]*\n?/).reject { |x| x == '' }
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'erubis'
4
+ require 'json'
5
+
6
+ module StackMaster
7
+ # This class is a modified version of `Erubis::Eruby`. It provides extra
8
+ # helper methods to ease the dynamic creation of CloudFormation templates
9
+ # with ERB. These helper methods are available within `<%= %>` expressions.
10
+ class CloudFormationTemplateEruby < Erubis::Eruby
11
+ # Adds the contents of an EC2 userdata script to the CloudFormation
12
+ # template. Allows using the ERB `<%= %>` expressions within the user data
13
+ # script to interpolate CloudFormation values.
14
+ def user_data_file(filepath)
15
+ JSON.pretty_generate({ 'Fn::Base64' => { 'Fn::Join' => ['', user_data_file_as_lines(filepath)] } })
16
+ end
17
+
18
+ # Evaluate the ERB template at the specified filepath and return the result
19
+ # as an array of lines. Allows using ERB `<%= %>` expressions to interpolate
20
+ # CloudFormation objects into the result.
21
+ def user_data_file_as_lines(filepath)
22
+ StackMaster::CloudFormationInterpolatingEruby.evaluate_file(filepath, self)
23
+ end
24
+
25
+ # Add the contents of another file into the CloudFormation template as a
26
+ # string. ERB `<%= %>` expressions within the referenced file are not
27
+ # evaluated.
28
+ def include_file(filepath)
29
+ JSON.pretty_generate(File.read(filepath))
30
+ end
31
+ end
32
+ end
@@ -5,19 +5,6 @@ module StackMaster
5
5
  module SparkleFormation
6
6
  TemplateFileNotFound = ::Class.new(StandardError)
7
7
 
8
- class SfEruby < Erubis::Eruby
9
- include Erubis::ArrayEnhancer
10
-
11
- def add_expr(src, code, indicator)
12
- case indicator
13
- when '='
14
- src << " #{@bufvar} << (" << code << ');'
15
- else
16
- super
17
- end
18
- end
19
- end
20
-
21
8
  class TemplateContext < AttributeStruct
22
9
  include ::SparkleFormation::SparkleAttribute
23
10
  include ::SparkleFormation::SparkleAttribute::Aws
@@ -49,47 +36,12 @@ module StackMaster
49
36
  end
50
37
  end
51
38
 
52
- # Splits up long strings with multiple lines in them to multiple strings
53
- # in the CF array. Makes the compiled template and diffs more readable.
54
- class CloudFormationLineFormatter
55
- def self.format(template)
56
- new(template).format
57
- end
58
-
59
- def initialize(template)
60
- @template = template
61
- end
62
-
63
- def format
64
- @template.flat_map do |lines|
65
- lines = lines.to_s if Symbol === lines
66
- if String === lines
67
- newlines = []
68
- lines.count("\n").times do
69
- newlines << "\n"
70
- end
71
- newlines = lines.split("\n").map do |line|
72
- "#{line}#{newlines.pop}"
73
- end
74
- if lines.start_with?("\n")
75
- newlines.insert(0, "\n")
76
- end
77
- newlines
78
- else
79
- lines
80
- end
81
- end
82
- end
83
- end
84
-
85
39
  module Template
86
40
  def self.render(prefix, file_name, vars)
87
41
  file_path = File.join(::SparkleFormation.sparkle_path, prefix, file_name)
88
- template = File.read(file_path)
89
42
  template_context = TemplateContext.build(vars, prefix)
90
- compiled_template = SfEruby.new(template).evaluate(template_context)
91
- CloudFormationLineFormatter.format(compiled_template)
92
- rescue Errno::ENOENT => e
43
+ CloudFormationInterpolatingEruby.evaluate_file(file_path, template_context)
44
+ rescue Errno::ENOENT
93
45
  Kernel.raise TemplateFileNotFound, "Could not find template file at path: #{file_path}"
94
46
  end
95
47
  end
@@ -3,13 +3,12 @@
3
3
  module StackMaster::TemplateCompilers
4
4
  class YamlErb
5
5
  def self.require_dependencies
6
- require 'erubis'
7
6
  require 'yaml'
8
7
  end
9
8
 
10
9
  def self.compile(template_dir, template, compile_time_parameters, _compiler_options = {})
11
10
  template_file_path = File.join(template_dir, template)
12
- template = Erubis::Eruby.new(File.read(template_file_path))
11
+ template = StackMaster::CloudFormationTemplateEruby.new(File.read(template_file_path))
13
12
  template.filename = template_file_path
14
13
 
15
14
  template.result(params: compile_time_parameters)
@@ -1,3 +1,3 @@
1
1
  module StackMaster
2
- VERSION = "2.13.4"
2
+ VERSION = "2.14.0"
3
3
  end
data/lib/stack_master.rb CHANGED
@@ -45,6 +45,8 @@ module StackMaster
45
45
  autoload :StackDefinition, 'stack_master/stack_definition'
46
46
  autoload :TemplateCompiler, 'stack_master/template_compiler'
47
47
  autoload :Identity, 'stack_master/identity'
48
+ autoload :CloudFormationInterpolatingEruby, 'stack_master/cloudformation_interpolating_eruby'
49
+ autoload :CloudFormationTemplateEruby, 'stack_master/cloudformation_template_eruby'
48
50
 
49
51
  autoload :StackDiffer, 'stack_master/stack_differ'
50
52
  autoload :Validator, 'stack_master/validator'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stack_master
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.13.4
4
+ version: 2.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Hodgkiss
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-08-02 00:00:00.000000000 Z
12
+ date: 2024-02-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -146,7 +146,7 @@ dependencies:
146
146
  version: 4.6.0
147
147
  - - "<"
148
148
  - !ruby/object:Gem::Version
149
- version: '5'
149
+ version: '6'
150
150
  type: :runtime
151
151
  prerelease: false
152
152
  version_requirements: !ruby/object:Gem::Requirement
@@ -156,7 +156,7 @@ dependencies:
156
156
  version: 4.6.0
157
157
  - - "<"
158
158
  - !ruby/object:Gem::Version
159
- version: '5'
159
+ version: '6'
160
160
  - !ruby/object:Gem::Dependency
161
161
  name: aws-sdk-acm
162
162
  requirement: !ruby/object:Gem::Requirement
@@ -488,6 +488,8 @@ files:
488
488
  - lib/stack_master/aws_driver/s3.rb
489
489
  - lib/stack_master/change_set.rb
490
490
  - lib/stack_master/cli.rb
491
+ - lib/stack_master/cloudformation_interpolating_eruby.rb
492
+ - lib/stack_master/cloudformation_template_eruby.rb
491
493
  - lib/stack_master/command.rb
492
494
  - lib/stack_master/commands/apply.rb
493
495
  - lib/stack_master/commands/compile.rb
@@ -577,8 +579,8 @@ licenses:
577
579
  metadata:
578
580
  bug_tracker_uri: https://github.com/envato/stack_master/issues
579
581
  changelog_uri: https://github.com/envato/stack_master/blob/master/CHANGELOG.md
580
- documentation_uri: https://www.rubydoc.info/gems/stack_master/2.13.4
581
- source_code_uri: https://github.com/envato/stack_master/tree/v2.13.4
582
+ documentation_uri: https://www.rubydoc.info/gems/stack_master/2.14.0
583
+ source_code_uri: https://github.com/envato/stack_master/tree/v2.14.0
582
584
  post_install_message:
583
585
  rdoc_options: []
584
586
  require_paths:
@@ -594,7 +596,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
594
596
  - !ruby/object:Gem::Version
595
597
  version: '0'
596
598
  requirements: []
597
- rubygems_version: 3.1.6
599
+ rubygems_version: 3.4.20
598
600
  signing_key:
599
601
  specification_version: 4
600
602
  summary: StackMaster is a sure-footed way of creating, updating and keeping track