class Ohai::System

The class used by Ohai::Application and Chef to actually collect data

Attributes

config[R]
data[RW]
logger[R]
provides_map[R]

Public Class Methods

new(config = {}) click to toggle source

the cli flag is used to determine if we're being constructed by something like chef-client (which doesn't set this flag) and which sets up its own loggers, or if we're coming from Ohai::Application and therefore need to configure Ohai's own logger.

# File lib/ohai/system.rb, line 49
def initialize(config = {})
  @cli = config[:invoked_from_cli]
  @plugin_path = ""
  @config = config
  @failed_plugins = []
  @logger = config[:logger] || Ohai::Log.with_child
  @logger.metadata = { system: "ohai", version: Ohai::VERSION }
  reset_system
end

Public Instance Methods

[](key) click to toggle source
# File lib/ohai/system.rb, line 79
def [](key)
  @data[key]
end
all_plugins(attribute_filter = nil) click to toggle source

Resets the system and loads then runs the plugins. This is the primary method called to run the system.

@param [Array<String>] attribute_filter the attributes to run. All will be run if not specified @return [void]

# File lib/ohai/system.rb, line 88
def all_plugins(attribute_filter = nil)
  # Reset the system when all_plugins is called since this function
  # can be run multiple times in order to pick up any changes in the
  # config or plugins with Chef.
  reset_system

  load_plugins
  run_plugins(true, attribute_filter)
end
attributes_print(a) click to toggle source
# File lib/ohai/system.rb, line 157
def attributes_print(a)
  data = @data
  a.split("/").each do |part|
    data = data[part]
  end
  raise ArgumentError, "I cannot find an attribute named #{a}!" if data.nil?
  case data
  when Hash, Mash, Array, Integer
    json_pretty_print(data)
  when String
    if data.respond_to?(:lines)
      json_pretty_print(data.lines.to_a)
    else
      json_pretty_print(data.to_a)
    end
  else
    raise ArgumentError, "I can only generate JSON for Hashes, Mashes, Arrays and Strings. You fed me a #{data.class}!"
  end
end
json_pretty_print(item = nil) click to toggle source

Pretty Print this object as JSON

# File lib/ohai/system.rb, line 153
def json_pretty_print(item = nil)
  FFI_Yajl::Encoder.new(pretty: true, validate_utf8: false).encode(item || @data)
end
load_plugins() click to toggle source

load all plugins by calling Ohai::Loader.load_all @see Ohai::Loader.load_all

# File lib/ohai/system.rb, line 100
def load_plugins
  @loader.load_all
end
reset_system() click to toggle source

clears the current collected data, clears the provides map for plugins, refreshes hints, and reconfigures ohai. In short this gets Ohai into a first run state

@return [void]

# File lib/ohai/system.rb, line 63
def reset_system
  @data = Mash.new
  @provides_map = ProvidesMap.new

  configure_ohai
  configure_logging if @cli

  @loader = Ohai::Loader.new(self)
  @runner = Ohai::Runner.new(self, true)

  Ohai::Hints.refresh_hints

  # Remove the previously defined plugins
  recursive_remove_constants(Ohai::NamedPlugin)
end
run_additional_plugins(plugin_path) click to toggle source
# File lib/ohai/system.rb, line 134
def run_additional_plugins(plugin_path)
  @loader.load_additional(plugin_path).each do |plugin|
    logger.trace "Running plugin #{plugin}"
    @runner.run_plugin(plugin)
  end

  freeze_strings!
end
run_plugins(safe = false, attribute_filter = nil) click to toggle source

run all plugins or those that match the attribute filter is provided

@param safe [Boolean] @param [Array<String>] attribute_filter the attributes to run. All will be run if not specified

@return [Mash]

# File lib/ohai/system.rb, line 110
def run_plugins(safe = false, attribute_filter = nil)
  begin
    @provides_map.all_plugins(attribute_filter).each do |plugin|
      @runner.run_plugin(plugin)
    end
  rescue Ohai::Exceptions::AttributeNotFound, Ohai::Exceptions::DependencyCycle => e
    logger.error("Encountered error while running plugins: #{e.inspect}")
    raise
  end
  critical_failed = Ohai::Config.ohai[:critical_plugins] & @runner.failed_plugins
  unless critical_failed.empty?
    msg = "The following Ohai plugins marked as critical failed: #{critical_failed}"
    if @cli
      logger.error(msg)
      exit(true)
    else
      raise Ohai::Exceptions::CriticalPluginFailure, "#{msg}. Failing Chef run."
    end
  end

  # Freeze all strings.
  freeze_strings!
end
to_json() click to toggle source

Serialize this object as a hash

# File lib/ohai/system.rb, line 146
def to_json
  FFI_Yajl::Encoder.new.encode(@data)
end

Private Instance Methods

configure_logging() click to toggle source
# File lib/ohai/system.rb, line 194
def configure_logging
  if Ohai.config[:log_level] == :auto
    Ohai::Log.level = :info
  else
    Ohai::Log.level = Ohai.config[:log_level]
  end
end
configure_ohai() click to toggle source
# File lib/ohai/system.rb, line 179
def configure_ohai
  Ohai.config.merge!(@config)

  # add any additional CLI passed directories to the plugin path excluding duplicates
  unless Ohai.config[:directory].nil?
    # make sure the directory config is an array since it could be a string set in client.rb
    Array(Ohai.config[:directory]).each do |dir|
      next if Ohai.config[:plugin_path].include?(dir)
      Ohai.config[:plugin_path] << dir
    end
  end

  logger.debug("Running Ohai with the following configuration: #{Ohai.config.configuration}")
end
freeze_strings!() click to toggle source

Freeze all string values in @data. This makes them immutable and saves a bit of RAM.

@api private @return [void]

# File lib/ohai/system.rb, line 207
def freeze_strings!
  # Recursive visitor pattern helper.
  visitor = lambda do |val|
    case val
    when Hash
      val.each_value { |v| visitor.call(v) }
    when Array
      val.each { |v| visitor.call(v) }
    when String
      val.freeze
    end
  end
  visitor.call(@data)
end