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.



49
50
51
52
53
54
55
56
57
# 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. = { system: "ohai", version: Ohai::VERSION }
  reset_system
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#dataObject

Returns the value of attribute data.



40
41
42
# File 'lib/ohai/system.rb', line 40

def data
  @data
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#provides_mapObject (readonly)

Returns the value of attribute provides_map.



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

def provides_map
  @provides_map
end

Instance Method Details

#[](key) ⇒ Object



79
80
81
# File 'lib/ohai/system.rb', line 79

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



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

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)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ohai/system.rb', line 163

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



159
160
161
# File 'lib/ohai/system.rb', line 159

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


102
103
104
# File 'lib/ohai/system.rb', line 102

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ void

This method returns an undefined value.

Parameters:



140
141
142
143
144
145
146
147
# File 'lib/ohai/system.rb', line 140

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:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ohai/system.rb', line 112

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_jsonObject

Serialize this object as a hash



152
153
154
# File 'lib/ohai/system.rb', line 152

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