Skip to content

Commit

Permalink
add plugin namespacing, platform option to collect_data
Browse files Browse the repository at this point in the history
  • Loading branch information
Claire McQuin committed Oct 17, 2013
1 parent 37b5fea commit 076e429
Showing 1 changed file with 104 additions and 29 deletions.
133 changes: 104 additions & 29 deletions lib/ohai/dsl/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Author:: Claire McQuin (<claire@opscode.com>)
# Copyright:: Copyright (c) 2013 Opscode, Inc.
# Copyright:: Copyright (c) 2008, 2013 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,17 +18,67 @@
# limitations under the License.
#

# @todo: move os to mixin
require 'ohai/os'
require 'ohai/mixin/command'
require 'ohai/mixin/seconds_to_human'

module Ohai
def self.plugin(&block)
Class.new(DSL::Plugin::VersionVII, &block)

# for plugin namespacing
module NamedPlugin
# dealing with ruby 1.8
if Module.method(:const_defined?).arity == 1
def self.strict_const_defined?(const)
const_defined?(const)
end
else
def self.strict_const_defined?(const)
const_defined?(const, false)
end
end
end

def self.plugin(name, &block)
plugin = nil
if NamedPlugin.strict_const_defined?(name)
plugin = NamedPlugin.const_get(name)
if plugin.version.eql?(:version6)
Ohai::Log.warn("Already loaded version 6 plugin #{name}")
else
plugin.class_eval(&block)
end
else
klass = Class.new(DSL::Plugin::VersionVII, &block)
plugin = NamedPlugin.const_set(name, klass)
end
plugin
end

def self.v6plugin(name_str, &block)
plugin = nil
name = nameify(name_str)
if NamedPlugin.strict_const_defined?(name)
# log @ debug-level mimics OHAI-6
Ohai::Log.debug("Already loaded plugin #{name}")
plugin = NamedPlugin.const_get(name)
else
klass = Class.new(DSL::Plugin::VersionVI, &block)
plugin = NamedPlugin.const_set(name, klass)
end
plugin
end

def self.v6plugin(&block)
Class.new(DSL::Plugin::VersionVI, &block)
def self.nameify(name_str)
return name_str if name_str.is_a?(Symbol)

parts = name_str.split(/[^a-zA-Z0-9]/)
name = ""
parts.each do |part|
next if part.eql?("")
name << part.capitalize
end

raise ArgumentError, "Invalid plugin name: #{name_str}" if name.eql?("")
name.to_sym
end

# cross platform /dev/null
Expand Down Expand Up @@ -75,26 +126,17 @@ def has_run?
# version 7 plugin class
#=====================================================
class VersionVII < Plugin
attr_reader :version

def initialize(controller, source)
super(controller, source)
@version = :version7
end

def version
def self.version
:version7
end

def dependencies
self.class.depends_attrs
end

def provides(*paths)
Ohai::Log.warn("[UNSUPPORTED OPERATION] \'provides\' is no longer supported in a \'collect_data\' context. Please specify \'provides\' before collecting plugin data. Ignoring command \'provides #{paths.join(", ")}")
end

def require_plugin(*args)
Ohai::Log.warn("[UNSUPPORTED OPERATION] \'require_plugin\' is no longer supported. Please use \'depends\' instead.\nIgnoring plugin(s) #{args.join(", ")}")
end

def self.provides_attrs
@provides_attrs ||= []
end
Expand All @@ -103,6 +145,10 @@ def self.depends_attrs
@depends_attrs ||= []
end

def self.data_collector
@data_collector ||= Mash.new
end

def self.provides(*attrs)
attrs.each do |attr|
provides_attrs << attr
Expand All @@ -115,29 +161,61 @@ def self.depends(*attrs)
end
end

def self.depends_os(*attrs)
attrs.each do |attr|
depends_attrs << "#{Ohai::OS.collect_os}/#{attr}"
def self.collect_data(platform = :default, *other_platforms, &block)
[platform, other_platforms].flatten.each do |plat|
if data_collector.has_key?(plat)
Ohai::Log.warn("Already defined collect_data on platform #{plat}")
else
collector[plat] = block
end
end
end

def dependencies
self.class.depends_attrs
end

def run_plugin
collector = self.class.data_collector
platform = collect_os

if collector.has_key?(platform)
self.instance_eval(&collector[platform])
elsif collector.has_key?(:default)
self.instance_eval(&collector[:default])
else
Ohai::Log.debug("No data to collect for plugin #{self.class.name}. Continuing...")
end
end

def self.collect_data(&block)
define_method(:run_plugin, &block)
def provides(*paths)
Ohai::Log.warn("[UNSUPPORTED OPERATION] \'provides\' is no longer supported in a \'collect_data\' context. Please specify \'provides\' before collecting plugin data. Ignoring command \'provides #{paths.join(", ")}")
end

def require_plugin(*args)
Ohai::Log.warn("[UNSUPPORTED OPERATION] \'require_plugin\' is no longer supported. Please use \'depends\' instead.\nIgnoring plugin(s) #{args.join(", ")}")
end
end

#=====================================================
# version 6 plugin class
#=====================================================
class VersionVI < Plugin
attr_reader :version

def initialize(controller, source)
super(controller, source)
@version = :version6
end

def version
def self.version
:version6
end

def self.collect_contents(contents)
define_method(:run_plugin) { self.instance_eval(contents) }
end

def provides(*paths)
paths.each do |path|
parts = path.split("/")
Expand All @@ -158,9 +236,6 @@ def require_plugin(*args)
@controller.require_plugin(*args)
end

def self.collect_contents(contents)
define_method(:run_plugin) { self.instance_eval(contents) }
end
end

#=====================================================
Expand Down

0 comments on commit 076e429

Please sign in to comment.