How to use update_preflight_check method of InspecPlugins.PluginManager Package

Best Inspec_ruby code snippet using InspecPlugins.PluginManager.update_preflight_check

cli_command.rb

Source:cli_command.rb Github

copy

Full Screen

...99 EOLD100 def update(plugin_name)101 pre_update_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }102 old_version = pre_update_versions.join(', ')103 update_preflight_check(plugin_name, pre_update_versions)104 begin105 installer.update(plugin_name)106 rescue Inspec::Plugin::V2::UpdateError => ex107 puts(red { 'Update error: ' } + ex.message + ' - update failed')108 exit 1109 end110 post_update_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }111 new_version = (post_update_versions - pre_update_versions).first112 puts(bold { plugin_name } + " plugin, version #{old_version} -> #{new_version}, updated from rubygems.org")113 end114 #--------------------------115 # uninstall116 #--------------------------117 desc 'uninstall PLUGIN_NAME', 'Uninstalls a gem- or path- based plugin'118 long_desc <<~EOLD119 Removes a plugin from the users configuration.120 In the case of a gem plugin (by far the most common), the plugin gem is removed, along121 with any of its dependencies that are no longer needed by anything else. Finally, the122 plugin configuration file is updated to reflect that the plugin is no longer present.123 In the case of a path-based plugin (often used for plugin development), no changes124 are made to the referenced plugin source code. Rather, the plugin's entry is simply removed125 from the plugin config file.126 EOLD127 def uninstall(plugin_name)128 status = Inspec::Plugin::V2::Registry.instance[plugin_name.to_sym]129 unless status130 puts(red { 'No such plugin installed: ' } + "#{plugin_name} is not installed - uninstall failed")131 exit 1132 end133 installer = Inspec::Plugin::V2::Installer.instance134 pre_uninstall_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }135 old_version = pre_uninstall_versions.join(', ')136 installer.uninstall(plugin_name)137 if status.installation_type == :path138 puts(bold { plugin_name } + ' path-based plugin install has been uninstalled')139 else140 puts(bold { plugin_name } + " plugin, version #{old_version}, has been uninstalled")141 end142 exit 0143 end144 private145 #==================================================================#146 # install breakdown147 #==================================================================#148 # These are broken down because rubocop complained.149 def install_from_gemfile(gem_file)150 unless File.exist? gem_file151 puts(red { 'No such plugin gem file ' } + gem_file + ' - installation failed.')152 exit 1153 end154 plugin_name_parts = File.basename(gem_file, '.gem').split('-')155 version = plugin_name_parts.pop156 plugin_name = plugin_name_parts.join('-')157 check_plugin_name(plugin_name, 'installation')158 installer.install(plugin_name, gem_file: gem_file)159 puts(bold { plugin_name } + " plugin, version #{version}, installed from local .gem file")160 exit 0161 end162 def install_from_path(path)163 unless File.exist? path164 puts(red { 'No such source code path ' } + path + ' - installation failed.')165 exit 1166 end167 plugin_name = File.basename(path, '.rb')168 # While installer.install does some rudimentary checking,169 # this file has good UI access, so we promise to validate the170 # input a lot and hand the installer a sure-thing.171 # Name OK?172 check_plugin_name(plugin_name, 'installation')173 # Already installed?174 if registry.known_plugin?(plugin_name.to_sym)175 puts(red { 'Plugin already installed' } + " - #{plugin_name} - Use 'inspec plugin list' to see previously installed plugin - installation failed.")176 exit 2177 end178 # Can we figure out how to load it?179 entry_point = install_from_path__apply_entry_point_heuristics(path)180 # If you load it, does it act like a plugin?181 install_from_path__probe_load(entry_point, plugin_name)182 # OK, install it!183 installer.install(plugin_name, path: entry_point)184 puts(bold { plugin_name } + ' plugin installed via source path reference, resolved to entry point ' + entry_point)185 exit 0186 end187 # Rationale for rubocop variances: It's a heuristics method, and will be full of188 # conditionals. The code is well-commented; refactoring into sub-methods would189 # reduce clarity.190 def install_from_path__apply_entry_point_heuristics(path) # rubocop: disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity191 given = Pathname.new(path)192 given = given.expand_path # Resolve any relative paths193 name_regex = /^(inspec|train)-/194 versioned_regex = /^(inspec|train)-[a-z0-9\-\_]+-\d+\.\d+\.\d+$/195 # What are the last four things like?196 parts = [197 given.parent.parent.basename,198 given.parent.basename,199 given.basename('.rb'),200 given.extname,201 ].map(&:to_s)202 # Case 1: Simplest case: it was a full entry point, as presented.203 # /home/you/projects/inspec-something/lib/inspec-something.rb204 # parts index: ^0^ ^1^ ^2^ ^3^205 if parts[0] =~ name_regex && parts[1] == 'lib' && parts[2] == parts[0] && parts[3] == '.rb'206 return given.to_s207 end208 # Case 2: Also easy: they either referred to the internal library directory,209 # or left the extansion off. Those are the same to us.210 # /home/you/projects/inspec-something/lib/inspec-something211 # parts index: ^0^ ^1^ ^2^ (3 is empty)212 if parts[0] =~ name_regex && parts[1] == 'lib' && parts[2] == parts[0] && parts[3].empty?213 return given.to_s + '.rb'214 end215 # Case 3: Maybe they were refering to a path that is inside a gem installation, or an exploded gem?216 # In that case, we'll have a version on the plugin name in part 0217 # /home/you/.gems/2.4.0/gems/inspec-something-3.45.1/lib/inspec-something.rb218 # parts index: ^0^ ^1^ ^2^ ^3^219 if parts[0] =~ versioned_regex && parts[1] == 'lib' && parts[0].start_with?(parts[2]) && parts[3] == '.rb'220 return given.to_s221 end222 # Case 4: Like case 3, but missing the .rb223 # /home/you/.gems/2.4.0/gems/inspec-something-3.45.1/lib/inspec-something224 # parts index: ^0^ ^1^ ^2^ ^3^ (empty)225 if parts[0] =~ versioned_regex && parts[1] == 'lib' && parts[0].start_with?(parts[2]) && parts[3].empty?226 return given.to_s + '.rb'227 end228 # Case 5: Easy to recognize, but harder to handle: they referred to the project root.229 # /home/you/projects/inspec-something230 # parts index: ^0^ ^1^ ^2^ (3 is empty)231 # 0 and 1 are not meaningful to us, but we hope to find a parts[2]/lib/inspec-something.rb.232 entry_point_guess = File.join(given.to_s, 'lib', parts[2] + '.rb')233 if parts[2] =~ name_regex && File.exist?(entry_point_guess)234 return entry_point_guess235 end236 # Well, if we got here, parts[2] matches an inspec/train prefix, but we have no idea about anything.237 # Give up.238 puts(red { 'Unrecognizable plugin structure' } + " - #{parts[2]} - When installing from a path, please provide the path of the entry point file - installation failed.")239 exit 1240 end241 def install_from_path__probe_load(entry_point, plugin_name)242 # Brazenly attempt to load a file, and see if it registers a plugin.243 begin244 require entry_point245 rescue LoadError => ex246 puts(red { 'Plugin contains errors' } + " - #{plugin_name} - Encountered errors while trying to test load the plugin entry point, resolved to #{entry_point} - installation failed")247 puts ex.message248 exit 1249 end250 # OK, the wheels didn't fall off. But is it a plugin?251 if plugin_name.to_s.start_with?('train')252 # Train internal names do not include the prix in their registry entries253 # And the registry is keyed on Strings254 registry_key = plugin_name.to_s.sub(/^train-/, '')255 unless Train::Plugins.registry.key?(registry_key)256 puts(red { 'Does not appear to be a plugin' } + " - #{plugin_name} - After probe-loading the supposed plugin, it did not register itself to Train. Ensure something inherits from 'Train.plugin(1)' - installation failed.")257 exit 1258 end259 else260 unless registry.known_plugin?(plugin_name.to_sym)261 puts(red { 'Does not appear to be a plugin' } + " - #{plugin_name} - After probe-loading the supposed plugin, it did not register itself to InSpec. Ensure something inherits from 'Inspec.plugin(2)' - installation failed.")262 exit 1263 end264 end265 end266 def install_from_remote_gem(plugin_name)267 requested_version = options[:version]268 check_plugin_name(plugin_name, 'installation')269 # Version pre-flighting270 pre_installed_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }271 install_from_remote_gem_verson_preflight_check(plugin_name, requested_version, pre_installed_versions)272 install_attempt_install(plugin_name)273 # Success messaging. What did we actually install?274 post_installed_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }275 new_version = (post_installed_versions - pre_installed_versions).first276 puts(bold { plugin_name } + " plugin, version #{new_version}, installed from rubygems.org")277 exit 0278 end279 def install_from_remote_gem_verson_preflight_check(plugin_name, requested_version, pre_installed_versions)280 return if pre_installed_versions.empty?281 # Everything past here in the block is a code 2 error282 # If they didn't ask for a specific version, they implicitly ask for the latest.283 # Do an expensive search to determine the latest version.284 unless requested_version285 latest_version = installer.search(plugin_name, exact: true, scope: :latest)286 latest_version = latest_version[plugin_name]&.last287 if latest_version && !requested_version288 requested_version = latest_version289 end290 end291 # Check for already-installed at desired version conditions292 they_explicitly_asked_for_a_version = !options[:version].nil?293 what_we_would_install_is_already_installed = pre_installed_versions.include?(requested_version)294 if what_we_would_install_is_already_installed && they_explicitly_asked_for_a_version295 puts(red { 'Plugin already installed at requested version' } + " - plugin #{plugin_name} #{requested_version} - refusing to install.")296 elsif what_we_would_install_is_already_installed && !they_explicitly_asked_for_a_version297 puts(red { 'Plugin already installed at latest version' } + " - plugin #{plugin_name} #{requested_version} - refusing to install.")298 else299 # There are existing versions installed, but none of them are what was requested300 puts(red { 'Update required' } + " - plugin #{plugin_name}, requested #{requested_version}, have #{pre_installed_versions.join(', ')}; use `inspec plugin update` - refusing to install.")301 end302 exit 2303 end304 # Rationale for RuboCop variance: This is a one-line method with heavy UX-focused error handling.305 def install_attempt_install(plugin_name) # rubocop: disable Metrics/AbcSize306 installer.install(plugin_name, version: options[:version])307 rescue Inspec::Plugin::V2::PluginExcludedError => ex308 puts(red { 'Plugin on Exclusion List' } + " - #{plugin_name} is listed as an incompatible gem - refusing to install.")309 puts "Rationale: #{ex.details.rationale}"310 puts 'Exclusion list location: ' + File.join(Inspec.src_root, 'etc', 'plugin_filters.json')311 puts 'If you disagree with this determination, please accept our apologies for the misunderstanding, and open an issue at https://github.com/inspec/inspec/issues/new'312 exit 2313 rescue Inspec::Plugin::V2::InstallError314 raise if Inspec::Log.level == :debug315 results = installer.search(plugin_name, exact: true)316 if results.empty?317 puts(red { 'No such plugin gem ' } + plugin_name + ' could be found on rubygems.org - installation failed.')318 elsif options[:version] && !results[plugin_name].include?(options[:version])319 puts(red { 'No such version' } + ' - ' + plugin_name + " exists, but no such version #{options[:version]} found on rubygems.org - installation failed.")320 else321 puts(red { 'Unknown error occured ' } + ' - installation failed.')322 end323 exit 1324 end325 #==================================================================#326 # update breakdown327 #==================================================================#328 def update_preflight_check(plugin_name, pre_update_versions)329 if pre_update_versions.empty?330 # Check for path install331 status = Inspec::Plugin::V2::Registry.instance[plugin_name.to_sym]332 if !status333 puts(red { 'No such plugin installed: ' } + "#{plugin_name} - update failed")334 exit 1335 elsif status.installation_type == :path336 puts(red { 'Cannot update path-based install: ' } + "#{plugin_name} is installed via path reference; use `inspec plugin uninstall` to remove - refusing to update")337 exit 2338 end339 end340 # Check for latest version (and implicitly, existance)341 latest_version = installer.search(plugin_name, exact: true, scope: :latest)342 latest_version = latest_version[plugin_name]&.last...

