Class | BoxGrinder::ExecHelper |
In: |
lib/boxgrinder-core/helpers/exec-helper.rb
lib/boxgrinder-core/helpers/exec-helper.rb |
Parent: | Object |
# File lib/boxgrinder-core/helpers/exec-helper.rb, line 25 25: def initialize( options = {} ) 26: @log = options[:log] || Logger.new(STDOUT) 27: end
# File lib/boxgrinder-core/helpers/exec-helper.rb, line 25 25: def initialize( options = {} ) 26: @log = options[:log] || Logger.new(STDOUT) 27: end
# File lib/boxgrinder-core/helpers/exec-helper.rb, line 29 29: def execute( command, options = {} ) 30: redacted = options[:redacted] || [] 31: 32: redacted_command = command 33: redacted.each { |word| redacted_command = redacted_command.gsub(word, '<REDACTED>') } 34: 35: @log.debug "Executing command: '#{redacted_command}'" 36: 37: output = "" 38: 39: # dirty workaround for ruby segfaults related to logger.rb 40: STDOUT.sync = true 41: STDERR.sync = true 42: 43: begin 44: status = Open4::popen4( command ) do |pid, stdin, stdout, stderr| 45: threads = [] 46: 47: threads << Thread.new(stdout) do |input_str| 48: input_str.each do |l| 49: l.chomp! 50: l.strip! 51: 52: output << "\n#{l}" 53: @log.debug l 54: end 55: end 56: 57: threads << Thread.new(stderr) do |input_str| 58: input_str.each do |l| 59: l.chomp! 60: l.strip! 61: 62: output << "\n#{l}" 63: @log.debug l 64: end 65: end 66: threads.each{|t|t.join} 67: end 68: 69: raise "process exited with wrong exit status: #{status.exitstatus}" if status.exitstatus != 0 70: 71: return output.strip 72: rescue => e 73: @log.error e.backtrace.join($/) 74: raise "An error occurred while executing command: '#{redacted_command}', #{e.message}" 75: end 76: end
# File lib/boxgrinder-core/helpers/exec-helper.rb, line 29 29: def execute( command, options = {} ) 30: redacted = options[:redacted] || [] 31: 32: redacted_command = command 33: redacted.each { |word| redacted_command = redacted_command.gsub(word, '<REDACTED>') } 34: 35: @log.debug "Executing command: '#{redacted_command}'" 36: 37: output = "" 38: 39: # dirty workaround for ruby segfaults related to logger.rb 40: STDOUT.sync = true 41: STDERR.sync = true 42: 43: begin 44: status = Open4::popen4( command ) do |pid, stdin, stdout, stderr| 45: threads = [] 46: 47: threads << Thread.new(stdout) do |input_str| 48: input_str.each do |l| 49: l.chomp! 50: l.strip! 51: 52: output << "\n#{l}" 53: @log.debug l 54: end 55: end 56: 57: threads << Thread.new(stderr) do |input_str| 58: input_str.each do |l| 59: l.chomp! 60: l.strip! 61: 62: output << "\n#{l}" 63: @log.debug l 64: end 65: end 66: threads.each{|t|t.join} 67: end 68: 69: raise "process exited with wrong exit status: #{status.exitstatus}" if status.exitstatus != 0 70: 71: return output.strip 72: rescue => e 73: @log.error e.backtrace.join($/) 74: raise "An error occurred while executing command: '#{redacted_command}', #{e.message}" 75: end 76: end