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

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

Methods included from Util::FileHelper

#which

Methods included from Mixin::SecondsToHuman

#seconds_to_human

Methods included from Mixin::Command

shell_out

Methods included from Mixin::OS

collect_os

Constructor Details

#initialize(data, logger) ⇒ VersionVII

Returns a new instance of VersionVII.



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

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.



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

def source
  @source
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.collect_data(platform = :default, *other_platforms, &block) ⇒ Object

define data collection methodology per platform

Parameters:

  • platform (Symbol) (defaults to: :default)

    the platform to collect data for

  • other_platforms (Array)

    additional platforms to collect data for

  • block (block)

    the actual code to collect data for the specified platforms



109
110
111
112
113
114
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 109

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

.data_collectorMash

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

Returns:



68
69
70
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 68

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

.depends(*attrs) ⇒ Object

set the attributes depended on by the plugin

Parameters:

  • attrs (Array)


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

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

.depends_attrsObject



60
61
62
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 60

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

.optional(opt = true) ⇒ Object

set the plugin optional state

Parameters:

  • opt (Boolean) (defaults to: true)


93
94
95
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 93

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

.optional?Boolean

check if the plugin is optional

Returns:

  • (Boolean)


100
101
102
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 100

def self.optional?
  !!@optional
end

.provides(*attrs) ⇒ Object

set the attributes provided by the plugin

Parameters:

  • attrs (Array)


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

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

.provides_attrsObject



56
57
58
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 56

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 platforms and this would include each of those platform specific file paths

Returns:

  • (Array)


52
53
54
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 52

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

.versionSymbol

return that we’re a v7 plugin

Returns:

  • (Symbol)


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

def self.version
  :version7
end

Instance Method Details

#configuration(option, *options) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 146

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)


117
118
119
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 117

def dependencies
  self.class.depends_attrs
end

#nameString

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

Returns:



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

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

#optional?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 134

def optional?
  self.class.optional?
end

#provides(*paths) ⇒ Object



138
139
140
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 138

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



142
143
144
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 142

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



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ohai/dsl/plugin/versionvii.rb', line 121

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

  if collector.key?(platform)
    instance_eval(&collector[platform])
  elsif collector.key?(:default)
    instance_eval(&collector[:default])
  else
    logger.trace("Plugin #{name}: No data to collect. Skipping...")
  end
end