Full Screen

Full Screen

update_preflight_check

Using AI Code Generation

copy

Full Screen

1cli.command(:update).update_preflight_check2cli.command(:update).command(:preflight).update_preflight_check3cli.command(:update).command(:preflight).command(:check).update_preflight_check4cli.command(:update).command(:preflight).command(:check).invoke(:update_preflight_check)

Full Screen

Full Screen

update_preflight_check

Using AI Code Generation

copy

Full Screen

1 class CliCommand < Inspec.plugin(2, :cli_command)2 class PluginManager < Inspec.plugin(2, :plugin_manager)3 class PluginManager < Inspec.plugin(2, :plugin_manager)4 class PluginManager < Inspec.plugin(2, :plugin_manager)5 [Plugin.new('test-plugin', '0.0.1', '0.0.2')]6 class PluginManager < Inspec.plugin(2, :plugin_manager)7 [Plugin.new('test-plugin', '0.0.2', '0.0.2')]

Full Screen

Full Screen

update_preflight_check

Using AI Code Generation

copy

Full Screen

1 class PluginManagerCLI < Inspec.plugin(2, :cli_command)2 def update_preflight_check(name)3 if installed_plugins.key?(name)4 Inspec::Plugin::V2::Installer.instance.install(name, :update)5 Inspec::Plugin::V2::Installer.instance.install(name)6 class PluginManagerCLI < Inspec.plugin(2, :cli_command)7 def update_preflight_check(name)

