Class: Inspec::Plugin::V2::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/plugin/v2/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Loader

Returns a new instance of Loader.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/inspec/plugin/v2/loader.rb', line 14

def initialize(options = {})
  @options = options
  @registry = Inspec::Plugin::V2::Registry.instance
  determine_plugin_conf_file
  read_conf_file
  unpack_conf_file

  # Old-style (v0, v1) co-distributed plugins were called 'bundles'
  # and were located in lib/bundles
  detect_bundled_plugins unless options[:omit_bundles]

  # New-style (v2) co-distributed plugins are in lib/plugins,
  # and may be safely loaded
  detect_core_plugins unless options[:omit_core_plugins]
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/inspec/plugin/v2/loader.rb', line 12

def options
  @options
end

#registryObject (readonly)

Returns the value of attribute registry.



12
13
14
# File 'lib/inspec/plugin/v2/loader.rb', line 12

def registry
  @registry
end

Instance Method Details

#activate(plugin_type, hook_name) ⇒ Object



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

def activate(plugin_type, hook_name)
  activator = registry.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

#activate_mentioned_cli_plugins(cli_args = ARGV) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/inspec/plugin/v2/loader.rb', line 87

def activate_mentioned_cli_plugins(cli_args = ARGV)
  # Get a list of CLI plugin activation hooks
  registry.find_activators(plugin_type: :cli_command).each do |act|
    next if act.activated
    # If there is anything in the CLI args with the same name, activate it
    # If the word 'help' appears in the first position, load all CLI plugins
    if cli_args.include?(act.activator_name.to_s) || cli_args[0] == 'help' || cli_args.size.zero?
      activate(:cli_command, act.activator_name)
      act.implementation_class.register_with_thor
    end
  end
end

#exit_on_load_errorObject

This should possibly be in either lib/inspec/cli.rb or Registry



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/inspec/plugin/v2/loader.rb', line 56

def exit_on_load_error
  if registry.any_load_failures?
    Inspec::Log.error 'Errors were encountered while loading plugins...'
    registry.plugin_statuses.select(&:load_exception).each do |plugin_status|
      Inspec::Log.error 'Plugin name: ' + plugin_status.name.to_s
      Inspec::Log.error 'Error: ' + plugin_status.load_exception.message
      if ARGV.include?('--debug')
        Inspec::Log.error 'Exception: ' + plugin_status.load_exception.class.name
        Inspec::Log.error 'Trace: ' + plugin_status.load_exception.backtrace.join("\n")
      end
    end
    Inspec::Log.error('Run again with --debug for a stacktrace.') unless ARGV.include?('--debug')
    exit 2
  end
end

#load_allObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/inspec/plugin/v2/loader.rb', line 30

def load_all
  registry.each do |plugin_name, plugin_details|
    # We want to capture literally any possible exception here, since we are storing them.
    # rubocop: disable Lint/RescueException
    begin
      # We could use require, but under testing, we need to repeatedly reload the same
      # plugin.  However, gems only work with require (rubygems dooes not overload `load`)
      if plugin_details.installation_type == :gem
        activate_managed_gems_for_plugin(plugin_name)
        require plugin_details.entry_point
      else
        load_path = plugin_details.entry_point
        load_path += '.rb' unless plugin_details.entry_point.end_with?('.rb')
        load load_path
      end
      plugin_details.loaded = true
      annotate_status_after_loading(plugin_name)
    rescue ::Exception => ex
      plugin_details.load_exception = ex
      Inspec::Log.error "Could not load plugin #{plugin_name}: #{ex.message}"
    end
    # rubocop: enable Lint/RescueException
  end
end