class Ohai::DSL::Plugin
Attributes
data[R]
failed[R]
logger[R]
transport_connection[RW]
Public Class Methods
new(data, logger)
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 98 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 127 def [](key) @data[key] end
[]=(key, value)
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 131 def []=(key, value) @data[key] = value end
attribute?(name, *keys)
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 145 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 135 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 153 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 163 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 190 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 141 def has_key?(name) @data.key?(name) end
has_run?()
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 119 def has_run? @has_run end
hint?(name)
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 194 def hint?(name) Ohai::Hints.hint?(name) end
method_missing(name, *args)
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 210 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 123 def reset! @has_run = false end
run()
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 109 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 199 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 149 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 174 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
target_mode?()
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 105 def target_mode? !!@transport_connection end
Private Instance Methods
safe_get_attribute(*keys)
click to toggle source
# File lib/ohai/dsl/plugin.rb, line 218 def safe_get_attribute(*keys) keys.inject(@data) do |attrs, key| unless attrs.nil? || attrs.is_a?(Array) || attrs.is_a?(Hash) raise TypeError, "Expected Hash but got #{attrs.class}." end attrs[key] end rescue NoMethodError # NoMethodError occurs when trying to access a key on nil nil end