Class: Ohai::System

Inherits:
Object
  • Object
show all
Includes:
Mixin::ConstantHelper
Defined in:
lib/ohai/system.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ConstantHelper

#recursive_remove_constants, #remove_constants, #strict_const_defined?

Constructor Details

#initialize(config = {}) ⇒ System

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.



52
53
54
55
56
57
58
59
60
# File 'lib/ohai/system.rb', line 52

def initialize(config = {})
  @cli = config[:invoked_from_cli]
  @plugin_path = ""
  @config = config
  @failed_plugins = []
  @logger = config[:logger] || Ohai::Log.with_child
  @logger. = { system: "ohai", version: Ohai::VERSION }
  reset_system
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



43
44
45
# File 'lib/ohai/system.rb', line 43

def config
  @config
end

#dataObject

Returns the value of attribute data.



42
43
44
# File 'lib/ohai/system.rb', line 42

def data
  @data
end

#loggerObject (readonly)

Returns the value of attribute logger.



45
46
47
# File 'lib/ohai/system.rb', line 45

def logger
  @logger
end

#provides_mapObject (readonly)

Returns the value of attribute provides_map.



44
45
46
# File 'lib/ohai/system.rb', line 44

def provides_map
  @provides_map
end

#transport_connectionTrain::Transport

get backend parameters for target mode

Returns:

  • (Train::Transport)


118
119
120
# File 'lib/ohai/system.rb', line 118

def transport_connection
  @transport_connection ||= Ohai::TrainTransport.new(logger).build_transport&.connection
end

Instance Method Details

#[](key) ⇒ Object



88
89
90
# File 'lib/ohai/system.rb', line 88

def [](key)
  @data[key]
end

#all_plugins(attribute_filter = nil) ⇒ void

This method returns an undefined value.

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

Parameters:

  • attribute_filter (Array<String>) (defaults to: nil)

    the attributes to run. All will be run if not specified



98
99
100
101
102
103
104
105
106
# File 'lib/ohai/system.rb', line 98

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) ⇒ Object

Raises:

  • (ArgumentError)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ohai/system.rb', line 180

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) ⇒ Object

Pretty Print this object as JSON



176
177
178
# File 'lib/ohai/system.rb', line 176

def json_pretty_print(item = nil)
  FFI_Yajl::Encoder.new(pretty: true, validate_utf8: false).encode(item || @data)
end

#load_pluginsObject

load all plugins by calling Ohai::Loader.load_all

See Also:

  • Loader.load_all


111
112
113
# File 'lib/ohai/system.rb', line 111

def load_plugins
  @loader.load_all
end

#reset_systemvoid

This method returns an undefined value.

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ohai/system.rb', line 66

def reset_system
  @data = Mash.new
  @provides_map = ProvidesMap.new

  configure_ohai
  configure_logging if @cli

  @loader = Ohai::Loader.new(self)

  Ohai::Hints.refresh_hints

  # Remove the previously defined plugins
  recursive_remove_constants(Ohai::NamedPlugin)
end

#run_additional_plugins(plugin_path) ⇒ void

This method returns an undefined value.

Parameters:



157
158
159
160
161
162
163
164
# File 'lib/ohai/system.rb', line 157

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) ⇒ Mash

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

Parameters:

  • safe (Boolean) (defaults to: false)
  • attribute_filter (Array<String>) (defaults to: nil)

    the attributes to run. All will be run if not specified

Returns:



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ohai/system.rb', line 128

def run_plugins(safe = false, attribute_filter = nil)
  begin
    @provides_map.all_plugins(attribute_filter).each do |plugin|
      runner.run_plugin(plugin)
    end
    transport_connection.close unless transport_connection.nil?
  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

#runnerObject



81
82
83
84
85
86
# File 'lib/ohai/system.rb', line 81

def runner
  @runner ||=
    Ohai::Runner.new(self, true).tap do |runner|
      runner.transport_connection = transport_connection unless transport_connection.nil?
    end
end

#to_jsonObject

Serialize this object as a hash



169
170
171
# File 'lib/ohai/system.rb', line 169

def to_json
  FFI_Yajl::Encoder.new.encode(@data)
end