-
Notifications
You must be signed in to change notification settings - Fork 681
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 wildcard/multiple server support to nginx_conf resource #2141
Add wildcard/multiple server support to nginx_conf resource #2141
Conversation
Signed-off-by: Jerry Aldrich <[email protected]>
lib/resources/nginx_conf.rb
Outdated
.map { |path| parse_nginx(path) } | ||
.map { |e| data.merge!(e) } | ||
.map { |e| data.merge!(e) { |_, v1, v2| v1 + v2 } } |
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 feel like this is wrong...just not sure how to make it better. When loading server configs from other files (e.g. /etc/nginx/conf.d/*.conf
) the merge!
would clobber the server
value and replace it. This appends the list to the key...but that assumes that v1
and v2
will always be lists...I don't like that assumption.
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.
should probably be an each
; also let's extract this piece as it feels to cover some complexity (for deep merging)
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.
By "lists" to you mean Array
s? Why wouldn't they be? If that's a concern, how do we protect against it?
We own the entire codepath here, so if there's additional promises that need to be made, we can make them.
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 looks like a great first start.
I left a note about adding additional test coverage by having it find multiple files. We should make sure when it counters multiple files that it properly iterates of each.
We also need to expand the test coverage to ensure that data from each file is present. So if your sample files include multiple servers, we should have a test that is able to pluck unique data out of each server to ensure we're properly including all data and not accidentally clobbering another server record. That should help raise the confidence level of this change.
@@ -0,0 +1 @@ | |||
/etc/nginx/conf.d/example.conf |
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.
For completeness of testing, I'd change this to return multiple files to ensure the code actually iterates over each of them.
lib/resources/nginx_conf.rb
Outdated
.map { |path| parse_nginx(path) } | ||
.map { |e| data.merge!(e) } | ||
.map { |e| data.merge!(e) { |_, v1, v2| v1 + v2 } } |
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.
By "lists" to you mean Array
s? Why wouldn't they be? If that's a concern, how do we protect against it?
We own the entire codepath here, so if there's additional promises that need to be made, we can make them.
lib/resources/nginx_conf.rb
Outdated
end | ||
|
||
# Walk through the remaining hash fields to find more references | ||
Hash[data.map { |k, v| [k, resolve_references(v, rel_path)] }] | ||
end | ||
|
||
# Merge conf into data list of parameters. |
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.
Needs better docs string 😁
Signed-off-by: Dominik Richter <[email protected]>
312d45c
to
839bcfe
Compare
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.
Agree with Adam's multi-file test case.
Otherwise this looks fantastic, thank you so much!!
- Add multiple mocked files in `/etc/nginx/conf.d` - Add multiple server definitions in file selected by wildcard - Modify comments in @dominik's addition Signed-off-by: Jerry Aldrich <[email protected]>
f7474b2
to
114b26e
Compare
Signed-off-by: Jerry Aldrich <[email protected]>
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.
Thank you for the great updates!!
lib/resources/nginx_conf.rb
Outdated
# Step through all conf items and create combined return value | ||
data.merge!(conf) do |_, v1, v2| | ||
# If both the data field and the conf field are arrays, then combine them | ||
next v1 + v2 if v1.is_a?(Array) && v2.is_a?(Array) |
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.
Maybe it's just me, but this use of next
feels pretty awkward. Any chance I can convince you guys to make this an if ... elsif ... else
instead? I think it would read so much cleaner.
I'd really love to see some tests actually check the data we've parsed, not just the count of objects we expect. It can be done in a later PR though. |
905af36
to
b074bc4
Compare
- Make tests more descriptive by checking parsed content - Refactor `next` logic to use if/elsif/else Signed-off-by: Jerry Aldrich <[email protected]>
b074bc4
to
e055bcc
Compare
@@ -93,13 +96,37 @@ def resolve_references(data, rel_path) | |||
if data.key?('include') | |||
data.delete('include').flatten | |||
.map { |x| File.expand_path(x, rel_path) } | |||
.map { |x| find_files(x) }.flatten |
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.
Thank you for the fix Jerry.
However this solution does not always include files (included with '*') because of issue#2135 (find_files uses the command resource).
I just tested this code over an ssh connection and it does not parse all the included conf files;
It works fine when I tested by changing the find_files code to use bash resource instead of command resource.
Hope you can find a solution for it.
Thank you again Jerry,
Rony
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 know we have a logged issue about the command resource specifically, but we should log an issue for the find_files
method too so we don't lose that context, and link it to this PR. I'm not going to hold off merging this PR since it's still a step in the right direction and will work fine for users not executing via sudo.
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.
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.
Haha sorry @jerryaldrichiii .
Thank you @adamleff I have posted and issue for find_files. issue #2157
Fixes #2035
Fixes #1998