1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
#
# Author:: Claire McQuin (<claire@chef.io>)
# Copyright:: Copyright (c) 2013-2016 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "chef-config/path_helper"
require "ohai/log"
require "ohai/mash"
require "ohai/dsl"
require "pathname"
module Ohai
# Ohai plugin loader. Finds all the plugins in your
# `Ohai.config[:plugin_path]` (supports a single or multiple path setting
# here), evaluates them and returns plugin objects.
class Loader
# Simple struct like objects to track the path of a plugin and the root
# directory of plugins in which we found it. We don't care about the
# relative paths of v7 plugins, but in v6 plugins, dependencies are
# specified by calling `require_plugin` with a relative path. To manage
# this, we track the path and root of each file as we discover them so we
# can feed this into the v6 "dependency solver" as we load them.
PluginFile = Struct.new(:path, :plugin_root) do
# Finds all the *.rb files under the configured paths in :plugin_path
def self.find_all_in(plugin_dir)
unless Dir.exist?(plugin_dir)
Ohai::Log.info("The plugin path #{plugin_dir} does not exist. Skipping...")
return []
end
Ohai::Log.debug("Searching for Ohai plugins in #{plugin_dir}")
# escape_glob_dir does not exist in 12.7 or below
if ChefConfig::PathHelper.respond_to?(:escape_glob_dir)
escaped = ChefConfig::PathHelper.escape_glob_dir(plugin_dir)
else
escaped = ChefConfig::PathHelper.escape_glob(plugin_dir)
end
Dir[File.join(escaped, "**", "*.rb")].map do |file|
new(file, plugin_dir)
end
end
end
def initialize(controller)
@controller = controller
@v7_plugin_classes = []
end
# Searches all plugin paths and returns an Array of PluginFile objects
# representing each plugin file.
def plugin_files_by_dir(dir = Ohai.config[:plugin_path])
Array(dir).inject([]) do |plugin_files, plugin_path|
plugin_files + PluginFile.find_all_in(plugin_path)
end
end
def load_all
plugin_files_by_dir.each do |plugin_file|
load_plugin_class(plugin_file.path, plugin_file.plugin_root)
end
collect_v7_plugins
end
def load_additional(from)
from = [ Ohai.config[:plugin_path], from].flatten
plugin_files_by_dir(from).collect do |plugin_file|
Ohai::Log.debug "Loading additional plugin: #{plugin_file}"
plugin = load_plugin_class(plugin_file.path, plugin_file.plugin_root)
load_v7_plugin(plugin)
end
end
# 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.
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)
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
# 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`
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
Ohai::Log.debug("Loading plugin at #{plugin_path}")
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
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
private
def collect_provides(plugin)
plugin_provides = plugin.class.provides_attrs
@controller.provides_map.set_providers_for(plugin, plugin_provides)
end
def collect_v7_plugins
@v7_plugin_classes.each do |plugin_class|
load_v7_plugin(plugin_class)
end
end
def load_v7_plugin_class(contents, plugin_path)
plugin_class = eval(contents, TOPLEVEL_BINDING, plugin_path)
unless plugin_class.kind_of?(Class) && plugin_class < Ohai::DSL::Plugin
raise Ohai::Exceptions::IllegalPluginDefinition, "Plugin file cannot contain any statements after the plugin definition"
end
plugin_class.sources << plugin_path
@v7_plugin_classes << plugin_class unless @v7_plugin_classes.include?(plugin_class)
plugin_class
rescue SystemExit, Interrupt
raise
rescue Ohai::Exceptions::InvalidPluginName => e
Ohai::Log.warn("Plugin Name Error: <#{plugin_path}>: #{e.message}")
rescue Ohai::Exceptions::IllegalPluginDefinition => e
Ohai::Log.warn("Plugin Definition Error: <#{plugin_path}>: #{e.message}")
rescue NoMethodError => e
Ohai::Log.warn("Plugin Method Error: <#{plugin_path}>: unsupported operation \'#{e.name}\'")
rescue SyntaxError => e
# split on occurrences of
# <env>: syntax error,
# <env>:##: syntax error,
# to remove from error message
parts = e.message.split(/<.*>[:[0-9]+]*: syntax error, /)
parts.each do |part|
next if part.length == 0
Ohai::Log.warn("Plugin Syntax Error: <#{plugin_path}>: #{part}")
end
rescue Exception, Errno::ENOENT => e
Ohai::Log.warn("Plugin Error: <#{plugin_path}>: #{e.message}")
Ohai::Log.debug("Plugin Error: <#{plugin_path}>: #{e.inspect}, #{e.backtrace.join('\n')}")
end
def load_v7_plugin(plugin_class)
plugin = plugin_class.new(@controller.data)
collect_provides(plugin)
plugin
end
end
end
|