| 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 207
207: def self.json_create(o)
208: ohai = new
209: o.each do |key, value|
210: ohai.data[key] = value unless key == "json_class"
211: end
212: ohai
213: end
# File lib/ohai/system.rb, line 34
34: def initialize
35: @data = Mash.new
36: @seen_plugins = Hash.new
37: @providers = Mash.new
38: @plugin_path = ""
39: end
# File lib/ohai/system.rb, line 105
105: def all_plugins
106: require_plugin('os')
107:
108: Ohai::Config[:plugin_path].each do |path|
109: [
110: Dir[File.join(path, '*')],
111: Dir[File.join(path, @data[:os], '**', '*')]
112: ].flatten.each do |file|
113: file_regex = Regexp.new("#{path}#{File::SEPARATOR}(.+).rb$")
114: md = file_regex.match(file)
115: if md
116: plugin_name = md[1].gsub(File::SEPARATOR, "::")
117: require_plugin(plugin_name) unless @seen_plugins.has_key?(plugin_name)
118: end
119: end
120: end
121: end
# File lib/ohai/system.rb, line 203
203: def attributes_print(a)
204: JSON.pretty_generate(@data[a])
205: end
# File lib/ohai/system.rb, line 123
123: def collect_providers(providers)
124: refreshments = []
125: if providers.is_a?(Mash)
126: providers.keys.each do |provider|
127: if provider.eql?("_providers")
128: refreshments << providers[provider]
129: else
130: refreshments << collect_providers(providers[provider])
131: end
132: end
133: else
134: refreshments << providers
135: end
136: refreshments.flatten.uniq
137: end
# File lib/ohai/system.rb, line 49
49: def each(&block)
50: @data.each do |key, value|
51: block.call(key, value)
52: end
53: end
# File lib/ohai/system.rb, line 63
63: def from(cmd)
64: status, stdout, stderr = run_command(:command => cmd)
65: return "" if stdout.nil?
66: stdout.chomp!.strip
67: 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 86
86: def from_with_regex(cmd, *regex_list)
87: regex_list.flatten.each do |regex|
88: status, stdout, stderr = run_command(:command => cmd)
89: return "" if stdout.nil?
90: stdout.chomp!.strip
91: md = stdout.match(regex)
92: return md[1]
93: end
94: end
Pretty Print this object as JSON
# File lib/ohai/system.rb, line 199
199: def json_pretty_print
200: JSON.pretty_generate(@data)
201: end
# File lib/ohai/system.rb, line 215
215: def method_missing(name, *args)
216: return get_attribute(name) if args.length == 0
217:
218: set_attribute(name, *args)
219: end
# File lib/ohai/system.rb, line 69
69: def provides(*paths)
70: paths.each do |path|
71: parts = path.split('/')
72: h = @providers
73: unless parts.length == 0
74: parts.shift if parts[0].length == 0
75: parts.each do |part|
76: h[part] ||= Mash.new
77: h = h[part]
78: end
79: end
80: h[:_providers] ||= []
81: h[:_providers] << @plugin_path
82: end
83: end
# File lib/ohai/system.rb, line 139
139: def refresh_plugins(path = '/')
140: parts = path.split('/')
141: if parts.length == 0
142: h = @providers
143: else
144: parts.shift if parts[0].length == 0
145: h = @providers
146: parts.each do |part|
147: break unless h.has_key?(part)
148: h = h[part]
149: end
150: end
151:
152: refreshments = collect_providers(h)
153: Ohai::Log.debug("Refreshing plugins: #{refreshments.join(", ")}")
154:
155: refreshments.each do |r|
156: @seen_plugins.delete(r) if @seen_plugins.has_key?(r)
157: end
158: refreshments.each do |r|
159: require_plugin(r) unless @seen_plugins.has_key?(r)
160: end
161: end
# File lib/ohai/system.rb, line 163
163: def require_plugin(plugin_name, force=false)
164: unless force
165: return true if @seen_plugins[plugin_name]
166: end
167:
168: @plugin_path = plugin_name
169:
170: filename = "#{plugin_name.gsub("::", File::SEPARATOR)}.rb"
171:
172: Ohai::Config[:plugin_path].each do |path|
173: check_path = File.expand_path(File.join(path, filename))
174: begin
175: @seen_plugins[plugin_name] = true
176: Ohai::Log.debug("Loading plugin #{plugin_name}")
177: from_file(check_path)
178: return true
179: rescue IOError => e
180: Ohai::Log.debug("No #{plugin_name} at #{check_path}")
181: rescue Exception,Errno::ENOENT => e
182: Ohai::Log.debug("Plugin #{plugin_name} threw exception #{e.inspect}")
183: end
184: end
185: end
# File lib/ohai/system.rb, line 59
59: def set(name, *value)
60: set_attribute(name, *value)
61: end
# File lib/ohai/system.rb, line 96
96: def set_attribute(name, *value)
97: @data[name] = *value
98: @data[name]
99: end
Serialize this object as a hash
# File lib/ohai/system.rb, line 192
192: def to_json(*a)
193: output = @data.clone
194: output["json_class"] = self.class.name
195: output.to_json(*a)
196: end