Class: Ohai::Application

Inherits:
Object
  • Object
show all
Includes:
Mixlib::CLI
Defined in:
lib/ohai/application.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit!(msg, err = -1)) ⇒ Object

Log a debug message to the Logger and then exit the application

Parameters:

  • msg (String)

    the message to log

  • err (Integer) (defaults to: -1))

    the exit code



171
172
173
174
# File 'lib/ohai/application.rb', line 171

def exit!(msg, err = -1)
  Ohai::Log.debug(msg)
  Process.exit err
end

.fatal!(msg, err = -1)) ⇒ Object

Log a fatal error message to both STDERR and the Logger, exit the application

Parameters:

  • msg (String)

    the message to log

  • err (Integer) (defaults to: -1))

    the exit code



162
163
164
165
166
# File 'lib/ohai/application.rb', line 162

def fatal!(msg, err = -1)
  STDERR.puts("FATAL: #{msg}")
  Ohai::Log.fatal(msg)
  Process.exit err
end

Instance Method Details

#config_file_defaultsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



122
123
124
# File 'lib/ohai/application.rb', line 122

def config_file_defaults
  Ohai::Config.save(true)
end

#config_file_settingsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



127
128
129
# File 'lib/ohai/application.rb', line 127

def config_file_settings
  Ohai::Config.save(false)
end

#configure_ohaiObject

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

Returns:

  • void



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ohai/application.rb', line 100

def configure_ohai
  @attributes = parse_options
  @attributes = nil if @attributes.empty?

  load_workstation_config

  merge_configs

  if config[:target]
    Ohai::Config.target_mode.host = config[:target]
    if URI.parse(Ohai::Config.target_mode.host).scheme
      train_config = Train.unpack_target_from_uri(Ohai::Config.target_mode.host)
      Ohai::Config.target_mode = train_config
    end
    Ohai::Config.target_mode.enabled = true
    Ohai::Config.node_name = Ohai::Config.target_mode.host unless Ohai::Config.node_name
  end

  Ohai::Log.init(Ohai.config[:log_location])
end

#merge_configsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See lib/chef/knife.rb in the chef/chef github repo



134
135
136
137
# File 'lib/ohai/application.rb', line 134

def merge_configs
  config.replace(config_file_defaults.merge(default_config).merge(config_file_settings).merge(config))
  Ohai::Config.merge!(config) # make them both the same
end

#runObject

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

Returns:

  • void



89
90
91
92
93
94
95
# File 'lib/ohai/application.rb', line 89

def run
  elapsed = Benchmark.realtime do
    configure_ohai
    run_application
  end
  Ohai::Log.debug("Ohai took #{elapsed} total seconds to run.")
end

#run_applicationObject

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

Returns:

  • void



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ohai/application.rb', line 143

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