summaryrefslogtreecommitdiff
path: root/lib/chef/resource/sysctl.rb
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2018-03-21 13:45:20 -0700
committerTim Smith <tsmith@chef.io>2018-03-21 13:45:20 -0700
commit68ea89b6e7d8e67de61a2aa5990c8aa232c3b1fe (patch)
treecea78675f4562d502cd8e887ba68b78944ce22e0 /lib/chef/resource/sysctl.rb
parenta3cb577ebfa05da077f76c6e7474fa394e7f8dc3 (diff)
downloadchef-68ea89b6e7d8e67de61a2aa5990c8aa232c3b1fe.tar.gz
Rename systctl_param to sysctl
Signed-off-by: Tim Smith <tsmith@chef.io>
Diffstat (limited to 'lib/chef/resource/sysctl.rb')
-rw-r--r--lib/chef/resource/sysctl.rb124
1 files changed, 124 insertions, 0 deletions
diff --git a/lib/chef/resource/sysctl.rb b/lib/chef/resource/sysctl.rb
new file mode 100644
index 0000000000..a3d684ef2f
--- /dev/null
+++ b/lib/chef/resource/sysctl.rb
@@ -0,0 +1,124 @@
+#
+# Copyright:: 2018, Webb Agile Solutions Ltd.
+# Copyright:: 2018, Chef Software Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require "chef/resource"
+
+class Chef
+ class Resource
+ class Sysctl < Chef::Resource
+ resource_name :sysctl
+ provides :sysctl
+ provides :sysctl_param
+
+ description "Use the sysctl resource to set kernel parameters using the sysctl"\
+ " command line tool and configuration files in the system's sysctl.d directory."\
+ "Configuration files managed by this resource are named 99-chef-KEYNAME.conf. If"\
+ " an existing value was already set for the value it will be backed up to the node"\
+ " and restored if the :remove action is used later."
+
+ introduced "14.0"
+
+ property :key, String,
+ description: "",
+ name_property: true
+
+ property :ignore_error, [TrueClass, FalseClass],
+ description: "",
+ default: false
+
+ property :value, [Array, String, Integer, Float],
+ description: "",
+ coerce: proc { |v| coerce_value(v) },
+ required: true
+
+ property :conf_dir, String,
+ description: "",
+ default: "/etc/sysctl.d"
+
+ def after_created
+ raise "The systctl resource requires Linux as it needs sysctl and the systctl.d directory functionality." unless node["os"] == "linux"
+ raise "The systctl resource does not support SLES releases less than 12 as it requires a systctl.d directory" if platform_family?("suse") && node["platform_version"].to_i < 12
+ end
+
+ def coerce_value(v)
+ case v
+ when Array
+ v.join(" ")
+ else
+ v.to_s
+ end
+ end
+
+ def get_sysctl_value(key)
+ o = shell_out("sysctl -n -e #{key}")
+ raise "Unknown sysctl key!" if o.error!
+ o.stdout.to_s.tr("\t", " ").strip
+ end
+
+ load_current_value do
+ value get_sysctl_value(key)
+ if node.normal["sysctl"]["backup"][key].empty?
+ node.normal["sysctl"]["backup"][key] = value
+ end
+ end
+
+ action :apply do
+ converge_if_changed do
+ # set it temporarily
+ set_sysctl_param(new_resource.key, new_resource.value)
+
+ directory new_resource.conf_dir
+
+ file "#{new_resource.conf_dir}/99-chef-#{new_resource.key}.conf" do
+ content "#{new_resource.key} = #{new_resource.value}"
+ end
+
+ execute "sysctl -p" do
+ command "sysctl -p"
+ action :run
+ end
+ end
+ end
+
+ action :remove do
+ # only converge the resource if the file actually exists to delete
+ if ::File.exist?("#{new_resource.conf_dir}/99-chef-#{new_resource.key}.conf")
+ converge_by "removing systctl value #{new_resource.key}" do
+ file "#{new_resource.conf_dir}/99-chef-#{new_resource.key}.conf" do
+ action :delete
+ end
+
+ backup_value = node["sysctl"]["backup"][new_resource.key]
+ set_sysctl_param(new_resource.key, backup_value) unless backup_value.empty?
+ node.rm("sysctl", "backup", new_resource.key)
+
+ execute "sysctl -p" do
+ command "sysctl -p"
+ action :run
+ end
+ end
+ end
+ end
+
+ action_class do
+ def set_sysctl_param(key, value)
+ shell_out!("sysctl #{'-e ' if new_resource.ignore_error}-w \"#{key}=#{value}\"")
+ end
+ end
+ end
+ end
+end