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

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

Overview

Use a service resource to start and stop services

Examples:

when command starts a service


### incorrect
command "/etc/init.d/mysql start"
command "/sbin/service/memcached start"

Constant Summary collapse

MSG =
'Use a service resource to start and stop services'
RESTRICT_ON_SEND =
[:command].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/rubocop/cop/chef/correctness/service_resource.rb', line 37

def on_send(node)
  execute_command?(node) do |command|
    if starts_service?(command)
      add_offense(command, severity: :refactor)
    end
  end
end

#starts_service?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
# File 'lib/rubocop/cop/chef/correctness/service_resource.rb', line 45

def starts_service?(cmd)
  cmd_str = cmd.to_s
  (cmd_str.include?('/etc/init.d') || ['service ', '/sbin/service ',
                                       'start ', 'stop ', 'invoke-rc.d '].any? do |service_cmd|
     cmd_str.start_with?(service_cmd)
   end) && %w(start stop restart reload).any? { |a| cmd_str.include?(a) }
end