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 238 238: def self.json_create(o) 239: ohai = new 240: o.each do |key, value| 241: ohai.data[key] = value unless key == "json_class" 242: end 243: ohai 244: 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: end
# File lib/ohai/system.rb, line 114 114: def all_plugins 115: require_plugin('os') 116: 117: Ohai::Config[:plugin_path].each do |path| 118: [ 119: Dir[File.join(path, '*')], 120: Dir[File.join(path, @data[:os], '**', '*')] 121: ].flatten.each do |file| 122: file_regex = Regexp.new("#{path}#{File::SEPARATOR}(.+).rb$") 123: md = file_regex.match(file) 124: if md 125: plugin_name = md[1].gsub(File::SEPARATOR, "::") 126: require_plugin(plugin_name) unless @seen_plugins.has_key?(plugin_name) 127: end 128: end 129: end 130: unless RUBY_PLATFORM =~ /mswin|mingw32|windows/ 131: # Catch any errant children who need to be reaped 132: begin 133: true while Process.wait(-1, Process::WNOHANG) 134: rescue Errno::ECHILD 135: end 136: end 137: true 138: end
# File lib/ohai/system.rb, line 225 225: def attributes_print(a) 226: raise ArgumentError, "I cannot find an attribute named #{a}!" unless @data.has_key?(a) 227: case a 228: when Hash,Mash,Array 229: JSON.pretty_generate(@data[a]) 230: when String 231: JSON.pretty_generate(@data[a].to_a) 232: else 233: raise ArgumentError, "I can only generate JSON for Hashes, Mashes, Arrays and Strings. You fed me a #{@data[a].class}!" 234: end 235: end
# File lib/ohai/system.rb, line 140 140: def collect_providers(providers) 141: refreshments = [] 142: if providers.is_a?(Mash) 143: providers.keys.each do |provider| 144: if provider.eql?("_providers") 145: refreshments << providers[provider] 146: else 147: refreshments << collect_providers(providers[provider]) 148: end 149: end 150: else 151: refreshments << providers 152: end 153: refreshments.flatten.uniq 154: end
# File lib/ohai/system.rb, line 58 58: def each(&block) 59: @data.each do |key, value| 60: block.call(key, value) 61: end 62: end
# File lib/ohai/system.rb, line 72 72: def from(cmd) 73: status, stdout, stderr = run_command(:command => cmd) 74: return "" if stdout.nil? || stdout.empty? 75: stdout.chomp!.strip 76: 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 95 95: def from_with_regex(cmd, *regex_list) 96: regex_list.flatten.each do |regex| 97: status, stdout, stderr = run_command(:command => cmd) 98: return "" if stdout.nil? || stdout.empty? 99: stdout.chomp!.strip 100: md = stdout.match(regex) 101: return md[1] 102: end 103: end
Pretty Print this object as JSON
# File lib/ohai/system.rb, line 221 221: def json_pretty_print 222: JSON.pretty_generate(@data) 223: end
# File lib/ohai/system.rb, line 246 246: def method_missing(name, *args) 247: return get_attribute(name) if args.length == 0 248: 249: set_attribute(name, *args) 250: end
# File lib/ohai/system.rb, line 78 78: def provides(*paths) 79: paths.each do |path| 80: parts = path.split('/') 81: h = @providers 82: unless parts.length == 0 83: parts.shift if parts[0].length == 0 84: parts.each do |part| 85: h[part] ||= Mash.new 86: h = h[part] 87: end 88: end 89: h[:_providers] ||= [] 90: h[:_providers] << @plugin_path 91: end 92: end
# File lib/ohai/system.rb, line 156 156: def refresh_plugins(path = '/') 157: parts = path.split('/') 158: if parts.length == 0 159: h = @providers 160: else 161: parts.shift if parts[0].length == 0 162: h = @providers 163: parts.each do |part| 164: break unless h.has_key?(part) 165: h = h[part] 166: end 167: end 168: 169: refreshments = collect_providers(h) 170: Ohai::Log.debug("Refreshing plugins: #{refreshments.join(", ")}") 171: 172: refreshments.each do |r| 173: @seen_plugins.delete(r) if @seen_plugins.has_key?(r) 174: end 175: refreshments.each do |r| 176: require_plugin(r) unless @seen_plugins.has_key?(r) 177: end 178: end
# File lib/ohai/system.rb, line 180 180: def require_plugin(plugin_name, force=false) 181: unless force 182: return true if @seen_plugins[plugin_name] 183: end 184: 185: if Ohai::Config[:disabled_plugins].include?(plugin_name) 186: Ohai::Log.debug("Skipping disabled plugin #{plugin_name}") 187: return false 188: end 189: 190: @plugin_path = plugin_name 191: 192: filename = "#{plugin_name.gsub("::", File::SEPARATOR)}.rb" 193: 194: Ohai::Config[:plugin_path].each do |path| 195: check_path = File.expand_path(File.join(path, filename)) 196: begin 197: @seen_plugins[plugin_name] = true 198: Ohai::Log.debug("Loading plugin #{plugin_name}") 199: from_file(check_path) 200: return true 201: rescue IOError => e 202: Ohai::Log.debug("No #{plugin_name} at #{check_path}") 203: rescue Exception,Errno::ENOENT => e 204: Ohai::Log.debug("Plugin #{plugin_name} threw exception #{e.inspect} #{e.backtrace.join("\n")}") 205: end 206: end 207: end
# File lib/ohai/system.rb, line 68 68: def set(name, *value) 69: set_attribute(name, *value) 70: end
# File lib/ohai/system.rb, line 105 105: def set_attribute(name, *values) 106: @data[name] = Array18(*values) 107: @data[name] 108: end
Serialize this object as a hash
# File lib/ohai/system.rb, line 214 214: def to_json(*a) 215: output = @data.clone 216: output["json_class"] = self.class.name 217: output.to_json(*a) 218: end