#!/usr/bin/env ruby require "base64" require "io/console" require "optparse" require "socket" usage_output = "USAGE:\n github-fast-env SCRIPT_PATH [options]" $options = {:verbose => false, :interactive => false} OptionParser.new do |options| options.banner = usage_output options.on("-v", "--verbose", "Show verbose output") do $options[:verbose] = true end options.on("-i", "--interactive", "Run an interactive session using a pseudoterminal") do $options[:interactive] = true end end.parse! if not ARGV or ARGV.empty? $stderr.puts "error: missing argument (script path)\n\n" $stderr.puts usage_output exit 1 end if ARGV.length > 1 $stderr.puts "error: only one script expected\n\n" $stderr.puts usage_output exit 1 end # TODO: support arguments # TODO: forward signals script_path = File.realpath(ARGV[0]) control_socket_path = "/tmp/github-fast-envd.sock" $control_socket = UNIXSocket.new(control_socket_path) $original_stdin = $stdin.dup $original_stdout = $stdout.dup $original_stderr = $stderr.dup def log(level, message) if level == "error" or $options[:verbose] $original_stderr.puts "[github-fast-env] #{level}: #{message}" end end def read_command ready_ios = IO.select([$control_socket], [], [], 10) if not ready_ios log "error", "timeout while communicating with github-fast-envd" exit 1 end response = $control_socket.readline.strip.split(" ", 2) if response.empty? log "error", "malformed response from github-fast-envd" exit 1 end command = response[0] if command == "error" if response.length < 2 log "error", "malformed error response from github-fast-envd" exit 1 end error_message = response[1] log "error", "#{error_message}" exit 1 end if command == "script_error" if response.length < 2 log "error", "malformed script error response from github-fast-envd" exit 1 end error_message = Base64.decode64(response[1]) $stderr.puts error_message exit 1 end arguments = response[1].nil? ? [] : response[1].split(" ") return command, arguments end log "info", "connected to control socket" encoded_script_path = Base64.encode64(script_path).delete("\n") read_ios = [$control_socket] if $options[:interactive] pseudoterminal_path = File.readlink("/proc/self/fd/0") encoded_pseudoterminal_path = Base64.encode64(pseudoterminal_path).delete("\n") $control_socket.puts "new v1 pseudoterminal #{encoded_pseudoterminal_path} #{encoded_script_path}" else $control_socket.puts "new v1 named-pipes #{encoded_script_path}" end pipes = {"stdin" => nil, "stdout" => nil, "stderr" => nil} while true command, arguments = read_command if command == "ready" break elsif command == "named-pipes" if arguments.empty? log "error", "malformed response" exit 1 end pipe_base_path = Base64.decode64(arguments[0]) log "info", "connecting to named pipes at #{pipe_base_path}" pipes["stdin"] = File::open("#{pipe_base_path}.stdin", "w") pipes["stdin"].sync = true pipes["stdout"] = File::open("#{pipe_base_path}.stdout", "r") pipes["stdout"].sync = true pipes["stderr"] = File::open("#{pipe_base_path}.stderr", "r") pipes["stderr"].sync = true read_ios += [$stdin, pipes["stdout"], pipes["stderr"]] log "info", "connected via named pipes" log "info", "ready" $control_socket.puts "ready" else log "error", "malformed response" exit 1 end end exit_code = "unknown" while read_ios.include?($control_socket) or read_ios.include?(pipes["stdout"]) or read_ios.include?(pipes["stderr"]) log "trace", read_ios.inspect ready_read_ios, _, _ = IO.select(read_ios, [], []) log "trace", "ready read IOs: #{ready_read_ios.inspect}" ready_read_ios.each do |ready_read_io| begin if ready_read_io.equal? $stdin log "trace", "writing to stdin" pipes["stdin"].write ready_read_io.readpartial(4096) elsif ready_read_io.equal? pipes["stdout"] log "trace", "reading from stdout" $stdout.write ready_read_io.readpartial(4096) elsif ready_read_io.equal? pipes["stderr"] log "trace", "reading from stderr" $stderr.write ready_read_io.readpartial(4096) elsif ready_read_io.equal? $control_socket log "trace", "reading from control socket" command, arguments = read_command if command != "done" log "error", "malformed response from github-fast-envd" end if !arguments.empty? exit_code = arguments[0] end else log "warn", "received input from unknown stream" end rescue EOFError log "trace", "closing stream #{ready_read_io}" ready_read_io.close end end read_ios = read_ios.select {|x| not x.closed?} end exit_code_is_numeric = Integer(exit_code) != nil rescue false if exit_code_is_numeric exit Integer(exit_code) end