class Vx::Lib::Container::Docker

Constants

Spawner

Attributes

image[R]
init[R]
memory[R]
memory_swap[R]
password[R]
remote_dir[R]
user[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/vx/lib/container/docker.rb, line 20
def initialize(options = {})
  @user           = options[:user]        || "vexor"
  @password       = options[:password]    || "vexor"
  @init           = options[:init]        || %w{ /sbin/my_init }
  @image          = options[:image]       || "vexor/trusty:2.0.1"
  @memory         = options[:memory].to_i
  @memory_swap    = options[:memory_swap].to_i
  @container_opts = options[:container_opts] || {}
end

Public Instance Methods

create_container_options() click to toggle source
# File lib/vx/lib/container/docker.rb, line 36
def create_container_options
  @create_container_options ||= {
    'Cmd'        => init,
    'Image'      => image,
    'Memory'     => memory,
    'MemorySwap' => memory_swap
  }
end
start(&block) click to toggle source
# File lib/vx/lib/container/docker.rb, line 30
def start(&block)
  start_container do |container|
    open_ssh_session(container, &block)
  end
end

Private Instance Methods

open_ssh_session(container) { |spawner| ... } click to toggle source
# File lib/vx/lib/container/docker.rb, line 47
def open_ssh_session(container)
  host = container.json['NetworkSettings']['IPAddress']

  ssh_options = {
    password:      password,
    port:          22,
    paranoid:      false,
    forward_agent: false,
    timeout:       3,
  }

  ssh = with_retries ::Net::SSH::AuthenticationFailed, ::Errno::ECONNREFUSED, ::Errno::ETIMEDOUT, ::Timeout::Error, limit: 5, sleep: 3 do
    ::Net::SSH.start host, user, ssh_options
  end

  re = yield Spawner.new(container, ssh)
  ssh.shutdown!
  re
end
start_container() { |container| ... } click to toggle source
# File lib/vx/lib/container/docker.rb, line 67
def start_container(&block)
  container =
    with_retries ::Excon::Errors::SocketError, ::Docker::Error::TimeoutError, limit: 5, sleep: 3 do
      ::Docker::Container.create create_container_options.merge(@container_opts)
    end

  container.start

  begin
    yield container
  ensure
    container.kill
    container.remove
  end
end