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



73
74
75
76
77
78
79
# File 'lib/mcrain/container_controller.rb', line 73

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_optionsObject



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

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

#containerDocker::Container

Returns:

  • (Docker::Container)


29
30
31
32
33
34
35
36
# File 'lib/mcrain/container_controller.rb', line 29

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_imageObject



38
39
40
# File 'lib/mcrain/container_controller.rb', line 38

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

#find_portnoObject



46
47
48
49
50
51
52
# File 'lib/mcrain/container_controller.rb', line 46

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

#hostObject



42
43
44
# File 'lib/mcrain/container_controller.rb', line 42

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

#infoObject



81
82
83
# File 'lib/mcrain/container_controller.rb', line 81

def info
  container.json
end

#ipObject



89
90
91
# File 'lib/mcrain/container_controller.rb', line 89

def ip
  info["NetworkSettings"]["IPAddress"]
end

#nameObject



85
86
87
# File 'lib/mcrain/container_controller.rb', line 85

def name
  info["Name"] # .sub(/\A\//, '')
end

#portObject



54
55
56
# File 'lib/mcrain/container_controller.rb', line 54

def port
  @port ||= find_portno
end

#ssh_uriObject



93
94
95
# File 'lib/mcrain/container_controller.rb', line 93

def ssh_uri
  "ssh://root@#{ip}:22"
end

#urlObject



58
59
60
# File 'lib/mcrain/container_controller.rb', line 58

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