summaryrefslogtreecommitdiff
path: root/lib/ohai
diff options
context:
space:
mode:
authorClaire McQuin <claire@opscode.com>2013-08-16 09:25:24 -0700
committerClaire McQuin <claire@opscode.com>2013-08-19 14:17:39 -0700
commit4b47df7a53dbf047a60905a1f97e1214c17e878b (patch)
tree24e8fe919f58b94059bcc22fdb270a8f1814bbf2 /lib/ohai
parent163c8a4201978b55d1bc6d409a45143ba054470e (diff)
downloadohai-4b47df7a53dbf047a60905a1f97e1214c17e878b.tar.gz
revise internals for loading plugins
Diffstat (limited to 'lib/ohai')
-rw-r--r--lib/ohai/dsl/plugin.rb108
-rw-r--r--lib/ohai/loader.rb85
-rw-r--r--lib/ohai/system.rb8
3 files changed, 96 insertions, 105 deletions
diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb
index 508c5d6c..092ad4e2 100644
--- a/lib/ohai/dsl/plugin.rb
+++ b/lib/ohai/dsl/plugin.rb
@@ -11,77 +11,101 @@
#
# 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.
+# 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 'ohai/os'
-
require 'ohai/mixin/command'
-require 'ohai/mixin/seconds_to_human'
module Ohai
- #=========================================================
- # define new plugin class
- #=========================================================
def self.plugin(&block)
- plugin_class = Class.new(DSL::Plugin, &block)
+ Class.new(DSL::Plugin::VersionVII, &block)
+ end
+
+ def self.v6plugin(&block)
+ Class.new(DSL::Plugin::VersionVI, &block)
end
module DSL
class Plugin
-
include Ohai::OS
include Ohai::Mixin::Command
- include Ohai::Mixin::SecondsToHuman
+
+ def initialize(controller)
+ @controller = controller
+ @data = controller.data
+ end
#=====================================================
- # plugin loading phase
+ # version 7 plugin class
#=====================================================
- def self.provides_attrs
- @provides_attrs ||= []
- end
+ class VersionVII < Plugin
+ def initialize(controller)
+ super(controller)
+ end
- def self.depends_attrs
- @depends_attrs ||= []
- end
+ def self.version
+ :version7
+ end
- def self.provides(*args)
- args.each do |attr|
- provides_attrs << attr
+ def self.provides_attrs
+ @provides_attrs ||= []
end
- end
- def self.depends(*args)
- args.each do |attr|
- depends_attrs << attr
+ def self.depends_attrs
+ @depends_attrs ||= []
end
- end
- def self.depends_os(*args)
- args.each do |attr|
- depends_attrs << "#{Ohai::OS.collect_os}/#{attr}"
+ def self.provides(*attrs)
+ attrs.each do |attr|
+ provides_attrs << attr
+ end
end
- end
- def self.collect_data(&block)
- define_method(:run, &block)
+ def self.depends(*attrs)
+ attrs.each do |attr|
+ depends_attrs << attr
+ end
+ end
+
+ def self.depends_os(*attrs)
+ attrs.each do |attr|
+ depends_attrs << "#{Ohai::OS.collect_os}/#{attr}"
+ end
+ end
+
+ def self.collect_data(&block)
+ define_method(:run, &block)
+ end
end
#=====================================================
- # plugin run phase
+ # version 6 plugin class
#=====================================================
- attr_reader :data
+ class VersionVI < Plugin
+ def initialize(controller)
+ super(controller)
+ end
- def initialize(controller)
- @controller = controller
- @data = controller.data
+ def self.version
+ :version6
+ end
+
+ def self.collect_contents(contents)
+ define_method(:run) { self.instance_eval(contents) }
+ end
end
-
+
+ #=====================================================
+ # plugin DSL methods
+ #=====================================================
+ # @note: v6 plugins only
def require_plugin(*args)
- # @todo: backwards compat
- # @controller.require_plugin(*args)
+ # @todo: log deprecation message
+ @controller.require_plugin(*args)
end
def hints
@@ -116,7 +140,9 @@ module Ohai
stdout.strip
end
- # Set the value equal to the stdout of the command, plus run through a regex - the first piece of match data is the value.
+ # Set the value equal to the stdout of the command, plus
+ # run through a regex - the first piece of match data is\
+ # the value.
def from_with_regex(cmd, *regex_list)
regex_list.flatten.each do |regex|
status, stdout, stderr = run_command(:command => cmd)
@@ -146,7 +172,9 @@ module Ohai
if File.exist?(filename)
begin
hash = @json_parser.parse(File.read(filename))
- hints[name] = hash || Hash.new # hint should exist because the file did, even if it didn't contain anything
+ hints[name] = hash || Hash.new # hint
+ # should exist because the file did, even if it didn't
+ # contain anything
rescue Yajl::ParseError => e
Ohai::Log.error("Could not parse hint file at #{filename}: #{e.message}")
end
diff --git a/lib/ohai/loader.rb b/lib/ohai/loader.rb
index 54d22620..6c2339f2 100644
--- a/lib/ohai/loader.rb
+++ b/lib/ohai/loader.rb
@@ -27,81 +27,48 @@ module Ohai
def initialize(controller)
@attributes = controller.attributes
- @plugins = controller.plugins
- @v6plugins = controller.v6plugins
- @sources = controller.sources
+ @v6_dependency_solver = controller.v6_dependency_solver
end
def load_plugin(plugin_path, plugin_name=nil)
- if version6?(plugin_path)
- save_plugin(plugin_path)
- else
- clean_up(plugin_path) if @sources.has_key?(plugin_path)
-
- plugin = nil
+ plugin = nil
+
+ contents = ""
+ begin
+ contents = IO.read(plugin_path)
+ rescue SystemExit, Interrupt
+ raise
+ rescue IOError, Errno::ENOENT
+ Ohai::Log.debug("Unable to open or read #{plugin_path}")
+ return plugin
+ end
+ if contents.include?("Ohai.plugin")
begin
- plugin = from_file(plugin_path)
+ plugin = self.instance_eval(contents, plugin_path, 1)
rescue SystemExit, Interrupt
raise
rescue Exception, Errno::ENOENT => e
- Ohai::Log.debug("Plugin #{plugin_name} threw exception #{e.inspect} #{e.backtrace.join("\n")}")
- return
+ Ohai::Log.debug("Plugin at #{plugin_path} threw exception #{e.inspect} #{e.backtrace.join("\n")}")
end
- if plugin.nil?
- Ohai::Log.debug("Unable to load plugin at #{plugin_path}")
- return
- end
-
- plugin_key = plugin_name || plugin.to_s
- register_plugin(plugin, plugin_path, plugin_key)
- collect_provides(plugin, plugin_key)
- end
- end
-
- private
-
- def version6?(plugin_path)
- File.open(plugin_path) { |f| f.grep(/Ohai\.plugin/).empty? }
- end
-
- def save_plugin(plugin_path)
- unless @sources.has_key?(plugin_path)
- Ohai::Config[:plugin_path].each do |path|
- file_regex = Regexp.new("#{File.expand_path(path)}#{File::SEPARATOR}(.+).rb$")
- md = file_regex.match(plugin_path)
- if md
- plugin_name = md[1].gsub(File::SEPARATOR, "::")
- @v6plugins[plugin_name] = plugin_path
- @sources[plugin_path] = plugin_name
- end
- end
+ collect_provides(plugin) unless plugin.nil?
+ else
+ plugin = Ohai.v6plugin do collect_contents contents end
end
- end
- def clean_up(file)
- key = @sources[file]
- @plugins[key][:provides].each do |attr|
- @attributes[attr][:providers].delete(key)
+ if plugin.nil?
+ Ohai::Log.debug("Unable to load plugin at #{plugin_path}")
+ else
+ @v6_dependency_solver[plugin_path] = plugin
end
- @plugins.delete(key)
- @sources.delete(file)
+ plugin
end
- def register_plugin(plugin, file, plugin_key)
- @plugins[plugin_key] ||= Mash.new
- @sources[file] = plugin_key
-
- p = @plugins[plugin_key]
- p[:plugin] = plugin
- p[:provides] = plugin.provides_attrs
- p[:depends] = plugin.depends_attrs
- end
-
+ private
- def collect_provides(plugin, plugin_key)
+ def collect_provides(plugin)
plugin_provides = plugin.provides_attrs
plugin_provides.each do |attr|
@@ -116,7 +83,7 @@ module Ohai
end
a[:providers] ||= []
- a[:providers] << plugin_key
+ a[:providers] << plugin
end
end
diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb
index f68b8073..b743893e 100644
--- a/lib/ohai/system.rb
+++ b/lib/ohai/system.rb
@@ -32,18 +32,14 @@ module Ohai
class System
attr_accessor :data
attr_reader :attributes
- attr_reader :plugins
- attr_reader :v6plugins
- attr_reader :sources
attr_reader :hints
+ attr_reader :v6_dependency_solver
def initialize
@data = Mash.new
@attributes = Hash.new
- @plugins = Mash.new
- @v6plugins = Hash.new
- @sources = Hash.new
@hints = Hash.new
+ @v6_dependency_solver = Hash.new
@plugin_path = ""
end