Class: RuboCop::Cop::Chef::ChefCorrectness::ServiceResource

Inherits:
RuboCop::Cop
  • 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


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

Constant Summary collapse

MSG =
'Use a service resource to start and stop services'.freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



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

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

#starts_service?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


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

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