module Mcrain::ContainerController

Public Class Methods

included(klass) click to toggle source
# File lib/mcrain/container_controller.rb, line 4
def self.included(klass)
  klass.extend(ClassMethods)
end

Public Instance Methods

add_volume_options(r, path_on_container, path_on_host) click to toggle source
# File lib/mcrain/container_controller.rb, line 72
def add_volume_options(r, path_on_container, path_on_host)
  r['Volumes'] ||= {}
  r['Volumes'][path_on_container] = {}
  r['HostConfig']['Binds'] ||= []
  r['HostConfig']['Binds'] << "#{path_on_host}:#{path_on_container}"
  self
end
build_docker_options() click to toggle source
# File lib/mcrain/container_controller.rb, line 61
def build_docker_options
  {
    'Image' => container_image,
    'HostConfig' => {
      'PortBindings' => {
        "#{self.class.port}/tcp" => [{ 'HostPort' => port.to_s }]
      }
    }
  }
end
container() click to toggle source

@return [Docker::Container]

# File lib/mcrain/container_controller.rb, line 28
def container
  unless @container
    options = build_docker_options
    Mcrain.logger.info("#{self.class.name}#container Docker::Container.create(#{options.inspect})")
    @container = Docker::Container.create(options)
  end
  @container
end
container_image() click to toggle source
# File lib/mcrain/container_controller.rb, line 37
def container_image
  self.class.container_image or raise "No container_image for #{self.class.name}"
end
find_portno() click to toggle source
# File lib/mcrain/container_controller.rb, line 45
def find_portno
  # 未使用のポートをシステムに割り当てさせてすぐ閉じてそれを利用する
  tmpserv = TCPServer.new(0)
  portno = tmpserv.local_address.ip_port
  tmpserv.close
  portno
end
host() click to toggle source
# File lib/mcrain/container_controller.rb, line 41
def host
  @host ||= URI.parse(ENV["DOCKER_HOST"] || "tcp://localhost").host
end
info() click to toggle source
# File lib/mcrain/container_controller.rb, line 80
def info
  container.json
end
ip() click to toggle source
# File lib/mcrain/container_controller.rb, line 88
def ip
  info["NetworkSettings"]["IPAddress"]
end
name() click to toggle source
# File lib/mcrain/container_controller.rb, line 84
def name
  info["Name"] # .sub(/\A\//, '')
end
port() click to toggle source
# File lib/mcrain/container_controller.rb, line 53
def port
  @port ||= find_portno
end
ssh_uri() click to toggle source
# File lib/mcrain/container_controller.rb, line 92
def ssh_uri
  "ssh://root@#{ip}:22"
end
url() click to toggle source
# File lib/mcrain/container_controller.rb, line 57
def url
  @url ||= "#{self.class.server_name}://#{host}:#{port}"
end