Class: Inspec::Plugin::V2::Loader
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
inspec_plugin_name?, train_plugin_name?, valid_plugin_name?
#loaded_recent_most_version_of?
Constructor Details
#initialize(options = {}) ⇒ Loader
Returns a new instance of Loader.
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/inspec/plugin/v2/loader.rb', line 16
def initialize(options = {})
@options = options
@registry = Inspec::Plugin::V2::Registry.instance
unless options[:omit_user_plugins]
@conf_file = Inspec::Plugin::V2::ConfigFile.new
read_conf_file_into_registry
end
detect_bundled_plugins unless options[:omit_bundles]
detect_core_plugins unless options[:omit_core_plugins]
detect_system_plugins unless options[:omit_sys_plugins]
registry.each do |plugin_name, status|
fixup_train_plugin_status(status) if train_plugin_name?(plugin_name)
end
end
|
Instance Attribute Details
#conf_file ⇒ Object
Returns the value of attribute conf_file.
9
10
11
|
# File 'lib/inspec/plugin/v2/loader.rb', line 9
def conf_file
@conf_file
end
|
#options ⇒ Object
Returns the value of attribute options.
9
10
11
|
# File 'lib/inspec/plugin/v2/loader.rb', line 9
def options
@options
end
|
#registry ⇒ Object
Returns the value of attribute registry.
9
10
11
|
# File 'lib/inspec/plugin/v2/loader.rb', line 9
def registry
@registry
end
|
Class Method Details
.find_gem_directory(gem_name, version = nil) ⇒ Object
164
165
166
167
|
# File 'lib/inspec/plugin/v2/loader.rb', line 164
def self.find_gem_directory(gem_name, version = nil)
selected_gemspec = find_gemspec_of(gem_name, version)
selected_gemspec&.full_gem_path
end
|
.find_gemspec_directory(gem_name, version = nil) ⇒ Object
169
170
171
172
|
# File 'lib/inspec/plugin/v2/loader.rb', line 169
def self.find_gemspec_directory(gem_name, version = nil)
selected_gemspec = find_gemspec_of(gem_name, version)
selected_gemspec&.loaded_from
end
|
.find_gemspec_of(gem_name, version = nil) ⇒ Object
174
175
176
177
178
179
180
181
|
# File 'lib/inspec/plugin/v2/loader.rb', line 174
def self.find_gemspec_of(gem_name, version = nil)
version = Gem::Version.new(version) if version && !version.is_a?(Gem::Version)
list_managed_gems
.select { |g| g.name == gem_name }
.sort_by(&:version)
.yield_self { |gems| version ? gems.find { |g| g.version == version } : gems.last }
end
|
.list_installed_plugin_gems ⇒ Array[Gem::Specification]
Lists all plugin gems found in the plugin_gem_path. This is simply all gems that begin with train- or inspec- and are not on the exclusion list.
195
196
197
|
# File 'lib/inspec/plugin/v2/loader.rb', line 195
def self.list_installed_plugin_gems
list_managed_gems.select { |spec| valid_plugin_name?(spec.name) }
end
|
.list_managed_gems ⇒ Array[Gem::Specification]
Lists all gems found in the plugin_gem_path.
156
157
158
|
# File 'lib/inspec/plugin/v2/loader.rb', line 156
def self.list_managed_gems
Dir.glob(File.join(plugin_gem_path, "specifications", "*.gemspec")).map { |p| Gem::Specification.load(p) }
end
|
.plugin_gem_path ⇒ Object
145
146
147
148
149
150
151
152
|
# File 'lib/inspec/plugin/v2/loader.rb', line 145
def self.plugin_gem_path
require "rbconfig" unless defined?(RbConfig)
ruby_abi_version = RbConfig::CONFIG["ruby_version"]
base_dir = Inspec.config_dir
base_dir = File.realpath base_dir if File.exist? base_dir
File.join(base_dir, "gems", ruby_abi_version)
end
|
Instance Method Details
#activate_managed_gems_for_plugin(plugin_gem_name, version_constraint = "> 0") ⇒ Object
‘Activating’ a gem adds it to the load path, so ‘require “gemname”’ will work. Given a gem name, this activates the gem and all of its dependencies, respecting version pinning needs.
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/inspec/plugin/v2/loader.rb', line 206
def activate_managed_gems_for_plugin(plugin_gem_name, version_constraint = "> 0")
plugin_deps = [Gem::Dependency.new(plugin_gem_name.to_s, version_constraint)]
managed_gem_set = Gem::Resolver::VendorSet.new
list_managed_gems.each do |spec|
if Gem::Specification.load spec.gem_dir
managed_gem_set.add_vendor_gem(spec.name, spec.gem_dir)
else
Dir.chdir(spec.gem_dir) do |dir|
managed_gem_set.add_vendor_gem(spec.name, spec.gem_dir)
end
end
end
distrib_gem_set = Gem::Resolver::CurrentSet.new
installed_gem_set = Gem::Resolver.compose_sets(managed_gem_set, distrib_gem_set)
resolver = Gem::Resolver.new(plugin_deps, installed_gem_set)
begin
solution = resolver.resolve
rescue Gem::UnsatisfiableDependencyError => gem_ex
ex = Inspec::Plugin::V2::LoadError.new(gem_ex.message)
raise ex
end
solution.each do |activation_request|
requested_gemspec = activation_request.full_spec
next if requested_gemspec.activated?
requested_gemspec.activate unless loaded_recent_most_version_of?(requested_gemspec)
end
end
|
#activate_mentioned_cli_plugins(cli_args = ARGV) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/inspec/plugin/v2/loader.rb', line 105
def activate_mentioned_cli_plugins(cli_args = ARGV)
registry.find_activators(plugin_type: :cli_command).each do |act|
next if act.activated?
activate_me = false
activate_me ||= ["help", "--help", nil].include?(cli_args.first)
activate_me ||= cli_args.include?(act.activator_name.to_s)
if cli_args.include?("automate") && act.activator_name.to_s.eql?("compliance")
activate_me = false
elsif cli_args.include?("compliance") && act.activator_name.to_s.eql?("automate")
activate_me = false
end
if activate_me
act.activate
act.implementation_class.register_with_thor
end
end
end
|
#exit_on_load_error ⇒ Object
This should possibly be in either lib/inspec/cli.rb or Registry
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/inspec/plugin/v2/loader.rb', line 89
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
|
#find_gem_directory(gem_name, version = nil) ⇒ Object
187
188
189
|
# File 'lib/inspec/plugin/v2/loader.rb', line 187
def find_gem_directory(gem_name, version = nil)
self.class.find_gem_directory(gem_name, version)
end
|
#find_gemspec_directory(gem_name, version = nil) ⇒ Object
183
184
185
|
# File 'lib/inspec/plugin/v2/loader.rb', line 183
def find_gemspec_directory(gem_name, version = nil)
self.class.find_gemspec_directory(gem_name, version)
end
|
#list_installed_plugin_gems ⇒ Object
199
200
201
|
# File 'lib/inspec/plugin/v2/loader.rb', line 199
def list_installed_plugin_gems
self.class.list_managed_gems
end
|
#list_managed_gems ⇒ Object
160
161
162
|
# File 'lib/inspec/plugin/v2/loader.rb', line 160
def list_managed_gems
self.class.list_managed_gems
end
|
#load_all ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/inspec/plugin/v2/loader.rb', line 46
def load_all
Gem.path << plugin_gem_path
Gem.refresh
registry.plugin_names.dup.each do |plugin_name|
plugin_details = registry[plugin_name]
next if plugin_details.loaded
begin
case plugin_details.installation_type
when :user_gem
activate_managed_gems_for_plugin(plugin_name)
require plugin_details.entry_point
when :system_gem
require plugin_details.entry_point
else
load_path = plugin_details.entry_point
next if load_path.end_with?(".gem")
load_path += ".rb" unless load_path.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
end
end
|
#plugin_gem_path ⇒ Object
141
142
143
|
# File 'lib/inspec/plugin/v2/loader.rb', line 141
def plugin_gem_path
self.class.plugin_gem_path
end
|