Class: Ohai::DSL::Plugin

Inherits:
Object
  • Object
show all
Includes:
Mixin::Command, Mixin::OS, Mixin::SecondsToHuman, Util::FileHelper
Defined in:
lib/ohai/dsl/plugin.rb,
lib/ohai/dsl/plugin/versionvii.rb

Direct Known Subclasses

VersionVII

Defined Under Namespace

Classes: VersionVII

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::FileHelper

#which

Methods included from Mixin::SecondsToHuman

#seconds_to_human

Methods included from Mixin::Command

shell_out

Methods included from Mixin::OS

collect_os

Constructor Details

#initialize(data, logger) ⇒ Plugin

Returns a new instance of Plugin.



94
95
96
97
98
99
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



202
203
204
205
206
# File 'lib/ohai/dsl/plugin.rb', line 202

def method_missing(name, *args)
  return get_attribute(name) if args.length == 0

  set_attribute(name, *args)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



90
91
92
# File 'lib/ohai/dsl/plugin.rb', line 90

def data
  @data
end

#failedObject (readonly)

Returns the value of attribute failed.



91
92
93
# File 'lib/ohai/dsl/plugin.rb', line 91

def failed
  @failed
end

#loggerObject (readonly)

Returns the value of attribute logger.



92
93
94
# File 'lib/ohai/dsl/plugin.rb', line 92

def logger
  @logger
end

Instance Method Details

#[](key) ⇒ Object



119
120
121
# File 'lib/ohai/dsl/plugin.rb', line 119

def [](key)
  @data[key]
end

#[]=(key, value) ⇒ Object



123
124
125
# File 'lib/ohai/dsl/plugin.rb', line 123

def []=(key, value)
  @data[key] = value
end

#attribute?(name, *keys) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/ohai/dsl/plugin.rb', line 137

def attribute?(name, *keys)
  !safe_get_attribute(name, *keys).nil?
end

#each(&block) ⇒ Object



127
128
129
130
131
# File 'lib/ohai/dsl/plugin.rb', line 127

def each(&block)
  @data.each do |key, value|
    yield(key, value)
  end
end

#from(cmd) ⇒ Object



145
146
147
148
149
150
# 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) ⇒ Object

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



155
156
157
158
159
160
161
162
163
164
# File 'lib/ohai/dsl/plugin.rb', line 155

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) ⇒ Object



182
183
184
# File 'lib/ohai/dsl/plugin.rb', line 182

def get_attribute(name, *keys)
  safe_get_attribute(name, *keys)
end

#has_key?(name) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/ohai/dsl/plugin.rb', line 133

def has_key?(name)
  @data.key?(name)
end

#has_run?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/ohai/dsl/plugin.rb', line 111

def has_run?
  @has_run
end

#hint?(name) ⇒ Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/ohai/dsl/plugin.rb', line 186

def hint?(name)
  Ohai::Hints.hint?(name)
end

#reset!Object



115
116
117
# File 'lib/ohai/dsl/plugin.rb', line 115

def reset!
  @has_run = false
end

#runObject



101
102
103
104
105
106
107
108
109
# 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_runObject

emulates the old plugin loading behavior



191
192
193
194
195
196
197
198
199
200
# File 'lib/ohai/dsl/plugin.rb', line 191

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) ⇒ Object



141
142
143
# File 'lib/ohai/dsl/plugin.rb', line 141

def set(name, *value)
  set_attribute(name, *value)
end

#set_attribute(name, *attrs, value) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ohai/dsl/plugin.rb', line 166

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