-
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
5 changed files
with
162 additions
and
2 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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'erubis' | ||
require 'json' | ||
|
||
module StackMaster | ||
# This class is a modified version of `Erubis::Eruby`. It provides extra | ||
# helper methods to ease the dynamic creation of CloudFormation templates | ||
# with ERB. These helper methods are available within `<%= %>` 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 template at the specified filepath 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 within the referenced file 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
124 changes: 124 additions & 0 deletions
124
spec/stack_master/cloudformation_template_eruby_spec.rb
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,124 @@ | ||
RSpec.describe(StackMaster::CloudFormationTemplateEruby) do | ||
subject(:evaluate) do | ||
eruby = described_class.new(template) | ||
eruby.evaluate(eruby) | ||
end | ||
|
||
describe('.user_data_file') do | ||
context('given a template that loads a simple user data script file') do | ||
let(:template) { <<~YAML} | ||
Resources: | ||
LaunchConfig: | ||
Type: 'AWS::AutoScaling::LaunchConfiguration' | ||
Properties: | ||
UserData: <%= user_data_file('my/userdata.sh') %> | ||
YAML | ||
|
||
before do | ||
allow(File).to receive(:read).with('my/userdata.sh').and_return(<<~SHELL) | ||
#!/bin/bash | ||
REGION=ap-southeast-2 | ||
echo $REGION | ||
SHELL | ||
end | ||
|
||
it 'embeds the script in the evaluated CFN template' do | ||
expect(evaluate).to eq(<<~YAML) | ||
Resources: | ||
LaunchConfig: | ||
Type: 'AWS::AutoScaling::LaunchConfiguration' | ||
Properties: | ||
UserData: { | ||
"Fn::Base64": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"#!/bin/bash\\n", | ||
"\\n", | ||
"REGION=ap-southeast-2\\n", | ||
"echo $REGION\\n" | ||
] | ||
] | ||
} | ||
} | ||
YAML | ||
end | ||
end | ||
|
||
context('given a template that loads a user data script file that includes another file') do | ||
let(:template) { <<~YAML} | ||
Resources: | ||
LaunchConfig: | ||
Type: 'AWS::AutoScaling::LaunchConfiguration' | ||
Properties: | ||
UserData: <%= user_data_file('my/userdata.sh') %> | ||
YAML | ||
|
||
before do | ||
allow(File).to receive(:read).with('my/userdata.sh').and_return(<<~SHELL) | ||
#!/bin/bash | ||
echo 'Hello from userdata.sh' | ||
<%= user_data_file_as_lines('my/other.sh') %> | ||
SHELL | ||
allow(File).to receive(:read).with('my/other.sh').and_return(<<~SHELL) | ||
echo 'Hello from other.sh' | ||
SHELL | ||
end | ||
|
||
it 'embeds the script in the evaluated CFN template' do | ||
expect(evaluate).to eq(<<~YAML) | ||
Resources: | ||
LaunchConfig: | ||
Type: 'AWS::AutoScaling::LaunchConfiguration' | ||
Properties: | ||
UserData: { | ||
"Fn::Base64": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"#!/bin/bash\\n", | ||
"echo 'Hello from userdata.sh'\\n", | ||
"echo 'Hello from other.sh'\\n", | ||
"\\n" | ||
] | ||
] | ||
} | ||
} | ||
YAML | ||
end | ||
end | ||
end | ||
|
||
describe('.include_file') do | ||
context('given a template that loads a lambda script') do | ||
let(:template) { <<~YAML} | ||
Resources: | ||
Function: | ||
Type: 'AWS::Lambda::Function' | ||
Properties: | ||
Code: | ||
ZipFile: <%= include_file('my/lambda.sh') %> | ||
YAML | ||
|
||
before do | ||
allow(File).to receive(:read).with('my/lambda.sh').and_return(<<~SHELL) | ||
#!/bin/bash | ||
echo 'Hello, world!' | ||
SHELL | ||
end | ||
|
||
it 'embeds the script in the evaluated CFN template' do | ||
expect(evaluate).to eq(<<~YAML) | ||
Resources: | ||
Function: | ||
Type: 'AWS::Lambda::Function' | ||
Properties: | ||
Code: | ||
ZipFile: "#!/bin/bash\\n\\necho 'Hello, world!'\\n" | ||
YAML | ||
end | ||
end | ||
end | ||
end |