class DockerJail::Base

Execute command in jail.

Attributes

container[R]

@attr_reader [Docker::Container] container docker-api's raw container @attr_reader [Array<String>] out stdout outputs @attr_reader [Array<String>] err stderr outputs

err[R]

@attr_reader [Docker::Container] container docker-api's raw container @attr_reader [Array<String>] out stdout outputs @attr_reader [Array<String>] err stderr outputs

out[R]

@attr_reader [Docker::Container] container docker-api's raw container @attr_reader [Array<String>] out stdout outputs @attr_reader [Array<String>] err stderr outputs

Public Class Methods

build_container(**opts) click to toggle source
# File lib/docker-jail/base.rb, line 51
def self.build_container(**opts)
  Docker::Container.create(build_option(opts))
end
build_option(**opts) click to toggle source
# File lib/docker-jail/base.rb, line 47
def self.build_option(**opts)
  @@base_opts.merge(opts)
end
delete_all() click to toggle source
# File lib/docker-jail/base.rb, line 43
def self.delete_all()
  Docker::Container.all(all:true).each{|c| c.delete(force:true)}
end
get_all() click to toggle source
# File lib/docker-jail/base.rb, line 39
def self.get_all()
  Docker::Container.all(all:true)
end
new(**opts) click to toggle source

@option opts [Hash] opts options to create Docker container @see docs.docker.com/engine/api/v1.26/ Docker API Reference @example

DockerJail::Base.new(Image: 'centos', Cmd: ['ls', '-a'], HostConfig: {Memory: 10*1024})
# File lib/docker-jail/base.rb, line 59
def initialize(**opts)
  @container = self.class.build_container(opts)
end

Public Instance Methods

delete() click to toggle source

Force delete container

# File lib/docker-jail/base.rb, line 97
def delete
  @container.delete(force: true)
end
exit_code() click to toggle source

@return [Integer]

# File lib/docker-jail/base.rb, line 125
def exit_code
  state['ExitCode']
end
finished_at() click to toggle source

@return [Time]

# File lib/docker-jail/base.rb, line 135
def finished_at
  Time.parse(state['FinishedAt'])
end
json() click to toggle source

@return [Hash] The container's all state and options

# File lib/docker-jail/base.rb, line 109
def json
  container.json
end
oom_killed?() click to toggle source

Memory limit exceeded @return [Bool]

# File lib/docker-jail/base.rb, line 120
def oom_killed?
  state['OOMKilled']
end
run(input=nil, &block) click to toggle source

Run container @param [StringIO] input Give input stream to stdin @yield deprecated. Because docker-api's block behavior is unstable @yieldparam [String] stream_name @yieldparam [String] chunk stdout or stderr partial string @return [Array<String>, Array<String>] stdout and stderr

# File lib/docker-jail/base.rb, line 69
def run(input=nil, &block)
  @out, @err = @container.tap(&:start).attach(logs: true, tty: false, stdin: input, &block)
end
run_timeout(timeout, input=nil, &block) click to toggle source

Run container with a time limit.

Container run in the sub thread.
the timeout value contain the container up time.
When container run timeout, value of #out and #err is undefined

@param (see run) @param [Numeric] timeout

execution time limit(seconds).
It's different from StopTimeout of Docker

@yield (see run) @yieldparam (see run) @return (see run) @raise [Timeout::Error] When container run timeout, raise Timeout::Error

# File lib/docker-jail/base.rb, line 85
def run_timeout(timeout, input=nil, &block)
  @timeout = false
  # Main thread
  Timeout.timeout(timeout) do
    # Sub thread
    return run(input, &block)
  end
rescue Timeout::Error
  @timeout = true
end
started_at() click to toggle source

@return [Time]

# File lib/docker-jail/base.rb, line 130
def started_at
  Time.parse(state['StartedAt'])
end
state() click to toggle source

@return [Hash]

# File lib/docker-jail/base.rb, line 114
def state
  json['State']
end
timeout?() click to toggle source

@return [nil] Time unlimited @return [true] Time limit exceeded @return [false] Finished within time limit

# File lib/docker-jail/base.rb, line 104
def timeout?
  @timeout
end