diff options
-rw-r--r-- | lib/ohai/config.rb | 4 | ||||
-rw-r--r-- | lib/ohai/dsl/plugin.rb | 11 | ||||
-rw-r--r-- | lib/ohai/dsl/plugin/versionvii.rb | 12 | ||||
-rw-r--r-- | lib/ohai/plugins/linux/lspci.rb | 1 | ||||
-rw-r--r-- | lib/ohai/plugins/linux/sessions.rb | 1 | ||||
-rw-r--r-- | lib/ohai/plugins/passwd.rb | 1 | ||||
-rw-r--r-- | lib/ohai/runner.rb | 4 | ||||
-rw-r--r-- | 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 @@ -177,6 +177,17 @@ Ohai.plugin(:Fails) do end EOF + with_plugin("optional.rb", <<EOF) +Ohai.plugin(:Optional) do + provides 'optional' + optional true + + collect_data(:default) do + optional("canteloupe") + end +end +EOF + it "should collect data from all the plugins" do Ohai.config[:plugin_path] = [ path_to(".") ] ohai.all_plugins @@ -230,6 +241,28 @@ EOF "The following Ohai plugins marked as critical failed: [:Fails]. Failing Chef run.") end end + + describe "when using :optional_plugins" do + it "should not run optional plugins by default" do + Ohai.config[:plugin_path] = [ path_to(".") ] + ohai.all_plugins + expect(ohai.data[:optional]).to be_nil + end + + it "should run optional plugins when specifically enabled" do + Ohai.config[:optional_plugins] = [ :Optional ] + Ohai.config[:plugin_path] = [ path_to(".") ] + ohai.all_plugins + expect(ohai.data[:optional]).to eq("canteloupe") + end + + it "should run optional plugins when all plugins are enabled" do + Ohai.config[:run_all_plugins] = true + Ohai.config[:plugin_path] = [ path_to(".") ] + ohai.all_plugins + expect(ohai.data[:optional]).to eq("canteloupe") + end + end end end |