-
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
funtion to get pgsql version, exposed version, cluster and fixed session #1758
Conversation
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 seems like a reasonable approach. I left some comments with ways we can clean up the code and reduce some complexity.
This sort of behavior change where we trust psql
's output before the actual data dir path is something we should document both in the resource itself (in the desc
section so it shows up in the help postgres
output) but also in the website docs. If someone has an old client in their path but a later-version data directory, they would get unexpected results.
Also, we need to add some tests. It looks like we don't have any for the postgres
resource today, so starting with the new method you've created is a fine start.
lib/resources/postgres.rb
Outdated
cluster = cluster_from_dir("/etc/postgresql/#{version}") | ||
@conf_dir = "/etc/postgresql/#{version}/#{cluster}" | ||
@data_dir = "/var/lib/postgresql/#{version}/#{cluster}" | ||
@version = version_from_psql.to_s |
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.
Why is to_s
necessary 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.
I suppose not, just being pedantic perhaps, ok we can remove it - I know it will return a string but just making sure. :)
lib/resources/postgres.rb
Outdated
@conf_dir = "/etc/postgresql/#{version}/#{cluster}" | ||
@data_dir = "/var/lib/postgresql/#{version}/#{cluster}" | ||
@version = version_from_psql.to_s | ||
@version = version_from_dir('/etc/postgresql') if @version.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.
This line and the line above it can be shortened to:
@version = version_from_psql || version_from_dir('/etc/postgresql')
lib/resources/postgres.rb
Outdated
else | ||
version_from_dir('/var/lib/pgsql/') | ||
end | ||
@version = version_from_psql.to_s |
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.
It looks like we can shorten this whole section to:
@version = version_from_psql || version_from_dir('/var/lib/pgsql/data') || version_from_dir('/var/lib/pgsql')
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.
well we wouldn't run version_from_dir on /var/lib/pgsql/data, it is just checking if that dir exists. If it does, it was assumed that means the version structure isn't the standard of /var/lib/pgsql/'version'/data thus we could not find the version number via the directories so it was just let unset.
Using the new function we will always get a version so this logic really don't need to be there any more, so we can shorten it. The else in this cases was to say if 'var/lib/pgsql/data' doesn't exist - we assume that we can run version from dir on '/var/lib/pgsql' and assume to find the ver dir waiting for us. I will see about cleaning up the logic.
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.
Oh yes, I remember why I left this. I updated the 'warn' statement to clarify why the logic is here.
If psql was not installed, the check for version.nil? also ensure that we still try to use the filesystem assuming we don't have an unversioned directory structure.
026be87
to
4d46847
Compare
@adamleff I completed the requested changes per your review. Are we good to go on this one? |
@adamleaf we good to go on this one as well?
…On May 11, 2017 09:28, "Aaron Lippold" ***@***.***> wrote:
@note <https://github.com/note>: this will also close #1551
<#1551>
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1758 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABauaMvwVKGTo9FS-YF4RrSBLsJ1GwVCks5r4wz-gaJpZM4NQG7D>
.
|
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.
Real close, @aaronlippold - just a couple more things I think we need to address.
lib/resources/postgres.rb
Outdated
@version = version_from_psql | ||
if @version.nil? | ||
if inspec.directory('/var/lib/pgsql/data').exist? | ||
warn 'PSQL did not return a version number and found /var/lib/pgsql/data. |
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'd personally get rid of this warning statement. I'm not sure it adds any value to the user.
If we choose to leave it, we should change the wording because an end-user shouldn't need to worry about what a "nil" is, etc.
Suggested wording:
Unable to determine PostgreSQL version: psql did not return a version number and unversioned data directories were found.
But my recommendation is to eliminate it. See below for another commend on exposing the detection mechanism to the users if that's the problem we're trying to solve.
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.
Ok. Agreed, just trying to not modify what I did not create but given that finding the version is much more stable using this method the warns may be unneeded.
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.
Always leave something better than when you found it. :) If that means modifying someone else's code, that's just fine... in fact, you're doing that with this PR anyways. :)
lib/resources/postgres.rb
Outdated
@@ -59,7 +55,7 @@ def initialize | |||
@data_dir = '/var/lib/pgsql/data' | |||
end | |||
|
|||
@service = 'postgresql' | |||
@service = 'postgresql-' + @version if @version.to_f >= 9.4 |
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 @version
is < 9.4? Then @service
will never get set.
This probably needs to be:
@service = 'postgresql'
@service += "-#{@version}" if @version.to_f >= 9.4
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.
Ok. I think this works as well. I can update that.
@@ -81,6 +77,16 @@ def verify_dirs | |||
end | |||
end | |||
|
|||
def version_from_psql |
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 don't think we need the warn here. I'd recommend we simplify this to:
return unless inspec.command('psql').exist?
inspec.command("psql --version | awk '{ print $NF }' | awk -F. '{ print $1\".\"$2 }'").stdout.strip
Ultimately if we need users to know how we determined how we found the version, we can create a method they can assert a value on... i.e.:
describe postgres do
its('detection_mechanism') { should eq 'psql' }
its('detection_mechanism') { should eq 'versioned_data_directory' }
its('detection_mechanism') { should eq 'unversioned_data_directory' }
end
I think the warnings clutter up the output too much, and for users using a structured formatter (i.e. junit, json, etc.), they have to worry about redirecting stderr out of the way.
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.
Ok. I was just following the previous convention from the code before I modified it. If you think the warn statements are no longer needed then I am fine with removing them. I just left them because @arlimus had them there before.
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.
These changes have been pushed.
4d46847
to
eeef5d1
Compare
I think the logic for Redhat is broken. I just spun up a new CentOS 7.3 box and
But we assume the version is part of the data directory path:
Looks like this needs some rework. |
@aaronlippold per our conversation, you're going to go back and refactor this to try to determine the correct directory but only return it if it actually exists, avoiding the situation where we get the version but the directory isn't named with the version.
|
2044937
to
e09476d
Compare
@adamleff added a function that will have a list of '@data_dir' locations. At the moment this is just used by the 'redhat' case but we could move this up so that all platforms could use it in a more general way. i.e. we could just make the verify_data_dir_location take an Array and:
|
@adamleff something like this:
This would also make it so we can drop all the platform family if, else statements as we would check them all anyway. The only thing that could be a problem with this approach is I am not sure what would happen if two installations of postgres were on the system. |
8798866
to
d4c88ae
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.
@aaronlippold I think we're a lot closer. See my comments on things I think we need to tidy up for cleanliness and consistency, then I think we're good.
lib/resources/postgres.rb
Outdated
a version number and unversioned data directories were found.' | ||
nil | ||
else | ||
version_from_dir('/var/lib/pgsql/') |
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.
Shouldn't this be:
@version = version_from_dir('/var/lib/pgsql/')
... #version_from_dir
doesn't actually set @version
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.
Yes most likely but we most likely missed it because we don't usually get to that branch of the tree in our tests. I updated it after ensuring that each case would return a number.
lib/resources/postgres.rb
Outdated
version_from_dir('/var/lib/pgsql/') | ||
end | ||
end | ||
verify_data_dir_location(@version) |
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'd like to see us remain consistent within a resource whether helper functions manipulate instance variables, or whether they return data to the caller who then decides whether to update an instance variable.
I prefer that methods do NOT manipulate instance variables because it makes it harder to test them and also could unknowingly pollute other tests.
I'd like to see this be:
@data_dir = verify_data_dir_locaton(@version)
... and have #verify_data_dir_location
changed to #locate_data_dir_location_by_version
and have it return a string containing the validated data directory.
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.
Ok. I switched it so that all the helper functions just set an instance var externally rather than internally and updated the function name.
lib/resources/postgres.rb
Outdated
inspec.command("psql --version | awk '{ print $NF }' | awk -F. '{ print $1\".\"$2 }'").stdout.strip | ||
end | ||
|
||
def verify_data_dir_location(ver = @version) |
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.
See comment above re: keeping this helper method isolated rather than having it modify instance variables. That will make it easier to write tests and will remain consistent with the other functions in this resource, such as version_from_psql
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.
the @Version instance var is not modified it is just used in the loop of the dir list, this function now just returns the instance var is it supporting rather than modifying it directly.
d4c88ae
to
7ca8339
Compare
included requested /var/lib/postgresql/data to cover inspec#1673 as well Signed-off-by: Aaron Lippold <[email protected]>
7ca8339
to
3c02de9
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.
Amazing work and great to have this detection in here. Thank you Aaron!!
Also thank you @adamleff for helping and improving this PR :)
Signed-off-by: Aaron Lippold [email protected]