2017-07-12 10:31:36 -04:00
|
|
|
# Gitaly note: JV: no RPC's here.
|
|
|
|
|
2017-01-04 13:43:06 -05:00
|
|
|
require 'open3'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Git
|
|
|
|
module Popen
|
2017-10-16 05:11:31 -04:00
|
|
|
FAST_GIT_PROCESS_TIMEOUT = 15.seconds
|
|
|
|
|
2017-11-03 10:31:18 -04:00
|
|
|
def popen(cmd, path, vars = {}, lazy_block: nil)
|
2017-01-04 13:43:06 -05:00
|
|
|
unless cmd.is_a?(Array)
|
|
|
|
raise "System commands must be given as an array of strings"
|
|
|
|
end
|
|
|
|
|
2017-09-12 17:52:43 -04:00
|
|
|
path ||= Dir.pwd
|
|
|
|
vars['PWD'] = path
|
2017-01-04 13:43:06 -05:00
|
|
|
options = { chdir: path }
|
|
|
|
|
2017-07-11 14:29:33 -04:00
|
|
|
cmd_output = ""
|
|
|
|
cmd_status = 0
|
2017-01-04 13:43:06 -05:00
|
|
|
Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
|
2018-01-29 03:51:25 -05:00
|
|
|
stdout.set_encoding(Encoding::ASCII_8BIT)
|
|
|
|
|
2017-09-12 17:52:43 -04:00
|
|
|
yield(stdin) if block_given?
|
|
|
|
stdin.close
|
|
|
|
|
2017-11-03 10:31:18 -04:00
|
|
|
if lazy_block
|
2018-01-31 14:25:38 -05:00
|
|
|
return [lazy_block.call(stdout.lazy), 0]
|
2017-11-03 10:31:18 -04:00
|
|
|
else
|
2017-11-06 08:44:57 -05:00
|
|
|
cmd_output << stdout.read
|
2017-11-03 10:31:18 -04:00
|
|
|
end
|
|
|
|
|
2017-07-11 14:29:33 -04:00
|
|
|
cmd_output << stderr.read
|
|
|
|
cmd_status = wait_thr.value.exitstatus
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
|
2017-07-11 14:29:33 -04:00
|
|
|
[cmd_output, cmd_status]
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
2017-10-16 05:11:31 -04:00
|
|
|
|
|
|
|
def popen_with_timeout(cmd, timeout, path, vars = {})
|
|
|
|
unless cmd.is_a?(Array)
|
|
|
|
raise "System commands must be given as an array of strings"
|
|
|
|
end
|
|
|
|
|
|
|
|
path ||= Dir.pwd
|
|
|
|
vars['PWD'] = path
|
|
|
|
|
|
|
|
unless File.directory?(path)
|
|
|
|
FileUtils.mkdir_p(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
rout, wout = IO.pipe
|
|
|
|
rerr, werr = IO.pipe
|
|
|
|
|
|
|
|
pid = Process.spawn(vars, *cmd, out: wout, err: werr, chdir: path, pgroup: true)
|
|
|
|
|
|
|
|
begin
|
|
|
|
status = process_wait_with_timeout(pid, timeout)
|
|
|
|
|
|
|
|
# close write ends so we could read them
|
|
|
|
wout.close
|
|
|
|
werr.close
|
|
|
|
|
|
|
|
cmd_output = rout.readlines.join
|
|
|
|
cmd_output << rerr.readlines.join # Copying the behaviour of `popen` which merges stderr into output
|
|
|
|
|
|
|
|
[cmd_output, status.exitstatus]
|
|
|
|
rescue Timeout::Error => e
|
|
|
|
kill_process_group_for_pid(pid)
|
|
|
|
|
|
|
|
raise e
|
|
|
|
ensure
|
|
|
|
wout.close unless wout.closed?
|
|
|
|
werr.close unless werr.closed?
|
|
|
|
|
|
|
|
rout.close
|
|
|
|
rerr.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_wait_with_timeout(pid, timeout)
|
|
|
|
deadline = timeout.seconds.from_now
|
|
|
|
wait_time = 0.01
|
|
|
|
|
|
|
|
while deadline > Time.now
|
|
|
|
sleep(wait_time)
|
|
|
|
_, status = Process.wait2(pid, Process::WNOHANG)
|
|
|
|
|
|
|
|
return status unless status.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
raise Timeout::Error, "Timeout waiting for process ##{pid}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def kill_process_group_for_pid(pid)
|
|
|
|
Process.kill("KILL", -pid)
|
|
|
|
Process.wait(pid)
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
end
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|