logstash/lib/pluginmanager/pack_installer/local.rb
Cas Donoghue 0e99f526c4
Bump jruby 9.4.13.0 (#17696)
* upgrade jruby to 9.4.11.0

* follow up cleanups

* follow up cleanups

* follow up cleanups

* follow up cleanups

* add licenses

* Update rakelib/plugins-metadata.json

* attempt to fix rubygems

* attempt to fix rubygems

* attempt to fix rubygems

* tweaks for jruby 9.4.12.0 and sha256 validation

* attempt at fixing rubygems

* attempt at fixing rubygems

* attempt at fixing rubygems

* attempt at fixing rubygems

* Bump jruby to 9.4.12.1

* Explicitly require 'set' to make `to_set` available

Something appears to have changed in code loading where this require
is no longer in the call chain. Explicitly require it here.

* Fix up dependency mapper

* Update bundler code with latest rubygems release

This commit makes 2 changes:

1. We were sending a `Gem::Platform` instance to Thor parser instead of a
string. Convert to string version of a platform name. With newly added checks in
Thor, we were getting `noMethodError` for `Gem::Platform`.

2.  It looks like there was a patch to get around a bug in bundler that was
reaching out to the network for local gems. This appears to have been fixed
upstream, this removes the patch.

* Restore patch

* Remove patch (TODO, link to full writeup)

* SPIKE: Take up jruby 9.4.13.0

* Patch bundler cache

Still validating this. Essentially ensure we check local gems when calling cache using
the updated rubygems class.

* WIP: test patch for acceptance tests

* Skip when unable to cache

* Cache every gem

* Fix acceptance test

* Force another route through builtin

* Fixup idea (it returns nil, not raise)

* Fix missing version update

* Fix qatest filter plugin

Without including a gemspec with a locally built gem logstash-plugin list
will not show it in the output.

* Use vendored gem instead of reaching out to rubygems

We vendor an udpated test gem for local install. Use that instead of
reaching out to rubygems.

* Document and comment on the patched bundler code

* Fix test

instead of downloading the "broken" one from rubygems (downloading the `.gem` file directly
does show up in plugin-list), using a vendored one polutes the cache. Use a new vendored
gem

---------

Co-authored-by: João Duarte <jsvduarte@gmail.com>
Co-authored-by: João Duarte <jsvd@users.noreply.github.com>
2025-06-24 15:24:52 -07:00

84 lines
3.5 KiB
Ruby

# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
require "pluginmanager/ui"
require "pluginmanager/bundler/logstash_injector"
require "pluginmanager/gem_installer"
require "pluginmanager/errors"
require "pluginmanager/pack_installer/pack"
require "bootstrap/util/compress"
module LogStash module PluginManager module PackInstaller
class Local
PACK_EXTENSION = ".zip"
LOGSTASH_PATTERN_RE = /logstash\/?/
attr_reader :local_file
def initialize(local_file)
@local_file = local_file
end
def execute
raise PluginManager::FileNotFoundError, "Can't file local file #{local_file}" unless ::File.exist?(local_file)
raise PluginManager::InvalidPackError, "Invalid format, the pack must be in zip format" unless valid_format?(local_file)
PluginManager.ui.info("Installing file: #{local_file}")
uncompressed_path = uncompress(local_file)
PluginManager.ui.debug("Pack uncompressed to #{uncompressed_path}")
pack = LogStash::PluginManager::PackInstaller::Pack.new(uncompressed_path)
raise PluginManager::InvalidPackError, "The pack must contains at least one plugin" unless pack.valid?
# Install the gems to make them available locally when bundler does his local resolution
post_install_messages = []
pack.gems.each do |packed_gem|
PluginManager.ui.debug("Installing, #{packed_gem.name}, version: #{packed_gem.version} file: #{packed_gem.file}")
post_install_messages << LogStash::PluginManager::GemInstaller::install(packed_gem.file, packed_gem.plugin?)
end
# Try to add the gems to the current gemfile and lock file, if successful
# both of them will be updated. This injector is similar to Bundler's own injector class
# minus the support for additionals source and doing local resolution only.
::Bundler::LogstashInjector.inject!(pack)
post_install_messages.compact.each do |message|
PluginManager.ui.info(message)
end
PluginManager.ui.info("Install successful")
rescue ::Bundler::BundlerError => e
raise PluginManager::InstallError.new(e), "An error occurred went installing plugins"
ensure
FileUtils.rm_rf(uncompressed_path) if uncompressed_path && Dir.exist?(uncompressed_path)
end
private
def uncompress(source)
temporary_directory = Stud::Temporary.pathname
LogStash::Util::Zip.extract(source, temporary_directory, LOGSTASH_PATTERN_RE)
temporary_directory
rescue Zip::Error => e
# OK Zip's handling of file is bit weird, if the file exist but is not a valid zip, it will raise
# a `Zip::Error` exception with a file not found message...
raise InvalidPackError, "Cannot uncompress the zip: #{source}"
end
def valid_format?(local_file)
::File.extname(local_file).downcase == PACK_EXTENSION
end
end
end end end