| 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 33
33: def initialize(options = {})
34: @log = options[:log] || Logger.new(STDOUT)
35: end
# File lib/boxgrinder-core/helpers/exec-helper.rb, line 33
33: def initialize(options = {})
34: @log = options[:log] || Logger.new(STDOUT)
35: end
# File lib/boxgrinder-core/helpers/exec-helper.rb, line 37
37: def execute(command, options = {})
38: redacted = options[:redacted] || []
39:
40: redacted_command = command
41: redacted.each { |word| redacted_command = redacted_command.gsub(word, '<REDACTED>') }
42:
43: @log.debug "Executing command: '#{redacted_command}'"
44:
45: output = ""
46:
47: # dirty workaround for ruby segfaults related to logger.rb
48: STDOUT.sync = true
49: STDERR.sync = true
50:
51: begin
52: pid, stdin, stdout, stderr = (RUBY_PLATFORM =~ /java/ ? IO : Open4).send(:popen4, command)
53: threads = []
54:
55: threads << Thread.new(stdout) do |out|
56: out.each do |l|
57: l.chomp!
58: l.strip!
59:
60: output << "\n#{l}"
61: @log.debug l
62: end
63: end
64:
65: threads << Thread.new(stderr) do |err|
66: err.each do |l|
67: l.chomp!
68: l.strip!
69:
70: output << "\n#{l}"
71: @log.debug l
72: end
73: end
74:
75: threads.each { |t| t.join }
76:
77: # Assume the process exited cleanly, which can cause some bad behaviour, but I don't see better way
78: # to get reliable status for processes both on MRI and JRuby
79: #
80: # http://jira.codehaus.org/browse/JRUBY-5673
81: status = OpenCascade.new(:exitstatus => 0)
82:
83: fakepid, status = Process.waitpid2(pid) if process_alive?(pid)
84:
85: raise "process exited with wrong exit status: #{status.exitstatus}" if !(RUBY_PLATFORM =~ /java/) and status.exitstatus != 0
86:
87: return output.strip
88: rescue Interrupt
89: raise InterruptionError.new(pid), "Program was interrupted."
90: rescue => e
91: @log.error e.backtrace.join($/)
92: raise "An error occurred while executing command: '#{redacted_command}', #{e.message}"
93: end
94: end
# File lib/boxgrinder-core/helpers/exec-helper.rb, line 37
37: def execute(command, options = {})
38: redacted = options[:redacted] || []
39:
40: redacted_command = command
41: redacted.each { |word| redacted_command = redacted_command.gsub(word, '<REDACTED>') }
42:
43: @log.debug "Executing command: '#{redacted_command}'"
44:
45: output = ""
46:
47: # dirty workaround for ruby segfaults related to logger.rb
48: STDOUT.sync = true
49: STDERR.sync = true
50:
51: begin
52: pid, stdin, stdout, stderr = (RUBY_PLATFORM =~ /java/ ? IO : Open4).send(:popen4, command)
53: threads = []
54:
55: threads << Thread.new(stdout) do |out|
56: out.each do |l|
57: l.chomp!
58: l.strip!
59:
60: output << "\n#{l}"
61: @log.debug l
62: end
63: end
64:
65: threads << Thread.new(stderr) do |err|
66: err.each do |l|
67: l.chomp!
68: l.strip!
69:
70: output << "\n#{l}"
71: @log.debug l
72: end
73: end
74:
75: threads.each { |t| t.join }
76:
77: # Assume the process exited cleanly, which can cause some bad behaviour, but I don't see better way
78: # to get reliable status for processes both on MRI and JRuby
79: #
80: # http://jira.codehaus.org/browse/JRUBY-5673
81: status = OpenCascade.new(:exitstatus => 0)
82:
83: fakepid, status = Process.waitpid2(pid) if process_alive?(pid)
84:
85: raise "process exited with wrong exit status: #{status.exitstatus}" if !(RUBY_PLATFORM =~ /java/) and status.exitstatus != 0
86:
87: return output.strip
88: rescue Interrupt
89: raise InterruptionError.new(pid), "Program was interrupted."
90: rescue => e
91: @log.error e.backtrace.join($/)
92: raise "An error occurred while executing command: '#{redacted_command}', #{e.message}"
93: end
94: end
# File lib/boxgrinder-core/helpers/exec-helper.rb, line 96
96: def process_alive?(pid)
97: begin
98: Process.getpgid(pid)
99: true
100: rescue Errno::ESRCH
101: false
102: end
103: end