| Class | Ohai::System |
| In: |
lib/ohai/system.rb
|
| Parent: | Object |
| data | [RW] | |
| seen_plugins | [RW] |
Create an Ohai::System from JSON
# File lib/ohai/system.rb, line 217
217: def self.json_create(o)
218: ohai = new
219: o.each do |key, value|
220: ohai.data[key] = value unless key == "json_class"
221: end
222: ohai
223: end
# File lib/ohai/system.rb, line 43
43: def initialize
44: @data = Mash.new
45: @seen_plugins = Hash.new
46: @providers = Mash.new
47: @plugin_path = ""
48: Ohai::Log.level(Ohai::Config.log_level)
49: end
# File lib/ohai/system.rb, line 115
115: def all_plugins
116: require_plugin('os')
117:
118: Ohai::Config[:plugin_path].each do |path|
119: [
120: Dir[File.join(path, '*')],
121: Dir[File.join(path, @data[:os], '**', '*')]
122: ].flatten.each do |file|
123: file_regex = Regexp.new("#{path}#{File::SEPARATOR}(.+).rb$")
124: md = file_regex.match(file)
125: if md
126: plugin_name = md[1].gsub(File::SEPARATOR, "::")
127: require_plugin(plugin_name) unless @seen_plugins.has_key?(plugin_name)
128: end
129: end
130: end
131: end
# File lib/ohai/system.rb, line 213
213: def attributes_print(a)
214: JSON.pretty_generate(@data[a])
215: end
# File lib/ohai/system.rb, line 133
133: def collect_providers(providers)
134: refreshments = []
135: if providers.is_a?(Mash)
136: providers.keys.each do |provider|
137: if provider.eql?("_providers")
138: refreshments << providers[provider]
139: else
140: refreshments << collect_providers(providers[provider])
141: end
142: end
143: else
144: refreshments << providers
145: end
146: refreshments.flatten.uniq
147: end
# File lib/ohai/system.rb, line 59
59: def each(&block)
60: @data.each do |key, value|
61: block.call(key, value)
62: end
63: end
# File lib/ohai/system.rb, line 73
73: def from(cmd)
74: status, stdout, stderr = run_command(:command => cmd)
75: return "" if stdout.nil?
76: stdout.chomp!.strip
77: end
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/system.rb, line 96
96: def from_with_regex(cmd, *regex_list)
97: regex_list.flatten.each do |regex|
98: status, stdout, stderr = run_command(:command => cmd)
99: return "" if stdout.nil?
100: stdout.chomp!.strip
101: md = stdout.match(regex)
102: return md[1]
103: end
104: end
Pretty Print this object as JSON
# File lib/ohai/system.rb, line 209
209: def json_pretty_print
210: JSON.pretty_generate(@data)
211: end
# File lib/ohai/system.rb, line 225
225: def method_missing(name, *args)
226: return get_attribute(name) if args.length == 0
227:
228: set_attribute(name, *args)
229: end
# File lib/ohai/system.rb, line 79
79: def provides(*paths)
80: paths.each do |path|
81: parts = path.split('/')
82: h = @providers
83: unless parts.length == 0
84: parts.shift if parts[0].length == 0
85: parts.each do |part|
86: h[part] ||= Mash.new
87: h = h[part]
88: end
89: end
90: h[:_providers] ||= []
91: h[:_providers] << @plugin_path
92: end
93: end
# File lib/ohai/system.rb, line 149
149: def refresh_plugins(path = '/')
150: parts = path.split('/')
151: if parts.length == 0
152: h = @providers
153: else
154: parts.shift if parts[0].length == 0
155: h = @providers
156: parts.each do |part|
157: break unless h.has_key?(part)
158: h = h[part]
159: end
160: end
161:
162: refreshments = collect_providers(h)
163: Ohai::Log.debug("Refreshing plugins: #{refreshments.join(", ")}")
164:
165: refreshments.each do |r|
166: @seen_plugins.delete(r) if @seen_plugins.has_key?(r)
167: end
168: refreshments.each do |r|
169: require_plugin(r) unless @seen_plugins.has_key?(r)
170: end
171: end
# File lib/ohai/system.rb, line 173
173: def require_plugin(plugin_name, force=false)
174: unless force
175: return true if @seen_plugins[plugin_name]
176: end
177:
178: @plugin_path = plugin_name
179:
180: filename = "#{plugin_name.gsub("::", File::SEPARATOR)}.rb"
181:
182: Ohai::Config[:plugin_path].each do |path|
183: check_path = File.expand_path(File.join(path, filename))
184: begin
185: @seen_plugins[plugin_name] = true
186: Ohai::Log.debug("Loading plugin #{plugin_name}")
187: from_file(check_path)
188: return true
189: rescue IOError => e
190: Ohai::Log.debug("No #{plugin_name} at #{check_path}")
191: rescue Exception,Errno::ENOENT => e
192: Ohai::Log.debug("Plugin #{plugin_name} threw exception #{e.inspect}")
193: end
194: end
195: end
# File lib/ohai/system.rb, line 69
69: def set(name, *value)
70: set_attribute(name, *value)
71: end
# File lib/ohai/system.rb, line 106
106: def set_attribute(name, *value)
107: @data[name] = *value
108: @data[name]
109: end
Serialize this object as a hash
# File lib/ohai/system.rb, line 202
202: def to_json(*a)
203: output = @data.clone
204: output["json_class"] = self.class.name
205: output.to_json(*a)
206: end