Class: RuboCop::Cop::Chef::Correctness::ServiceResource

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/chef/correctness/service_resource.rb

Overview

Use the service resource to manage services instead of shelling out. The resource is idempotent, picks the right init system for the platform, and reports correctly on what it changed. A shelled out command runs on every converge whether or not anything needed to change, so the run reports the resource as updated every time.

Examples:


# bad
execute 'restart apache' do
  command 'systemctl restart httpd'
end

execute '/etc/init.d/httpd start'

# good
service 'httpd' do
  action :restart
end

Constant Summary collapse

MSG =
'Use the service resource to manage services instead of shelling out to an init script, service, systemctl, or a similar tool'
RESTRICT_ON_SEND =
i(command code execute).freeze
SERVICE_ACTIONS =

the state changing verbs. status is left out: a status check is usually a guard rather than something the service resource replaces

%w(
  start stop restart reload force-reload try-restart reload-or-restart
  enable disable mask unmask
).freeze
ACTION_COMMANDS =

commands whose service action is captured and checked against SERVICE_ACTIONS

[
  %r{\A/(?:etc/init\.d|etc/rc\.d|usr/local/etc/rc\.d)/\S+\s+(\S+)}, # /etc/init.d/httpd start
  %r{\A(?:/usr/sbin/|/sbin/)?service\s+\S+\s+(\S+)},                # service httpd start
  %r{\A(?:/usr/bin/|/bin/)?systemctl\s+(?:--\S+\s+)*(\S+)\s+\S+},   # systemctl start httpd
  /\Ainvoke-rc\.d\s+\S+\s+(\S+)/,                                   # invoke-rc.d httpd start
  /\Ainitctl\s+(\S+)\s+\S+/,                                        # initctl start httpd
  /\Asvcadm\s+(\S+)\s+\S+/,                                         # svcadm enable httpd
].freeze
DIRECT_COMMANDS =

commands that manage a service whatever the rest of the line says

[
  /\A(?:start|stop|restart)\s+\S+\z/,                               # upstart: start httpd
  /\Alaunchctl\s+(?:load|unload|start|stop|enable|disable|bootstrap|bootout)\s/,
  /\Achkconfig\s+(?:--\S+\s+)*\S+\s+(?:on|off)\z/,                  # chkconfig httpd on
  /\Aupdate-rc\.d\s/,                                               # update-rc.d httpd defaults
  /\A(?:sc|net)\s+(?:start|stop|config)\s+\S+/i,                    # windows: net start httpd
].freeze
SHELL_OPERATORS =

a command doing more than the one service action can't be swapped for the resource

['&&', '||', ';', '|', '`', '$('].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/rubocop/cop/chef/correctness/service_resource.rb', line 77

def on_send(node)
  command_property?(node) do |command|
    add_offense(command, severity: :refactor) if manages_service?(command.value)
  end

  execute_with_command_name?(node) do |command|
    add_offense(node, severity: :refactor) if manages_service?(command.value)
  end
end