Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ The format is based on [Keep a Changelog], and this project adheres to
### Changed

- Use GitHub Actions for the CI build instead of Travis CI ([#353]).
- Templates compiled with `cfndsl` have a pretty format ([#356]).
- Update `cfndsl` requirement from < 1.0 to ~> 1 ([#356]). The changes in
version 1 are potentially breaking for projects using `cfndsl` templates.

[Unreleased]: https://github.com/envato/stack_master/compare/v2.12.0...HEAD
[#353]: https://github.com/envato/stack_master/pull/353
[#356]: https://github.com/envato/stack_master/pull/356

## [2.12.0] - 2020-10-22

Expand Down
69 changes: 69 additions & 0 deletions features/compile_with_cfndsl.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
Feature: Compile command with a CfnDsl template

Scenario: Run compile stack on CfnDsl template
Given a file named "stack_master.yml" with:
"""
template_compilers:
rb: cfndsl
stacks:
us_east_1:
myapp_vpc:
template: myapp_vpc.rb
"""
And a directory named "parameters"
And a file named "parameters/myapp_vpc.yml" with:
"""
KeyName: my-key
compile_time_parameters:
cidr_block: 10.200.0.0/16
"""
And a directory named "templates"
And a file named "templates/myapp_vpc.rb" with:
"""
CloudFormation do
Description "Test template"

Parameter("KeyName") do
Description "Key name"
Type "String"
end

VPC(:Vpc) do
CidrBlock external_parameters[:CidrBlock]
end

Output(:VpcId) do
Description "A VPC ID"
Value Ref("Vpc")
end
end
"""
When I run `stack_master compile us-east-1 myapp-vpc`
Then the output should contain all of these lines:
| Executing compile on myapp-vpc in us-east-1 |
| "AWSTemplateFormatVersion": "2010-09-09", |
| "Description": "Test template", |
| "Parameters": { |
| "KeyName": { |
| "Type": "String" |
| "Description": "Key name" |
| } |
| }, |
| "Resources": { |
| "Vpc": { |
| "Properties": { |
| "CidrBlock": "10.200.0.0/16" |
| }, |
| "Type": "AWS::EC2::VPC" |
| } |
| }, |
| "Outputs": { |
| "VpcId": { |
| "Description": "A VPC ID", |
| "Value": { |
| "Ref": "Vpc" |
| } |
| } |
| } |
| } |
And the exit status should be 0
71 changes: 71 additions & 0 deletions features/compile_with_sparkle_formation.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Feature: Compile command with a SparkleFormation template

Scenario: Run compile stack on SparkleFormation template
Given a file named "stack_master.yml" with:
"""
stacks:
us_east_1:
myapp_vpc:
template: myapp_vpc.rb
"""
And a directory named "parameters"
And a file named "parameters/myapp_vpc.yml" with:
"""
KeyName: my-key
compile_time_parameters:
cidr_block: 10.200.0.0/16
"""
And a directory named "templates"
And a file named "templates/myapp_vpc.rb" with:
"""
SparkleFormation.new(:myapp_vpc,
compile_time_parameters: { cidr_block: { type: :string }}) do
description "Test template"

parameters.key_name do
description 'Key name'
type 'String'
end

resources.vpc do
type 'AWS::EC2::VPC'
properties do
cidr_block '10.200.0.0/16'
end
end

outputs.vpc_id do
description 'A VPC ID'
value ref!(:vpc)
end
end
"""
When I run `stack_master compile us-east-1 myapp-vpc`
Then the output should contain all of these lines:
| Executing compile on myapp-vpc in us-east-1 |
| { |
| "Description": "Test template", |
| "Parameters": { |
| "KeyName": { |
| "Description": "Key name", |
| "Type": "String" |
| } |
| }, |
| "Resources": { |
| "Vpc": { |
| "Type": "AWS::EC2::VPC", |
| "Properties": { |
| "CidrBlock": "10.200.0.0/16" |
| } |
| } |
| }, |
| "Outputs": { |
| "VpcId": { |
| "Description": "A VPC ID", |
| "Value": { |
| "Ref": "Vpc" |
| } |
| } |
| } |
| } |
And the exit status should be 0
2 changes: 1 addition & 1 deletion lib/stack_master/commands/compile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Compile
include Commander::UI

def perform
puts(proposed_stack.template_body)
StackMaster.stdout.puts(proposed_stack.template_body)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Print the compiled template to the StackMaster configured STDOUT.

This allows the printed content to be captured during Cucumber tests, so we can write assertions for it.

end

private
Expand Down
5 changes: 3 additions & 2 deletions lib/stack_master/template_compilers/cfndsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ module StackMaster::TemplateCompilers
class Cfndsl
def self.require_dependencies
require 'cfndsl'
require 'json'
end

def self.compile(template_dir, template, compile_time_parameters, _compiler_options = {})
CfnDsl.disable_binding
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This option has been removed in later versions of cfndsl.

CfnDsl::ExternalParameters.defaults.clear # Ensure there's no leakage across invocations
CfnDsl::ExternalParameters.defaults(compile_time_parameters.symbolize_keys)
template_file_path = File.join(template_dir, template)
::CfnDsl.eval_file_with_extras(template_file_path).to_json
json_hash = ::CfnDsl.eval_file_with_extras(template_file_path).as_json
JSON.pretty_generate(json_hash)
Comment on lines -12 to +13
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Use a pretty format for the compiled template. This is consistent with how we compile Sparkle Formation templates.

end

StackMaster::TemplateCompiler.register(:cfndsl, self)
Expand Down
2 changes: 1 addition & 1 deletion lib/stack_master/template_compilers/sparkle_formation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def self.compile(template_dir, template, compile_time_parameters, compiler_optio
sparkle_template.compile_state = create_state(definitions, compile_time_parameters)
end

JSON.pretty_generate(sparkle_template)
JSON.pretty_generate(sparkle_template.dump)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I noticed the compiled SparkleFormation template wasn't always pretty formatted during Cucumber tests.

We avoid this problem by dumping the template to a hash before converting it to a JSON string.

end

private
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/templates/rb/cfndsl/sample-ctp-repeated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Output(:One,FnBase64( Ref("One")))

EC2_Instance(:MyInstance) {
DisableApiTermination external_parameters["DisableApiTermination"]
DisableApiTermination external_parameters.fetch(:DisableApiTermination, "false")
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In V1, cfndsl doesn't allow compile time parameters to be missing. We need to provide a default.

InstanceType external_parameters["InstanceType"]
ImageId "ami-12345678"
}
Expand Down
2 changes: 1 addition & 1 deletion spec/stack_master/template_compilers/cfndsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def compile
it 'does not leak compile time params across invocations' do
expect {
compile_time_parameters.delete("DisableApiTermination")
}.to change { JSON.parse(compile)["Resources"]["MyInstance"]["Properties"]["DisableApiTermination"] }.from('true').to(nil)
}.to change { JSON.parse(compile)["Resources"]["MyInstance"]["Properties"]["DisableApiTermination"] }.from('true').to('false')
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Test the default is used, when the compile time parameter is not provided.

end
end
end
Expand Down
2 changes: 1 addition & 1 deletion stack_master.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "sparkle_formation", "~> 3"
spec.add_dependency "table_print"
spec.add_dependency "deep_merge"
spec.add_dependency "cfndsl", "< 1.0"
spec.add_dependency "cfndsl", "~> 1"
spec.add_dependency "multi_json"
spec.add_dependency "hashdiff", "~> 1"
spec.add_dependency "ejson_wrapper"
Expand Down