Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to run the bin/logstash-plugin install <pack> outside of the logstash directory #6602

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/pluginmanager/bundler/logstash_injector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
require "bundler/dependency"
require "bundler/dsl"
require "bundler/injector"
require "bundler/shared_helpers"
require "pluginmanager/gemfile"
require "pathname"


# This class cannot be in the logstash namespace, because of the way the DSL
# class interact with the other libraries
module Bundler
module SharedHelpers
def default_bundle_dir
Pathname.new(LogStash::Environment::LOGSTASH_HOME)
end
end
end

module Bundler
class LogstashInjector < ::Bundler::Injector
def self.inject!(new_deps, options = { :gemfile => LogStash::Environment::GEMFILE, :lockfile => LogStash::Environment::LOCKFILE })
Expand All @@ -20,7 +30,12 @@ def self.inject!(new_deps, options = { :gemfile => LogStash::Environment::GEMFIL
dependencies = new_deps.dependencies.collect(&method(:dependency))

injector = new(bundler_format)
injector.inject(gemfile, lockfile, dependencies)

# Some of the internal classes requires to be inside the LOGSTASH_HOME to find the relative
# path of the core gems.
Dir.chdir(LogStash::Environment::LOGSTASH_HOME) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this undo the chdir after it's done?

Copy link
Contributor Author

@ph ph Jan 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes its a block, it will return to the original directory:

puts "current dir: #{Dir.pwd}"

Dir.chdir("/Users/ph") do
  puts "current dir: #{Dir.pwd}"
end
puts "current dir: #{Dir.pwd}"
⚙ph@sashimi/tmp ruby testing.rb
current dir: /private/tmp
current dir: /Users/ph
current dir: /private/tmp
⚙ph@sashimi/tmp

injector.inject(gemfile, lockfile, dependencies)
end
end

def self.dependency(plugin)
Expand Down
10 changes: 7 additions & 3 deletions qa/integration/services/logstash_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def install(plugin_name)
run("install #{plugin_name}")
end

def run_raw(cmd_parameters)
def run_raw(cmd_parameters, change_dir = true)
out = Tempfile.new("content")
out.sync = true

Expand All @@ -248,8 +248,12 @@ def run_raw(cmd_parameters)
process = ChildProcess.build(cmd, *parts)
process.io.stdout = process.io.stderr = out

Dir.chdir(@logstash_home) do
Bundler.with_clean_env do
Bundler.with_clean_env do
if change_dir
Dir.chdir(@logstash_home) do
process.start
end
else
process.start
end
end
Expand Down
35 changes: 32 additions & 3 deletions qa/integration/specs/cli/install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require_relative "../../services/logstash_service"
require_relative "../../framework/helpers"
require "logstash/devutils/rspec/spec_helper"
require "stud/temporary"
require "fileutils"

def gem_in_lock_file?(pattern, lock_file)
content = File.read(lock_file)
Expand All @@ -19,8 +21,10 @@ def gem_in_lock_file?(pattern, lock_file)
@pack_directory = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "fixtures", "logstash-dummy-pack"))
end

context "pack" do
shared_examples "install from a pack" do
let(:pack) { "file://#{File.join(@pack_directory, "logstash-dummy-pack.zip")}" }
let(:install_command) { "bin/logstash-plugin install" }
let(:change_dir) { true }

# When you are on anything by linux we wont disable the internet with seccomp
if RbConfig::CONFIG["host_os"] == "linux"
Expand All @@ -38,7 +42,7 @@ def gem_in_lock_file?(pattern, lock_file)
let(:offline_wrapper_cmd) { File.join(offline_wrapper_path, "offline") }

it "successfully install the pack" do
execute = @logstash_plugin.run_raw("#{offline_wrapper_cmd} bin/logstash-plugin install #{pack}")
execute = @logstash_plugin.run_raw("#{offline_wrapper_cmd} #{install_command} #{pack}", change_dir)

expect(execute.stderr_and_stdout).to match(/Install successful/)
expect(execute.exit_code).to eq(0)
Expand All @@ -50,9 +54,10 @@ def gem_in_lock_file?(pattern, lock_file)
end
end
else

context "with internet connection" do
it "successfully install the pack" do
execute = @logstash_plugin.install(pack)
execute = @logstash_plugin.run_raw("#{install_command} #{pack}", change_dir)

expect(execute.stderr_and_stdout).to match(/Install successful/)
expect(execute.exit_code).to eq(0)
Expand All @@ -65,4 +70,28 @@ def gem_in_lock_file?(pattern, lock_file)
end
end
end

context "pack" do
context "when the command is run in the `$LOGSTASH_HOME`" do
include_examples "install from a pack"
end

context "when the command is run outside of the `$LOGSTASH_HOME`" do
include_examples "install from a pack" do
let(:change_dir) { false }
let(:install_command) { "#{@logstash.logstash_home}/bin/logstash-plugin install" }

before :all do
@current = Dir.pwd
tmp = Stud::Temporary.pathname
FileUtils.mkdir_p(tmp)
Dir.chdir(tmp)
end

after :all do
Dir.chdir(@current)
end
end
end
end
end