-
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
filesystem resource: inspect linux filesystems #2441
Conversation
pulling chef:master
Signed-off-by: Vern Burton <[email protected]>
Signed-off-by: Vern Burton <[email protected]>
Signed-off-by: Vern Burton <[email protected]>
Signed-off-by: Vern Burton <[email protected]>
Signed-off-by: Vern Burton <[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.
Great first pass, @tarcinil! Left you a bunch of comments, let me know if anything is unclear.
lib/resources/df.rb
Outdated
|
||
def initialize(partition) | ||
@partition = partition | ||
@cmd = inspec.command("df #{@partition} --output=size | sed \"/blocks/d; s/ *//g\"") |
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.
Since you made partition
an attr_reader
(and it's available as partition
as a method argument), you can drop the @
here.
lib/resources/df.rb
Outdated
|
||
def initialize(partition) | ||
@partition = partition | ||
@cmd = inspec.command("df #{@partition} --output=size | sed \"/blocks/d; s/ *//g\"") |
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 there's any value in storing the command in an instance variable since it's only ever used in the size
method... other than to perhaps prevent the command from being executed twice.
Instead, we can memoize the size value in the size
method to avoid this. Example below.
lib/resources/df.rb
Outdated
|
||
def initialize(partition) | ||
@partition = partition | ||
@cmd = inspec.command("df #{@partition} --output=size | sed \"/blocks/d; s/ *//g\"") |
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.
--output=size
is not a globally available flag. For example, it doesn't work on macOS or FreeBSD, to my knowledge.
We need to use the new supports
keyword here to limit this resource to only executing on the operation systems on which this is supported. I would recommend we start with the "linux" family to start. @jquick, could you chime in here and help @tarcinil with the proper supports
call that will accomplish this?
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 unable to find an existing example of supports
as it pertained to resources.
I used a pre-existing pattern of inspec.os.linux?
which appears to depend on chef/train in the background.
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 have found the supports?
/supported?
functions from lib/inspec/plugins/resource.rb
and lib/resources/platform.rb
, but I am unable to find documentation that would allow me to implement it. I await implementation details from @jquick.
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.
Hey @tarcinil. You are correct that the documentation is lacking for the new supports features for resources, this will be addressed. This was implemented via #1661 (section 4). For this resource you will want to add something like:
supports os_family: 'linux'
After you set the 'name' on line 3 in your new resource. This will skip any tests using this resource unless the platform is a linux base.
lib/resources/df.rb
Outdated
end | ||
|
||
def size | ||
@cmd.stdout.to_i |
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.
So, rather than storing the command
object in an instance var, I'd change this to something like this:
@size ||= begin
cmd = inspec.command("df #{@partition} --output=size | sed \"/blocks/d; s/ *//g\"").stdout
raise Inspec::Exceptions::ResourceFailed, "Unable to get available space for partition #{partition}" if cmd.stdout.nil? || cmd.stdout.empty? || !cmd.exit_status.zero?
cmd.stdout.to_i
end
This will ensure the command is only run once (as we'll cache the size in an instance variable) and also does some additional error handling.
I'd also suggest just getting the raw output from the command and doing the string manipulation/parsing in Ruby instead of piping to sed
.
lib/resources/df.rb
Outdated
end | ||
|
||
def to_s | ||
"DiskSpace #{@partition}" |
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.
Same here - can just change this to #{partition}
thanks to it being made into an attr_reader
test/unit/resources/df_test.rb
Outdated
@@ -0,0 +1,22 @@ | |||
# encoding: utf-8 | |||
# author: Christoph Hartmann |
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.
You can remove these two author lines.
Signed-off-by: Vern Burton <[email protected]>
Signed-off-by: Vern Burton <[email protected]>
* removing author lines * using attr_reader functions * using ruby string functions rather than pipe to sed * adding os family detection * using ResourceFailed as the pattern already existed for OS family detection * using if for future case support for unix and unix-like (FreeBSD) Signed-off-by: Vern Burton <[email protected]>
… resource says that it is not supported on windows/unix. Signed-off-by: Vern Burton <[email protected]>
@adamleff Comments have been addressed, ready for another review. |
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 is looking really solid, @tarcinil! One last small fix and we'll be good to go, I think.
lib/resources/df.rb
Outdated
if inspec.os.linux? | ||
command = "df #{partition} --output=size" | ||
else | ||
raise Inspec::Exceptions::ResourceFailed, 'The `df` resource is not supported on your OS.' |
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 supports
stuff you added takes care of this for you so you can remove all of this logic and just declare your command. 🙂
And you should move the command into the @size
memoifier so it looks something like this and is the first line of the size
method:
@size ||= begin
cmd = inspec.command("df #{partition} --output=size")
raise ...
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 thought as much as well, but would it be nice to have for future support of other OS families?
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.
Sure, but if we're not going to account for that in this PR, we should leave it out.
If we add future family support, I'd recommend doing two things:
- Flag the resource for the additional family support using the
supports
method - Change the line to:
cmd = inspec.command(df_command_for_family)
... and supply a new df_command_for_family
method that returns a command string appropriate for the node under test.
Signed-off-by: Vern Burton <[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.
Excellent work, @tarcinil!
df
resource
Hey @tarcinil - one last thing, and I'm sorry I didn't think about this sooner. I think we should rename this resource to Also, if we switch to use a different method of inspecting filesystem properties other than the What do you think? |
@adamleff, I think that is a good argument. I will look at making the changes on Tuesday most likely. |
I have time to fix this tonight. It has been a busy week between getting engaged and snowmageddon 2018. |
No problem, @tarcinil! Hope you're surviving the snow... no rush on this, and I thank you again for your contribution! |
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.
Excellent work, @tarcinil! Thanks for introducing this new resource. I'm sure we'll be able to continue adding to it in the future.
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 @tarcinil
fixes #1831
adding df resource
I do convert the
cmd.stdout
to an integer. I believe that as a full-blown resource, that pattern was better rather than relying oncmp
to deal with it.