-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce user_data_file
helper method for YAML ERB templates
#377
Conversation
d61039c
to
166a4a6
Compare
# this capability to enrich user data scripts with data and parameters pulled | ||
# from the AWS CloudFormation service. The evaluation produces an array of | ||
# objects ready for use in a CloudFormation `Fn::Join` intrinsic function. | ||
class CloudFormationInterpolatingEruby < Erubis::Eruby |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extract this class out of the stack_master/sparkle_formation/template_file.rb
file.
Allow the functionality to be reused between the SparkleFormation and YAML ERB template compilers.
# 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extend Eruby
with helper methods exposed to the YAML ERB templates.
Description: A test case for storing the userdata script in a dedicated file | ||
|
||
Resources: | ||
LaunchConfig: | ||
Type: 'AWS::AutoScaling::LaunchConfiguration' | ||
Properties: | ||
UserData: <%= user_data_file(File.join(__dir__, 'user_data.sh.erb')) %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test fixture shows how the user_data_file
helper method can be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! 👍
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this pretty hard to read. How about this?
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 | |
lines.scan(/[^\n]*\n?/).reject { |x| x=="" } |
This should cover the same idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Proposed in #378.
Context
The StackMaster SparkleFormation template compiler provides a neat
_user_data_file
convenience method that provides an easy way to include userdata script files in CloudFormation templates.stack_master/lib/stack_master/sparkle_formation/template_file.rb
Lines 105 to 108 in feab2eb
Change
Introduce a similar
user_data_file
method for the YAML ERB template compiler. This will allow users to do something like this within a.yml.erb
template: