summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2018-06-11 14:42:21 -0700
committerGitHub <noreply@github.com>2018-06-11 14:42:21 -0700
commitd650a27edfe19352018b9651aebfca2fb9c035aa (patch)
tree881a22c216ed1247d6bc905280c950cb60b780dc
parent283292519a2bc8f2d4e62abc98d987caac3d2b92 (diff)
parent89fa6a74b020d3e982c1221f07b7212bb20280ce (diff)
downloadchef-d650a27edfe19352018b9651aebfca2fb9c035aa.tar.gz
Merge pull request #7165 from chef/kernel_module
Add kernel_module resource from the kernel_module cookbook
-rw-r--r--lib/chef/resource/kernel_module.rb129
-rw-r--r--lib/chef/resources.rb1
-rw-r--r--spec/unit/resource/kernel_module_spec.rb48
3 files changed, 178 insertions, 0 deletions
diff --git a/lib/chef/resource/kernel_module.rb b/lib/chef/resource/kernel_module.rb
new file mode 100644
index 0000000000..bea56dedc2
--- /dev/null
+++ b/lib/chef/resource/kernel_module.rb
@@ -0,0 +1,129 @@
+#
+# Resource:: kernel_module
+#
+# The MIT License (MIT)
+#
+# Copyright 2016-2018, Shopify Inc.
+# Copyright 2018, Chef Software, Inc.
+
+require "chef/resource"
+
+class Chef
+ class Resource
+ class KernelModule < Chef::Resource
+ preview_resource true
+ resource_name :kernel_module
+
+ description "Use the kernel_module resource to manage kernel modules on Linux systems. This resource can load, unload, blacklist, install, and uninstall modules."
+ introduced "14.3"
+
+ property :modname, String,
+ description: "The name of the kernel module.",
+ name_property: true, identity: true
+
+ property :load_dir, String,
+ description: "The directory to load modules from.",
+ default: "/etc/modules-load.d"
+
+ property :unload_dir, String,
+ description: "The modprobe.d directory.",
+ default: "/etc/modprobe.d"
+
+ action :install do
+ description "Load kernel module, and ensure it loads on reboot"
+
+ # load the module first before installing
+ new_resource.run_action(:load)
+
+ directory new_resource.load_dir do
+ recursive true
+ end
+
+ file "#{new_resource.load_dir}/#{new_resource.modname}.conf" do
+ content "#{new_resource.modname}\n"
+ notifies :run, "execute[update initramfs]"
+ end
+
+ execute "update initramfs" do
+ command initramfs_command
+ action :nothing
+ end
+ end
+
+ action :uninstall do
+ description "Unload a kernel module and remove module config, so it doesn't load on reboot."
+
+ file "#{new_resource.load_dir}/#{new_resource.modname}.conf" do
+ action :delete
+ notifies :run, "execute[update initramfs]"
+ end
+
+ file "#{new_resource.unload_dir}/blacklist_#{new_resource.modname}.conf" do
+ action :delete
+ notifies :run, "execute[update initramfs]"
+ end
+
+ execute "update initramfs" do
+ command initramfs_command
+ action :nothing
+ end
+
+ new_resource.run_action(:unload)
+ end
+
+ action :blacklist do
+ description "Blacklist a kernel module."
+
+ file "#{new_resource.unload_dir}/blacklist_#{new_resource.modname}.conf" do
+ content "blacklist #{new_resource.modname}"
+ notifies :run, "execute[update initramfs]"
+ end
+
+ execute "update initramfs" do
+ command initramfs_command
+ action :nothing
+ end
+
+ new_resource.run_action(:unload)
+ end
+
+ action :load do
+ description "Load a kernel module."
+
+ unless module_loaded?
+ converge_by("load kernel module #{new_resource.modname}") do
+ shell_out!("modprobe #{new_resource.modname}")
+ end
+ end
+ end
+
+ action :unload do
+ description "Unload kernel module"
+
+ if module_loaded?
+ converge_by("unload kernel module #{new_resource.modname}") do
+ shell_out!("modprobe -r #{new_resource.modname}")
+ end
+ end
+ end
+
+ action_class do
+ # determine the correct command to regen the initramfs based on platform
+ # @return [String]
+ def initramfs_command
+ if platform_family?("debian")
+ "update-initramfs -u"
+ else
+ "dracut -f"
+ end
+ end
+
+ # see if the module is listed in /proc/modules or not
+ # @return [Boolean]
+ def module_loaded?
+ /^#{new_resource.modname}/.match?(::File.read("/proc/modules"))
+ end
+ end
+ end
+ end
+end
diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb
index c001e56055..d942518b21 100644
--- a/lib/chef/resources.rb
+++ b/lib/chef/resources.rb
@@ -49,6 +49,7 @@ require "chef/resource/homebrew_cask"
require "chef/resource/homebrew_package"
require "chef/resource/homebrew_tap"
require "chef/resource/ifconfig"
+require "chef/resource/kernel_module"
require "chef/resource/ksh"
require "chef/resource/launchd"
require "chef/resource/link"
diff --git a/spec/unit/resource/kernel_module_spec.rb b/spec/unit/resource/kernel_module_spec.rb
new file mode 100644
index 0000000000..03cda28471
--- /dev/null
+++ b/spec/unit/resource/kernel_module_spec.rb
@@ -0,0 +1,48 @@
+#
+# 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::KernelModule do
+ let(:resource) { Chef::Resource::KernelModule.new("foo") }
+
+ it "sets resource name as :kernel_module" do
+ expect(resource.resource_name).to eql(:kernel_module)
+ end
+
+ it "is not a preview resource in Chef 15" do
+ pending("Chef 15") unless Chef::VERSION.start_with?("15")
+ expect(resource.class.preview_resource).to be_falsey
+ end
+
+ it "sets the default action as :install" do
+ expect(resource.action).to eql([:install])
+ end
+
+ it "sets the modname property as its name property" do
+ expect(resource.modname).to eql("foo")
+ end
+
+ it "supports :create and :flush actions" do
+ expect { resource.action :install }.not_to raise_error
+ expect { resource.action :uninstall }.not_to raise_error
+ expect { resource.action :blacklist }.not_to raise_error
+ expect { resource.action :load }.not_to raise_error
+ expect { resource.action :unload }.not_to raise_error
+ expect { resource.action :delete }.to raise_error(ArgumentError)
+ end
+end