class Ohai::DSL::Plugin
Attributes
data[R]
failed[R]
logger[R]
Public Class Methods
new(data, logger)
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 94 def initialize(data, logger) @data = data @logger = logger.with_child({ subsystem: "plugin", plugin: name }) @has_run = false @failed = false end
Public Instance Methods
[](key)
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 119 def [](key) @data[key] end
[]=(key, value)
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 123 def []=(key, value) @data[key] = value end
attribute?(name, *keys)
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 137 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 127 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 145 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 154 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 180 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 133 def has_key?(name) @data.key?(name) end
has_run?()
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 111 def has_run? @has_run end
hint?(name)
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 184 def hint?(name) Ohai::Hints.hint?(name) end
method_missing(name, *args)
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 200 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 115 def reset! @has_run = false end
run()
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 101 def run @has_run = true if Ohai.config[:disabled_plugins].include?(name) logger.trace("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 189 def safe_run run rescue Ohai::Exceptions::Error => e @failed = true raise e rescue => e @failed = true logger.trace("Plugin #{name} threw #{e.inspect}") e.backtrace.each { |line| logger.trace( line ) } end
set(name, *value)
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 141 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 164 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 |atts, key| atts[key] ||= Mash.new atts[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 208 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