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

Add support for user data files in sparkleformation templates #117

Merged
merged 15 commits into from
Jul 15, 2016

Conversation

stevehodgkiss
Copy link
Contributor

@stevehodgkiss stevehodgkiss commented Jul 14, 2016

Based on the code in this comment sparkleformation/sparkle_formation#54 (comment)

With this change, you can write the following in templates:

  user_data user_data_file!('web.erb', role: :web)

It will look in templates/user_data/* for user data files, read the template and compile it with each line being a separate string in the final array. The files can look like this and make use of SF helpers:

 #!/bin/bash

REGION=<%= region! %>
ROLE=<%= has_var?(:role) ? role : 'hmm' %>
echo $REGION

Renders like this:

+        "UserData": {
+          "Fn::Base64": {
+            "Fn::Join": [
+              "",
+              [
+                "#!/bin/bash\n",
+                "\n",
+                "REGION=",
+                {
+                  "Ref": "AWS::Region"
+                },
+                "ROLE=web",
+                "\n",
+                "echo $REGION\n"
+              ]
+            ]
+          }
+        }
+

Ref: #104

@stevehodgkiss stevehodgkiss changed the title Add support for user data files in sparkle formation Add support for user data files in sparkleformation Jul 14, 2016
@stevehodgkiss stevehodgkiss force-pushed the user_data_file_sparkle_support branch from cab5071 to aede9bf Compare July 14, 2016 07:39
def add_expr(src, code, indicator)
case indicator
when '='
src << " #{@bufvar} << (" << code << ');'
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure what this means.

Copy link
Contributor

@taonic taonic Jul 15, 2016

Choose a reason for hiding this comment

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

Ok, I think I have a rough idea what this line does. But I wonder if this can be simplified a bit as the brackets could be redundant in the following evaluation, e.g.

src << " #{@bufvar} << #{code};"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That breaks the test for custom vars, instead of returning the variables value it returns true.

       -"Fn::Base64" => {"Fn::Join"=>["", ["#!/bin/bash\n", "test_var", "\n", "yes", "\n"]]},
       +"Fn::Base64" => {"Fn::Join"=>["", ["#!/bin/bash\n", "test_var", "\n", true, "\n"]]},

@stevehodgkiss stevehodgkiss force-pushed the user_data_file_sparkle_support branch from f7cb323 to 01d33a1 Compare July 14, 2016 08:21
@stevehodgkiss stevehodgkiss changed the title Add support for user data files in sparkleformation Add support for user data files in sparkleformation templates Jul 14, 2016
@simpsora
Copy link
Contributor

I'm pretty excited for this! I haven't tested it out yet (perhaps tomorrow), but it looks very promising.

👍 on green.

@patrobinson
Copy link
Contributor

Should this be a separate Gem? I think this would be useful when using plain Sparkle without stack_master.

That being said, I like this approach. 👍

@stevehodgkiss stevehodgkiss force-pushed the user_data_file_sparkle_support branch from dabee46 to ab915ca Compare July 15, 2016 05:23
end
end

class TemplateContext < AttributeStruct
Copy link
Contributor

@taonic taonic Jul 15, 2016

Choose a reason for hiding this comment

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

If we are monkey patching SparkleFormation::SparkleAttribute::Aws why can't we also monkey patch SparkleFormation::SparkleStruct. That way we don't have to define this TempalteContext just for erubis evaluation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why would that be better? Variables require defining methods, so this throw away class means no side effects between multiple user data evaluations in the same template/process, if we patched SF then we'd need to clean those up so I prefer this approach instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hum.. that actually make good sense.

@stevehodgkiss
Copy link
Contributor Author

Made some updates, added custom vars. I think this is ready to be merged now.

@patrobinson yeah it could be a separate gem but it could also live in SparkleFormation itself. For now I'll leave it here.

@ejoubaud
Copy link
Contributor

LGTM

@stevehodgkiss stevehodgkiss force-pushed the user_data_file_sparkle_support branch from 98ebded to 3113822 Compare July 15, 2016 10:58
@simpsora
Copy link
Contributor

👍, I'm pretty happy with this. Nice job @stevehodgkiss!

Now let's get to work on command hooks and sync_master :-D

@stevehodgkiss stevehodgkiss merged commit c6ab289 into master Jul 15, 2016
@stevehodgkiss stevehodgkiss deleted the user_data_file_sparkle_support branch July 15, 2016 11:15
@taonic
Copy link
Contributor

taonic commented Jul 15, 2016

Belated 👍 Nice one!

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.

None yet

5 participants