Sha256: e0d356db1a9e5b2eba6e9d57f919449e052da148ab4b520d68cd47ad6b5ac09e

Contents?: true

Size: 1.52 KB

Versions: 6

Compression:

Stored size: 1.52 KB

Contents

require 'construi/image'

module Construi

  class Container
    private_class_method :new

    def initialize(container)
      @container = container
    end

    def id
      @container.id
    end

    def delete
      @container.delete
    end

    def attach_stdout
      @container.attach(:stream => true, :logs => true) { |s, c| puts c; $stdout.flush }
      true
    rescue Docker::Error::TimeoutError
      puts 'Failed to attach to stdout'.yellow
      false
    end

    def commit
      Image.wrap(@container.commit)
    end

    def run
      @container.start
      attached = attach_stdout
      status_code = @container.wait['StatusCode']

      puts @container.logs(:stdout => true) unless attached

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

      commit
    end

    def ==(other)
      other.is_a? Container and id == other.id
    end

    def self.create(image, cmd, env)
      wrap Docker::Container.create(
        'Cmd' => cmd.split,
        'Image' => image.id,
        'Env' => env.to_json,
        'Tty' => false,
        'WorkingDir' => '/var/workspace',
        'HostConfig' => { 'Binds' => ["#{Dir.pwd}:/var/workspace"] })
    end

    def self.wrap(container)
      new container
    end

    def self.use(image, cmd, env)
      container = create(image, cmd, env)
      yield container
    ensure
      container.delete unless container.nil?
    end

    def self.run(image, cmd, env)
      use(image, cmd, env, &:run)
    end

    class Error < StandardError
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
construi-0.35.2 lib/construi/container.rb
construi-0.35.1 lib/construi/container.rb
construi-0.35.0 lib/construi/container.rb
construi-0.33.0 lib/construi/container.rb
construi-0.32.0 lib/construi/container.rb
construi-0.31.0 lib/construi/container.rb