summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/resource/sysctl.rb111
-rw-r--r--lib/chef/resources.rb1
-rw-r--r--spec/unit/resource/sysctl_spec.rb56
3 files changed, 168 insertions, 0 deletions
diff --git a/lib/chef/resource/sysctl.rb b/lib/chef/resource/sysctl.rb
new file mode 100644
index 0000000000..ed9013d7cc
--- /dev/null
+++ b/lib/chef/resource/sysctl.rb
@@ -0,0 +1,111 @@
+#
+# 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: "The kernel parameter key in dotted format.",
+ name_property: true
+
+ property :ignore_error, [TrueClass, FalseClass],
+ description: "Ignore any errors when setting the value on the command line.",
+ default: false
+
+ property :value, [Array, String, Integer, Float],
+ description: "The value to set.",
+ coerce: proc { |v| coerce_value(v) },
+ required: true
+
+ property :conf_dir, String,
+ description: "The configuration directory to write the config to.",
+ 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 #{key}!" if o.error?
+ o.stdout.to_s.tr("\t", " ").strip
+ end
+
+ load_current_value do
+ value get_sysctl_value(key)
+ 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"
+ 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 config at #{new_resource.conf_dir}/99-chef-#{new_resource.key}.conf" do
+ file "#{new_resource.conf_dir}/99-chef-#{new_resource.key}.conf" do
+ action :delete
+ end
+
+ execute "sysctl -p"
+ 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
diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb
index a5d5423bfe..a62cb22c8c 100644
--- a/lib/chef/resources.rb
+++ b/lib/chef/resources.rb
@@ -89,6 +89,7 @@ require "chef/resource/scm"
require "chef/resource/script"
require "chef/resource/service"
require "chef/resource/sudo"
+require "chef/resource/sysctl"
require "chef/resource/systemd_unit"
require "chef/resource/windows_service"
require "chef/resource/subversion"
diff --git a/spec/unit/resource/sysctl_spec.rb b/spec/unit/resource/sysctl_spec.rb
new file mode 100644
index 0000000000..934db9b9e2
--- /dev/null
+++ b/spec/unit/resource/sysctl_spec.rb
@@ -0,0 +1,56 @@
+#
+# Copyright:: Copyright 2018, Chef Software, Inc.
+# License:: Apache License, Version 2.0
+#
+# 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 "spec_helper"
+
+describe Chef::Resource::Sysctl do
+ let(:resource) { Chef::Resource::Sysctl.new("something.something") }
+
+ it "sets resource name as :sysctl" do
+ expect(resource.resource_name).to eql(:sysctl)
+ end
+
+ it "sets the default action as :apply" do
+ expect(resource.action).to eql([:apply])
+ end
+
+ it "sets the key property as its name property" do
+ expect(resource.key).to eql("something.something")
+ end
+
+ it "coerces Arrays in the value property to space delimited Strings" do
+ resource.value [1, 2, 3]
+ expect(resource.value).to eql("1 2 3")
+ end
+
+ it "coerces Integers in the value property to Strings" do
+ resource.value 1
+ expect(resource.value).to eql("1")
+ end
+
+ it "coerces Floats in the value property to Strings" do
+ resource.value 1.1
+ expect(resource.value).to eql("1.1")
+ end
+
+ it "supports :apply and :remove actions" do
+ expect { resource.action :apply }.not_to raise_error
+ expect { resource.action :remove }.not_to raise_error
+ expect { resource.action :delete }.to raise_error(ArgumentError)
+ expect { resource.action :install }.to raise_error(ArgumentError)
+ end
+end