-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide
user_data_file
helper method for YAML ERB templates
- Loading branch information
Showing
6 changed files
with
97 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'erubis' | ||
|
||
module StackMaster | ||
# This class is a modified version of the `Erubis::Eruby` class. It allows | ||
# for the use of the `<%= %>` expressions to interpolate CloudFormation | ||
# values into the source string. | ||
class CloudFormationInterpolatingEruby < Erubis::Eruby | ||
include Erubis::ArrayEnhancer | ||
|
||
def self.evaluate_file(source_path, context = Erubis::Context.new) | ||
template_contents = File.read(source_path) | ||
eruby = new(template_contents) | ||
eruby.filename = source_path | ||
eruby.evaluate(context) | ||
end | ||
|
||
# Evaluate the source string and return an array of objects ready for use | ||
# in a CloudFormation `Fn::Join` intrinsic function. | ||
# | ||
# @example Produces an array | ||
# CloudFormationInterpolatingEruby.new("my_variable=<%= { 'Ref' => 'Param1' } %>;").evaluate | ||
# #=> ['my_variable=', { 'Ref' => 'Param1' }, ';'] | ||
def evaluate(context = Erubis::Context.new) | ||
format_lines_for_cloudformation(super) | ||
end | ||
|
||
def add_expr(src, code, indicator) | ||
if indicator == '=' | ||
src << " #{@bufvar} << (" << code << ');' | ||
else | ||
super | ||
end | ||
end | ||
|
||
private | ||
|
||
# Split up long strings containing multiple lines. One string per line in the | ||
# CloudFormation array makes the compiled template and diffs more readable. | ||
def format_lines_for_cloudformation(source) | ||
source.flat_map do |lines| | ||
lines = lines.to_s if lines.is_a?(Symbol) | ||
next(lines) unless lines.is_a?(String) | ||
|
||
newlines = Array.new(lines.count("\n"), "\n") | ||
newlines = lines.split("\n").map { |line| "#{line}#{newlines.pop}" } | ||
newlines.insert(0, "\n") if lines.start_with?("\n") | ||
newlines | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'erubis' | ||
require 'json' | ||
|
||
module StackMaster | ||
# This class is a modified version of the `Erubis::Eruby` class. It | ||
# provides extra helper methods to ease the creation of CloudFormation | ||
# templates. These helper methods are available within ERB `<%= %> | ||
# expressions`. | ||
class CloudFormationTemplateEruby < Erubis::Eruby | ||
# Adds the contents of an EC2 userdata script to the CloudFormation | ||
# template. | ||
# | ||
# Allows using the ERB `<%= %>` expressions within the user data script | ||
# to interpolate CloudFormation values. | ||
def user_data_file(filepath) | ||
JSON.pretty_generate({ 'Fn::Base64' => { 'Fn::Join' => ['', user_data_file_as_lines(filepath)] } }) | ||
end | ||
|
||
# Evaluate the ERB file and return the result as an array of lines. | ||
# | ||
# Allows using ERB `<%= %>` expressions to interpolate CloudFormation | ||
# objects into the result. | ||
def user_data_file_as_lines(filepath) | ||
StackMaster::CloudFormationInterpolatingEruby.evaluate_file(filepath, self) | ||
end | ||
|
||
# Add the contents of another file into the CloudFormation template as a | ||
# string. ERB `<%= %>` expressions are not evaluated. | ||
def include_file(filepath) | ||
JSON.pretty_generate(File.read(filepath)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters