Class: Fluent::ExecSudoOutput

Inherits:
ExecOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_exec_sudo.rb

Instance Method Summary collapse

Constructor Details

#initializeExecSudoOutput

Returns a new instance of ExecSudoOutput.



7
8
9
10
# File 'lib/fluent/plugin/out_exec_sudo.rb', line 7

def initialize
  super
  require 'open3'
end

Instance Method Details

#write(chunk) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fluent/plugin/out_exec_sudo.rb', line 14

def write(chunk)
  if @user.nil?
    super
    return true
  end

  if chunk.respond_to?(:path)
    prog = "sudo -u #{@user} -i sh -c '#{@command} #{chunk.path}'"
  else
    tmpfile = Tempfile.new("fluent-plugin-exec-su-")
    chunk.write_to(tmpfile)
    tmpfile.close
    prog = "sudo -u #{@user} -i sh -c '#{@command} #{tmpfile.path}'"
  end

  stdout, stderr, status = Open3.capture3(prog)
  ecode = status.to_i
  tmpfile.delete if tmpfile

  if ecode != 0
    log.debug "command stderr: #{stderr}"
    raise "command returns #{ecode}: #{prog}"
  end
end