summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhil Dibowitz <phil@ipom.com>2017-10-04 15:17:14 -0700
committerGitHub <noreply@github.com>2017-10-04 15:17:14 -0700
commit978bcf908e9ac8321d79a1a55dd4d4c3e4c6666e (patch)
treeacefe3ac40f64f754a561d5a6c5f56e00eb4cb85
parentda3e87f8e1f84e60c96a38cdb9eaea2a42ac8b0c (diff)
downloadohai-978bcf908e9ac8321d79a1a55dd4d4c3e4c6666e.tar.gz
Critical Plugins (#1064)
This adds a new Ohai configuration `critical_plugins` which will cause Ohai to exit (or `fail` if inside of Chef) if the plugins in the list do not run successfully. This feature only supports Ohai 7-style named plugins, not the ancient Ohai 6-style stuff. Tests to follow, but: ``` $ cat /home/phild/ohai.rb ohai.critical_plugins << :Filesystem $ ./bin/ohai -c /home/phild/ohai.rb [2017-10-03T13:43:14-07:00] INFO: The plugin path /etc/chef/ohai/plugins does not exist. Skipping... [2017-10-03T13:43:14-07:00] ERROR: The following plugins marked as critical failed: [:Filesystem] ``` And when I run Chef: ``` [2017-10-03T13:44:11-07:00] FATAL: RuntimeError: The following plugins marked as critical failed: [:Filesystem2]. Failing Chef run. ``` This fixes #879. Signed-off-by: Phil Dibowitz <phil@ipom.com>
-rw-r--r--RELEASE_NOTES.md10
-rw-r--r--lib/ohai/config.rb1
-rw-r--r--lib/ohai/dsl/plugin.rb4
-rw-r--r--lib/ohai/exception.rb1
-rw-r--r--lib/ohai/runner.rb5
-rw-r--r--lib/ohai/system.rb11
-rw-r--r--spec/unit/system_spec.rb26
7 files changed, 58 insertions, 0 deletions
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 657a2911..5f53e49e 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -1,3 +1,13 @@
+# Unreleased
+
+### Critical Plugins
+
+Users can now specify a list of plugins which are `critical`. Critical plugins will cause Ohai to fail if they do not run successfully (and thus cause a Chef run using Ohai to fail). The syntax for this is:
+
+```
+ohai.critical_plugins << :Filesystem
+```
+
# Ohai Release Notes 13.5
### Correctly detect IPv6 routes ending in ::
diff --git a/lib/ohai/config.rb b/lib/ohai/config.rb
index 0f90f944..aa73900b 100644
--- a/lib/ohai/config.rb
+++ b/lib/ohai/config.rb
@@ -35,6 +35,7 @@ module Ohai
default :log_location, STDERR
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, []
end
end
diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb
index e3694ac1..f187aeaf 100644
--- a/lib/ohai/dsl/plugin.rb
+++ b/lib/ohai/dsl/plugin.rb
@@ -85,10 +85,12 @@ module Ohai
include Ohai::Util::FileHelper
attr_reader :data
+ attr_reader :failed
def initialize(data)
@data = data
@has_run = false
+ @failed = false
end
def run
@@ -182,8 +184,10 @@ module Ohai
def safe_run
run
rescue Ohai::Exceptions::Error => e
+ @failed = true
raise e
rescue => e
+ @failed = true
Ohai::Log.debug("Plugin #{name} threw #{e.inspect}")
e.backtrace.each { |line| Ohai::Log.debug( line ) }
end
diff --git a/lib/ohai/exception.rb b/lib/ohai/exception.rb
index 5db9c597..0cae0995 100644
--- a/lib/ohai/exception.rb
+++ b/lib/ohai/exception.rb
@@ -29,5 +29,6 @@ module Ohai
class DependencyNotFound < Error; end
class AttributeSyntaxError < Error; end
class PluginConfigError < Error; end
+ class CriticalPluginFailure < Error; end
end
end
diff --git a/lib/ohai/runner.rb b/lib/ohai/runner.rb
index 6368cbe3..05fd7cfc 100644
--- a/lib/ohai/runner.rb
+++ b/lib/ohai/runner.rb
@@ -23,11 +23,13 @@ require "benchmark"
module Ohai
class Runner
+ attr_reader :failed_plugins
# safe_run: set to true if this runner will run plugins in
# safe-mode. default false.
def initialize(controller, safe_run = false)
@provides_map = controller.provides_map
@safe_run = safe_run
+ @failed_plugins = []
end
# Runs plugins and any un-run dependencies.
@@ -86,6 +88,9 @@ module Ohai
if dependency_providers.empty?
@safe_run ? next_plugin.safe_run : next_plugin.run
+ if next_plugin.failed
+ @failed_plugins << next_plugin.name
+ end
else
visited << next_plugin << dependency_providers.first
end
diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb
index 1002e198..dafa90bc 100644
--- a/lib/ohai/system.rb
+++ b/lib/ohai/system.rb
@@ -47,6 +47,7 @@ module Ohai
@cli = config[:invoked_from_cli]
@plugin_path = ""
@config = config
+ @failed_plugins = []
reset_system
end
@@ -107,6 +108,16 @@ module Ohai
Ohai::Log.error("Encountered error while running plugins: #{e.inspect}")
raise
end
+ critical_failed = Ohai::Config.ohai[:critical_plugins] & @runner.failed_plugins
+ unless critical_failed.empty?
+ msg = "The following Ohai plugins marked as critical failed: #{critical_failed}"
+ if @cli
+ Ohai::Log.error(msg)
+ exit(true)
+ else
+ raise Ohai::Exceptions::CriticalPluginFailure, "#{msg}. Failing Chef run."
+ end
+ end
# Freeze all strings.
freeze_strings!
diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb
index b212ea0b..a2e85843 100644
--- a/spec/unit/system_spec.rb
+++ b/spec/unit/system_spec.rb
@@ -43,6 +43,7 @@ describe "Ohai::System" do
config = {
disabled_plugins: [ :Foo, :Baz ],
directory: "/some/extra/plugins",
+ critical_plugins: [ :Foo, :Bar ],
}
Ohai::System.new(config)
config.each do |option, value|
@@ -309,6 +310,15 @@ Ohai.plugin(:Park) do
end
EOF
+ with_plugin("fails.rb", <<EOF)
+Ohai.plugin(:Fails) do
+ provides 'fails'
+ collect_data(:default) do
+ fail 'thing'
+ end
+end
+EOF
+
it "should collect data from all the plugins" do
Ohai.config[:plugin_path] = [ path_to(".") ]
ohai.all_plugins
@@ -343,6 +353,22 @@ EOF
expect(ohai.data[:park]).to eq("plants")
end
end
+
+ describe "when using :critical_plugins" do
+ before do
+ Ohai.config[:critical_plugins] = [ :Fails ]
+ end
+ after do
+ Ohai.config[:critical_plugins] = []
+ end
+
+ it "should fail when critical plugins fail" do
+ Ohai.config[:plugin_path] = [ path_to(".") ]
+ expect(Ohai::Log).to receive(:error).with(/marked as critical/)
+ ohai.all_plugins
+ end
+
+ end
end
when_plugins_directory "contains v6 & v7 plugins in different directories" do