Class: Ohai::System

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ConstantHelper

#recursive_remove_constants, #remove_constants, #strict_const_defined?

Constructor Details

#initializeSystem

Returns a new instance of System.



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

def initialize
  @plugin_path = ""
  reset_system
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



36
37
38
# File 'lib/ohai/system.rb', line 36

def data
  @data
end

#provides_mapObject (readonly)

Returns the value of attribute provides_map.



37
38
39
# File 'lib/ohai/system.rb', line 37

def provides_map
  @provides_map
end

#v6_dependency_solverObject (readonly)

Returns the value of attribute v6_dependency_solver.



38
39
40
# File 'lib/ohai/system.rb', line 38

def v6_dependency_solver
  @v6_dependency_solver
end

Instance Method Details

#[](key) ⇒ Object



60
61
62
# File 'lib/ohai/system.rb', line 60

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

#all_plugins(attribute_filter = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/ohai/system.rb', line 64

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)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ohai/system.rb', line 184

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,Fixnum
    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

#have_v6_plugin?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def have_v6_plugin?(name)
  @v6_dependency_solver.values.any? {|v6plugin| v6plugin.name == name }
end

#json_pretty_print(item = nil) ⇒ Object

Pretty Print this object as JSON



180
181
182
# File 'lib/ohai/system.rb', line 180

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

#load_pluginsObject



74
75
76
# File 'lib/ohai/system.rb', line 74

def load_plugins
  @loader.load_all
end

#pathify_v6_plugin(plugin_name) ⇒ Object



106
107
108
109
# File 'lib/ohai/system.rb', line 106

def pathify_v6_plugin(plugin_name)
  path_components = plugin_name.split("::")
  File.join(path_components) + ".rb"
end

#refresh_plugins(attribute_filter = nil) ⇒ Object

Re-runs plugins that provide the attributes specified by attribute_filter. If attribute_filter is not given, re-runs all plugins.

Note that dependencies will not be re-run, so you must specify all of the attributes you want refreshed in the attribute_filter

This method takes a naive approach to v6 plugins: it simply re-runs all of them whenever called.



162
163
164
165
166
167
168
# File 'lib/ohai/system.rb', line 162

def refresh_plugins(attribute_filter=nil)
  Ohai::Hints.refresh_hints()
  @provides_map.all_plugins(attribute_filter).each do |plugin|
    plugin.reset!
  end
  run_plugins(true, attribute_filter)
end

#require_plugin(plugin_ref, force = false) ⇒ Object

Below APIs are from V6. Make sure that you are not breaking backwards compatibility if you are changing any of the APIs below.



116
117
118
119
120
121
122
123
124
125
126
127
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 116

def require_plugin(plugin_ref, force=false)
  plugins = [ ]
  # This method is only callable by version 6 plugins.
  # First we check if there exists a v6 plugin that fulfills the dependency.
  if @v6_dependency_solver.has_key? pathify_v6_plugin(plugin_ref)
    # Note that: partial_path looks like Plugin::Name
    # keys for @v6_dependency_solver are in form 'plugin/name.rb'
    plugins << @v6_dependency_solver[pathify_v6_plugin(plugin_ref)]
  else
    # While looking up V7 plugins we need to convert the plugin_ref to an attribute.
    attribute = plugin_ref.gsub("::", "/")
    begin
      plugins = @provides_map.find_providers_for([attribute])
    rescue Ohai::Exceptions::AttributeNotFound
      Ohai::Log.debug("Can not find any v7 plugin that provides #{attribute}")
      plugins = [ ]
    end
  end

  if plugins.empty?
    raise Ohai::Exceptions::DependencyNotFound, "Can not find a plugin for dependency #{plugin_ref}"
  else
    plugins.each do |plugin|
      begin
        @runner.run_plugin(plugin)
      rescue SystemExit, Interrupt
        raise
      rescue Ohai::Exceptions::DependencyCycle, Ohai::Exceptions::AttributeNotFound => e
        Ohai::Log.error("Encountered error while running plugins: #{e.inspect}")
        raise
      rescue Exception,Errno::ENOENT => e
        Ohai::Log.debug("Plugin #{plugin.name} threw exception #{e.inspect} #{e.backtrace.join("\n")}")
      end
    end
  end
end

#reset_systemObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ohai/system.rb', line 45

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

  @v6_dependency_solver = Hash.new

  @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_plugins(safe = false, attribute_filter = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ohai/system.rb', line 78

def run_plugins(safe = false, attribute_filter = nil)
  # First run all the version 6 plugins
  @v6_dependency_solver.values.each do |v6plugin|
    @runner.run_plugin(v6plugin)
  end

  # Users who are migrating from ohai 6 may give one or more Ohai 6 plugin
  # names as the +attribute_filter+. In this case we return early because
  # the v7 plugin provides map will not have an entry for this plugin.
  if attribute_filter and Array(attribute_filter).all? {|filter_item| have_v6_plugin?(filter_item) }
    return true
  end

  # Then run all the version 7 plugins
  begin
    @provides_map.all_plugins(attribute_filter).each { |plugin|
      @runner.run_plugin(plugin)
    }
  rescue Ohai::Exceptions::AttributeNotFound, Ohai::Exceptions::DependencyCycle => e
    Ohai::Log.error("Encountered error while running plugins: #{e.inspect}")
    raise
  end
end

#to_jsonObject

Serialize this object as a hash



173
174
175
# File 'lib/ohai/system.rb', line 173

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