Class: Inspec::Plugin::V2::Registry

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/inspec/plugin/v2/registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



21
22
23
# File 'lib/inspec/plugin/v2/registry.rb', line 21

def initialize
  @registry = {}
end

Instance Attribute Details

#registryObject (readonly)

Returns the value of attribute registry.



13
14
15
# File 'lib/inspec/plugin/v2/registry.rb', line 13

def registry
  @registry
end

Instance Method Details

#__resetObject

Provided for test support. Purges the registry.



96
97
98
# File 'lib/inspec/plugin/v2/registry.rb', line 96

def __reset
  @registry.clear
end

#activate(plugin_type, hook_name) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/inspec/plugin/v2/registry.rb', line 70

def activate(plugin_type, hook_name)
  activator = find_activators(plugin_type: plugin_type, activator_name: hook_name).first
  # We want to capture literally any possible exception here, since we are storing them.
  # rubocop: disable Lint/RescueException
  begin
    impl_class = activator.activation_proc.call
    activator.activated?(true)
    activator.implementation_class = impl_class
  rescue Exception => ex
    activator.exception = ex
    Inspec::Log.error "Could not activate #{activator.plugin_type} hook named '#{activator.activator_name}' for plugin #{plugin_name}"
  end
  # rubocop: enable Lint/RescueException
end

#any_load_failures?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/inspec/plugin/v2/registry.rb', line 25

def any_load_failures?
  !plugin_statuses.select(&:load_exception).empty?
end

#find_activators(filters = {}) ⇒ Object

Finds Activators matching criteria (all optional) you specify as a Hash.

Parameters:

  • plugin_name (Symbol)

    Restricts the search to the given plugin

  • plugin_type (Symbol)

    Restricts the search to the given plugin type

  • activator_name (Symbol)

    Name of the activator

  • implementation_class (Class)

    Implementation class returned by an already-actived plugin type



62
63
64
65
66
67
68
# File 'lib/inspec/plugin/v2/registry.rb', line 62

def find_activators(filters = {})
  plugin_statuses.map(&:activators).flatten.select do |act|
    [:plugin_name, :plugin_type, :activator_name, :implementation_class].all? do |criteria|
      !filters.key?(criteria) || act[criteria] == filters[criteria]
    end
  end
end

#find_status_by_class(klass) ⇒ Object



52
53
54
# File 'lib/inspec/plugin/v2/registry.rb', line 52

def find_status_by_class(klass)
  registry.values.detect { |status| status.plugin_class == klass }
end

#known_countObject



40
41
42
# File 'lib/inspec/plugin/v2/registry.rb', line 40

def known_count
  registry.values.count
end

#loaded_countObject



36
37
38
# File 'lib/inspec/plugin/v2/registry.rb', line 36

def loaded_count
  loaded_plugin_names.count
end

#loaded_plugin?(name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/inspec/plugin/v2/registry.rb', line 29

def loaded_plugin?(name)
  # HACK: Status is normally the source of truth for loadedness, unless it is a train plugin; then the Train::Registry is the source of truth.
  # Also, InSpec registry is keyed on Symbols; Train is keyed on Strings.
  return registry.dig(name.to_sym, :loaded) unless name.to_s.start_with?('train-')
  Train::Plugins.registry.key?(name.to_s.sub(/^train-/, ''))
end

#loaded_plugin_namesObject



44
45
46
# File 'lib/inspec/plugin/v2/registry.rb', line 44

def loaded_plugin_names
  registry.keys.select { |name| loaded_plugin?(name) }
end

#path_based_plugin?(name) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/inspec/plugin/v2/registry.rb', line 48

def path_based_plugin?(name)
  known_plugin?(name.to_sym) && registry[name.to_sym].installation_type == :path
end

#register(name, status) ⇒ Object Also known as: []=



85
86
87
88
89
90
91
# File 'lib/inspec/plugin/v2/registry.rb', line 85

def register(name, status)
  if known_plugin? name
    Inspec::Log.debug "PluginLoader: refusing to re-register plugin '#{name}': an existing plugin with that name was loaded via #{registry[name].installation_type}-loading from #{registry[name].entry_point}"
  else
    registry[name.to_sym] = status
  end
end