#!/usr/bin/env ruby require 'fileutils' require_relative '../lib/hoosegow' inmate_dir = '/hoosegow/inmate' inmate_file = File.join(inmate_dir, 'inmate.rb') if File.exist?(inmate_file) system 'ls', '-l', inmate_dir puts "ERROR: #{inmate_file} must not exist!" exit 1 end FileUtils.mkpath(inmate_dir) File.write(inmate_file, < method, :args => args, :block? => !block.nil? result = hoosegow.send(method, *args, &block) p :result => result rescue => error p :error => error #puts error.backtrace end def each_hoosegow puts '*'*10, 'no_proxy => true' yield Hoosegow.new(:no_proxy => true) puts '*'*10, 'no_proxy => false' with_fake_docker do |docker| yield docker.inject(Hoosegow.new) end end def with_fake_docker yield FakeDocker.new end class FakeDocker def inject(hoosegow) docker = self hoosegow.define_singleton_method(:docker) { docker } hoosegow end def run_container(image_name, input_data) Open3.popen3("./bin/hoosegow") do |stdin, stdout, stderr, wait_thr| stdin.write(input_data) stdin.close ios = [stdout, stderr] while ios.any? do readers, _, _ = IO.select(ios, [], [], 1) if readers.nil? break unless wait_thr.alive? else readers.each do |reader| begin type = reader == stdout ? 1 : 2 data = reader.read_nonblock(2**15) yield([type, data.bytesize].pack('CxxxN') + data) rescue EOFError ios.delete(reader) end end end end wait_thr.join end end end main