class Ohai::DSL::Plugin

Attributes

data[R]
failed[R]

Public Class Methods

new(data) click to toggle source
# File lib/ohai/dsl/plugin.rb, line 90
def initialize(data)
  @data = data
  @has_run = false
  @failed = false
end

Public Instance Methods

[](key) click to toggle source
# File lib/ohai/dsl/plugin.rb, line 114
def [](key)
  @data[key]
end
[]=(key, value) click to toggle source
# File lib/ohai/dsl/plugin.rb, line 118
def []=(key, value)
  @data[key] = value
end
attribute?(name, *keys) click to toggle source
# File lib/ohai/dsl/plugin.rb, line 132
def attribute?(name, *keys)
  !safe_get_attribute(name, *keys).nil?
end
each() { |key, value| ... } click to toggle source
# File lib/ohai/dsl/plugin.rb, line 122
def each(&block)
  @data.each do |key, value|
    yield(key, value)
  end
end
from(cmd) click to toggle source
# File lib/ohai/dsl/plugin.rb, line 140
def from(cmd)
  _status, stdout, _stderr = run_command(:command => cmd)
  return "" if stdout.nil? || stdout.empty?
  stdout.strip
end
from_with_regex(cmd, *regex_list) click to toggle source

Set the value equal to the stdout of the command, plus run through a regex - the first piece of match data is\ the value.

# File lib/ohai/dsl/plugin.rb, line 149
def from_with_regex(cmd, *regex_list)
  regex_list.flatten.each do |regex|
    _status, stdout, _stderr = run_command(:command => cmd)
    return "" if stdout.nil? || stdout.empty?
    stdout.chomp!.strip
    md = stdout.match(regex)
    return md[1]
  end
end
get_attribute(name, *keys) click to toggle source
# File lib/ohai/dsl/plugin.rb, line 175
def get_attribute(name, *keys)
  safe_get_attribute(name, *keys)
end
has_key?(name) click to toggle source
# File lib/ohai/dsl/plugin.rb, line 128
def has_key?(name)
  @data.has_key?(name)
end
has_run?() click to toggle source
# File lib/ohai/dsl/plugin.rb, line 106
def has_run?
  @has_run
end
hint?(name) click to toggle source
# File lib/ohai/dsl/plugin.rb, line 179
def hint?(name)
  Ohai::Hints.hint?(name)
end
method_missing(name, *args) click to toggle source
# File lib/ohai/dsl/plugin.rb, line 195
def method_missing(name, *args)
  return get_attribute(name) if args.length == 0

  set_attribute(name, *args)
end
reset!() click to toggle source
# File lib/ohai/dsl/plugin.rb, line 110
def reset!
  @has_run = false
end
run() click to toggle source
# File lib/ohai/dsl/plugin.rb, line 96
def run
  @has_run = true

  if Ohai.config[:disabled_plugins].include?(name)
    Ohai::Log.debug("Skipping disabled plugin #{name}")
  else
    run_plugin
  end
end
safe_run() click to toggle source

emulates the old plugin loading behavior

# File lib/ohai/dsl/plugin.rb, line 184
def safe_run
  run
rescue Ohai::Exceptions::Error => e
  @failed = true
  raise e
rescue => e
  @failed = true
  Ohai::Log.debug("Plugin #{name} threw #{e.inspect}")
  e.backtrace.each { |line| Ohai::Log.debug( line ) }
end
set(name, *value) click to toggle source
# File lib/ohai/dsl/plugin.rb, line 136
def set(name, *value)
  set_attribute(name, *value)
end
set_attribute(name, *attrs, value) click to toggle source
# File lib/ohai/dsl/plugin.rb, line 159
def set_attribute(name, *attrs, value)
  # Initialize the path in the @data Mash with new Mashes, if needed.
  # Will raise a TypeError if we hit a subattribute that is not a
  # Hash, Mash, or Array.
  keys = [name] + attrs
  attribute = keys[0..-2].inject(@data) do |attrs, key|
    attrs[key] ||= Mash.new
    attrs[key]
  end

  # Set the subattribute to the value.
  attr_name = attrs.empty? ? name : attrs[-1]
  attribute[attr_name] = value
  @data[name]
end

Private Instance Methods

safe_get_attribute(*keys) click to toggle source
# File lib/ohai/dsl/plugin.rb, line 203
def safe_get_attribute(*keys)
  keys.inject(@data) do |attrs, key|
    unless attrs.nil? || attrs.is_a?(Array) || attrs.is_a?(Hash)
      raise TypeError.new("Expected Hash but got #{attrs.class}.")
    end
    attrs[key]
  end
rescue NoMethodError
  # NoMethodError occurs when trying to access a key on nil
  nil
end