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 6, 2018
1 parent db5f1da commit a68d4de
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 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

# 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
31 changes: 31 additions & 0 deletions spec/inputs/exec_spec.rb
Original file line number Diff line number Diff line change
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 ""
end

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

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

0 comments on commit a68d4de

Please sign in to comment.