Full Screen

Full Screen

update_preflight_check

Using AI Code Generation

copy

Full Screen

1 class PluginManagerCLI < Inspec.plugin(2, :cli_command)2 update_preflight_check('inspec-plugin-manager')3 class PluginManagerCLI < Inspec.plugin(2, :cli_command)4 update_preflight_check('inspec-plugin-manager')5 class PluginManagerCLI < Inspec.plugin(2, :cli_command)6 update_preflight_check('inspec-plugin-manager')

Full Screen

Full Screen

update_preflight_check

Using AI Code Generation

copy

Full Screen

1 class PluginManager < Inspec.plugin(2, :cli_command)2 class PluginManager < Inspec.plugin(2, :cli_command)3 class PluginManager < Inspec.plugin(2, :cli_command)4 class PluginManager < Inspec.plugin(2, :cli_command)

Full Screen

Full Screen

update_preflight_check

Using AI Code Generation

copy

Full Screen

1 class PluginManagerCLI < Inspec.plugin(2, :cli_command)2 def update_preflight_check(name)3 if installed_plugins.key?(name)4 Inspec::Plugin::V2::Installer.instance.install(name, :update)5 Inspec::Plugin::V2::Installer.instance.install(name)6 class PluginManagerCLI < Inspec.plugin(2, :cli_command)7 def update_preflight_check(name)

Full Screen

Full Screen

update_preflight_check

Using AI Code Generation

copy

Full Screen

1 class PluginManager < Inspec.plugin(2, :cli_command)2 class PluginManager < Inspec.plugin(2, :cli_command)3 class PluginManager < Inspec.plugin(2, :cli_command)4 class PluginManager < Inspec.plugin(2, :cli_command)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful