class RuboCop::Cop::Chef::Modernize::ExecuteSysctl
Chef
Infra Client 14.0 and later includes a sysctl resource that should be used to idempotently load sysctl values instead of templating files and using execute to load them.
@example
### incorrect file '/etc/sysctl.d/ipv4.conf' do notifies :run, 'execute[sysctl -p /etc/sysctl.d/ipv4.conf]', :immediately content '9000 65500' end execute 'sysctl -p /etc/sysctl.d/ipv4.conf' do action :nothing end ### correct sysctl 'net.ipv4.ip_local_port_range' do value '9000 65500' end
Constants
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
Source
# File lib/rubocop/cop/chef/modernize/execute_sysctl.rb, line 59 def on_block(node) match_property_in_resource?(:execute, 'command', node) do |code_property| property_data = method_arg_ast_to_string(code_property) return unless property_data && property_data.match?(%r{^(/sbin/)?sysctl -p}i) add_offense(node, severity: :refactor) end end
block execute resources
Source
# File lib/rubocop/cop/chef/modernize/execute_sysctl.rb, line 51 def on_send(node) # use a regex on source instead of .value in case there's string interpolation which adds a complex dstr type # with a nested string and a begin. Source allows us to avoid a lot of defensive programming here return unless node&.arguments.first&.source&.match?(/^("|')sysctl -p/) add_offense(node, severity: :refactor) end
non block execute resources