Class | Ohai::System |
In: |
lib/ohai/system.rb
|
Parent: | Object |
data | [RW] | |
hints | [RW] | |
seen_plugins | [RW] |
# File lib/ohai/system.rb, line 35 35: def initialize 36: @data = Mash.new 37: @seen_plugins = Hash.new 38: @providers = Mash.new 39: @plugin_path = "" 40: @hints = Hash.new 41: end
# File lib/ohai/system.rb, line 128 128: def all_plugins 129: require_plugin('os') 130: 131: Ohai::Config[:plugin_path].each do |path| 132: [ 133: Dir[File.join(path, '*')], 134: Dir[File.join(path, @data[:os], '**', '*')] 135: ].flatten.each do |file| 136: file_regex = Regexp.new("#{path}#{File::SEPARATOR}(.+).rb$") 137: md = file_regex.match(file) 138: if md 139: plugin_name = md[1].gsub(File::SEPARATOR, "::") 140: require_plugin(plugin_name) unless @seen_plugins.has_key?(plugin_name) 141: end 142: end 143: end 144: unless RUBY_PLATFORM =~ /mswin|mingw32|windows/ 145: # Catch any errant children who need to be reaped 146: begin 147: true while Process.wait(-1, Process::WNOHANG) 148: rescue Errno::ECHILD 149: end 150: end 151: true 152: end
# File lib/ohai/system.rb, line 242 242: def attributes_print(a) 243: data = @data 244: a.split("/").each do |part| 245: data = data[part] 246: end 247: raise ArgumentError, "I cannot find an attribute named #{a}!" if data.nil? 248: case data 249: when Hash,Mash,Array,Fixnum 250: json_pretty_print(data) 251: when String 252: if data.respond_to?(:lines) 253: json_pretty_print(data.lines.to_a) 254: else 255: json_pretty_print(data.to_a) 256: end 257: else 258: raise ArgumentError, "I can only generate JSON for Hashes, Mashes, Arrays and Strings. You fed me a #{data.class}!" 259: end 260: end
# File lib/ohai/system.rb, line 154 154: def collect_providers(providers) 155: refreshments = [] 156: if providers.is_a?(Mash) 157: providers.keys.each do |provider| 158: if provider.eql?("_providers") 159: refreshments << providers[provider] 160: else 161: refreshments << collect_providers(providers[provider]) 162: end 163: end 164: else 165: refreshments << providers 166: end 167: refreshments.flatten.uniq 168: end
# File lib/ohai/system.rb, line 51 51: def each(&block) 52: @data.each do |key, value| 53: block.call(key, value) 54: end 55: end
# File lib/ohai/system.rb, line 65 65: def from(cmd) 66: status, stdout, stderr = run_command(:command => cmd) 67: return "" if stdout.nil? || stdout.empty? 68: stdout.strip 69: 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 88 88: def from_with_regex(cmd, *regex_list) 89: regex_list.flatten.each do |regex| 90: status, stdout, stderr = run_command(:command => cmd) 91: return "" if stdout.nil? || stdout.empty? 92: stdout.chomp!.strip 93: md = stdout.match(regex) 94: return md[1] 95: end 96: end
# File lib/ohai/system.rb, line 107 107: def hint?(name) 108: @json_parser ||= Yajl::Parser.new 109: 110: return @hints[name] if @hints[name] 111: 112: Ohai::Config[:hints_path].each do |path| 113: filename = File.join(path, "#{name}.json") 114: if File.exist?(filename) 115: begin 116: hash = @json_parser.parse(File.read(filename)) 117: @hints[name] = hash || Hash.new # hint should exist because the file did, even if it didn't contain anything 118: rescue Yajl::ParseError => e 119: Ohai::Log.error("Could not parse hint file at #{filename}: #{e.message}") 120: end 121: end 122: end 123: 124: @hints[name] 125: end
Pretty Print this object as JSON
# File lib/ohai/system.rb, line 238 238: def json_pretty_print(item=nil) 239: Yajl::Encoder.new(:pretty => true).encode(item || @data) 240: end
# File lib/ohai/system.rb, line 262 262: def method_missing(name, *args) 263: return get_attribute(name) if args.length == 0 264: 265: set_attribute(name, *args) 266: end
# File lib/ohai/system.rb, line 71 71: def provides(*paths) 72: paths.each do |path| 73: parts = path.split('/') 74: h = @providers 75: unless parts.length == 0 76: parts.shift if parts[0].length == 0 77: parts.each do |part| 78: h[part] ||= Mash.new 79: h = h[part] 80: end 81: end 82: h[:_providers] ||= [] 83: h[:_providers] << @plugin_path 84: end 85: end
# File lib/ohai/system.rb, line 170 170: def refresh_plugins(path = '/') 171: parts = path.split('/') 172: if parts.length == 0 173: h = @providers 174: else 175: parts.shift if parts[0].length == 0 176: h = @providers 177: parts.each do |part| 178: break unless h.has_key?(part) 179: h = h[part] 180: end 181: end 182: 183: refreshments = collect_providers(h) 184: Ohai::Log.debug("Refreshing plugins: #{refreshments.join(", ")}") 185: 186: # remove the hints cache 187: @hints = Hash.new 188: 189: refreshments.each do |r| 190: @seen_plugins.delete(r) if @seen_plugins.has_key?(r) 191: end 192: refreshments.each do |r| 193: require_plugin(r) unless @seen_plugins.has_key?(r) 194: end 195: end
# File lib/ohai/system.rb, line 197 197: def require_plugin(plugin_name, force=false) 198: unless force 199: return true if @seen_plugins[plugin_name] 200: end 201: 202: if Ohai::Config[:disabled_plugins].include?(plugin_name) 203: Ohai::Log.debug("Skipping disabled plugin #{plugin_name}") 204: return false 205: end 206: 207: @plugin_path = plugin_name 208: 209: filename = "#{plugin_name.gsub("::", File::SEPARATOR)}.rb" 210: 211: Ohai::Config[:plugin_path].each do |path| 212: check_path = File.expand_path(File.join(path, filename)) 213: begin 214: @seen_plugins[plugin_name] = true 215: Ohai::Log.debug("Loading plugin #{plugin_name}") 216: from_file(check_path) 217: return true 218: rescue Errno::ENOENT => e 219: Ohai::Log.debug("No #{plugin_name} at #{check_path}") 220: rescue SystemExit, Interrupt 221: raise 222: rescue Exception,Errno::ENOENT => e 223: Ohai::Log.debug("Plugin #{plugin_name} threw exception #{e.inspect} #{e.backtrace.join("\n")}") 224: end 225: end 226: end
# File lib/ohai/system.rb, line 61 61: def set(name, *value) 62: set_attribute(name, *value) 63: end
# File lib/ohai/system.rb, line 98 98: def set_attribute(name, *values) 99: @data[name] = Array18(*values) 100: @data[name] 101: end
Serialize this object as a hash
# File lib/ohai/system.rb, line 233 233: def to_json 234: Yajl::Encoder.new.encode(@data) 235: end