class Chef::Resource::Sysctl

Public Instance Methods

after_created() click to toggle source
# File lib/chef/resource/sysctl.rb, line 52
def after_created
  raise "The sysctl resource requires Linux as it needs sysctl and the sysctl.d directory functionality." unless node["os"] == "linux"
  raise "The sysctl resource does not support SLES releases less than 12 as it requires a sysctl.d directory" if platform_family?("suse") && node["platform_version"].to_i < 12
end
coerce_value(v) click to toggle source
# File lib/chef/resource/sysctl.rb, line 57
def coerce_value(v)
  case v
  when Array
    v.join(" ")
  else
    v.to_s
  end
end
set_sysctl_param(key, value) click to toggle source
# File lib/chef/resource/sysctl.rb, line 115
def set_sysctl_param(key, value)
  shell_out!("sysctl #{'-e ' if new_resource.ignore_error}-w \"#{key}=#{value}\"")
end

Private Instance Methods

get_sysctl_value(key) click to toggle source

shellout to sysctl to get the current value ignore missing keys by using '-e' convert tabs to spaces since sysctl tab deliminates multivalue parameters strip the newline off the end of the output as well

Chef creates a file in sysctld with parameter configuration Thus this config will persists even after rebooting the system User can be in a half configured state, where he has already updated the value which he wants to be configured from the resource Therefore we need an extra check with sysctld to ensure a correct idempotency

# File lib/chef/resource/sysctl.rb, line 133
def get_sysctl_value(key)
  val = shell_out!("sysctl -n -e #{key}").stdout.tr("\t", " ").strip
  raise unless val == get_sysctld_value(key)
  val
end
get_sysctld_value(key) click to toggle source

Check if chef has already configured a value for the given key and return the value. Raise in case this conf file needs to be created or updated

# File lib/chef/resource/sysctl.rb, line 142
def get_sysctld_value(key)
  raise unless ::File.exist?("/etc/sysctl.d/99-chef-#{key.tr('/', '.')}.conf")
  k, v = ::File.read("/etc/sysctl.d/99-chef-#{key.tr('/', '.')}.conf").match(/(.*) = (.*)/).captures
  raise "Unknown sysctl key!" if k.nil?
  raise "Unknown sysctl value!" if v.nil?
  v
end