Class: Ohai::DSL::Plugin
Defined Under Namespace
Classes: VersionVII
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#[](key) ⇒ Object
-
#[]=(key, value) ⇒ Object
-
#attribute?(name, *keys) ⇒ Boolean
-
#each(&block) ⇒ Object
-
#from(cmd) ⇒ Object
-
#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.
-
#get_attribute(name, *keys) ⇒ Object
-
#has_key?(name) ⇒ Boolean
-
#has_run? ⇒ Boolean
-
#hint?(name) ⇒ Boolean
-
#initialize(data, logger) ⇒ Plugin
constructor
A new instance of Plugin.
-
#method_missing(name, *args) ⇒ Object
-
#reset! ⇒ Object
-
#run ⇒ Object
-
#safe_run ⇒ Object
emulates the old plugin loading behavior.
-
#set(name, *value) ⇒ Object
-
#set_attribute(name, *attrs, value) ⇒ Object
#which
#seconds_to_human
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
#data ⇒ Object
Returns the value of attribute data.
90
91
92
|
# File 'lib/ohai/dsl/plugin.rb', line 90
def data
@data
end
|
#failed ⇒ Object
Returns the value of attribute failed.
91
92
93
|
# File 'lib/ohai/dsl/plugin.rb', line 91
def failed
@failed
end
|
#logger ⇒ Object
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
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
133
134
135
|
# File 'lib/ohai/dsl/plugin.rb', line 133
def has_key?(name)
@data.key?(name)
end
|
#has_run? ⇒ Boolean
111
112
113
|
# File 'lib/ohai/dsl/plugin.rb', line 111
def has_run?
@has_run
end
|
#hint?(name) ⇒ 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
|
#run ⇒ Object
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_run ⇒ Object
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)
keys = [name] + attrs
attribute = keys[0..-2].inject(@data) do |atts, key|
atts[key] ||= Mash.new
atts[key]
end
attr_name = attrs.empty? ? name : attrs[-1]
attribute[attr_name] = value
@data[name]
end
|