Class: Ohai::DSL::Plugin

Inherits:
Object
  • Object
show all
Includes:
Mixin::OS, Mixin::SecondsToHuman, Mixin::ShellOut, Mixin::TrainHelpers, Mixin::Which
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 Mixin::TrainHelpers

#dir_glob

Methods included from Mixin::SecondsToHuman

#seconds_to_human

Methods included from Mixin::ShellOut

#shell_out

Methods included from Mixin::OS

#collect_os, #collect_os_local, #collect_os_target, #nonruby_target?

Constructor Details

#initialize(data, logger) ⇒ Plugin

Returns a new instance of Plugin.



98
99
100
101
102
103
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



210
211
212
213
214
# 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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#failedObject (readonly)

Returns the value of attribute failed.



94
95
96
# File 'lib/ohai/dsl/plugin.rb', line 94

def failed
  @failed
end

#loggerObject (readonly)

Returns the value of attribute logger.



95
96
97
# File 'lib/ohai/dsl/plugin.rb', line 95

def logger
  @logger
end

#transport_connectionObject

Returns the value of attribute transport_connection.



96
97
98
# File 'lib/ohai/dsl/plugin.rb', line 96

def transport_connection
  @transport_connection
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



131
132
133
# File 'lib/ohai/dsl/plugin.rb', line 131

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

#attribute?(name, *keys) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/ohai/dsl/plugin.rb', line 145

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

#each(&block) ⇒ Object



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

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

#from(cmd) ⇒ Object



153
154
155
156
157
158
# 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) ⇒ 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.



163
164
165
166
167
168
169
170
171
172
# 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) ⇒ Object



190
191
192
# File 'lib/ohai/dsl/plugin.rb', line 190

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

#has_key?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#has_run?Boolean

Returns:

  • (Boolean)


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

def has_run?
  @has_run
end

#hint?(name) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/ohai/dsl/plugin.rb', line 194

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

#reset!Object



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

def reset!
  @has_run = false
end

#runObject



109
110
111
112
113
114
115
116
117
# 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_runObject

emulates the old plugin loading behavior



199
200
201
202
203
204
205
206
207
208
# 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) ⇒ Object



149
150
151
# File 'lib/ohai/dsl/plugin.rb', line 149

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

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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# 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?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/ohai/dsl/plugin.rb', line 105

def target_mode?
  !!@transport_connection
end