From 48c29a3a729f43334a890142dbba9584791bc99e Mon Sep 17 00:00:00 2001 From: Thom May Date: Fri, 16 Feb 2018 16:45:06 +0000 Subject: Support optional plugins Some plugins are useful enough to ship in Ohai, but should be off by default. Those plugins can be (and are) marked as optional. Implements RFC 103 Signed-off-by: Thom May --- lib/ohai/config.rb | 4 ++++ lib/ohai/dsl/plugin.rb | 11 ++--------- lib/ohai/dsl/plugin/versionvii.rb | 12 ++++++++++++ lib/ohai/plugins/linux/lspci.rb | 1 + lib/ohai/plugins/linux/sessions.rb | 1 + lib/ohai/plugins/passwd.rb | 1 + lib/ohai/runner.rb | 4 ++++ spec/unit/system_spec.rb | 33 +++++++++++++++++++++++++++++++++ 8 files changed, 58 insertions(+), 9 deletions(-) diff --git a/lib/ohai/config.rb b/lib/ohai/config.rb index aa73900b..9c24a7bf 100644 --- a/lib/ohai/config.rb +++ b/lib/ohai/config.rb @@ -36,6 +36,10 @@ module Ohai default :plugin, Ohai::PluginConfig.new { |h, k| h[k] = Ohai::PluginConfig.new } default :plugin_path, [ File.expand_path(File.join(File.dirname(__FILE__), "plugins")), ChefConfig::Config.platform_specific_path("/etc/chef/ohai/plugins") ] default :critical_plugins, [] + # causes all optional plugins to be run. + default :run_all_plugins, false + # optional plugins are the set of plugins that are marked optional but you wish to run. + default :optional_plugins, [] end end diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb index a0d8c150..96867780 100644 --- a/lib/ohai/dsl/plugin.rb +++ b/lib/ohai/dsl/plugin.rb @@ -32,15 +32,8 @@ module Ohai name.is_a?(Symbol) && name.to_s.match(/^[^A-Z]|_/).nil? end - # dealing with ruby 1.8 - if Module.method(:const_defined?).arity == 1 - def self.strict_const_defined?(const) - const_defined?(const) - end - else - def self.strict_const_defined?(const) - const_defined?(const, false) - end + def self.strict_const_defined?(const) + const_defined?(const, false) end end diff --git a/lib/ohai/dsl/plugin/versionvii.rb b/lib/ohai/dsl/plugin/versionvii.rb index cd8dc13c..75cc65c0 100644 --- a/lib/ohai/dsl/plugin/versionvii.rb +++ b/lib/ohai/dsl/plugin/versionvii.rb @@ -66,6 +66,14 @@ module Ohai end end + def self.optional(opt = true) + @optional = opt + end + + def self.optional? + !!@optional + end + def self.collect_data(platform = :default, *other_platforms, &block) [platform, other_platforms].flatten.each do |plat| if data_collector.has_key?(plat) @@ -93,6 +101,10 @@ module Ohai end end + def optional? + self.class.optional? + end + def provides(*paths) Ohai::Log.warn("[UNSUPPORTED OPERATION] \'provides\' is no longer supported in a \'collect_data\' context. Please specify \'provides\' before collecting plugin data. Ignoring command \'provides #{paths.join(", ")}") end diff --git a/lib/ohai/plugins/linux/lspci.rb b/lib/ohai/plugins/linux/lspci.rb index 9e3b2ac3..fa03747e 100644 --- a/lib/ohai/plugins/linux/lspci.rb +++ b/lib/ohai/plugins/linux/lspci.rb @@ -20,6 +20,7 @@ Ohai.plugin(:Lspci) do depends "platform" provides "pci" + optional true collect_data(:linux) do devices = Mash.new diff --git a/lib/ohai/plugins/linux/sessions.rb b/lib/ohai/plugins/linux/sessions.rb index 7fb2b5ae..2d53813e 100644 --- a/lib/ohai/plugins/linux/sessions.rb +++ b/lib/ohai/plugins/linux/sessions.rb @@ -18,6 +18,7 @@ Ohai.plugin(:Sessions) do provides "sessions/by_session", "sessions/by_user" + optional true collect_data(:linux) do loginctl_path = which("loginctl") diff --git a/lib/ohai/plugins/passwd.rb b/lib/ohai/plugins/passwd.rb index 2539fa26..824153ae 100644 --- a/lib/ohai/plugins/passwd.rb +++ b/lib/ohai/plugins/passwd.rb @@ -2,6 +2,7 @@ Ohai.plugin(:Passwd) do require "etc" provides "etc", "current_user" + optional true def fix_encoding(str) str.force_encoding(Encoding.default_external) if str.respond_to?(:force_encoding) diff --git a/lib/ohai/runner.rb b/lib/ohai/runner.rb index ba3985dc..6f623d38 100644 --- a/lib/ohai/runner.rb +++ b/lib/ohai/runner.rb @@ -59,6 +59,10 @@ module Ohai end def run_v7_plugin(plugin) + return true if plugin.optional? && + !Ohai.config[:run_all_plugins] && + !Ohai.config[:optional_plugins].include?(plugin.name) + visited = [ plugin ] until visited.empty? next_plugin = visited.pop diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb index 57341e76..e0631cd8 100644 --- a/spec/unit/system_spec.rb +++ b/spec/unit/system_spec.rb @@ -175,6 +175,17 @@ Ohai.plugin(:Fails) do fail 'thing' end end +EOF + + with_plugin("optional.rb", <