Class: Ohai::Loader

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

Overview

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

Defined Under Namespace

Classes: PluginFile, V6PluginClass

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Loader


51
52
53
54
55
# File 'lib/ohai/loader.rb', line 51

def initialize(controller)
  @controller = controller
  @v6_plugin_classes = []
  @v7_plugin_classes = []
end

Instance Method Details

#load_allObject


65
66
67
68
69
70
71
72
# File 'lib/ohai/loader.rb', line 65

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

  collect_v6_plugins
  collect_v7_plugins
end

#load_plugin(plugin_path, plugin_dir_path = nil) ⇒ Object

Load a specified file as an ohai plugin and creates an instance of it. Not used by ohai itself, but can be used to load a plugin for testing purposes. plugin_dir_path is required when loading a v6 plugin.


78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ohai/loader.rb', line 78

def load_plugin(plugin_path, plugin_dir_path = nil)
  plugin_class = load_plugin_class(plugin_path, plugin_dir_path)
  return nil unless plugin_class.kind_of?(Class)
  case
  when plugin_class < Ohai::DSL::Plugin::VersionVI
    load_v6_plugin(plugin_class, plugin_path, plugin_dir_path)
  when 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, plugin_dir_path = nil) ⇒ Object

Reads the file specified by `plugin_path` and returns a class object for the ohai plugin defined therein.

If `plugin_dir_path` is given, and the file at `plugin_path` is a v6 plugin, the 'relative path' of the plugin (used by `require_plugin()`) is computed by finding the relative path from `plugin_dir_path` to `plugin_path`


97
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 97

def load_plugin_class(plugin_path, plugin_dir_path=nil)
  # Read the contents of the plugin to understand if it's a V6 or V7 plugin.
  contents = ""
  begin
    contents << IO.read(plugin_path)
  rescue IOError, Errno::ENOENT
    Ohai::Log.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
    Ohai::Log.warn("[DEPRECATION] Plugin at #{plugin_path} is a version 6 plugin. \
Version 6 plugins will not be supported in future releases of Ohai. \
Please upgrade your plugin to version 7 plugin syntax. \
For more information visit here: docs.chef.io/ohai_custom.html")

    load_v6_plugin_class(contents, plugin_path, plugin_dir_path)
  end
end

#plugin_files_by_dirObject

Searches all plugin paths and returns an Array of PluginFile objects representing each plugin file.


59
60
61
62
63
# File 'lib/ohai/loader.rb', line 59

def plugin_files_by_dir
  Array(Ohai::Config[:plugin_path]).inject([]) do |plugin_files, plugin_path|
    plugin_files + PluginFile.find_all_in(plugin_path)
  end
end