Skip to main content

Chef/RedundantCode/ServiceGuardOnStopDisable

Cookstyle cops page

The Cookstyle cops department: Chef/RedundantCode

Enabled by defaultSupports autocorrectionTarget Chef Version
EnabledYesAll Versions

The :stop and :disable actions of the service resource are already no-ops when the service isn’t installed, so guarding them with a check for the init script or unit file adds nothing. The base provider only converges these actions when current_resource.running/current_resource.enabled is set, and the platform providers never set that state for a service they can’t find.

The guard is also actively harmful because it hardcodes a single path while the provider searches several. A guard on /usr/local/etc/rc.d/snmpd skips a FreeBSD host running the service out of /etc/rc.d, and the run then reports success while leaving the service enabled.

This cop only fires when every action is :stop or :disable. Actions like :start, :enable, :restart, and :reload are asserted against the init script, so a guard there is meaningful. The systemd_unit resource shells out unconditionally, so its guards are meaningful as well and are never flagged.

Examples

# bad
service 'snmpd' do
  action %i(stop disable)
  only_if { ::File.exist?('/usr/local/etc/rc.d/snmpd') }
end

# good
service 'snmpd' do
  action %i(stop disable)
end

Configurable attributes

NameDefault valueConfigurable values
Version Added9.0.0String
Include
    Array

    Thank you for your feedback!

    ×