Skip to content

Commit

Permalink
Fixes logstash-plugins#25: Implement environment inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
viranch committed Nov 9, 2018
1 parent db5f1da commit 66b022f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lib/logstash/inputs/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class LogStash::Inputs::Exec < LogStash::Inputs::Base
# Command to run. For example : `uptime`
config :command, :validate => :string, :required => true

# Whether logstash's environment variables should be passed to the command
config :inherit_environment, :validate => :boolean, :default => false

# Interval to run the command. Value is in seconds.
# Either `interval` or `schedule` option must be defined.
config :interval, :validate => :number
Expand All @@ -34,7 +37,7 @@ def register
@logger.info("Registering Exec Input", :type => @type, :command => @command, :interval => @interval, :schedule => @schedule)
@hostname = Socket.gethostname
@io = nil

if (@interval.nil? && @schedule.nil?) || (@interval && @schedule)
raise LogStash::ConfigurationError, "exec input: either 'interval' or 'schedule' option must be defined."
end
Expand Down Expand Up @@ -94,7 +97,11 @@ def execute(queue)
private

def run_command
@io = IO.popen(@command)
if @inherit_environment
@io = IO.popen(ENV, @command)
else
@io = IO.popen(@command)
end
output = @io.read
@io.close # required in order to read $?
exit_status = $?.exitstatus # should be threadsafe as per rb_thread_save_context
Expand Down
33 changes: 32 additions & 1 deletion spec/inputs/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
let(:loggr) { double('loggr') }

before :each do
expect(described_class).to receive(:logger).and_return(loggr).exactly(7).times
expect(described_class).to receive(:logger).and_return(loggr).exactly(8).times
allow(loggr).to receive(:info)
allow(loggr).to receive(:info?)
allow(loggr).to receive(:warn)
Expand Down Expand Up @@ -69,6 +69,37 @@

end

context "when inheriting environment" do
let(:input) { described_class.new("command" => "env | grep '^FOO='", "interval" => 0) }
let(:queue) { [] }

let(:input_env) { described_class.new("command" => "env | grep '^FOO='", "inherit_environment" => true, "interval" => 0) }
let(:queue_env) { [] }

before do
ENV["FOO"] = "BAR"

input.register
input.execute(queue)

input_env.register
input_env.execute(queue_env)
end

after do
input.stop
input_env.stop
end

it "didn't inherit the environment" do
expect(queue.pop.get('message')).to eq "\n"
end

it "inherited the environment" do
expect(queue_env.pop.get('message')).to eq "FOO=BAR\n"
end
end

context "when scheduling" do
let(:input) { described_class.new("command" => "ls", "schedule" => "* * * * * UTC") }
let(:queue) { [] }
Expand Down

0 comments on commit 66b022f

Please sign in to comment.