Class: Ohai::Loader

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

Overview

Ohai plugin loader. Finds all the plugins specified in the Ohai.config :plugin_path (supports a single or multiple path setting here), evaluates them and returns plugin objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Loader

Returns a new instance of Loader.



33
34
35
36
37
# File 'lib/ohai/loader.rb', line 33

def initialize(controller)
  @controller = controller
  @logger = controller.logger.with_child(subsystem: "loader")
  @v7_plugin_classes = []
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



31
32
33
# File 'lib/ohai/loader.rb', line 31

def logger
  @logger
end

Instance Method Details

#load_additional(from) ⇒ Object

load additional plugins classes from a given directory

Parameters:

  • from (String)

    path to a directory with additional plugins to load



70
71
72
73
74
75
76
# File 'lib/ohai/loader.rb', line 70

def load_additional(from)
  from = [ Ohai.config[:plugin_path], from].flatten
  plugin_files_by_dir(from).collect do |plugin_file|
    logger.trace "Loading additional plugin: #{plugin_file}"
    load_v7_plugin(load_plugin_class(plugin_file))
  end
end

#load_allArray<String>

loads all plugin classes

Returns:



60
61
62
63
64
65
66
# File 'lib/ohai/loader.rb', line 60

def load_all
  plugin_files_by_dir.each do |plugin_file|
    load_plugin_class(plugin_file)
  end

  collect_v7_plugins
end

#load_plugin(plugin_path) ⇒ Object

Load a specified file as an ohai plugin and creates an instance of it. Not used by ohai itself, but is used in the specs to load plugins for testing

Parameters:



83
84
85
86
87
88
89
90
91
92
# File 'lib/ohai/loader.rb', line 83

def load_plugin(plugin_path)
  plugin_class = load_plugin_class(plugin_path)
  return nil unless plugin_class.is_a?(Class)

  if plugin_class < Ohai::DSL::Plugin::VersionVII
    load_v7_plugin(plugin_class)
  else
    raise Exceptions::IllegalPluginDefinition, "cannot create plugin of type #{plugin_class}"
  end
end

#load_plugin_class(plugin_path) ⇒ Object

load an ohai plugin object class from file

Parameters:

  • plugin_path

    String the path to the ohai plugin

Returns:

  • (Object)

    class object for the ohai plugin defined in the file



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ohai/loader.rb', line 98

def load_plugin_class(plugin_path)
  # Read the contents of the plugin to understand if it's a V6 or V7 plugin.
  contents = ""
  begin
    logger.trace("Loading plugin at #{plugin_path}")
    contents << IO.read(plugin_path)
  rescue IOError, Errno::ENOENT
    logger.warn("Unable to open or read plugin at #{plugin_path}")
    return nil
  end

  # We assume that a plugin is a V7 plugin if it contains Ohai.plugin in its contents.
  if contents.include?("Ohai.plugin")
    load_v7_plugin_class(contents, plugin_path)
  else
    raise Exceptions::IllegalPluginDefinition, "[DEPRECATION] Plugin at #{plugin_path}"\
    " is a version 6 plugin. Version 6 plugins are no longer supported by Ohai. This"\
    " plugin will need to be updated to the v7 Ohai plugin format. See"\
    " https://docs.chef.io/ohai_custom.html for v7 syntax."
  end
end

#plugin_files_by_dir(plugin_dir = ) ⇒ Array<String>

Searches all plugin paths and returns an Array of file paths to plugins

Parameters:

  • dir (Array, String)

    directory/directories to load plugins from

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ohai/loader.rb', line 43

def plugin_files_by_dir(plugin_dir = Ohai.config[:plugin_path])
  Array(plugin_dir).map do |path|
    if Dir.exist?(path)
      Ohai::Log.trace("Searching for Ohai plugins in #{path}")

      escaped = ChefConfig::PathHelper.escape_glob_dir(path)
      Dir[File.join(escaped, "**", "*.rb")]
    else
      Ohai::Log.debug("The plugin path #{path} does not exist. Skipping...")
      []
    end
  end.flatten
end