-
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
Add lint and compile commands #245
Conversation
Make it easy to use cfn-lint on the templated stacks. The templated stack is written to a temporary file which cfn-lint is run on. Since the error messages contain the path to the json node, it's not that important to preserve the rendered json with matching lines. Unfortunately there's no special error code for success/failure. We can just forward the original output. Having cfn-lint is not a requirement for stack_master and the missing binary is handled without exploding.
Failure:
Success:
|
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.
Are we able to test this? Even if we stub out system call, just to make sure future changes to Stack class are caught :)
lib/stack_master/commands/lint.rb
Outdated
return | ||
end | ||
|
||
Tempfile.open(['stack', '.json']) do |f| |
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.
What if it's a YAML template?
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 was under the impression that proposed_stack.template_body will always be json. Is that incorrect?
If it can be yaml, that's supported too, just need to change the suffix.
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.
Yeah the template sent to CF is always JSON from StackMaster (even if the original template is YAML). Turns out YAML is sent through as is https://github.com/envato/stack_master/blob/master/lib/stack_master/template_compilers/yaml.rb
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.
AFAIK we support native YAML templates and we submit them as YAML to CloudFormation. I do not know though if template_body will always be json.
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'm not sure if this belongs in StackMaster. I imagine it'd be pretty easy to wire up using a compile command, which I realise now we don't have but I think it'd be a useful addition - stack_master compile region stack | cfn-lint
.
lib/stack_master/commands/lint.rb
Outdated
return | ||
end | ||
|
||
Tempfile.open(['stack', '.json']) do |f| |
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.
Yeah the template sent to CF is always JSON from StackMaster (even if the original template is YAML). Turns out YAML is sent through as is https://github.com/envato/stack_master/blob/master/lib/stack_master/template_compilers/yaml.rb
lib/stack_master/commands/lint.rb
Outdated
end | ||
|
||
Tempfile.open(['stack', '.json']) do |f| | ||
f.write(proposed_stack.template_body) |
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.
An alternative to using files could be to send the template as STDIN to cfn-lint (assuming it supports that).
I'd be happy with the compile command instead. cfn-lint doesn't do stdin, but you can always use I'd prefer the But I can go with consensus. Are the strong feelings about the approach either way? |
I'm not strongly opposed to including a |
👍 to |
Prevents sending incomplete file to cfn-lint
lib/stack_master/commands/lint.rb
Outdated
def perform | ||
unless cfn_lint_available | ||
puts "Failed to run cfn-lint, do you have it installed and available in $PATH?" | ||
return |
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 should exit with a non-zero exit code.
lib/stack_master/commands/lint.rb
Outdated
f.write(proposed_stack.template_body) | ||
f.flush | ||
system('cfn-lint', f.path).nil? | ||
puts "cfn-lint run complete" |
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.
Can we pass the exit code from cfn-lint back?
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.
cf-lint doesn't care :( it's always 0, whether it fails or not.
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.
Boo
lib/stack_master/commands/lint.rb
Outdated
Tempfile.open(['stack', ".#{proposed_stack.template_format}"]) do |f| | ||
f.write(proposed_stack.template_body) | ||
f.flush | ||
system('cfn-lint', f.path).nil? |
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.
What's the point of the nil check here?
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.
copy-pasta
Make it easy to use cfn-lint on the templated stacks. The templated
stack is written to a temporary file which cfn-lint is run on. Since
the error messages contain the path to the json node, it's not that
important to preserve the rendered json with matching lines.
Unfortunately there's no special error code for success/failure. We can
just forward the original output.
Having cfn-lint is not a requirement for stack_master and the missing
binary is handled without exploding.
Also add a
compile
command which outputs the processed stacktemplate for use in other tools.