Module: Mcrain::ContainerController

Included in:
Base, Riak::Node
Defined in:
lib/mcrain/container_controller.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
# File 'lib/mcrain/container_controller.rb', line 5

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#add_volume_options(r, path_on_container, path_on_host) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/mcrain/container_controller.rb', line 63

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 ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/mcrain/container_controller.rb', line 52

def build_docker_options
  {
    'Image' => container_image,
    'HostConfig' => {
      'PortBindings' => {
        "#{self.class.port}/tcp" => [{ 'HostPort' => port.to_s }]
      }
    }
  }
end

#container ⇒ Docker::Container

Returns:

  • (Docker::Container)


19
20
21
22
23
24
25
26
# File 'lib/mcrain/container_controller.rb', line 19

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 ⇒ Object



28
29
30
# File 'lib/mcrain/container_controller.rb', line 28

def container_image
  self.class.container_image or raise "No container_image for #{self.class.name}"
end

#find_portno ⇒ Object



36
37
38
39
40
41
42
# File 'lib/mcrain/container_controller.rb', line 36

def find_portno
  # 未使用のポートをシステムに割り当てさせてすぐ閉じてそれを利用する
  tmpserv = TCPServer.new(0)
  portno = tmpserv.local_address.ip_port
  tmpserv.close
  portno
end

#host ⇒ Object



32
33
34
# File 'lib/mcrain/container_controller.rb', line 32

def host
  @host ||= URI.parse(ENV["DOCKER_HOST"] || "tcp://localhost").host
end

#port ⇒ Object



44
45
46
# File 'lib/mcrain/container_controller.rb', line 44

def port
  @port ||= find_portno
end

#url ⇒ Object



48
49
50
# File 'lib/mcrain/container_controller.rb', line 48

def url
  @url ||= "#{self.class.server_name}://#{host}:#{port}"
end