class Ohai::Application

The Application class is what is called by the Ohai CLI binary. It handles:

- CLI options and attribute arguments
- Collecting data via the Ohai::System class
- Printing the results returned via the Ohai::System class

Public Class Methods

exit!(msg, err = -1) click to toggle source

Log a debug message to the Logger and then exit the application @param msg [String] the message to log @param err [Integer] the exit code

# File lib/ohai/application.rb, line 131
def exit!(msg, err = -1)
  Ohai::Log.debug(msg)
  Process.exit err
end
fatal!(msg, err = -1) click to toggle source

Log a fatal error message to both STDERR and the Logger, exit the application @param msg [String] the message to log @param err [Integer] the exit code

# File lib/ohai/application.rb, line 122
def fatal!(msg, err = -1)
  STDERR.puts("FATAL: #{msg}")
  Ohai::Log.fatal(msg)
  Process.exit err
end

Public Instance Methods

configure_ohai() click to toggle source

parses the CLI options, loads the config file if present, and initializes logging

@return void

# File lib/ohai/application.rb, line 90
def configure_ohai
  @attributes = parse_options
  @attributes = nil if @attributes.empty?

  load_workstation_config

  Ohai::Log.init(Ohai.config[:log_location])
end
run() click to toggle source

the method called by the Ohai binary to actually run the whole application

@return void

# File lib/ohai/application.rb, line 79
def run
  elapsed = Benchmark.measure do
    configure_ohai
    run_application
  end
  Ohai::Log.debug("Ohai took #{elapsed.total} total seconds to run.")
end
run_application() click to toggle source

Passes config and attributes arguments to Ohai::System then prints the results. Called by the run method after config / logging have been initialized

@return void

# File lib/ohai/application.rb, line 103
def run_application
  config[:invoked_from_cli] = true
  config[:logger] = Ohai::Log.with_child
  ohai = Ohai::System.new(config)
  ohai.all_plugins(@attributes)

  if @attributes
    @attributes.each do |a|
      puts ohai.attributes_print(a)
    end
  else
    puts ohai.json_pretty_print
  end
end

Private Instance Methods

load_workstation_config() click to toggle source
# File lib/ohai/application.rb, line 139
def load_workstation_config
  config_loader = ChefConfig::WorkstationConfigLoader.new(
    config[:config_file], Ohai::Log
  )
  begin
    config_loader.load
  rescue ChefConfig::ConfigurationError => config_error
    Ohai::Application.fatal!(config_error.message)
  end
end