Refactor: revisit logstash bootstraping bits (#13791)

* Refactor: logstash boot to know what to do
* Refactor: reduce LOAD_PATH - load jars directly
* Refactor: unused requires in runner
* Refactor: boot - require libs when to be used
This commit is contained in:
Karol Bucek 2022-02-17 13:59:38 +01:00 committed by GitHub
parent f1b17be5d1
commit 4f32c4fd48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 9 additions and 19 deletions

View file

@ -15,10 +15,6 @@
# specific language governing permissions and limitations
# under the License.
require "fileutils"
require "stringio"
require 'set'
module LogStash
module Bundler
extend self
@ -115,6 +111,7 @@ module LogStash
require "bundler"
require "bundler/cli"
require "fileutils"
# create Gemfile from template iff it does not exist
unless ::File.exists?(Environment::GEMFILE_PATH)
FileUtils.copy(
@ -213,7 +210,7 @@ module LogStash
# @param plugin_names [Array] logstash plugin names that are going to update
# @return [Array] gem names that plugins depend on, including logstash plugins
def expand_logstash_mixin_dependencies(plugin_names)
def expand_logstash_mixin_dependencies(plugin_names); require 'set'
plugin_names = Array(plugin_names) if plugin_names.is_a?(String)
# get gem names in Gemfile.lock. If file doesn't exist, it will be generated
@ -299,7 +296,7 @@ module LogStash
# capture any $stdout from the passed block. also trap any exception in that block, in which case the trapped exception will be returned
# @param [Proc] the code block to execute
# @return [String, Exception] the captured $stdout string and any trapped exception or nil if none
def capture_stdout(&block)
def capture_stdout(&block); require 'stringio'
old_stdout = $stdout
$stdout = StringIO.new("", "w")
begin

View file

@ -76,10 +76,7 @@ module LogStash
end
end
# when launched as a script, not require'd, (currently from bin/logstash and bin/logstash-plugin) the first
# argument is the path of a Ruby file to require and a LogStash::Runner class is expected to be
# defined and exposing the LogStash::Runner#main instance method which will be called with the current ARGV
# currently lib/logstash/runner.rb and lib/pluginmanager/main.rb are called using this.
# when launched as a script, not require'd, (currently from bin/logstash)
if $0 == __FILE__
bundler_options = {:without => [:build, :development]}
## Check for dev flags - this needs to be done before the runner is invoked to set bundler options
@ -89,7 +86,7 @@ if $0 == __FILE__
LogStash::Bundler.setup!(bundler_options)
require_relative "patches/jar_dependencies"
require ARGV.shift
require 'logstash/runner'
exit_status = LogStash::Runner.run("bin/logstash", ARGV)
exit(exit_status || 0)
end

View file

@ -23,9 +23,8 @@ require "java"
# wrapper.
unless $LS_JARS_LOADED
jar_path = File.join(File.dirname(File.dirname(__FILE__)), "jars")
$:.unshift jar_path
Dir.glob(jar_path + '/*.jar') do |jar|
require File.basename(jar)
Dir.glob("#{jar_path}/*.jar") do |jar|
load jar
end
java_import org.logstash.RubyUtil
end

View file

@ -32,7 +32,6 @@ if !LogStash::OSS
end
require "clamp"
require "net/http"
require "logstash-core/logstash-core"
require "logstash/environment"
@ -50,7 +49,6 @@ require 'logstash/plugins'
require "logstash/modules/util"
require "logstash/bootstrap_check/default_config"
require "logstash/bootstrap_check/persisted_queue_config"
require "set"
require 'logstash/deprecation_message'
java_import 'org.logstash.FileLockFactory'

View file

@ -241,10 +241,9 @@ public final class Logstash implements Runnable, AutoCloseable {
* @return RubyInstanceConfig
*/
private static RubyInstanceConfig buildConfig(final Path home, final String[] args) {
final String[] arguments = new String[args.length + 2];
System.arraycopy(args, 0, arguments, 2, args.length);
final String[] arguments = new String[args.length + 1];
System.arraycopy(args, 0, arguments, 1, args.length);
arguments[0] = safePath(home, "lib", "bootstrap", "environment.rb");
arguments[1] = safePath(home, "logstash-core", "lib", "logstash", "runner.rb");
return initRubyConfig(home, arguments);
}