Class: Ohai::DSL::Plugin::VersionVII

Inherits:
Ohai::DSL::Plugin show all
Defined in:
lib/ohai/dsl/plugin/versionvii.rb

Overview

The class for the “Version 7” plugin format we introduced in Ohai 7. This is the 2nd generation of Ohai plugin and the previous generation (V6) was removed in Ohai 14

Instance Attribute Summary collapse

Attributes inherited from Ohai::DSL::Plugin

#data, #failed, #logger, #transport_connection

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Ohai::DSL::Plugin

#[], #[]=, #attribute?, #each, #from, #from_with_regex, #get_attribute, #has_key?, #has_run?, #hint?, #method_missing, #reset!, #run, #safe_run, #set, #set_attribute, #target_mode?

Methods included from Mixin::TrainHelpers

#dir_glob

Methods included from Mixin::SecondsToHuman

#seconds_to_human

Methods included from Mixin::ShellOut

#shell_out

Methods included from Mixin::OS

#collect_os, #collect_os_local, #collect_os_target, #nonruby_target?

Constructor Details

#initialize(data, logger) ⇒ VersionVII

Returns a new instance of VersionVII.



30
31
32
33
34
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 30

def initialize(data, logger)
  super(data, logger)
  @source = self.class.sources
  @version = :version7
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ohai::DSL::Plugin

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



28
29
30
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 28

def source
  @source
end

#versionObject (readonly)

Returns the value of attribute version.



27
28
29
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 27

def version
  @version
end

Class Method Details

.collect_data(*os_list, &block) ⇒ Object

define data collection methodology per os

Parameters:

  • os (Array<Symbol>)

    the list of oses to collect data for

  • block (block)

    the actual code to collect data for the specified os



111
112
113
114
115
116
117
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 111

def self.collect_data(*os_list, &block)
  os_list = [ :default ] if os_list.empty?
  os_list.flatten.each do |os|
    Ohai::Log.warn("collect_data already defined on os '#{os}' for #{self}, last plugin seen will be used") if data_collector.key?(os)
    data_collector[os] = block
  end
end

.data_collectorMash

A block per os for actually performing data collection constructed by the collect_data method

Returns:



70
71
72
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 70

def self.data_collector
  @data_collector ||= Mash.new
end

.depends(*attrs) ⇒ Object

set the attributes depended on by the plugin

Parameters:

  • attrs (Array)


86
87
88
89
90
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 86

def self.depends(*attrs)
  attrs.each do |attr|
    depends_attrs << attr unless depends_attrs.include?(attr)
  end
end

.depends_attrsObject



62
63
64
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 62

def self.depends_attrs
  @depends_attrs ||= []
end

.optional(opt = true) ⇒ Object

set the plugin optional state

Parameters:

  • opt (Boolean) (defaults to: true)


95
96
97
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 95

def self.optional(opt = true)
  @optional = opt
end

.optional?Boolean

check if the plugin is optional

Returns:

  • (Boolean)


102
103
104
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 102

def self.optional?
  !!@optional
end

.provides(*attrs) ⇒ Object

set the attributes provided by the plugin

Parameters:

  • attrs (Array)


77
78
79
80
81
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 77

def self.provides(*attrs)
  attrs.each do |attr|
    provides_attrs << attr unless provides_attrs.include?(attr)
  end
end

.provides_attrsObject



58
59
60
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 58

def self.provides_attrs
  @provides_attrs ||= []
end

.sourcesArray

The source of the plugin on disk. This is an array since a plugin may exist for multiple oses and this would include each of those os specific file paths

Returns:

  • (Array)


54
55
56
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 54

def self.sources
  @source_list ||= []
end

.versionSymbol

return that we’re a v7 plugin

Returns:

  • (Symbol)


46
47
48
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 46

def self.version
  :version7
end

Instance Method Details

#configuration(option, *options) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 159

def configuration(option, *options)
  return nil if plugin_config.nil? || !plugin_config.key?(option)

  value = plugin_config[option]
  options.each do |opt|
    return nil unless value.key?(opt)

    value = value[opt]
  end
  value
end

#dependenciesArray

Returns:

  • (Array)


120
121
122
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 120

def dependencies
  self.class.depends_attrs
end

#nameString

the plugin name we use through Ohai (Foo) vs. the class name (Ohai::NamedPlugin::Foo)

Returns:



39
40
41
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 39

def name
  self.class.name.split("Ohai::NamedPlugin::")[1].to_sym
end

#optional?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 147

def optional?
  self.class.optional?
end

#provides(*paths) ⇒ Object



151
152
153
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 151

def provides(*paths)
  logger.warn("[UNSUPPORTED OPERATION] \'provides\' is no longer supported in a \'collect_data\' context. Please specify \'provides\' before collecting plugin data. Ignoring command \'provides #{paths.join(", ")}")
end

#require_plugin(*args) ⇒ Object



155
156
157
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 155

def require_plugin(*args)
  logger.warn("[UNSUPPORTED OPERATION] \'require_plugin\' is no longer supported. Please use \'depends\' instead.\nIgnoring plugin(s) #{args.join(", ")}")
end

#run_pluginObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 124

def run_plugin
  collector = self.class.data_collector
  os = collect_os

  # :default - means any remote or local unix or windows host
  # :target  - means any remote API which is not unix/windows or otherwise rubyable (cisco switches, IPMI console, HTTP API, etc)
  #
  # Do not be confused by the fact that collectors tagged :target do not run against e.g. target-mode ubuntu boxes, that is not
  # what :target is intended for.  Also, do not be confused by the fact that collectors tagged :default do not run by default against
  # pure-target mode targets like switches.  That is all intended behavior, the names are problematic.  The :default nomenclature was
  # invented 10 years before target mode and we are stuck with it.
  #
  if collector.key?(os)
    instance_eval(&collector[os])
  elsif collector.key?(:default) && !nonruby_target?
    instance_eval(&collector[:default])
  elsif collector.key?(:target) && nonruby_target?
    instance_eval(&collector[:target])
  else
    logger.trace("Plugin #{name}: No data to collect. Skipping...")
  end
end