Chef/RedundantCode/ServiceGuardOnStopDisable
The Cookstyle cops department: Chef/RedundantCode
| Enabled by default | Supports autocorrection | Target Chef Version |
|---|---|---|
| Enabled | Yes | All 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
| Name | Default value | Configurable values |
|---|---|---|
| Version Added | 9.0.0 | String |
| Include | Array |