Skip to content
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

service resource: attempt a SysV fallback if SystemD unit file is not found #2473

Merged
merged 3 commits into from Jan 23, 2018

Conversation

jerryaldrichiii
Copy link
Contributor

This modifies the enabled? check to fallback to sysv_service in the event that a .service file cannot be found.

For example: On Debian 8.7 the stock apache2 package does not deploy a .service file but deploys a SysV style service. This causes systemctl is-enabled to fail when the service is in fact enabled.

This closes #1708

This modifies the `enabled?` check to fallback to `sysv_service` in the
event that a `.service` file cannot be found.

For example: On Debian 8.7 the stock apache2 package does not deploy a
`.service` file but deploys a SysV style service. This causes
`systemctl is-enabled` to fail when the service is in fact enabled.

Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>
@jerryaldrichiii jerryaldrichiii requested a review from a team as a code owner January 20, 2018 00:38
@adamleff adamleff changed the title service resource: Fix no .service + systemd bug service resource: attempt a SysV fallback if SystemD unit file is not found Jan 23, 2018
Copy link
Contributor

@adamleff adamleff left a comment

Choose a reason for hiding this comment

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

Functionally looks good, @jerryaldrichiii. I think we can cleanup the test helper before merging though.

test/helper.rb Outdated
@@ -195,6 +195,11 @@ def md.directory?
mock.mock_command('', '', '', 0)
}

cmd_stderr = lambda { |x|
stderr = ::File.read(::File.join(scriptpath, '/unit/mock/cmd/'+x))
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need to do string concatenation or interpolation when we're already in a File.join call. It's also customary to remove the leading and trailing slashes and let File.join do its thing:

stderr = ::File.read(::File.join(scriptpath, 'unit/mock/cmd', x))

I also question whether we really need another helper here, especially when this is also doing an exit 1 like cmd_exit_1 is already doing. Should we just enhance cmd_exit_1 to accept an optional path to stderr output?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with all those points! Update incoming.

Thanks for your always stellar reviews!

Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>
test/helper.rb Outdated
stderr = ::File.read(::File.join(scriptpath, '/unit/mock/cmd/'+x))
mock.mock_command('', '', stderr, 1)
cmd_exit_1 = lambda { |x = nil|
stderr = ::File.read(::File.join(scriptpath, 'unit/mock/cmd', x)) if x
Copy link
Contributor

Choose a reason for hiding this comment

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

You might think I'm nitpicking here, but I think we can eliminate the conditional in the following line and just let stderr always be the right thing (rather than mock_command having another in-line conditional):

stderr = x.nil? ? '' : ::File.read(::File.join(scriptpath, 'unit/mock/cmd', x))
mock.mock_command('', '', stderr, 1)

That's a bit more direct and clear, would you agree?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, I agree, elegant use of the ternary. Not nitpicky at all! 😄

Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>
@adamleff adamleff added the Type: Bug Feature not working as expected label Jan 23, 2018
Copy link
Contributor

@adamleff adamleff left a comment

Choose a reason for hiding this comment

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

LGTM, @jerryaldrichiii - thanks!

Copy link
Contributor

@arlimus arlimus left a comment

Choose a reason for hiding this comment

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

Great handling of a corner-case, love the fallback @jerryaldrichiii thank you!

@arlimus arlimus merged commit 9854698 into master Jan 23, 2018
@arlimus arlimus deleted the ja/fix-systemd-debian branch January 23, 2018 20:34
@tas50
Copy link
Contributor

tas50 commented Jan 25, 2018

@jerryaldrichiii You've solved my #1 inspec bug. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Feature not working as expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

systemctl is-enabled fails on Debian for SysV units
4 participants