class Construi::Container

Attributes

name[R]

Public Class Methods

create(image, options = {}) click to toggle source
# File lib/construi/container.rb, line 68
def self.create(image, options = {})
  env = options[:env] || []
  privileged = options[:privileged] || false
  links = options[:links] || []
  volumes = options[:volumes] || []
  volumes_from = options[:volumes_from] || []

  host_config = {
    'Binds' => ["#{Dir.pwd}:/var/workspace"].concat(volumes),
    'Privileged' => privileged,
    'Links' => links,
    'VolumesFrom' => volumes_from
  }

  create_options = {
    'Image' => image.id,
    'Env' => env,
    'Tty' => false,
    'WorkingDir' => '/var/workspace',
    'HostConfig' => host_config
  }

  create_options['Cmd'] = options[:cmd].split if options.key?(:cmd)

  wrap Docker::Container.create(create_options), options
end
new(container, options = {}) click to toggle source
# File lib/construi/container.rb, line 9
def initialize(container, options = {})
  @container = container
  @name = options[:name] || @container.id
  @log_lifecycle = options[:log_lifecycle] || false
end
run(image, options = {}) click to toggle source
# File lib/construi/container.rb, line 106
def self.run(image, options = {})
  use image, options, &:run
end
use(image, options = {}) { |container| ... } click to toggle source
# File lib/construi/container.rb, line 99
def self.use(image, options = {})
  container = create image, options
  yield container
ensure
  container.delete unless container.nil?
end
wrap(container, options = {}) click to toggle source
# File lib/construi/container.rb, line 95
def self.wrap(container, options = {})
  new container, options
end

Public Instance Methods

==(other) click to toggle source
# File lib/construi/container.rb, line 64
def ==(other)
  other.is_a? Container and id == other.id
end
attach_stdout() click to toggle source
# File lib/construi/container.rb, line 37
def attach_stdout
  Thread.new do
    @container.attach(:stream => true, :logs => true) { |_, c| Console.output name, c }
  end
end
commit() click to toggle source
# File lib/construi/container.rb, line 51
def commit
  Image.wrap(@container.commit)
end
delete() click to toggle source
# File lib/construi/container.rb, line 30
def delete
  stop
  @container.kill
  @container.delete force: true, v: true
  log_lifecycle "Deleted container: '#{name}'"
end
id() click to toggle source
# File lib/construi/container.rb, line 15
def id
  @container.id
end
log_lifecycle(msg) click to toggle source
# File lib/construi/container.rb, line 47
def log_lifecycle(msg)
  Console.progress msg if log_lifecycle?
end
log_lifecycle?() click to toggle source
# File lib/construi/container.rb, line 43
def log_lifecycle?
  @log_lifecycle
end
run() click to toggle source
# File lib/construi/container.rb, line 55
def run
  start
  status_code = @container.wait['StatusCode']

  raise RunError, "Cmd returned status code: #{status_code}" unless status_code == 0

  commit
end
start() click to toggle source
# File lib/construi/container.rb, line 19
def start
  log_lifecycle "Starting container: '#{name}'..."
  @container.start!
  attach_stdout
end
stop() click to toggle source
# File lib/construi/container.rb, line 25
def stop
  log_lifecycle "Stopping container: '#{name}'..."
  @container.stop
end