Class: Ohai::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/ohai/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, safe_run = false) ⇒ Runner

safe_run: set to true if this runner will run plugins in safe-mode. default false.



33
34
35
36
37
38
39
# File 'lib/ohai/runner.rb', line 33

def initialize(controller, safe_run = false)
  @provides_map = controller.provides_map
  @safe_run = safe_run
  @failed_plugins = []
  @logger = controller.logger.with_child
  @logger. = { subsystem: "runner" }
end

Instance Attribute Details

#failed_pluginsObject (readonly)

Returns the value of attribute failed_plugins.



27
28
29
# File 'lib/ohai/runner.rb', line 27

def failed_plugins
  @failed_plugins
end

#loggerObject (readonly)

Returns the value of attribute logger.



27
28
29
# File 'lib/ohai/runner.rb', line 27

def logger
  @logger
end

#transport_connectionObject

Returns the value of attribute transport_connection.



29
30
31
# File 'lib/ohai/runner.rb', line 29

def transport_connection
  @transport_connection
end

Instance Method Details

#fetch_plugins(attributes) ⇒ Array

Parameters:

  • attributes (Array)

Returns:

  • (Array)


113
114
115
# File 'lib/ohai/runner.rb', line 113

def fetch_plugins(attributes)
  @provides_map.find_closest_providers_for(attributes)
end

#get_cycle(plugins, cycle_start) ⇒ Object

Given a list of plugins and the first plugin in the cycle, returns the list of plugin source files responsible for the cycle. Does not include plugins that aren’t a part of the cycle



120
121
122
123
124
125
# File 'lib/ohai/runner.rb', line 120

def get_cycle(plugins, cycle_start)
  cycle = plugins.drop_while { |plugin| !plugin.eql?(cycle_start) }
  names = []
  cycle.each { |plugin| names << plugin.name }
  names
end

#run_plugin(plugin) ⇒ Object

Runs plugins and any un-run dependencies. If force is set to true, then this plugin and its dependencies will be run even if they have been run before.

Parameters:

Returns:

  • void



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ohai/runner.rb', line 49

def run_plugin(plugin)
  elapsed = Benchmark.realtime do
    unless plugin.is_a?(Ohai::DSL::Plugin)
      raise Ohai::Exceptions::InvalidPlugin, "Invalid plugin #{plugin} (must be an Ohai::DSL::Plugin or subclass)"
    end

    begin
      if plugin.version == :version7
        run_v7_plugin(plugin)
      else
        raise Ohai::Exceptions::InvalidPlugin, "Invalid plugin version #{plugin.version} for plugin #{plugin}"
      end
    rescue Ohai::Exceptions::Error, SystemExit # SystemExit: abort or exit from plug-in should exit Ohai with failure code
      raise
    rescue Exception => e
      logger.warn("Plugin #{plugin.name} threw exception #{e.inspect} #{e.backtrace.join("\n")}")
    end
  end
  logger.trace("Plugin #{plugin.name} took #{"%f" % elapsed.truncate(6)} seconds to run.")
end

#run_v7_plugin(plugin) ⇒ Object

Returns void.

Parameters:

Returns:

  • void



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ohai/runner.rb', line 74

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

    next if next_plugin.has_run?

    if visited.include?(next_plugin)
      raise Ohai::Exceptions::DependencyCycle, "Dependency cycle detected. Please refer to the following plugins: #{get_cycle(visited, plugin).join(", ")}"
    end

    dependency_providers = fetch_plugins(next_plugin.dependencies)

    # Remove the already ran plugins from dependencies if force is not set
    # Also remove the plugin that we are about to run from dependencies as well.
    dependency_providers.delete_if do |dep_plugin|
      dep_plugin.has_run? || dep_plugin.eql?(next_plugin)
    end

    if dependency_providers.empty?
      next_plugin.transport_connection = transport_connection
      @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
  end
end