Skip to content
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

Merged
merged 3 commits into from
Feb 5, 2024

Conversation

orien
Copy link
Member

@orien orien commented Feb 2, 2024

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.

def _user_data_file(file_name, vars = {})
base64!(join!(Template.render('user_data', file_name, vars)))
end
alias_method :user_data_file!, :_user_data_file

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:

Resources:
  LaunchConfig:
    Type: 'AWS::AutoScaling::LaunchConfiguration'
    Properties:
      UserData: <%= user_data_file('path/to/my/userdata.sh') %>

@orien orien force-pushed the yaml-template-userdata branch from d61039c to 166a4a6 Compare February 3, 2024 12:01
@orien orien marked this pull request as ready for review February 3, 2024 12:12
# 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
Copy link
Member Author

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
Copy link
Member Author

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.

Comment on lines +1 to +7
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')) %>
Copy link
Member Author

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.

Copy link
Contributor

@petervandoros petervandoros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! 👍

@orien orien merged commit de715c4 into master Feb 5, 2024
18 checks passed
@orien orien deleted the yaml-template-userdata branch February 5, 2024 03:27
Comment on lines +53 to +56
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
Copy link
Contributor

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?

Suggested change
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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proposed in #378.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants